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