6a75d65efd4f9aada053fcbd33dcc5ca9640cd12
[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 import com.google.common.collect.Sets;
26 import javax.servlet.DispatcherType;
27
28 import org.eclipse.jetty.http.HttpVersion;
29 import org.eclipse.jetty.server.*;
30 import org.eclipse.jetty.servlet.DefaultServlet;
31 import org.eclipse.jetty.servlet.ServletContextHandler;
32 import org.eclipse.jetty.servlet.ServletHolder;
33 import org.eclipse.jetty.util.ssl.SslContextFactory;
34 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
35
36 import java.util.Properties;
37
38 /**
39  * A  Jetty server which supports:
40  *      - http and https (simultaneously for dev env)
41  *  - REST API context
42  *  - static html pages (for documentation).
43  */
44 public class JettyServer extends BaseLoggingClass {
45
46     private Server server;
47
48
49     public Server getServer() {
50         return server;
51     }
52
53     public JettyServer(Properties params) throws Exception {
54
55         server = new Server();
56         int httpPort = Integer.valueOf(params.getProperty("IntHttpPort", "80"));
57         int sslPort = Integer.valueOf(params.getProperty("IntHttpsPort", "443"));
58         boolean allowHttp = Boolean.valueOf(params.getProperty("HttpAllowed", "false"));
59         serverLogger.info("port params: http=" + httpPort + " https=" + sslPort);
60         serverLogger.info("allowHttp=" + allowHttp);
61
62         // HTTP Server
63         HttpConfiguration http_config = new HttpConfiguration();
64         http_config.setSecureScheme("https");
65         http_config.setSecurePort(sslPort);
66         http_config.setOutputBufferSize(32768);
67
68         try (ServerConnector httpConnector = new ServerConnector(server, new HttpConnectionFactory(http_config))) {
69             httpConnector.setPort(httpPort);
70             httpConnector.setIdleTimeout(30000);
71
72             // HTTPS Server
73
74             HttpConfiguration https_config = new HttpConfiguration(http_config);
75             https_config.addCustomizer(new SecureRequestCustomizer());
76             SslContextFactory sslContextFactory = new SslContextFactory.Server();
77             sslContextFactory.setWantClientAuth(true);
78
79             setUpKeystore(params, sslContextFactory);
80             setUpTrustStore(params, sslContextFactory);
81
82             if (sslPort != 0) {
83                 try (ServerConnector sslConnector = new ServerConnector(server,
84                     new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
85                     new HttpConnectionFactory(https_config))) {
86                     sslConnector.setPort(sslPort);
87                     server.addConnector(sslConnector);
88                     serverLogger.info("Starting sslConnector on port " + sslPort + " for https");
89                 }
90             } else {
91                 serverLogger.info("NOT starting sslConnector because InHttpsPort param is " + sslPort );
92             }
93             if (allowHttp) {
94                 serverLogger.info("Starting httpConnector on port " + httpPort);
95                 server.addConnector(httpConnector);
96             } else {
97                 serverLogger.info("NOT starting httpConnector because HttpAllowed param is " + allowHttp);
98             }
99         }
100
101         // Set context for servlet.  This is shared for http and https
102         ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
103         context.setContextPath("/");
104         server.setHandler(context);
105
106         ServletHolder jerseyServlet = context
107             .addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/webapi/*");
108         jerseyServlet.setInitOrder(1);
109         jerseyServlet.setInitParameter("jersey.config.server.provider.packages", "org.onap.dmaap.dbcapi.resources");
110         jerseyServlet.setInitParameter("javax.ws.rs.Application", "org.onap.dmaap.dbcapi.server.ApplicationConfig");
111
112         // also serve up some static pages...
113         ServletHolder staticServlet = context.addServlet(DefaultServlet.class, "/*");
114         staticServlet.setInitParameter("resourceBase", "www");
115         staticServlet.setInitParameter("pathInfoOnly", "true");
116
117         registerAuthFilters(context);
118
119         try {
120
121             serverLogger.info("Starting jetty server");
122             String unit_test = params.getProperty("UnitTest", "No");
123             serverLogger.info("UnitTest=" + unit_test);
124             if (unit_test.equals("No")) {
125                 server.start();
126                 server.dumpStdErr();
127                 server.join();
128             }
129         } catch (Exception e) {
130             errorLogger.error("Exception " + e);
131         } finally {
132             server.destroy();
133         }
134
135     }
136
137     private void registerAuthFilters(ServletContextHandler context) {
138         context.addFilter("org.onap.dmaap.dbcapi.resources.AAFAuthenticationFilter", "/webapi/*",
139             Sets.newEnumSet(Sets.newHashSet(DispatcherType.FORWARD, DispatcherType.REQUEST), DispatcherType.class));
140         context.addFilter("org.onap.dmaap.dbcapi.resources.AAFAuthorizationFilter", "/webapi/*",
141             Sets.newEnumSet(Sets.newHashSet(DispatcherType.FORWARD, DispatcherType.REQUEST), DispatcherType.class));
142     }
143
144     private void setUpKeystore(Properties params, SslContextFactory sslContextFactory) {
145         String keystore = params.getProperty("KeyStoreFile", "etc/keystore");
146         logger.info("https Server using keystore at " + keystore);
147         sslContextFactory.setKeyStorePath(keystore);
148         sslContextFactory.setKeyStorePassword(params.getProperty("KeyStorePassword", "changeit"));
149         sslContextFactory.setKeyManagerPassword(params.getProperty("KeyPassword", "changeit"));
150     }
151
152     private void setUpTrustStore(Properties params, SslContextFactory sslContextFactory) {
153         String truststore = params.getProperty("TrustStoreFile", "etc/org.onap.dmaap-bc.trust.jks");
154         logger.info("https Server using truststore at " + truststore);
155         sslContextFactory.setTrustStorePath(truststore);
156         sslContextFactory.setTrustStoreType(params.getProperty("TrustStoreType", "jks"));
157         sslContextFactory.setTrustStorePassword(params.getProperty("TrustStorePassword", "changeit"));
158     }
159 }