]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/runtime/CmsDeploymentImpl.java
Improve SSH server. Rename node directory to private.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / runtime / CmsDeploymentImpl.java
1 package org.argeo.cms.internal.runtime;
2
3 import static org.argeo.api.cms.CmsConstants.CONTEXT_PATH;
4
5 import java.util.Map;
6 import java.util.TreeMap;
7
8 import org.argeo.api.cms.CmsConstants;
9 import org.argeo.api.cms.CmsDeployment;
10 import org.argeo.api.cms.CmsLog;
11 import org.argeo.api.cms.CmsState;
12 import org.argeo.cms.CmsDeployProperty;
13 import org.argeo.cms.CmsSshd;
14 import org.argeo.cms.internal.http.CmsAuthenticator;
15 import org.argeo.cms.internal.http.PublicCmsAuthenticator;
16
17 import com.sun.net.httpserver.HttpContext;
18 import com.sun.net.httpserver.HttpHandler;
19 import com.sun.net.httpserver.HttpServer;
20
21 /** Implementation of a CMS deployment. */
22 public class CmsDeploymentImpl implements CmsDeployment {
23 private final CmsLog log = CmsLog.getLog(getClass());
24
25 private CmsState cmsState;
26
27 // Expectations
28 private boolean httpExpected = false;
29 private boolean sshdExpected = false;
30
31 // HTTP
32 private HttpServer httpServer;
33 private Map<String, HttpHandler> httpHandlers = new TreeMap<>();
34 private Map<String, CmsAuthenticator> httpAuthenticators = new TreeMap<>();
35
36 // SSHD
37 private CmsSshd cmsSshd;
38
39 public void start() {
40 log.debug(() -> "CMS deployment available");
41 }
42
43 public void stop() {
44 }
45
46 public void setCmsState(CmsState cmsState) {
47 this.cmsState = cmsState;
48
49 String httpPort = this.cmsState.getDeployProperty(CmsDeployProperty.HTTP_PORT.getProperty());
50 String httpsPort = this.cmsState.getDeployProperty(CmsDeployProperty.HTTPS_PORT.getProperty());
51 httpExpected = httpPort != null || httpsPort != null;
52
53 String sshdPort = this.cmsState.getDeployProperty(CmsDeployProperty.SSHD_PORT.getProperty());
54 sshdExpected = sshdPort != null;
55 }
56
57 public void setHttpServer(HttpServer httpServer) {
58 this.httpServer = httpServer;
59 // create contexts whose handles had already been published
60 for (String contextPath : httpHandlers.keySet()) {
61 HttpHandler httpHandler = httpHandlers.get(contextPath);
62 CmsAuthenticator authenticator = httpAuthenticators.get(contextPath);
63 createHttpContext(contextPath, httpHandler, authenticator);
64 }
65 }
66
67 public void addHttpHandler(HttpHandler httpHandler, Map<String, String> properties) {
68 final String contextPath = properties.get(CONTEXT_PATH);
69 if (contextPath == null) {
70 log.warn("Property " + CONTEXT_PATH + " not set on HTTP handler " + properties + ". Ignoring it.");
71 return;
72 }
73 boolean isPublic = Boolean.parseBoolean(properties.get(CmsConstants.CONTEXT_PUBLIC));
74 CmsAuthenticator authenticator = isPublic ? new PublicCmsAuthenticator() : new CmsAuthenticator();
75 httpHandlers.put(contextPath, httpHandler);
76 httpAuthenticators.put(contextPath, authenticator);
77 if (httpServer == null)
78 return;
79 else
80 createHttpContext(contextPath, httpHandler, authenticator);
81 }
82
83 public void createHttpContext(String contextPath, HttpHandler httpHandler, CmsAuthenticator authenticator) {
84 HttpContext httpContext = httpServer.createContext(contextPath);
85 // we want to set the authenticator BEFORE the handler actually becomes active
86 httpContext.setAuthenticator(authenticator);
87 httpContext.setHandler(httpHandler);
88 log.debug(() -> "Added handler " + contextPath + " : " + httpHandler.getClass().getName());
89 }
90
91 public void removeHttpHandler(HttpHandler httpHandler, Map<String, String> properties) {
92 final String contextPath = properties.get(CmsConstants.CONTEXT_PATH);
93 if (contextPath == null)
94 return; // ignore silently
95 httpHandlers.remove(contextPath);
96 if (httpServer == null)
97 return;
98 httpServer.removeContext(contextPath);
99 log.debug(() -> "Removed handler " + contextPath + " : " + httpHandler.getClass().getName());
100 }
101
102 public boolean allExpectedServicesAvailable() {
103 if (httpExpected && httpServer == null)
104 return false;
105 if (sshdExpected && cmsSshd == null)
106 return false;
107 return true;
108 }
109
110 public void setCmsSshd(CmsSshd cmsSshd) {
111 this.cmsSshd = cmsSshd;
112 }
113
114 }