|
12 | 12 | */
|
13 | 13 | package org.openhab.ui.internal;
|
14 | 14 |
|
| 15 | +import java.io.IOException; |
| 16 | +import java.net.MalformedURLException; |
| 17 | +import java.net.URL; |
15 | 18 | import java.util.Map;
|
16 | 19 |
|
| 20 | +import javax.servlet.http.HttpServletRequest; |
| 21 | +import javax.servlet.http.HttpServletResponse; |
| 22 | + |
| 23 | +import org.openhab.core.config.core.ConfigConstants; |
17 | 24 | import org.openhab.core.net.HttpServiceUtil;
|
18 | 25 | import org.osgi.framework.BundleContext;
|
19 | 26 | import org.osgi.service.component.ComponentContext;
|
|
33 | 40 | * @author Yannick Schaus - Initial contribution
|
34 | 41 | */
|
35 | 42 | @Component(immediate = true, name = "org.openhab.ui")
|
36 |
| -public class UIService { |
| 43 | +public class UIService implements HttpContext { |
| 44 | + |
| 45 | + private static final String APP_BASE = "app"; |
| 46 | + private static final String STATIC_PATH = "/static"; |
| 47 | + private static final String STATIC_BASE = ConfigConstants.getConfigFolder() + "/html"; |
37 | 48 |
|
38 | 49 | private final Logger logger = LoggerFactory.getLogger(UIService.class);
|
39 | 50 |
|
40 | 51 | protected HttpService httpService;
|
| 52 | + protected HttpContext defaultHttpContext; |
| 53 | + |
| 54 | + @Override |
| 55 | + public boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException { |
| 56 | + return defaultHttpContext.handleSecurity(request, response); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public URL getResource(String name) { |
| 61 | + if (name.startsWith(APP_BASE + STATIC_PATH) && !name.endsWith("/")) { |
| 62 | + try { |
| 63 | + URL url = new java.io.File(STATIC_BASE + name.substring(new String(APP_BASE + STATIC_PATH).length())) |
| 64 | + .toURI().toURL(); |
| 65 | + logger.trace("Serving static file from {}", url); |
| 66 | + return url; |
| 67 | + } catch (MalformedURLException e) { |
| 68 | + logger.error("Error while serving static content: {}", e.getMessage()); |
| 69 | + return defaultHttpContext.getResource(name); |
| 70 | + } |
| 71 | + } else { |
| 72 | + return defaultHttpContext.getResource(name); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public String getMimeType(String name) { |
| 78 | + return defaultHttpContext.getMimeType(name); |
| 79 | + } |
41 | 80 |
|
42 | 81 | @Activate
|
43 | 82 | protected void activate(ComponentContext componentContext, Map<String, Object> properties) {
|
44 | 83 | BundleContext bundleContext = componentContext.getBundleContext();
|
45 |
| - HttpContext httpContext = httpService.createDefaultHttpContext(); |
| 84 | + defaultHttpContext = httpService.createDefaultHttpContext(); |
46 | 85 | try {
|
47 |
| - httpService.registerResources("/", "app", httpContext); |
| 86 | + httpService.registerResources("/", APP_BASE, this); |
48 | 87 | if (HttpServiceUtil.getHttpServicePort(bundleContext) > 0) {
|
49 | 88 | logger.info("Started UI on port {}", HttpServiceUtil.getHttpServicePort(bundleContext));
|
50 | 89 | } else {
|
|
0 commit comments