Fix cardinality of HTTP handlers
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / runtime / CmsDeploymentImpl.java
index 44a2866f8df50613cbeede05262e66c660272428..421ff661aeb2decf812a3f9a2baec23a4ebbd31b 100644 (file)
 package org.argeo.cms.internal.runtime;
 
-import java.io.IOException;
-import java.net.URL;
-import java.util.Dictionary;
+import static org.argeo.api.cms.CmsConstants.CONTEXT_PATH;
 
+import java.util.Map;
+import java.util.TreeMap;
+
+import org.argeo.api.cms.CmsConstants;
 import org.argeo.api.cms.CmsDeployment;
 import org.argeo.api.cms.CmsLog;
 import org.argeo.api.cms.CmsState;
-import org.argeo.cms.internal.osgi.DeployConfig;
-import org.osgi.service.http.HttpService;
+import org.argeo.cms.CmsDeployProperty;
+import org.argeo.cms.CmsSshd;
+import org.argeo.cms.internal.http.CmsAuthenticator;
+import org.argeo.cms.internal.http.PublicCmsAuthenticator;
+
+import com.sun.net.httpserver.HttpContext;
+import com.sun.net.httpserver.HttpHandler;
+import com.sun.net.httpserver.HttpServer;
 
 /** Implementation of a CMS deployment. */
 public class CmsDeploymentImpl implements CmsDeployment {
        private final CmsLog log = CmsLog.getLog(getClass());
 
-       // Readiness
+       private CmsState cmsState;
+
+       // Expectations
        private boolean httpExpected = false;
-       private HttpService httpService;
+       private boolean sshdExpected = false;
 
-       private CmsState cmsState;
-       private DeployConfig deployConfig;
+       // HTTP
+       private HttpServer httpServer;
+       private Map<String, HttpHandler> httpHandlers = new TreeMap<>();
+       private Map<String, CmsAuthenticator> httpAuthenticators = new TreeMap<>();
+
+       // SSHD
+       private CmsSshd cmsSshd;
 
        public void start() {
-               httpExpected = deployConfig.getProps(KernelConstants.JETTY_FACTORY_PID, "default") != null;
-               if (deployConfig.hasDomain()) {
-                       loadIpaJaasConfiguration();
-               }
+               log.debug(() -> "CMS deployment available");
        }
 
-       public void addFactoryDeployConfig(String factoryPid, Dictionary<String, Object> props) {
-               deployConfig.putFactoryDeployConfig(factoryPid, props);
-               deployConfig.save();
-               try {
-                       deployConfig.loadConfigs();
-               } catch (IOException e) {
-                       throw new IllegalStateException(e);
-               }
+       public void stop() {
        }
 
-       public Dictionary<String, Object> getProps(String factoryPid, String cn) {
-               return deployConfig.getProps(factoryPid, cn);
-       }
+       public void setCmsState(CmsState cmsState) {
+               this.cmsState = cmsState;
+
+               String httpPort = this.cmsState.getDeployProperty(CmsDeployProperty.HTTP_PORT.getProperty());
+               String httpsPort = this.cmsState.getDeployProperty(CmsDeployProperty.HTTPS_PORT.getProperty());
+               httpExpected = httpPort != null || httpsPort != null;
 
-       public boolean isHttpAvailableOrNotExpected() {
-               return (httpExpected ? httpService != null : true);
+               String sshdPort = this.cmsState.getDeployProperty(CmsDeployProperty.SSHD_PORT.getProperty());
+               sshdExpected = sshdPort != null;
        }
 
-       private void loadIpaJaasConfiguration() {
-               if (System.getProperty(KernelConstants.JAAS_CONFIG_PROP) == null) {
-                       String jaasConfig = KernelConstants.JAAS_CONFIG_IPA;
-                       URL url = getClass().getClassLoader().getResource(jaasConfig);
-                       KernelUtils.setJaasConfiguration(url);
-                       log.debug("Set IPA JAAS configuration.");
+       public void setHttpServer(HttpServer httpServer) {
+               this.httpServer = httpServer;
+               // create contexts whose handles had already been published
+               for (String contextPath : httpHandlers.keySet()) {
+                       HttpHandler httpHandler = httpHandlers.get(contextPath);
+                       CmsAuthenticator authenticator = httpAuthenticators.get(contextPath);
+                       createHttpContext(contextPath, httpHandler, authenticator);
                }
        }
 
-       public void stop() {
-               if (deployConfig != null) {
-                       deployConfig.save();
+       public void addHttpHandler(HttpHandler httpHandler, Map<String, String> properties) {
+               final String contextPath = properties.get(CONTEXT_PATH);
+               if (contextPath == null) {
+                       log.warn("Property " + CONTEXT_PATH + " not set on HTTP handler " + properties + ". Ignoring it.");
+                       return;
+               }
+               boolean isPublic = Boolean.parseBoolean(properties.get(CmsConstants.CONTEXT_PUBLIC));
+               CmsAuthenticator authenticator = isPublic ? new PublicCmsAuthenticator() : new CmsAuthenticator();
+               httpHandlers.put(contextPath, httpHandler);
+               httpAuthenticators.put(contextPath, authenticator);
+               if (httpServer == null) {
+                       return;
+               } else {
+                       createHttpContext(contextPath, httpHandler, authenticator);
                }
        }
 
-       public void setDeployConfig(DeployConfig deployConfig) {
-               this.deployConfig = deployConfig;
+       public void createHttpContext(String contextPath, HttpHandler httpHandler, CmsAuthenticator authenticator) {
+               HttpContext httpContext = httpServer.createContext(contextPath);
+               // we want to set the authenticator BEFORE the handler actually becomes active
+               httpContext.setAuthenticator(authenticator);
+               httpContext.setHandler(httpHandler);
+               log.debug(() -> "Added handler " + contextPath + " : " + httpHandler.getClass().getName());
        }
 
-       public void setCmsState(CmsState cmsState) {
-               this.cmsState = cmsState;
+       public void removeHttpHandler(HttpHandler httpHandler, Map<String, String> properties) {
+               final String contextPath = properties.get(CmsConstants.CONTEXT_PATH);
+               if (contextPath == null)
+                       return; // ignore silently
+               httpHandlers.remove(contextPath);
+               if (httpServer == null)
+                       return;
+               httpServer.removeContext(contextPath);
+               log.debug(() -> "Removed handler " + contextPath + " : " + httpHandler.getClass().getName());
+       }
+
+       public boolean allExpectedServicesAvailable() {
+               if (httpExpected && httpServer == null)
+                       return false;
+               if (sshdExpected && cmsSshd == null)
+                       return false;
+               return true;
        }
 
-       public void setHttpService(HttpService httpService) {
-               this.httpService = httpService;
+       public void setCmsSshd(CmsSshd cmsSshd) {
+               this.cmsSshd = cmsSshd;
        }
 
 }