mirror of
https://github.com/danieldemus/openhab-core.git
synced 2026-07-29 12:34:22 +02:00
Fix broken ProxyServletService (#3436)
Reverts the partially working Whiteboard config changes of #3252 back to a working implementation using the HttpService. The configurable threading makes this a non-trivial Servlet for use with the Whiteboard. Fixes openhab/openhab-webui#1782 Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
+28
-8
@@ -24,6 +24,7 @@ import java.util.Map;
|
|||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
|
|
||||||
import javax.servlet.Servlet;
|
import javax.servlet.Servlet;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.ServletRequest;
|
import javax.servlet.ServletRequest;
|
||||||
import javax.servlet.http.HttpServlet;
|
import javax.servlet.http.HttpServlet;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
@@ -43,11 +44,12 @@ import org.openhab.core.types.State;
|
|||||||
import org.openhab.core.ui.items.ItemUIRegistry;
|
import org.openhab.core.ui.items.ItemUIRegistry;
|
||||||
import org.osgi.service.component.annotations.Activate;
|
import org.osgi.service.component.annotations.Activate;
|
||||||
import org.osgi.service.component.annotations.Component;
|
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.component.annotations.Reference;
|
||||||
import org.osgi.service.component.annotations.ReferenceCardinality;
|
import org.osgi.service.component.annotations.ReferenceCardinality;
|
||||||
import org.osgi.service.component.annotations.ReferencePolicy;
|
import org.osgi.service.component.annotations.ReferencePolicy;
|
||||||
import org.osgi.service.http.whiteboard.propertytypes.HttpWhiteboardServletName;
|
import org.osgi.service.http.HttpService;
|
||||||
import org.osgi.service.http.whiteboard.propertytypes.HttpWhiteboardServletPattern;
|
import org.osgi.service.http.NamespaceException;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@@ -78,8 +80,6 @@ import org.slf4j.LoggerFactory;
|
|||||||
*/
|
*/
|
||||||
@NonNullByDefault
|
@NonNullByDefault
|
||||||
@Component(immediate = true, property = { "service.pid=org.openhab.proxy" })
|
@Component(immediate = true, property = { "service.pid=org.openhab.proxy" })
|
||||||
@HttpWhiteboardServletName(ProxyServletService.PROXY_ALIAS)
|
|
||||||
@HttpWhiteboardServletPattern(ProxyServletService.PROXY_ALIAS + "/*")
|
|
||||||
public class ProxyServletService extends HttpServlet {
|
public class ProxyServletService extends HttpServlet {
|
||||||
|
|
||||||
/** the alias for this servlet */
|
/** the alias for this servlet */
|
||||||
@@ -95,18 +95,34 @@ public class ProxyServletService extends HttpServlet {
|
|||||||
|
|
||||||
private @Nullable Servlet impl;
|
private @Nullable Servlet impl;
|
||||||
|
|
||||||
|
protected final HttpService httpService;
|
||||||
protected final ItemUIRegistry itemUIRegistry;
|
protected final ItemUIRegistry itemUIRegistry;
|
||||||
protected final List<SitemapProvider> sitemapProviders = new CopyOnWriteArrayList<>();
|
protected final List<SitemapProvider> sitemapProviders = new CopyOnWriteArrayList<>();
|
||||||
|
|
||||||
@Activate
|
@Activate
|
||||||
public ProxyServletService(@Reference ItemUIRegistry itemUIRegistry, Map<String, Object> config) {
|
public ProxyServletService(@Reference HttpService httpService, @Reference ItemUIRegistry itemUIRegistry,
|
||||||
|
Map<String, Object> config) {
|
||||||
|
this.httpService = httpService;
|
||||||
this.itemUIRegistry = itemUIRegistry;
|
this.itemUIRegistry = itemUIRegistry;
|
||||||
|
|
||||||
Servlet servlet = getImpl();
|
Servlet servlet = getImpl();
|
||||||
|
|
||||||
logger.debug("Starting up '{}' servlet at /{}", servlet.getServletInfo(), PROXY_ALIAS);
|
logger.debug("Starting up '{}' servlet at /{}", servlet.getServletInfo(), PROXY_ALIAS);
|
||||||
|
try {
|
||||||
|
httpService.registerServlet("/" + PROXY_ALIAS, servlet, propsFromConfig(config, servlet),
|
||||||
|
httpService.createDefaultHttpContext());
|
||||||
|
} catch (NamespaceException | ServletException e) {
|
||||||
|
logger.error("Error during servlet startup: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Hashtable<String, @Nullable String> props = propsFromConfig(config);
|
@Deactivate
|
||||||
|
protected void deactivate() {
|
||||||
|
try {
|
||||||
|
httpService.unregister("/" + PROXY_ALIAS);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
// ignore, had not been registered before
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
|
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
|
||||||
@@ -142,7 +158,7 @@ public class ProxyServletService extends HttpServlet {
|
|||||||
* @param config the OSGi config, may be <code>null</code>
|
* @param config the OSGi config, may be <code>null</code>
|
||||||
* @return properties to pass to servlet for initialization
|
* @return properties to pass to servlet for initialization
|
||||||
*/
|
*/
|
||||||
private Hashtable<String, @Nullable String> propsFromConfig(Map<String, Object> config) {
|
private Hashtable<String, @Nullable String> propsFromConfig(Map<String, Object> config, Servlet servlet) {
|
||||||
Hashtable<String, @Nullable String> props = new Hashtable<>();
|
Hashtable<String, @Nullable String> props = new Hashtable<>();
|
||||||
|
|
||||||
for (String key : config.keySet()) {
|
for (String key : config.keySet()) {
|
||||||
@@ -155,6 +171,10 @@ public class ProxyServletService extends HttpServlet {
|
|||||||
String.valueOf(Math.max(DEFAULT_MAX_THREADS, Runtime.getRuntime().availableProcessors())));
|
String.valueOf(Math.max(DEFAULT_MAX_THREADS, Runtime.getRuntime().availableProcessors())));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (servlet instanceof AsyncProxyServlet) {
|
||||||
|
props.put("async-supported", "true");
|
||||||
|
}
|
||||||
|
|
||||||
return props;
|
return props;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -235,7 +255,7 @@ public class ProxyServletService extends HttpServlet {
|
|||||||
String itemName = widget.getItem();
|
String itemName = widget.getItem();
|
||||||
if (itemName != null) {
|
if (itemName != null) {
|
||||||
State state = itemUIRegistry.getItemState(itemName);
|
State state = itemUIRegistry.getItemState(itemName);
|
||||||
if (state != null && state instanceof StringType) {
|
if (state instanceof StringType) {
|
||||||
try {
|
try {
|
||||||
uri = createURIFromString(state.toString());
|
uri = createURIFromString(state.toString());
|
||||||
request.setAttribute(ATTR_URI, uri);
|
request.setAttribute(ATTR_URI, uri);
|
||||||
|
|||||||
+1
-1
@@ -85,7 +85,7 @@ public class ProxyServletServiceTest {
|
|||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
service = new ProxyServletService(itemUIRegistryMock, Map.of());
|
service = new ProxyServletService(httpServiceMock, itemUIRegistryMock, Map.of());
|
||||||
service.sitemapProviders.add(sitemapProviderMock);
|
service.sitemapProviders.add(sitemapProviderMock);
|
||||||
|
|
||||||
sitemapMock = mock(Sitemap.class);
|
sitemapMock = mock(Sitemap.class);
|
||||||
|
|||||||
Reference in New Issue
Block a user