4d063106f07316258df7636ec7e7263d5bfe4746
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / server / JettyServer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dmaap.dbcapi.server;
22
23
24 import java.util.Properties;
25
26 import javax.net.ssl.SSLContext;
27
28 import org.apache.log4j.Logger;
29 import org.eclipse.jetty.security.SecurityHandler;
30 import org.eclipse.jetty.server.Connector;
31 import org.eclipse.jetty.server.HttpConfiguration;
32 import org.eclipse.jetty.server.HttpConnectionFactory;
33 import org.eclipse.jetty.server.SecureRequestCustomizer;
34 import org.eclipse.jetty.server.Server;
35 import org.eclipse.jetty.server.ServerConnector;
36 import org.eclipse.jetty.server.SslConnectionFactory;
37 import org.eclipse.jetty.servlet.DefaultServlet;
38 import org.eclipse.jetty.servlet.ServletContextHandler;
39 import org.eclipse.jetty.servlet.ServletHolder;
40 import org.eclipse.jetty.util.ssl.SslContextFactory;
41  
42
43
44
45 import com.att.eelf.configuration.EELFLogger;
46 import com.att.eelf.configuration.EELFManager;
47
48 import org.onap.dmaap.dbcapi.aaf.database.LoadSchema;
49 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
50 /**
51  * A  Jetty server which supports:
52  *      - http and https (simultaneously for dev env)
53  *  - REST API context
54  *  - static html pages (for documentation).
55  */
56 public class JettyServer extends BaseLoggingClass {
57
58     public JettyServer( Properties params ) throws Exception {
59
60         Server server = new Server();
61         int httpPort = Integer.valueOf(params.getProperty("IntHttpPort", "80" ));
62         int sslPort = Integer.valueOf(params.getProperty("IntHttpsPort", "443" ));
63         boolean allowHttp = Boolean.valueOf(params.getProperty("HttpAllowed", "false"));
64         serverLogger.info( "port params: http=" + httpPort + " https=" + sslPort );
65         serverLogger.info( "allowHttp=" + allowHttp );
66         
67         // HTTP Server
68
69         HttpConfiguration http_config = new HttpConfiguration();
70         http_config.setSecureScheme("https");
71         http_config.setSecurePort(sslPort);
72         http_config.setOutputBufferSize(32768);
73
74         
75         
76         ServerConnector httpConnector = new ServerConnector(server, new HttpConnectionFactory(http_config));
77         httpConnector.setPort(httpPort);  
78         httpConnector.setIdleTimeout(30000);
79   
80         
81         // HTTPS Server
82  
83         HttpConfiguration https_config = new HttpConfiguration(http_config);
84         https_config.addCustomizer(new SecureRequestCustomizer());
85         SslContextFactory sslContextFactory = new SslContextFactory();
86         String keystore = params.getProperty("KeyStoreFile", "etc/keystore");
87         logger.info( "https Server using keystore at " + keystore );
88         String keystorePwd = params.getProperty( "KeyStorePassword", "changeit");
89         String keyPwd = params.getProperty("KeyPassword", "changeit");
90  
91
92         sslContextFactory.setKeyStorePath(keystore);
93         sslContextFactory.setKeyStorePassword(keystorePwd);
94         sslContextFactory.setKeyManagerPassword(keyPwd);     
95
96   
97                 ServerConnector sslConnector = null;
98                 if ( sslPort != 0 ) {
99                         sslConnector = new ServerConnector(server,
100                                         new SslConnectionFactory(sslContextFactory, "http/1.1"),
101                                         new HttpConnectionFactory(https_config));
102                         sslConnector.setPort(sslPort);
103                 if ( allowHttp ) {
104                 logger.info("Starting httpConnector on port " + httpPort );
105                 logger.info("Starting sslConnector on port " +   sslPort + " for https");
106                         server.setConnectors( new Connector[] { httpConnector, sslConnector });
107                 } else {
108                 logger.info("NOT starting httpConnector because HttpAllowed param is " + allowHttp  );  
109                 logger.info("Starting sslConnector on port " +   sslPort + " for https");
110                         server.setConnectors( new Connector[] { sslConnector });        
111                 }
112                 }
113                 else {
114             serverLogger.info("NOT starting sslConnector on port " +   sslPort + " for https");
115                 if ( allowHttp ) {
116                 serverLogger.info("Starting httpConnector on port " + httpPort );
117                         server.setConnectors( new Connector[] { httpConnector });
118                         } 
119         } 
120  
121         // Set context for servlet.  This is shared for http and https
122         ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
123         context.setContextPath("/");
124         server.setHandler( context );
125
126         ServletHolder jerseyServlet = context.addServlet( org.glassfish.jersey.servlet.ServletContainer.class, "/webapi/*");
127         jerseyServlet.setInitOrder(1);
128         jerseyServlet.setInitParameter("jersey.config.server.provider.packages", "org.onap.dmaap.dbcapi.resources" );   
129         jerseyServlet.setInitParameter("javax.ws.rs.Application", "org.onap.dmaap.dbcapi.server.ApplicationConfig" );
130         
131         // also serve up some static pages...
132         ServletHolder staticServlet = context.addServlet(DefaultServlet.class,"/*");
133         staticServlet.setInitParameter("resourceBase","www");
134         staticServlet.setInitParameter("pathInfoOnly","true");
135
136         try {
137
138             serverLogger.info("Starting jetty server");
139                 server.start();
140                 server.dumpStdErr();
141             server.join();
142         } catch ( Exception e ) {
143                 errorLogger.error( "Exception " + e );
144                 errorLogger.error( "possibly unable to use keystore " + keystore + " with passwords " + keystorePwd +  " and " + keyPwd );
145                 //System.exit(1);
146         } finally {
147                 server.destroy();
148         }
149         
150     }
151 }