2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property.
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.dmaap.dbcapi.server;
25 import com.google.common.collect.Sets;
26 import javax.servlet.DispatcherType;
27 import org.eclipse.jetty.server.*;
28 import org.eclipse.jetty.servlet.DefaultServlet;
29 import org.eclipse.jetty.servlet.ServletContextHandler;
30 import org.eclipse.jetty.servlet.ServletHolder;
31 import org.eclipse.jetty.util.ssl.SslContextFactory;
32 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
34 import java.util.Properties;
37 * A Jetty server which supports:
38 * - http and https (simultaneously for dev env)
40 * - static html pages (for documentation).
42 public class JettyServer extends BaseLoggingClass {
44 private Server server;
47 public Server getServer() {
51 public JettyServer(Properties params) throws Exception {
53 server = new Server();
54 int httpPort = Integer.valueOf(params.getProperty("IntHttpPort", "80"));
55 int sslPort = Integer.valueOf(params.getProperty("IntHttpsPort", "443"));
56 boolean allowHttp = Boolean.valueOf(params.getProperty("HttpAllowed", "false"));
57 serverLogger.info("port params: http=" + httpPort + " https=" + sslPort);
58 serverLogger.info("allowHttp=" + allowHttp);
61 HttpConfiguration http_config = new HttpConfiguration();
62 http_config.setSecureScheme("https");
63 http_config.setSecurePort(sslPort);
64 http_config.setOutputBufferSize(32768);
66 try (ServerConnector httpConnector = new ServerConnector(server, new HttpConnectionFactory(http_config))) {
67 httpConnector.setPort(httpPort);
68 httpConnector.setIdleTimeout(30000);
72 HttpConfiguration https_config = new HttpConfiguration(http_config);
73 https_config.addCustomizer(new SecureRequestCustomizer());
74 SslContextFactory sslContextFactory = new SslContextFactory();
75 sslContextFactory.setWantClientAuth(true);
77 setUpKeystore(params, sslContextFactory);
78 setUpTrustStore(params, sslContextFactory);
81 try (ServerConnector sslConnector = new ServerConnector(server,
82 new SslConnectionFactory(sslContextFactory, "http/1.1"),
83 new HttpConnectionFactory(https_config))) {
84 sslConnector.setPort(sslPort);
86 logger.info("Starting httpConnector on port " + httpPort);
87 logger.info("Starting sslConnector on port " + sslPort + " for https");
88 server.setConnectors(new Connector[]{httpConnector, sslConnector});
90 logger.info("NOT starting httpConnector because HttpAllowed param is " + allowHttp);
91 logger.info("Starting sslConnector on port " + sslPort + " for https");
92 server.setConnectors(new Connector[]{sslConnector});
96 serverLogger.info("NOT starting sslConnector on port " + sslPort + " for https");
98 serverLogger.info("Starting httpConnector on port " + httpPort);
99 server.setConnectors(new Connector[]{httpConnector});
104 // Set context for servlet. This is shared for http and https
105 ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
106 context.setContextPath("/");
107 server.setHandler(context);
109 ServletHolder jerseyServlet = context
110 .addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/webapi/*");
111 jerseyServlet.setInitOrder(1);
112 jerseyServlet.setInitParameter("jersey.config.server.provider.packages", "org.onap.dmaap.dbcapi.resources");
113 jerseyServlet.setInitParameter("javax.ws.rs.Application", "org.onap.dmaap.dbcapi.server.ApplicationConfig");
115 // also serve up some static pages...
116 ServletHolder staticServlet = context.addServlet(DefaultServlet.class, "/*");
117 staticServlet.setInitParameter("resourceBase", "www");
118 staticServlet.setInitParameter("pathInfoOnly", "true");
120 registerAuthFilters(context);
124 serverLogger.info("Starting jetty server");
125 String unit_test = params.getProperty("UnitTest", "No");
126 serverLogger.info("UnitTest=" + unit_test);
127 if (unit_test.equals("No")) {
132 } catch (Exception e) {
133 errorLogger.error("Exception " + e);
140 private void registerAuthFilters(ServletContextHandler context) {
141 context.addFilter("org.onap.dmaap.dbcapi.resources.AAFAuthenticationFilter", "/webapi/*",
142 Sets.newEnumSet(Sets.newHashSet(DispatcherType.FORWARD, DispatcherType.REQUEST), DispatcherType.class));
143 context.addFilter("org.onap.dmaap.dbcapi.resources.AAFAuthorizationFilter", "/webapi/*",
144 Sets.newEnumSet(Sets.newHashSet(DispatcherType.FORWARD, DispatcherType.REQUEST), DispatcherType.class));
147 private void setUpKeystore(Properties params, SslContextFactory sslContextFactory) {
148 String keystore = params.getProperty("KeyStoreFile", "etc/keystore");
149 logger.info("https Server using keystore at " + keystore);
150 sslContextFactory.setKeyStorePath(keystore);
151 sslContextFactory.setKeyStorePassword(params.getProperty("KeyStorePassword", "changeit"));
152 sslContextFactory.setKeyManagerPassword(params.getProperty("KeyPassword", "changeit"));
155 private void setUpTrustStore(Properties params, SslContextFactory sslContextFactory) {
156 String truststore = params.getProperty("TrustStoreFile", "etc/org.onap.dmaap-bc.trust.jks");
157 logger.info("https Server using truststore at " + truststore);
158 sslContextFactory.setTrustStorePath(truststore);
159 sslContextFactory.setTrustStoreType(params.getProperty("TrustStoreType", "jks"));
160 sslContextFactory.setTrustStorePassword(params.getProperty("TrustStorePassword", "changeit"));