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