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;
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;
33 import java.util.Properties;
35 * A Jetty server which supports:
36 * - http and https (simultaneously for dev env)
38 * - static html pages (for documentation).
40 public class JettyServer extends BaseLoggingClass {
41 private Server server;
44 public Server getServer() {
48 public JettyServer( Properties params ) throws Exception {
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 );
57 String keystorePwd = null;
62 HttpConfiguration http_config = new HttpConfiguration();
63 http_config.setSecureScheme("https");
64 http_config.setSecurePort(sslPort);
65 http_config.setOutputBufferSize(32768);
69 try(ServerConnector httpConnector = new ServerConnector(server, new HttpConnectionFactory(http_config))) {
70 httpConnector.setPort(httpPort);
71 httpConnector.setIdleTimeout(30000);
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");
85 sslContextFactory.setKeyStorePath(keystore);
86 sslContextFactory.setKeyStorePassword(keystorePwd);
87 sslContextFactory.setKeyManagerPassword(keyPwd);
91 try(ServerConnector sslConnector = new ServerConnector(server,
92 new SslConnectionFactory(sslContextFactory, "http/1.1"),
93 new HttpConnectionFactory(https_config))) {
94 sslConnector.setPort(sslPort);
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});
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});
106 serverLogger.info("NOT starting sslConnector on port " + sslPort + " for https");
108 serverLogger.info("Starting httpConnector on port " + httpPort);
109 server.setConnectors(new Connector[]{httpConnector});
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 );
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" );
124 // also serve up some static pages...
125 ServletHolder staticServlet = context.addServlet(DefaultServlet.class,"/*");
126 staticServlet.setInitParameter("resourceBase","www");
127 staticServlet.setInitParameter("pathInfoOnly","true");
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" ) ) {
139 } catch ( Exception e ) {
140 errorLogger.error( "Exception " + e );
141 errorLogger.error( "possibly unable to use keystore " + keystore + " with passwords " + keystorePwd + " and " + keyPwd );