100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Add skeleton for JS servlet
Christian Bauer397fce8ee8aa
diff --git a/container/src/main/java/org/openremote/container/security/IdentityResource.java b/container/src/main/java/org/openremote/container/security/IdentityResource.javaindex 90d2ec359..b17c37456 100644--- a/container/src/main/java/org/openremote/container/security/IdentityResource.java+++ b/container/src/main/java/org/openremote/container/security/IdentityResource.java@@ -20,7 +20,7 @@ public class IdentityResource extends WebResource {@Produces(MediaType.APPLICATION_JSON)public ClientInstall getClientInstall(@PathParam("clientId") String clientId) {SecuredClientApplication clientApplication =- identityService.getSecureClientApplication(getRealm(), clientId);+ identityService.getSecuredClientApplication(getRealm(), clientId);if (clientApplication == null)throw new WebApplicationException(Response.Status.NOT_FOUND);return clientApplication.clientInstall;diff --git a/container/src/main/java/org/openremote/container/security/IdentityService.java b/container/src/main/java/org/openremote/container/security/IdentityService.javaindex 4d70de11a..0033ef704 100644--- a/container/src/main/java/org/openremote/container/security/IdentityService.java+++ b/container/src/main/java/org/openremote/container/security/IdentityService.java@@ -13,7 +13,6 @@ import org.keycloak.adapters.KeycloakDeployment;import org.keycloak.common.enums.SslRequired;import org.keycloak.common.util.PemUtils;import org.keycloak.representations.adapters.config.AdapterConfig;-import org.openremote.container.Constants;import org.openremote.container.Container;import org.openremote.container.ContainerService;import org.openremote.container.observable.RetryWithDelay;@@ -132,7 +131,7 @@ public abstract class IdentityService implements ContainerService {String realm = request.getQueryParamValue("realm");if (realm == null || realm.length() == 0)throw new IllegalStateException("Can't authenticate API call without realm parameter");- SecuredClientApplication clientApplication = getSecureClientApplication(realm, getClientId());+ SecuredClientApplication clientApplication = getSecuredClientApplication(realm, getClientId());if (clientApplication == null)throw new RuntimeException("No Keycloak client application found for realm: " + realm);return clientApplication.keycloakDeployment;@@ -223,7 +222,7 @@ public abstract class IdentityService implements ContainerService {).toBlocking().singleOrDefault(false);}- public SecuredClientApplication getSecureClientApplication(String realm, String clientId) {+ public SecuredClientApplication getSecuredClientApplication(String realm, String clientId) {try {return clientApplicationCache.get(new SecuredClientApplication.Key(realm, clientId));} catch (Exception ex) {diff --git a/container/src/main/java/org/openremote/container/web/WebService.java b/container/src/main/java/org/openremote/container/web/WebService.javaindex e8cc00b5b..c5b501322 100644--- a/container/src/main/java/org/openremote/container/web/WebService.java+++ b/container/src/main/java/org/openremote/container/web/WebService.java@@ -47,6 +47,7 @@ public abstract class WebService implements ContainerService {public static final int WEB_SERVER_LISTEN_PORT_DEFAULT = 8080;public static final String API_PATH = "/api";+ public static final String JSAPI_PATH = "/jsapi";public static final String STATIC_PATH = "/static";protected final Pattern PATTERN_STATIC = Pattern.compile(Pattern.quote(STATIC_PATH) + "(\\/.*)?");protected final Pattern PATTERN_REALM_ROOT = Pattern.compile("\\/([a-z]+)\\/?");@@ -92,9 +93,11 @@ public abstract class WebService implements ContainerService {protected Undertow.Builder build(Container container, Undertow.Builder builder) {Path docRoot = getStaticResourceDocRoot(container);- HttpHandler staticResourceHandler = docRoot != null ? createStaticResourceHandler(container, docRoot) : null;+ WebApplication webApplication = getWebApplication(container);- HttpHandler apiHandler = createApiHandler(container);+ HttpHandler staticResourceHandler = docRoot != null ? createStaticResourceHandler(container, docRoot) : null;+ HttpHandler apiHandler = createApiHandler(container, webApplication);+ HttpHandler jsApiHandler = createJsApiHandler(container, webApplication);HttpHandler handler = exchange -> {String requestPath = exchange.getRequestPath();@@ -105,7 +108,6 @@ public abstract class WebService implements ContainerService {for (Map.Entry<String, HttpHandler> entry : getPrefixRoutes().entrySet()) {if (requestPath.startsWith(entry.getKey())) {LOG.fine("Handling with '" + entry.getValue().getClass().getName() + "' path prefix: " + entry.getKey());- ;entry.getValue().handleRequest(exchange);handled = true;break;@@ -148,6 +150,13 @@ public abstract class WebService implements ContainerService {}}+ // Serve JavaScript API with path /jsapi/*+ if (jsApiHandler != null && requestPath.startsWith(JSAPI_PATH)) {+ LOG.fine("Serving JS API call: " + requestPath);+ jsApiHandler.handleRequest(exchange);+ return;+ }+// Serve API with path /<realm>/*Matcher realmSubMatcher = PATTERN_REALM_SUB.matcher(requestPath);if (apiHandler != null && realmSubMatcher.matches()) {@@ -199,15 +208,12 @@ public abstract class WebService implements ContainerService {.setMimeMappings(mimeBuilder.build());}- protected HttpHandler createApiHandler(Container container) {- Application restApplication = getRestApplication(container);- if (restApplication == null)+ protected HttpHandler createApiHandler(Container container, WebApplication webApplication) {+ if (webApplication == null)return null;- LOG.fine("Registering REST application: " + restApplication.getClass().getName());-ResteasyDeployment resteasyDeployment = new ResteasyDeployment();- resteasyDeployment.setApplication(restApplication);+ resteasyDeployment.setApplication(webApplication);// Custom providers (these only apply to server applications, not client calls)resteasyDeployment.getActualProviderClasses().add(JacksonConfig.class);@@ -222,7 +228,7 @@ public abstract class WebService implements ContainerService {.setContextPath(API_PATH).addServletContextAttribute(ResteasyDeployment.class.getName(), resteasyDeployment).addServlet(restServlet).setDeploymentName("RESTEasy Deployment")- .setClassLoader(restApplication.getClass().getClassLoader());+ .setClassLoader(Container.class.getClassLoader());if (getKeycloakConfigResolver() != null) {resteasyDeployment.setSecurityEnabled(true);@@ -240,6 +246,36 @@ public abstract class WebService implements ContainerService {}}+ protected HttpHandler createJsApiHandler(Container container, WebApplication webApplication) {+ if (webApplication == null)+ return null;+ /*+ TODO We don't really need security and realms etc. here.+ Anyone should be able to get API client code/metadata.+ Servles.defaultContainer() can be called multiple times.+ */+ /*+ ServletInfo jsApiServlet = Servlets.servlet("My Servlet", MyServlet.class)+ .setAsyncSupported(true)+ .setLoadOnStartup(1)+ .addMapping("/*");++ DeploymentInfo deploymentInfo = new DeploymentInfo()+ .setContextPath(JSAPI_PATH)+ .addServlet(jsApiServlet).setDeploymentName("JS API Servlet")+ .setClassLoader(Container.class.getClassLoader());++ try {+ DeploymentManager manager = Servlets.defaultContainer().addDeployment(deploymentInfo);+ manager.deploy();+ return manager.start();+ } catch (Exception ex) {+ throw new RuntimeException(ex);+ }+ */+ return null;+ }+/*** Override this method to serve static resources from file system on the /static/* path.*/@@ -247,7 +283,7 @@ public abstract class WebService implements ContainerService {return null;}- protected WebApplication getRestApplication(Container container) {+ protected WebApplication getWebApplication(Container container) {if (getApiClasses() != null || getApiSingletons() != null) {return new WebApplication(container, getApiClasses(), getApiSingletons());}