From f788ce679f4d4557669e4a0c37a75761b112170c Mon Sep 17 00:00:00 2001 From: altaroca <8048513+altaroca@users.noreply.github.com> Date: Sat, 29 Jan 2022 19:30:22 +0100 Subject: [PATCH 1/3] make webui work with non-root context path (#1290) --- .../web/app/services/oh3storage.service.js | 6 ++--- .../org.openhab.ui/web/src/components/app.vue | 11 ++++++++-- bundles/org.openhab.ui/web/src/index.html | 20 +++++++++++++++++ .../org.openhab.ui/web/src/js/openhab/api.js | 22 +++++++++++++------ .../org.openhab.ui/web/src/js/openhab/sse.js | 12 ++++++++-- .../org.openhab.ui/web/src/pages/profile.vue | 2 +- 6 files changed, 58 insertions(+), 15 deletions(-) diff --git a/bundles/org.openhab.ui.habpanel/web/app/services/oh3storage.service.js b/bundles/org.openhab.ui.habpanel/web/app/services/oh3storage.service.js index 26902a1272..789e3ba8b9 100644 --- a/bundles/org.openhab.ui.habpanel/web/app/services/oh3storage.service.js +++ b/bundles/org.openhab.ui.habpanel/web/app/services/oh3storage.service.js @@ -26,9 +26,9 @@ } else { $http({ method: 'POST', - url: '/rest/auth/token', - data: 'grant_type=refresh_token&client_id=' + window.location.origin + - '&redirect_uri=' + window.location.origin + '&refresh_token=' + refreshToken, + url: document.baseURI + '/rest/auth/token', + data: 'grant_type=refresh_token&client_id=' + document.baseURI + + '&redirect_uri=' + document.baseURI + '&refresh_token=' + refreshToken, headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }) .then(function (data) { diff --git a/bundles/org.openhab.ui/web/src/components/app.vue b/bundles/org.openhab.ui/web/src/components/app.vue index b107de8124..c77678f075 100644 --- a/bundles/org.openhab.ui/web/src/components/app.vue +++ b/bundles/org.openhab.ui/web/src/components/app.vue @@ -280,6 +280,12 @@ export default { theme = 'aurora' } + let contextRoot = '/' + if (window.document.baseURI) { + contextRoot = new URL(window.document.baseURI).pathname + if (contextRoot.slice(-1) === '/') contextRoot = contextRoot.slice(0, -1) + } + return { init: false, ready: false, @@ -307,6 +313,7 @@ export default { iosSwipeBack: !this.$device.ios || this.$device.cordova, auroraSwipeBack: !this.$device.ios || this.$device.cordova, pushState: true, // !this.$device.cordova + pushStateRoot: contextRoot, pushStateSeparator: '' }, // Enable panel left visibility breakpoint @@ -376,7 +383,7 @@ export default { return this.ready && this.user && this.user.roles && this.user.roles.indexOf('administrator') >= 0 }, serverDisplayUrl () { - return (this.serverUrl || window.location.origin) + return (this.serverUrl || window.document.baseURI || window.location.origin) } }, methods: { @@ -510,7 +517,7 @@ export default { this.cleanSession().then(() => { this.loggedIn = false this.$f7.views.main.router.navigate('/', { animate: false, clearPreviousHistory: true }) - window.location = window.location.origin + window.location = window.document.baseURI if (this.$device.cordova) { this.loginScreenOpened = true } diff --git a/bundles/org.openhab.ui/web/src/index.html b/bundles/org.openhab.ui/web/src/index.html index 756e93531b..a943b0bb57 100644 --- a/bundles/org.openhab.ui/web/src/index.html +++ b/bundles/org.openhab.ui/web/src/index.html @@ -26,6 +26,26 @@ <% } %> + diff --git a/bundles/org.openhab.ui/web/src/js/openhab/api.js b/bundles/org.openhab.ui/web/src/js/openhab/api.js index c7a803c240..bb74e03d0d 100644 --- a/bundles/org.openhab.ui/web/src/js/openhab/api.js +++ b/bundles/org.openhab.ui/web/src/js/openhab/api.js @@ -9,6 +9,14 @@ function wrapPromise (f7promise) { }) } +function basedURI (uri) { + if (uri.charAt(0) === '/') { + if (window.document.baseURI.slice(-1) === '/') { + return window.document.baseURI.slice(0, -1) + uri + } else return window.document.baseURI + uri + } else return uri +} + Framework7.request.setup({ xhrFields: { withCredentials: true }, beforeSend (xhr) { @@ -33,19 +41,19 @@ export default { getPlain (uri, data, contentType) { return wrapPromise(Framework7.request.promise({ method: 'GET', - url: uri, + url: basedURI(uri), data, processData: false, contentType: contentType || 'text/plain' })) }, post (uri, data, dataType) { - return wrapPromise(Framework7.request.promise.postJSON(uri, data, dataType)) + return wrapPromise(Framework7.request.promise.postJSON(basedURI(uri), data, dataType)) }, postPlain (uri, data, dataType, contentType) { return wrapPromise(Framework7.request.promise({ method: 'POST', - url: uri, + url: basedURI(uri), data, processData: false, contentType: contentType || 'text/plain', @@ -55,7 +63,7 @@ export default { put (uri, data) { return wrapPromise(Framework7.request.promise({ method: 'PUT', - url: uri, + url: basedURI(uri), data: JSON.stringify(data), processData: false, // dataType: 'json', @@ -65,7 +73,7 @@ export default { putPlain (uri, data, dataType, contentType) { return wrapPromise(Framework7.request.promise({ method: 'PUT', - url: uri, + url: basedURI(uri), data, processData: false, // dataType: 'json', @@ -76,13 +84,13 @@ export default { head (uri) { return wrapPromise(Framework7.request.promise({ method: 'HEAD', - url: uri + url: basedURI(uri) })) }, delete (uri, data) { return wrapPromise(Framework7.request.promise({ method: 'DELETE', - url: uri, + url: basedURI(uri), processData: false, // dataType: 'json', contentType: 'application/json' diff --git a/bundles/org.openhab.ui/web/src/js/openhab/sse.js b/bundles/org.openhab.ui/web/src/js/openhab/sse.js index 4444b7b24b..c6bd96f90d 100644 --- a/bundles/org.openhab.ui/web/src/js/openhab/sse.js +++ b/bundles/org.openhab.ui/web/src/js/openhab/sse.js @@ -1,6 +1,14 @@ import { EventSourcePolyfill, NativeEventSource } from 'event-source-polyfill' import { getAccessToken, getTokenInCustomHeader, getBasicCredentials, getRequireToken } from './auth' +function basedURI (uri) { + if (uri.charAt(0) === '/') { + if (window.document.baseURI.slice(-1) === '/') { + return window.document.baseURI.slice(0, -1) + uri + } else return window.document.baseURI + uri + } else return uri +} + let openSSEClients = [] function newSSEConnection (path, readyCallback, messageCallback, errorCallback) { @@ -53,10 +61,10 @@ function newSSEConnection (path, readyCallback, messageCallback, errorCallback) export default { connect (path, topics, messageCallback, errorCallback) { - return newSSEConnection(path, null, messageCallback, errorCallback) + return newSSEConnection(basedURI(path), null, messageCallback, errorCallback) }, connectStateTracker (path, readyCallback, updateCallback, errorCallback) { - return newSSEConnection(path, readyCallback, updateCallback, errorCallback) + return newSSEConnection(basedURI(path), readyCallback, updateCallback, errorCallback) }, close (client, callback) { if (!client) return diff --git a/bundles/org.openhab.ui/web/src/pages/profile.vue b/bundles/org.openhab.ui/web/src/pages/profile.vue index 492edd9bca..ba9f8db331 100644 --- a/bundles/org.openhab.ui/web/src/pages/profile.vue +++ b/bundles/org.openhab.ui/web/src/pages/profile.vue @@ -216,7 +216,7 @@ export default { this.cleanSession().then(() => { this.loggedIn = false this.$f7.views.main.router.navigate('/', { animate: false, clearPreviousHistory: true }) - window.location = window.location.origin + window.location = window.document.baseURI if (this.$device.cordova) { this.loginScreenOpened = true } From 4715184661b0be141832a210c89af9a28fc43856 Mon Sep 17 00:00:00 2001 From: altaroca <8048513+altaroca@users.noreply.github.com> Date: Fri, 8 Apr 2022 08:49:11 +0200 Subject: [PATCH 2/3] quick fix for widget's 'stylesheet' parameter exception --- .../org.openhab.ui/web/src/components/widgets/widget-mixin.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bundles/org.openhab.ui/web/src/components/widgets/widget-mixin.js b/bundles/org.openhab.ui/web/src/components/widgets/widget-mixin.js index 3928fe6a63..5a4cadd03c 100644 --- a/bundles/org.openhab.ui/web/src/components/widgets/widget-mixin.js +++ b/bundles/org.openhab.ui/web/src/components/widgets/widget-mixin.js @@ -87,7 +87,9 @@ export default { if (this.context && this.context.component.config && this.context.component.config.stylesheet) { this.cssUid = 'scoped-' + this.$f7.utils.id() - this.$el.classList.add(this.cssUid) + var el = this.$el + if (!el.classList) el = el.parentElement + el.classList.add(this.cssUid) let style = document.createElement('style') style.id = this.cssUid From 5544876831f72cf7630b537f955dc8e5c8169d45 Mon Sep 17 00:00:00 2001 From: altaroca <8048513+altaroca@users.noreply.github.com> Date: Fri, 8 Apr 2022 09:40:38 +0200 Subject: [PATCH 3/3] Fix NotNull check --- .../org/openhab/ui/habot/nlp/internal/OpenNLPInterpreter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bundles/org.openhab.ui.habot/src/main/java/org/openhab/ui/habot/nlp/internal/OpenNLPInterpreter.java b/bundles/org.openhab.ui.habot/src/main/java/org/openhab/ui/habot/nlp/internal/OpenNLPInterpreter.java index f92044fda1..aab9c099e7 100644 --- a/bundles/org.openhab.ui.habot/src/main/java/org/openhab/ui/habot/nlp/internal/OpenNLPInterpreter.java +++ b/bundles/org.openhab.ui.habot/src/main/java/org/openhab/ui/habot/nlp/internal/OpenNLPInterpreter.java @@ -102,7 +102,7 @@ public String getLabel(Locale locale) { public String interpret(Locale locale, String text) throws InterpretationException { ChatReply reply = reply(locale, text); if (reply == null) { - return null; + throw new InterpretationException("reply is null"); } return reply.getAnswer();