register static context handler (#423)
* register static context handler Signed-off-by: Kai Kreuzer <kai@openhab.org>pull/426/head
parent
bfd03fd889
commit
38a2624df6
|
@ -12,6 +12,7 @@ Import-Package:
|
||||||
org.apache.commons.io,
|
org.apache.commons.io,
|
||||||
org.eclipse.jdt.annotation;resolution:=optional,
|
org.eclipse.jdt.annotation;resolution:=optional,
|
||||||
org.eclipse.jetty.server,
|
org.eclipse.jetty.server,
|
||||||
|
org.eclipse.jetty.server.handler,
|
||||||
org.eclipse.jetty.servlet,
|
org.eclipse.jetty.servlet,
|
||||||
org.eclipse.jetty.util.resource,
|
org.eclipse.jetty.util.resource,
|
||||||
org.openhab.ui.dashboard,
|
org.openhab.ui.dashboard,
|
||||||
|
|
|
@ -11,4 +11,12 @@
|
||||||
|
|
||||||
<packaging>eclipse-plugin</packaging>
|
<packaging>eclipse-plugin</packaging>
|
||||||
<name>openHAB Start UI</name>
|
<name>openHAB Start UI</name>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.openhab.core</groupId>
|
||||||
|
<artifactId>org.openhab.ui.dashboard</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -10,6 +10,8 @@ package org.openhab.ui.start.internal;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.util.Dictionary;
|
||||||
|
import java.util.Hashtable;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
|
@ -17,6 +19,8 @@ import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.eclipse.jetty.server.handler.ContextHandler;
|
||||||
|
import org.eclipse.jetty.server.handler.ResourceHandler;
|
||||||
import org.eclipse.jetty.servlet.DefaultServlet;
|
import org.eclipse.jetty.servlet.DefaultServlet;
|
||||||
import org.openhab.ui.dashboard.DashboardReady;
|
import org.openhab.ui.dashboard.DashboardReady;
|
||||||
import org.osgi.framework.Bundle;
|
import org.osgi.framework.Bundle;
|
||||||
|
@ -45,6 +49,11 @@ import org.slf4j.LoggerFactory;
|
||||||
@Component(immediate = true)
|
@Component(immediate = true)
|
||||||
public class RootServlet extends DefaultServlet {
|
public class RootServlet extends DefaultServlet {
|
||||||
|
|
||||||
|
private static final String STATIC_CONTENT_FOLDER = "html";
|
||||||
|
private static final String OPENHAB_CONF_SYSPROPERTY = "openhab.conf";
|
||||||
|
private static final String STATIC_CONTENT_URL = "/static";
|
||||||
|
public static final String START_URL = "/start/index";
|
||||||
|
|
||||||
private static final long serialVersionUID = -2091860295954594917L;
|
private static final long serialVersionUID = -2091860295954594917L;
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(RootServlet.class);
|
private final Logger logger = LoggerFactory.getLogger(RootServlet.class);
|
||||||
|
@ -70,8 +79,8 @@ public class RootServlet extends DefaultServlet {
|
||||||
if (dashboardStarted != null) {
|
if (dashboardStarted != null) {
|
||||||
// all is up and running
|
// all is up and running
|
||||||
if (req.getRequestURI().equals("/")) {
|
if (req.getRequestURI().equals("/")) {
|
||||||
resp.sendRedirect("/start/index");
|
resp.sendRedirect(START_URL);
|
||||||
} else if (!req.getRequestURI().startsWith("/static/")) {
|
} else if (!req.getRequestURI().startsWith(STATIC_CONTENT_URL)) {
|
||||||
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
|
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
|
||||||
resp.setContentType("text/html;charset=UTF-8");
|
resp.setContentType("text/html;charset=UTF-8");
|
||||||
resp.getWriter().append(page404);
|
resp.getWriter().append(page404);
|
||||||
|
@ -105,8 +114,21 @@ public class RootServlet extends DefaultServlet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||||
@Activate
|
@Activate
|
||||||
protected void activate(ComponentContext context) {
|
protected void activate(ComponentContext context) {
|
||||||
|
|
||||||
|
// register static content context handler
|
||||||
|
ContextHandler staticContent = new ContextHandler();
|
||||||
|
ResourceHandler handler = new ResourceHandler();
|
||||||
|
handler.setDirectoriesListed(false);
|
||||||
|
handler.setResourceBase(System.getProperty(OPENHAB_CONF_SYSPROPERTY) + "/" + STATIC_CONTENT_FOLDER);
|
||||||
|
staticContent.setHandler(handler);
|
||||||
|
Dictionary props = new Hashtable();
|
||||||
|
props.put("contextPath", STATIC_CONTENT_URL);
|
||||||
|
context.getBundleContext().registerService(ContextHandler.class.getName(), staticContent, props);
|
||||||
|
|
||||||
|
// register servlet
|
||||||
try {
|
try {
|
||||||
httpService.registerServlet("/", this, new Properties(), httpService.createDefaultHttpContext());
|
httpService.registerServlet("/", this, new Properties(), httpService.createDefaultHttpContext());
|
||||||
} catch (ServletException | NamespaceException e) {
|
} catch (ServletException | NamespaceException e) {
|
||||||
|
|
Loading…
Reference in New Issue