2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dmaap.dbcapi.server;
24 import java.util.Properties;
26 import javax.net.ssl.SSLContext;
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;
45 import com.att.eelf.configuration.EELFLogger;
46 import com.att.eelf.configuration.EELFManager;
48 import org.onap.dmaap.dbcapi.aaf.database.LoadSchema;
49 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
51 * A Jetty server which supports:
52 * - http and https (simultaneously for dev env)
54 * - static html pages (for documentation).
56 public class JettyServer extends BaseLoggingClass {
58 public JettyServer( Properties params ) throws Exception {
60 Server server = new Server();
61 int httpPort = Integer.valueOf(params.getProperty("IntHttpPort", "80" ));
62 int sslPort = Integer.valueOf(params.getProperty("IntHttpsPort", "443" ));
63 boolean allowHttp = Boolean.valueOf(params.getProperty("HttpAllowed", "false"));
64 serverLogger.info( "port params: http=" + httpPort + " https=" + sslPort );
65 serverLogger.info( "allowHttp=" + allowHttp );
69 HttpConfiguration http_config = new HttpConfiguration();
70 http_config.setSecureScheme("https");
71 http_config.setSecurePort(sslPort);
72 http_config.setOutputBufferSize(32768);
76 ServerConnector httpConnector = new ServerConnector(server, new HttpConnectionFactory(http_config));
77 httpConnector.setPort(httpPort);
78 httpConnector.setIdleTimeout(30000);
83 HttpConfiguration https_config = new HttpConfiguration(http_config);
84 https_config.addCustomizer(new SecureRequestCustomizer());
85 SslContextFactory sslContextFactory = new SslContextFactory();
86 String keystore = params.getProperty("KeyStoreFile", "etc/keystore");
87 logger.info( "https Server using keystore at " + keystore );
88 String keystorePwd = params.getProperty( "KeyStorePassword", "changeit");
89 String keyPwd = params.getProperty("KeyPassword", "changeit");
92 sslContextFactory.setKeyStorePath(keystore);
93 sslContextFactory.setKeyStorePassword(keystorePwd);
94 sslContextFactory.setKeyManagerPassword(keyPwd);
97 ServerConnector sslConnector = null;
99 sslConnector = new ServerConnector(server,
100 new SslConnectionFactory(sslContextFactory, "http/1.1"),
101 new HttpConnectionFactory(https_config));
102 sslConnector.setPort(sslPort);
104 logger.info("Starting httpConnector on port " + httpPort );
105 logger.info("Starting sslConnector on port " + sslPort + " for https");
106 server.setConnectors( new Connector[] { httpConnector, sslConnector });
108 logger.info("NOT starting httpConnector because HttpAllowed param is " + allowHttp );
109 logger.info("Starting sslConnector on port " + sslPort + " for https");
110 server.setConnectors( new Connector[] { sslConnector });
114 serverLogger.info("NOT starting sslConnector on port " + sslPort + " for https");
116 serverLogger.info("Starting httpConnector on port " + httpPort );
117 server.setConnectors( new Connector[] { httpConnector });
121 // Set context for servlet. This is shared for http and https
122 ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
123 context.setContextPath("/");
124 server.setHandler( context );
126 ServletHolder jerseyServlet = context.addServlet( org.glassfish.jersey.servlet.ServletContainer.class, "/webapi/*");
127 jerseyServlet.setInitOrder(1);
128 jerseyServlet.setInitParameter("jersey.config.server.provider.packages", "org.onap.dmaap.dbcapi.resources" );
129 jerseyServlet.setInitParameter("javax.ws.rs.Application", "org.onap.dmaap.dbcapi.server.ApplicationConfig" );
131 // also serve up some static pages...
132 ServletHolder staticServlet = context.addServlet(DefaultServlet.class,"/*");
133 staticServlet.setInitParameter("resourceBase","www");
134 staticServlet.setInitParameter("pathInfoOnly","true");
138 serverLogger.info("Starting jetty server");
142 } catch ( Exception e ) {
143 errorLogger.error( "Exception " + e );
144 errorLogger.error( "possibly unable to use keystore " + keystore + " with passwords " + keystorePwd + " and " + keyPwd );