Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <[email protected]>
  • Loading branch information
J-N-K committed Jan 7, 2023
1 parent c4706ed commit 8ed5fdf
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

Expand All @@ -27,13 +27,13 @@
import org.openhab.core.items.ItemRegistry;
import org.openhab.ui.basic.internal.render.PageRenderer;
import org.openhab.ui.basic.render.RenderException;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.http.HttpContext;
import org.osgi.service.http.HttpService;
import org.osgi.service.http.whiteboard.propertytypes.HttpWhiteboardServletAsyncSupported;
import org.osgi.service.http.whiteboard.propertytypes.HttpWhiteboardServletName;
import org.osgi.service.http.whiteboard.propertytypes.HttpWhiteboardServletPattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -46,10 +46,13 @@
*/
@Component(immediate = true, service = {})
@NonNullByDefault
public class ManifestServlet extends BaseServlet {
@HttpWhiteboardServletAsyncSupported(asyncSupported = true)
@HttpWhiteboardServletName(ManifestServlet.WEBAPP_ALIAS)
@HttpWhiteboardServletPattern(ManifestServlet.WEBAPP_ALIAS + "/" + ManifestServlet.MANIFEST_NAME)
public class ManifestServlet extends HttpServlet {

private static final long serialVersionUID = 4591967180619528326L;

public static final String WEBAPP_ALIAS = "/basicui";
public static final String MANIFEST_NAME = "manifest.json";

private static final String MANIFEST_CONTENT_TYPE = "application/json;charset=UTF-8";
Expand All @@ -58,23 +61,14 @@ public class ManifestServlet extends BaseServlet {

private final PageRenderer renderer;

protected final ItemRegistry itemRegistry;

@Activate
public ManifestServlet(final @Reference HttpService httpService,
final @Reference HttpContextFactoryService httpContextFactoryService,
final @Reference ItemRegistry itemRegistry, final @Reference PageRenderer renderer) {
super(httpService, httpContextFactoryService, itemRegistry);
this.renderer = renderer;
}

@Activate
protected void activate(Map<String, Object> configProps, BundleContext bundleContext) {
HttpContext httpContext = createHttpContext(bundleContext.getBundle());
super.activate(WEBAPP_ALIAS + "/" + MANIFEST_NAME, httpContext);
}

@Deactivate
protected void deactivate() {
super.deactivate(WEBAPP_ALIAS + "/" + MANIFEST_NAME);
this.itemRegistry = itemRegistry;
}

private void generateManifest(ServletResponse res, @Nullable String sitemapName)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (c) 2010-2022 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.ui.internal;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.http.context.ServletContextHelper;
import org.osgi.service.http.whiteboard.HttpWhiteboardConstants;

/**
* The {@link UIContext} is the shared context for Main UI servlets
*
* @author Jan N. Klug - Initial contribution
*/
@NonNullByDefault
@Component(property = { HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=org.openhab.ui.context",
HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH + "=/",
HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_INIT_PARAM_PREFIX + "acceptRanges=true",
HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_INIT_PARAM_PREFIX + "dirAllowed=false",
HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_INIT_PARAM_PREFIX + "redirectWelcome=false",
HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_INIT_PARAM_PREFIX + "preCompressed=true",
HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_INIT_PARAM_PREFIX
+ "etags=true" }, service = ServletContextHelper.class)
public class UIContext extends ServletContextHelper {
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
import java.net.URL;
import java.nio.channels.ReadableByteChannel;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.Map;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -33,13 +32,14 @@
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.util.resource.Resource;
import org.openhab.core.OpenHAB;
import org.ops4j.pax.web.service.WebContainer;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.http.HttpContext;
import org.osgi.service.http.HttpService;
import org.osgi.service.http.NamespaceException;
import org.osgi.service.http.whiteboard.propertytypes.HttpWhiteboardContext;
import org.osgi.service.http.whiteboard.propertytypes.HttpWhiteboardServletName;
import org.osgi.service.http.whiteboard.propertytypes.HttpWhiteboardServletPattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -51,57 +51,36 @@
* @author Dan Cunningham - Convert file serving to a custom servlet
*/

@Component(immediate = true, name = "org.openhab.ui", property = { "httpContext.id:String=oh-ui-http-ctx" })
@Component(immediate = true, service = Servlet.class, name = "org.openhab.ui")
@NonNullByDefault
@HttpWhiteboardServletName(UIServlet.SERVLET_NAME)
@HttpWhiteboardServletPattern(UIServlet.SERVLET_NAME + "*")
@HttpWhiteboardContext(name = "=org.openhab.ui.context", path = "=/")
public class UIServlet extends DefaultServlet {

private static final long serialVersionUID = 1L;

private final Logger logger = LoggerFactory.getLogger(UIServlet.class);

private static final String APP_BASE = "app";
private static final String SERVLET_NAME = "/";
public static final String SERVLET_NAME = "/";
private static final String STATIC_PATH = "/static";
private static final String STATIC_BASE = OpenHAB.getConfigFolder() + "/html";
private static final String[] COMPRESS_EXT = { "gz", "br" };

private final HttpContext defaultHttpContext;
private final HttpService httpService;
private long bundleModifiedTime = 0;
private final long bundleModifiedTime;
private @Nullable ContextHandler contextHandler;

@Activate
public UIServlet(final @Reference HttpService httpService, final @Reference HttpContext httpContext) {
super();
defaultHttpContext = httpService.createDefaultHttpContext();
this.httpService = httpService;
}

@Activate
protected void activate(Map<String, Object> config) {
try {
logger.debug("Starting up {} at {}", getClass().getSimpleName(), SERVLET_NAME);
httpService.registerServlet(SERVLET_NAME, this, new Hashtable<>(), defaultHttpContext);
bundleModifiedTime = (System.currentTimeMillis() / 1000) * 1000; // round milliseconds
} catch (NamespaceException e) {
logger.error("Error during servlet registration - alias {} already in use", SERVLET_NAME, e);
} catch (ServletException e) {
logger.error("Error during servlet registration", e);
}
}

@Deactivate
protected void deactivate() {
httpService.unregister(SERVLET_NAME);
public UIServlet(final @Reference WebContainer webContainer) {
this.defaultHttpContext = webContainer.createDefaultHttpContext();
logger.debug("Starting up {} at {}", getClass().getSimpleName(), SERVLET_NAME);
bundleModifiedTime = (System.currentTimeMillis() / 1000) * 1000; // round milliseconds
}

@Override
public void init() throws UnavailableException {
setInitParameter("acceptRanges", "true");
setInitParameter("dirAllowed", "false");
setInitParameter("redirectWelcome", "false");
setInitParameter("precompressed", "true");
setInitParameter("etags", "true");
contextHandler = ContextHandler.getCurrentContext().getContextHandler();
super.init();
}
Expand All @@ -117,7 +96,7 @@ public void init() throws UnavailableException {
if (name.startsWith(STATIC_PATH) && !name.endsWith("/")) {
URL url = null;
try {
url = new java.io.File(STATIC_BASE + name.substring(new String(STATIC_PATH).length())).toURI().toURL();
url = new java.io.File(STATIC_BASE + name.substring(STATIC_PATH.length())).toURI().toURL();
} catch (MalformedURLException e) {
logger.error("Error while serving static content: {}", e.getMessage());
url = defaultHttpContext.getResource(APP_BASE + name);
Expand All @@ -131,9 +110,9 @@ public void init() throws UnavailableException {
}
} else {
URL url = defaultHttpContext.getResource(APP_BASE + name);
// if we don't find a resource, and its not a check for a compressed version, return the app base and let
// if we don't find a resource, and it's not a check for a compressed version, return the app base and let
// the Vue.js main UI handle routing
if (url == null && !Arrays.stream(COMPRESS_EXT).anyMatch(entry -> name.endsWith(entry))) {
if (url == null && Arrays.stream(COMPRESS_EXT).noneMatch(name::endsWith)) {
// sending a directory will trigger "getWelcomeFile" to be called which is required to avoid
// Jetty doing a 302 redirect which breaks Vue.js routing when reloading the browser on some pages
url = defaultHttpContext.getResource(APP_BASE);
Expand Down Expand Up @@ -170,18 +149,14 @@ protected void doGet(@Nullable HttpServletRequest request, @Nullable HttpServlet
return "/index.html";
}

private void setInitParameter(String name, String value) {
getServletContext().setInitParameter(CONTEXT_INIT + name, value);
}

/**
*
* Wraps a resource and returns a consistent time for bundled files
*
*/
class ResourceWrapper extends Resource {

private Resource baseResource;
private final Resource baseResource;

public ResourceWrapper(Resource baseResource) {
this.baseResource = baseResource;
Expand Down Expand Up @@ -258,7 +233,7 @@ public String[] list() {
}

@Override
public Resource addPath(@Nullable String path) throws IOException, MalformedURLException {
public Resource addPath(@Nullable String path) throws IOException {
return baseResource.addPath(path);
}
}
Expand Down
7 changes: 4 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<bnd.version>6.4.0</bnd.version>
<eea.version>2.3.0</eea.version>
<jackson.version>2.14.1</jackson.version>
<karaf.version>4.3.7</karaf.version>
<karaf.version>4.4.2</karaf.version>
<ohc.version>4.0.0-SNAPSHOT</ohc.version>
<sat.version>0.13.0</sat.version>
<spotless.version>2.28.0</spotless.version>
Expand Down Expand Up @@ -365,7 +365,8 @@ Import-Package: \\
</headerDefinitions>
<includes>
<include>**/org/openhab/**/*.java</include>
<include>**/features/**/header.xml</include>
<include>**/feature.xml</include>
<include>**/OSGI-INF/*.xml</include>
</includes>
<excludes>
<exclude>target/**</exclude>
Expand All @@ -374,7 +375,7 @@ Import-Package: \\
</excludes>
<useDefaultExcludes>true</useDefaultExcludes>
<properties>
<year>2023</year>
<year>2022</year>
</properties>
<encoding>UTF-8</encoding>
</configuration>
Expand Down

0 comments on commit 8ed5fdf

Please sign in to comment.