1 package org
.argeo
.cms
.internal
.runtime
;
3 import static org
.argeo
.api
.cms
.CmsConstants
.CONTEXT_PATH
;
6 import java
.util
.TreeMap
;
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
;
17 import com
.sun
.net
.httpserver
.HttpContext
;
18 import com
.sun
.net
.httpserver
.HttpHandler
;
19 import com
.sun
.net
.httpserver
.HttpServer
;
21 /** Implementation of a CMS deployment. */
22 public class CmsDeploymentImpl
implements CmsDeployment
{
23 private final CmsLog log
= CmsLog
.getLog(getClass());
25 private CmsState cmsState
;
28 private boolean httpExpected
= false;
29 private boolean sshdExpected
= false;
32 private HttpServer httpServer
;
33 private Map
<String
, HttpHandler
> httpHandlers
= new TreeMap
<>();
34 private Map
<String
, CmsAuthenticator
> httpAuthenticators
= new TreeMap
<>();
37 private CmsSshd cmsSshd
;
40 log
.debug(() -> "CMS deployment available");
46 public void setCmsState(CmsState cmsState
) {
47 this.cmsState
= cmsState
;
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;
53 String sshdPort
= this.cmsState
.getDeployProperty(CmsDeployProperty
.SSHD_PORT
.getProperty());
54 sshdExpected
= sshdPort
!= null;
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
);
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.");
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) {
80 createHttpContext(contextPath
, httpHandler
, authenticator
);
84 public void createHttpContext(String contextPath
, HttpHandler httpHandler
, CmsAuthenticator authenticator
) {
85 HttpContext httpContext
= httpServer
.createContext(contextPath
);
86 // we want to set the authenticator BEFORE the handler actually becomes active
87 httpContext
.setAuthenticator(authenticator
);
88 httpContext
.setHandler(httpHandler
);
89 log
.debug(() -> "Added handler " + contextPath
+ " : " + httpHandler
.getClass().getName());
92 public void removeHttpHandler(HttpHandler httpHandler
, Map
<String
, String
> properties
) {
93 final String contextPath
= properties
.get(CmsConstants
.CONTEXT_PATH
);
94 if (contextPath
== null)
95 return; // ignore silently
96 httpHandlers
.remove(contextPath
);
97 if (httpServer
== null)
99 httpServer
.removeContext(contextPath
);
100 log
.debug(() -> "Removed handler " + contextPath
+ " : " + httpHandler
.getClass().getName());
103 public boolean allExpectedServicesAvailable() {
104 if (httpExpected
&& httpServer
== null)
106 if (sshdExpected
&& cmsSshd
== null)
111 public void setCmsSshd(CmsSshd cmsSshd
) {
112 this.cmsSshd
= cmsSshd
;