Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Moved webservice specific code from AbstractHTTPServerProtocol to WebService for better reusability (#1773)

Michael Rademaker8c2b12c0ec80
diff --git a/agent/src/main/java/org/openremote/agent/protocol/http/AbstractHTTPServerProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/http/AbstractHTTPServerProtocol.java
index e8c66396d..378fb6f57 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/http/AbstractHTTPServerProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/http/AbstractHTTPServerProtocol.java
@@ -19,7 +19,6 @@
*/
package org.openremote.agent.protocol.http;
-import com.google.common.collect.Lists;
import io.undertow.server.HttpHandler;
import io.undertow.servlet.Servlets;
import io.undertow.servlet.api.DeploymentInfo;
@@ -31,7 +30,6 @@ import org.jboss.resteasy.plugins.interceptors.CorsFilter;
import org.jboss.resteasy.plugins.server.servlet.HttpServlet30Dispatcher;
import org.jboss.resteasy.spi.ResteasyDeployment;
import org.openremote.agent.protocol.AbstractProtocol;
-import org.openremote.container.json.JacksonConfig;
import org.openremote.container.security.IdentityService;
import org.openremote.container.web.*;
import org.openremote.model.Constants;
@@ -45,12 +43,15 @@ import org.openremote.model.util.TextUtil;
import jakarta.servlet.ServletException;
import jakarta.ws.rs.core.Application;
+
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
+import static org.openremote.container.web.WebService.configureDeploymentInfo;
+import static org.openremote.container.web.WebService.getStandardProviders;
import static org.openremote.container.web.WebService.pathStartsWithHandler;
import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
@@ -95,13 +96,7 @@ public abstract class AbstractHTTPServerProtocol<T extends AbstractHTTPServerPro
public static final Pattern PATH_REGEX = Pattern.compile("^[\\w/_]+$", Pattern.CASE_INSENSITIVE);
private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, AbstractHTTPServerProtocol.class);
public static final HTTPMethod[] DEFAULT_ALLOWED_METHODS = HTTPMethod.values();
- protected DeploymentInstance deployment;
- protected static WebServiceExceptions.DefaultResteasyExceptionMapper defaultResteasyExceptionMapper;
- protected static WebServiceExceptions.ForbiddenResteasyExceptionMapper forbiddenResteasyExceptionMapper;
- protected static JacksonConfig jacksonConfig;
- protected static AlreadyGzippedWriterInterceptor alreadyGzippedWriterInterceptor;
- protected static ClientErrorExceptionHandler clientErrorExceptionHandler;
- protected static WebServiceExceptions.ServletUndertowExceptionHandler undertowExceptionHandler;
+ protected WebService.DeploymentInstance deployment;
protected Container container;
protected boolean devMode;
protected IdentityService identityService;
@@ -122,15 +117,6 @@ public abstract class AbstractHTTPServerProtocol<T extends AbstractHTTPServerPro
webService = container.getService(WebService.class);
- if (defaultResteasyExceptionMapper == null) {
- defaultResteasyExceptionMapper = new WebServiceExceptions.DefaultResteasyExceptionMapper(devMode);
- forbiddenResteasyExceptionMapper = new WebServiceExceptions.ForbiddenResteasyExceptionMapper(devMode);
- undertowExceptionHandler = new WebServiceExceptions.ServletUndertowExceptionHandler(devMode);
- jacksonConfig = new JacksonConfig();
- alreadyGzippedWriterInterceptor = new AlreadyGzippedWriterInterceptor();
- clientErrorExceptionHandler = new ClientErrorExceptionHandler();
- }
-
Application application = createApplication();
ResteasyDeployment deployment = createDeployment(application);
DeploymentInfo deploymentInfo = createDeploymentInfo(deployment);
@@ -144,7 +130,7 @@ public abstract class AbstractHTTPServerProtocol<T extends AbstractHTTPServerPro
}
protected Application createApplication() {
- List<Object> providers = getStandardProviders();
+ List<Object> providers = getStandardProviders(this.devMode);
providers = providers == null ? new ArrayList<>() : providers;
providers.addAll(getApiSingletons());
return new WebApplication(container, null, providers);
@@ -234,10 +220,10 @@ public abstract class AbstractHTTPServerProtocol<T extends AbstractHTTPServerPro
*/
protected String getDeploymentPath() throws IllegalArgumentException {
String path = agent.getDeploymentPath()
- .map(String::toLowerCase)
- .orElseThrow(() ->
- new IllegalArgumentException(
- "Required deployment path attribute is missing or invalid: " + agent));
+ .map(String::toLowerCase)
+ .orElseThrow(() ->
+ new IllegalArgumentException(
+ "Required deployment path attribute is missing or invalid: " + agent));
String deploymentPath = getDeploymentPathPrefix() + "/" + path;
@@ -249,24 +235,6 @@ public abstract class AbstractHTTPServerProtocol<T extends AbstractHTTPServerPro
return deploymentPath;
}
- /**
- * Get standard JAX-RS providers that are used in the deployment.
- */
- protected List<Object> getStandardProviders() {
- return Lists.newArrayList(
- defaultResteasyExceptionMapper,
- forbiddenResteasyExceptionMapper,
- jacksonConfig,
- alreadyGzippedWriterInterceptor,
- clientErrorExceptionHandler
- );
- }
-
- protected void configureDeploymentInfo(DeploymentInfo deploymentInfo) {
- // This will catch anything not handled by Resteasy/Servlets, such as IOExceptions "at the wrong time"
- deploymentInfo.setExceptionHandler(undertowExceptionHandler);
- }
-
/**
* Get a unique deployment name for this instance.
*/
@@ -304,7 +272,7 @@ public abstract class AbstractHTTPServerProtocol<T extends AbstractHTTPServerPro
// Add the handler before the greedy deployment handler
webService.getRequestHandlers().add(0, requestHandler);
- deployment = new DeploymentInstance(deploymentInfo, requestHandler);
+ deployment = new WebService.DeploymentInstance(deploymentInfo, requestHandler);
} catch (ServletException e) {
LOG.severe("Failed to deploy deployment: " + deploymentInfo.getDeploymentName());
}
@@ -321,14 +289,14 @@ public abstract class AbstractHTTPServerProtocol<T extends AbstractHTTPServerPro
LOG.info("Un-registering HTTP Server Protocol request handler '"
+ this.getClass().getSimpleName()
+ "' for request path: "
- + deployment.deploymentInfo.getContextPath());
- webService.getRequestHandlers().remove(deployment.requestHandler);
- DeploymentManager manager = Servlets.defaultContainer().getDeployment(deployment.deploymentInfo.getDeploymentName());
+ + deployment.getDeploymentInfo().getContextPath());
+ webService.getRequestHandlers().remove(deployment.getRequestHandler());
+ DeploymentManager manager = Servlets.defaultContainer().getDeployment(deployment.getDeploymentInfo().getDeploymentName());
if (manager != null) {
manager.stop();
manager.undeploy();
}
- Servlets.defaultContainer().removeDeployment(deployment.deploymentInfo);
+ Servlets.defaultContainer().removeDeployment(deployment.getDeploymentInfo());
} catch (Exception ex) {
LOG.log(Level.WARNING,
"An exception occurred whilst un-deploying protocol instance: " + this,
diff --git a/container/src/main/java/org/openremote/container/web/WebService.java b/container/src/main/java/org/openremote/container/web/WebService.java
index 0858306b3..cb60dadae 100644
--- a/container/src/main/java/org/openremote/container/web/WebService.java
+++ b/container/src/main/java/org/openremote/container/web/WebService.java
@@ -19,6 +19,7 @@
*/
package org.openremote.container.web;
+import com.google.common.collect.Lists;
import io.undertow.Undertow;
import io.undertow.security.api.SecurityContext;
import io.undertow.security.idm.Account;
@@ -85,6 +86,24 @@ public abstract class WebService implements ContainerService {
}
}
+ public static class DeploymentInstance {
+ protected DeploymentInfo deploymentInfo;
+ protected WebService.RequestHandler requestHandler;
+
+ public DeploymentInstance(DeploymentInfo deploymentInfo, WebService.RequestHandler requestHandler) {
+ this.deploymentInfo = deploymentInfo;
+ this.requestHandler = requestHandler;
+ }
+
+ public WebService.RequestHandler getRequestHandler() {
+ return requestHandler;
+ }
+
+ public DeploymentInfo getDeploymentInfo() {
+ return deploymentInfo;
+ }
+ }
+
// Change this to 0.0.0.0 to bind on all interfaces, enabling
// access of the manager service from other devices in your LAN
public static final String OR_WEBSERVER_LISTEN_HOST = "OR_WEBSERVER_LISTEN_HOST";
@@ -106,6 +125,13 @@ public abstract class WebService implements ContainerService {
protected Undertow undertow;
protected List<RequestHandler> httpHandlers = new ArrayList<>();
protected URI containerHostUri;
+ protected static WebServiceExceptions.DefaultResteasyExceptionMapper defaultResteasyExceptionMapper;
+ protected static WebServiceExceptions.ForbiddenResteasyExceptionMapper forbiddenResteasyExceptionMapper;
+ protected static JacksonConfig jacksonConfig;
+ protected static AlreadyGzippedWriterInterceptor alreadyGzippedWriterInterceptor;
+ protected static ClientErrorExceptionHandler clientErrorExceptionHandler;
+ protected static WebServiceExceptions.ServletUndertowExceptionHandler undertowExceptionHandler;
+
protected static String getLocalIpAddress() throws Exception {
return Inet4Address.getLocalHost().getHostAddress();
@@ -216,6 +242,32 @@ public abstract class WebService implements ContainerService {
}
}
+ public static void configureDeploymentInfo(DeploymentInfo deploymentInfo) {
+ // This will catch anything not handled by Resteasy/Servlets, such as IOExceptions "at the wrong time"
+ deploymentInfo.setExceptionHandler(undertowExceptionHandler);
+ }
+
+ /**
+ * Get standard JAX-RS providers that are used in the deployment.
+ */
+ public static List<Object> getStandardProviders(boolean devMode) {
+ if (defaultResteasyExceptionMapper == null) {
+ defaultResteasyExceptionMapper = new WebServiceExceptions.DefaultResteasyExceptionMapper(devMode);
+ forbiddenResteasyExceptionMapper = new WebServiceExceptions.ForbiddenResteasyExceptionMapper(devMode);
+ undertowExceptionHandler = new WebServiceExceptions.ServletUndertowExceptionHandler(devMode);
+ jacksonConfig = new JacksonConfig();
+ alreadyGzippedWriterInterceptor = new AlreadyGzippedWriterInterceptor();
+ clientErrorExceptionHandler = new ClientErrorExceptionHandler();
+ }
+ return Lists.newArrayList(
+ defaultResteasyExceptionMapper,
+ forbiddenResteasyExceptionMapper,
+ jacksonConfig,
+ alreadyGzippedWriterInterceptor,
+ clientErrorExceptionHandler
+ );
+ }
+
/**
* When a request comes in the handlers are called in order until a handler returns a non null value; when this
* happens the returned {@link RequestHandler} is invoked.