Skip to content

Commit

Permalink
Upgrade to Karaf 4.4.3
Browse files Browse the repository at this point in the history
* Rework Servlets to use Http Whiteboard annotations in favor of proprietary `org.openhab.core.io.http.servlet` classes

Also-by: Jan N. Klug <[email protected]>
Signed-off-by: Wouter Born <[email protected]>
  • Loading branch information
wborn committed Jan 15, 2023
1 parent e012d94 commit 8aace62
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public EList<Widget> renderWidget(Widget w, StringBuilder sb, String sitemap) th
snippet = snippet.replace("%purelabel%", purelabel);
}
snippet = snippet.replace("%frequency%", frequency);
snippet = snippet.replace("%servletname%", WebAppServlet.SERVLET_NAME);
snippet = snippet.replace("%servletname%", WebAppServlet.SERVLET_PATH);

String style = "";
String color = itemUIRegistry.getLabelColor(w);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public StringBuilder processPage(String id, String sitemap, String label, EList<
labelPlain = labelPlain.replace("[", "").replace("]", "");
}
snippet = snippet.replace("%label%", escapeHtml(labelPlain));
snippet = snippet.replace("%servletname%", WebAppServlet.SERVLET_NAME);
snippet = snippet.replace("%servletname%", WebAppServlet.SERVLET_PATH);
snippet = snippet.replace("%sitemap%", sitemap);
snippet = snippet.replace("%htmlclass%", config.getCssClassList());
snippet = snippet.replace("%icon_type%", ICON_TYPE);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@

import java.io.IOException;

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

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.events.EventPublisher;
import org.openhab.core.io.http.HttpContextFactoryService;
import org.openhab.core.items.GroupItem;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemNotFoundException;
Expand All @@ -30,12 +31,15 @@
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.types.Command;
import org.openhab.core.types.TypeParser;
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.HttpService;
import org.osgi.service.http.whiteboard.propertytypes.HttpWhiteboardResource;
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;

/**
* This servlet receives events from the web app and sends these as
Expand All @@ -45,32 +49,27 @@
* @author Stefan Bußweiler - Migration to new ESH event concept
*
*/
@Component(immediate = true, service = {})
@Component(immediate = true, service = Servlet.class)
@HttpWhiteboardResource(pattern = "/basicui/*", prefix = "/web")
@HttpWhiteboardServletAsyncSupported(asyncSupported = true)
@HttpWhiteboardServletName(CmdServlet.SERVLET_PATH)
@HttpWhiteboardServletPattern(CmdServlet.SERVLET_PATH + "/*")
@NonNullByDefault
public class CmdServlet extends BaseServlet {
public class CmdServlet extends HttpServlet {

private static final long serialVersionUID = 4813813926991230571L;

public static final String SERVLET_NAME = "CMD";
public static final String SERVLET_PATH = "/basicui/CMD";

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

private final EventPublisher eventPublisher;
private final ItemRegistry itemRegistry;

@Activate
public CmdServlet(final @Reference HttpService httpService,
final @Reference HttpContextFactoryService httpContextFactoryService,
final @Reference ItemRegistry itemRegistry, final @Reference EventPublisher eventPublisher) {
super(httpService, httpContextFactoryService, itemRegistry);
public CmdServlet(final @Reference ItemRegistry itemRegistry, final @Reference EventPublisher eventPublisher) {
this.eventPublisher = eventPublisher;
}

@Activate
protected void activate(BundleContext bundleContext) {
super.activate(WEBAPP_ALIAS + "/" + SERVLET_NAME, bundleContext);
}

@Deactivate
protected void deactivate() {
httpService.unregister(WEBAPP_ALIAS + "/" + SERVLET_NAME);
this.itemRegistry = itemRegistry;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,43 @@

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

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

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.io.http.HttpContextFactoryService;
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;

/**
* This is the manifest servlet for the Basic UI. It dynamically genrates a
* This is the manifest servlet for the Basic UI. It dynamically generates a
* manifest on the sitemap model.
*
* @author Kevin Haunschmied - Initial contribution
*
*/
@Component(immediate = true, service = {})
@Component(immediate = true, service = Servlet.class)
@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 @@ -59,24 +60,10 @@ public class ManifestServlet extends BaseServlet {
private final PageRenderer renderer;

@Activate
public ManifestServlet(final @Reference HttpService httpService,
final @Reference HttpContextFactoryService httpContextFactoryService,
final @Reference ItemRegistry itemRegistry, final @Reference PageRenderer renderer) {
super(httpService, httpContextFactoryService, itemRegistry);
public ManifestServlet(final @Reference PageRenderer renderer) {
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);
}

private void generateManifest(ServletResponse res, @Nullable String sitemapName)
throws IOException, RenderException {
PrintWriter resWriter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.emf.common.util.EList;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.config.core.ConfigurableService;
import org.openhab.core.io.http.HttpContextFactoryService;
import org.openhab.core.io.rest.sitemap.SitemapSubscriptionService;
import org.openhab.core.items.ItemRegistry;
import org.openhab.core.model.sitemap.SitemapProvider;
import org.openhab.core.model.sitemap.sitemap.LinkableWidget;
import org.openhab.core.model.sitemap.sitemap.Sitemap;
Expand All @@ -46,9 +45,9 @@
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
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.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 @@ -63,8 +62,11 @@
@Component(immediate = true, service = Servlet.class, configurationPid = "org.openhab.basicui", //
property = Constants.SERVICE_PID + "=org.openhab.basicui")
@ConfigurableService(category = "ui", label = "Basic UI", description_uri = WebAppServlet.CONFIG_URI)
@HttpWhiteboardServletAsyncSupported(asyncSupported = true)
@HttpWhiteboardServletName(WebAppServlet.SERVLET_PATH)
@HttpWhiteboardServletPattern(WebAppServlet.SERVLET_PATH + "/*")
@NonNullByDefault
public class WebAppServlet extends BaseServlet {
public class WebAppServlet extends HttpServlet {

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

Expand All @@ -73,7 +75,7 @@ public class WebAppServlet extends BaseServlet {
protected static final String CONFIG_URI = "ui:basic";

/** the name of the servlet to be used in the URL */
public static final String SERVLET_NAME = "app";
public static final String SERVLET_PATH = "/basicui/app";

private static final String CONTENT_TYPE_ASYNC = "application/xml;charset=UTF-8";
private static final String CONTENT_TYPE = "text/html;charset=UTF-8";
Expand All @@ -84,11 +86,8 @@ public class WebAppServlet extends BaseServlet {
protected final Set<SitemapProvider> sitemapProviders = new CopyOnWriteArraySet<>();

@Activate
public WebAppServlet(final @Reference HttpService httpService,
final @Reference HttpContextFactoryService httpContextFactoryService,
final @Reference ItemRegistry itemRegistry, final @Reference SitemapSubscriptionService subscriptions,
public WebAppServlet(final @Reference SitemapSubscriptionService subscriptions,
final @Reference PageRenderer renderer) {
super(httpService, httpContextFactoryService, itemRegistry);
this.subscriptions = subscriptions;
this.renderer = renderer;
}
Expand All @@ -104,15 +103,6 @@ public void removeSitemapProvider(SitemapProvider sitemapProvider) {

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

try {
httpService.registerResources(WEBAPP_ALIAS, "web", httpContext);
} catch (NamespaceException e) {
logger.error("Could not register static resources under {}", WEBAPP_ALIAS, e);
}

modified(configProps);
}

Expand All @@ -124,8 +114,6 @@ protected void modified(Map<String, Object> configProps) {

@Deactivate
protected void deactivate() {
super.deactivate(WEBAPP_ALIAS + "/" + SERVLET_NAME);
httpService.unregister(WEBAPP_ALIAS);
logger.info("Stopped Basic UI");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
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.HttpService;
import org.osgi.service.http.NamespaceException;
import org.osgi.service.http.whiteboard.propertytypes.HttpWhiteboardResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -31,29 +29,21 @@
*
*/
@Component(service = Tile.class, immediate = true)
@HttpWhiteboardResource(pattern = HABPanelTile.RESOURCE_PATH + "/*", prefix = "/web")
@NonNullByDefault
public class HABPanelTile implements Tile {

public static final String HABPANEL_ALIAS = "/habpanel";
public static final String RESOURCE_PATH = "/habpanel";

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

private final HttpService httpService;

@Activate
public HABPanelTile(final @Reference HttpService httpService) {
this.httpService = httpService;
try {
httpService.registerResources(HABPANEL_ALIAS, "web", null);
logger.info("Started HABPanel at {}", HABPANEL_ALIAS);
} catch (NamespaceException e) {
logger.error("Error during HABPanel startup: {}", e.getMessage());
}
public HABPanelTile() {
logger.info("Started HABPanel at {}", RESOURCE_PATH);
}

@Deactivate
protected void deactivate() {
httpService.unregister(HABPANEL_ALIAS);
logger.info("Stopped HABPanel");
}

Expand All @@ -64,7 +54,7 @@ public String getName() {

@Override
public String getUrl() {
return "/habpanel/index.html";
return RESOURCE_PATH + "/index.html";
}

@Override
Expand All @@ -74,6 +64,6 @@ public String getUrl() {

@Override
public String getImageUrl() {
return "/habpanel/tile.png";
return RESOURCE_PATH + "/tile.png";
}
}
Loading

0 comments on commit 8aace62

Please sign in to comment.