1 package org
.argeo
.cms
.jetty
;
3 import java
.io
.IOException
;
4 import java
.nio
.file
.Files
;
5 import java
.nio
.file
.Path
;
8 import javax
.servlet
.ServletContext
;
9 import javax
.servlet
.ServletException
;
10 import javax
.websocket
.DeploymentException
;
11 import javax
.websocket
.server
.ServerContainer
;
12 import javax
.websocket
.server
.ServerEndpointConfig
;
13 import com
.sun
.net
.httpserver
.Authenticator
;
14 import com
.sun
.net
.httpserver
.HttpContext
;
16 import org
.argeo
.api
.cms
.CmsState
;
17 import org
.argeo
.cms
.CmsDeployProperty
;
18 import org
.argeo
.cms
.websocket
.server
.CmsWebSocketConfigurator
;
19 import org
.argeo
.cms
.websocket
.server
.TestEndpoint
;
20 import org
.eclipse
.jetty
.server
.session
.SessionHandler
;
21 import org
.eclipse
.jetty
.servlet
.ServletContextHandler
;
22 import org
.eclipse
.jetty
.websocket
.javax
.server
.config
.JavaxWebSocketServletContainerInitializer
;
23 import org
.eclipse
.jetty
.websocket
.javax
.server
.config
.JavaxWebSocketServletContainerInitializer
.Configurator
;
25 public class CmsJettyServer
extends JettyHttpServer
{
26 private static final String CONTEXT_TEMPDIR
= "javax.servlet.context.tempdir";
27 // Equinox compatibility
28 private static final String INTERNAL_CONTEXT_CLASSLOADER
= "org.eclipse.equinox.http.jetty.internal.ContextClassLoader";
29 // private static final CmsLog log = CmsLog.getLog(CmsJettyServer.class);
31 // private Server server;
32 // private Path tempDir;
34 // private ServerConnector httpConnector;
35 // private ServerConnector httpsConnector;
39 // private ServerContainer wsServerContainer;
40 private ServerEndpointConfig
.Configurator wsEndpointConfigurator
;
42 private CmsState cmsState
;
44 // private Authenticator defaultAuthenticator;
46 protected void addServlets(ServletContextHandler servletContextHandler
) throws ServletException
{
52 tempDir
= Files
.createTempDirectory("jetty");
53 } catch (IOException e
) {
54 throw new IllegalStateException("Cannot create temp dir", e
);
59 protected ServletContextHandler
createRootContextHandler() {
60 ServletContextHandler servletContextHandler
= new ServletContextHandler();
61 servletContextHandler
.setAttribute(INTERNAL_CONTEXT_CLASSLOADER
,
62 Thread
.currentThread().getContextClassLoader());
63 servletContextHandler
.setClassLoader(this.getClass().getClassLoader());
64 servletContextHandler
.setContextPath("/");
66 servletContextHandler
.setAttribute(CONTEXT_TEMPDIR
, tempDir
.toAbsolutePath().toFile());
67 SessionHandler handler
= new SessionHandler();
68 handler
.setMaxInactiveInterval(-1);
69 servletContextHandler
.setSessionHandler(handler
);
71 return servletContextHandler
;
75 protected void configureRootContextHandler(ServletContextHandler servletContextHandler
) throws ServletException
{
76 addServlets(servletContextHandler
);
77 enableWebSocket(servletContextHandler
);
82 // public synchronized HttpContext createContext(String path) {
83 // HttpContext httpContext = super.createContext(path);
84 // httpContext.setAuthenticator(defaultAuthenticator);
85 // return httpContext;
88 protected void enableWebSocket(ServletContextHandler servletContextHandler
) {
89 String webSocketEnabled
= getDeployProperty(CmsDeployProperty
.WEBSOCKET_ENABLED
);
91 if (webSocketEnabled
!= null && webSocketEnabled
.equals(Boolean
.toString(true))) {
92 // JavaxWebSocketServletContainerInitializer.configure(servletContextHandler, new Configurator() {
95 // public void accept(ServletContext servletContext, ServerContainer serverContainer)
96 // throws DeploymentException {
97 //// wsServerContainer = serverContainer;
99 // CmsWebSocketConfigurator wsEndpointConfigurator = new CmsWebSocketConfigurator();
101 // ServerEndpointConfig config = ServerEndpointConfig.Builder
102 // .create(TestEndpoint.class, "/ws/test/events/{topic}").configurator(wsEndpointConfigurator)
105 // serverContainer.addEndpoint(config);
106 // } catch (DeploymentException e) {
107 // throw new IllegalStateException("Cannot initalise the WebSocket server runtime.", e);
114 protected String
getDeployProperty(CmsDeployProperty deployProperty
) {
115 return cmsState
.getDeployProperty(deployProperty
.getProperty());
118 public void setCmsState(CmsState cmsState
) {
119 this.cmsState
= cmsState
;
122 // public void setDefaultAuthenticator(Authenticator defaultAuthenticator) {
123 // this.defaultAuthenticator = defaultAuthenticator;