748eedcfebee58eba3abb172766299d30f98386f
[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.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         private Server server;
58
59
60         public Server getServer() {
61                 return server;
62         }
63
64     public JettyServer( Properties params ) throws Exception {
65
66         server = new Server();
67         int httpPort = Integer.valueOf(params.getProperty("IntHttpPort", "80" ));
68         int sslPort = Integer.valueOf(params.getProperty("IntHttpsPort", "443" ));
69         boolean allowHttp = Boolean.valueOf(params.getProperty("HttpAllowed", "false"));
70         serverLogger.info( "port params: http=" + httpPort + " https=" + sslPort );
71         serverLogger.info( "allowHttp=" + allowHttp );
72         String keystore=null;
73         String keystorePwd = null;
74         String keyPwd = null;
75         
76         // HTTP Server
77
78         HttpConfiguration http_config = new HttpConfiguration();
79         http_config.setSecureScheme("https");
80         http_config.setSecurePort(sslPort);
81         http_config.setOutputBufferSize(32768);
82
83         
84         
85         try(ServerConnector httpConnector = new ServerConnector(server, new HttpConnectionFactory(http_config))) {
86                         httpConnector.setPort(httpPort);
87                         httpConnector.setIdleTimeout(30000);
88
89
90                         // HTTPS Server
91
92                         HttpConfiguration https_config = new HttpConfiguration(http_config);
93                         https_config.addCustomizer(new SecureRequestCustomizer());
94                         SslContextFactory sslContextFactory = new SslContextFactory();
95                         keystore = params.getProperty("KeyStoreFile", "etc/keystore");
96                         logger.info("https Server using keystore at " + keystore);
97                         keystorePwd = params.getProperty("KeyStorePassword", "changeit");
98                         keyPwd = params.getProperty("KeyPassword", "changeit");
99
100
101                         sslContextFactory.setKeyStorePath(keystore);
102                         sslContextFactory.setKeyStorePassword(keystorePwd);
103                         sslContextFactory.setKeyManagerPassword(keyPwd);
104
105
106                         if (sslPort != 0) {
107                 try(ServerConnector sslConnector = new ServerConnector(server,
108                                                 new SslConnectionFactory(sslContextFactory, "http/1.1"),
109                                                 new HttpConnectionFactory(https_config))) {
110                     sslConnector.setPort(sslPort);
111                     if (allowHttp) {
112                         logger.info("Starting httpConnector on port " + httpPort);
113                         logger.info("Starting sslConnector on port " + sslPort + " for https");
114                         server.setConnectors(new Connector[]{httpConnector, sslConnector});
115                     } else {
116                         logger.info("NOT starting httpConnector because HttpAllowed param is " + allowHttp);
117                         logger.info("Starting sslConnector on port " + sslPort + " for https");
118                         server.setConnectors(new Connector[]{sslConnector});
119                     }
120                 }
121                         } else {
122                                 serverLogger.info("NOT starting sslConnector on port " + sslPort + " for https");
123                                 if (allowHttp) {
124                                         serverLogger.info("Starting httpConnector on port " + httpPort);
125                                         server.setConnectors(new Connector[]{httpConnector});
126                                 }
127                         }
128                 }
129  
130         // Set context for servlet.  This is shared for http and https
131         ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
132         context.setContextPath("/");
133         server.setHandler( context );
134
135         ServletHolder jerseyServlet = context.addServlet( org.glassfish.jersey.servlet.ServletContainer.class, "/webapi/*");
136         jerseyServlet.setInitOrder(1);
137         jerseyServlet.setInitParameter("jersey.config.server.provider.packages", "org.onap.dmaap.dbcapi.resources" );   
138         jerseyServlet.setInitParameter("javax.ws.rs.Application", "org.onap.dmaap.dbcapi.server.ApplicationConfig" );
139         
140         // also serve up some static pages...
141         ServletHolder staticServlet = context.addServlet(DefaultServlet.class,"/*");
142         staticServlet.setInitParameter("resourceBase","www");
143         staticServlet.setInitParameter("pathInfoOnly","true");
144
145         try {
146
147             serverLogger.info("Starting jetty server");
148                 String unit_test = params.getProperty("UnitTest", "No");
149             serverLogger.info("UnitTest=" + unit_test);
150                         if ( unit_test.equals( "No" ) ) {
151                         server.start();
152                         server.dumpStdErr();
153                 server.join();
154                         }
155         } catch ( Exception e ) {
156                 errorLogger.error( "Exception " + e );
157                 errorLogger.error( "possibly unable to use keystore " + keystore + " with passwords " + keystorePwd +  " and " + keyPwd );
158                 //System.exit(1);
159         } finally {
160                 server.destroy();
161         }
162         
163     }
164 }