update link to upper-constraints.txt
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / ProvServer.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dmaap.datarouter.provisioning;
22
23 import com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
25 import java.security.Security;
26 import java.util.Properties;
27 import org.eclipse.jetty.http.HttpVersion;
28 import org.eclipse.jetty.server.Connector;
29 import org.eclipse.jetty.server.CustomRequestLog;
30 import org.eclipse.jetty.server.Handler;
31 import org.eclipse.jetty.server.HttpConfiguration;
32 import org.eclipse.jetty.server.HttpConnectionFactory;
33 import org.eclipse.jetty.server.Server;
34 import org.eclipse.jetty.server.ServerConnector;
35 import org.eclipse.jetty.server.SslConnectionFactory;
36 import org.eclipse.jetty.server.handler.ContextHandlerCollection;
37 import org.eclipse.jetty.server.handler.DefaultHandler;
38 import org.eclipse.jetty.server.handler.HandlerCollection;
39 import org.eclipse.jetty.server.handler.RequestLogHandler;
40 import org.eclipse.jetty.servlet.ServletContextHandler;
41 import org.eclipse.jetty.servlet.ServletHolder;
42 import org.eclipse.jetty.util.ssl.SslContextFactory;
43 import org.eclipse.jetty.util.thread.QueuedThreadPool;
44 import org.jetbrains.annotations.NotNull;
45
46
47 public class ProvServer {
48
49     public static final EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");
50
51     private static Server server;
52
53     private ProvServer() {
54     }
55
56     static Server getServerInstance() {
57         if (server == null) {
58             server = createProvServer(ProvRunner.getProvProperties());
59         }
60         return server;
61     }
62
63     private static Server createProvServer(Properties provProps) {
64         Security.setProperty("networkaddress.cache.ttl", "4");
65         QueuedThreadPool queuedThreadPool = getQueuedThreadPool();
66
67         server = new Server(queuedThreadPool);
68         server.setStopAtShutdown(true);
69         server.setStopTimeout(5000);
70         server.setDumpAfterStart(false);
71         server.setDumpBeforeStop(false);
72
73         HttpConfiguration httpConfiguration = getHttpConfiguration();
74
75         //HTTP Connector
76         try (ServerConnector httpServerConnector = new ServerConnector(server,
77             new HttpConnectionFactory(httpConfiguration))) {
78             httpServerConnector.setPort(Integer.parseInt(provProps.getProperty(
79                 "org.onap.dmaap.datarouter.provserver.http.port", "80")));
80             httpServerConnector.setAcceptQueueSize(2);
81             httpServerConnector.setIdleTimeout(30000);
82
83             ServletContextHandler servletContextHandler = getServletContextHandler(provProps);
84             ContextHandlerCollection contextHandlerCollection = new ContextHandlerCollection();
85             contextHandlerCollection.addHandler(servletContextHandler);
86
87             CustomRequestLog customRequestLog = getCustomRequestLog(provProps);
88             RequestLogHandler requestLogHandler = new RequestLogHandler();
89             requestLogHandler.setRequestLog(customRequestLog);
90
91             server.setRequestLog(customRequestLog);
92
93             // Server's Handler collection
94             HandlerCollection handlerCollection = new HandlerCollection();
95             handlerCollection.setHandlers(new Handler[]{contextHandlerCollection, new DefaultHandler()});
96             handlerCollection.addHandler(requestLogHandler);
97
98             if (Boolean.TRUE.equals(ProvRunner.getTlsEnabled())) {
99                 // HTTPS configuration
100                 int httpsPort = Integer.parseInt(
101                     provProps.getProperty("org.onap.dmaap.datarouter.provserver.https.port", "443"));
102                 httpConfiguration.setSecureScheme("https");
103                 httpConfiguration.setSecurePort(httpsPort);
104                 HttpConfiguration httpsConfiguration = new HttpConfiguration(httpConfiguration);
105                 httpsConfiguration.setRequestHeaderSize(8192);
106                 // HTTPS connector
107                 try (ServerConnector httpsServerConnector = new ServerConnector(server,
108                     new SslConnectionFactory(getSslContextFactory(), HttpVersion.HTTP_1_1.asString()),
109                     new HttpConnectionFactory(httpsConfiguration))) {
110                     httpsServerConnector.setPort(httpsPort);
111                     httpsServerConnector.setIdleTimeout(30000);
112                     httpsServerConnector.setAcceptQueueSize(2);
113                     intlogger.info("ProvServer: TLS enabled. Setting up both HTTP/S connectors.");
114                     server.setConnectors(new Connector[]{httpServerConnector, httpsServerConnector});
115                 }
116             } else {
117                 intlogger.info("ProvServer: TLS disabled. Setting up HTTP connector only.");
118                 server.setConnectors(new Connector[]{httpServerConnector});
119             }
120             server.setHandler(handlerCollection);
121         }
122         return server;
123     }
124
125     @NotNull
126     private static QueuedThreadPool getQueuedThreadPool() {
127         // Server's thread pool
128         QueuedThreadPool queuedThreadPool = new QueuedThreadPool();
129         queuedThreadPool.setMinThreads(10);
130         queuedThreadPool.setMaxThreads(200);
131         queuedThreadPool.setDetailedDump(false);
132         return queuedThreadPool;
133     }
134
135     @NotNull
136     private static SslContextFactory.Server getSslContextFactory() {
137         SslContextFactory.Server sslContextFactoryServer = ProvRunner.getProvTlsManager().getSslContextFactoryServer();
138         sslContextFactoryServer.setExcludeCipherSuites(
139             "SSL_RSA_WITH_DES_CBC_SHA",
140             "SSL_DHE_RSA_WITH_DES_CBC_SHA",
141             "SSL_DHE_DSS_WITH_DES_CBC_SHA",
142             "SSL_RSA_EXPORT_WITH_RC4_40_MD5",
143             "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA",
144             "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA",
145             "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA"
146         );
147         sslContextFactoryServer.addExcludeProtocols("SSLv3");
148         intlogger.info("Unsupported protocols: " + String.join(",", sslContextFactoryServer.getExcludeProtocols()));
149         intlogger.info("Supported protocols: " + String.join(",", sslContextFactoryServer.getIncludeProtocols()));
150         intlogger.info("Unsupported ciphers: " + String.join(",", sslContextFactoryServer.getExcludeCipherSuites()));
151         intlogger.info("Supported ciphers: " + String.join(",", sslContextFactoryServer.getIncludeCipherSuites()));
152         return sslContextFactoryServer;
153     }
154
155     @NotNull
156     private static CustomRequestLog getCustomRequestLog(Properties provProps) {
157         String filename = provProps.getProperty(
158             "org.onap.dmaap.datarouter.provserver.accesslog.dir") + "/request.log.yyyy_mm_dd";
159         String format = "yyyyMMdd";
160         return new CustomRequestLog(filename, format);
161     }
162
163     @NotNull
164     private static HttpConfiguration getHttpConfiguration() {
165         HttpConfiguration httpConfiguration = new HttpConfiguration();
166         httpConfiguration.setOutputBufferSize(32768);
167         httpConfiguration.setRequestHeaderSize(8192);
168         httpConfiguration.setResponseHeaderSize(8192);
169         httpConfiguration.setSendServerVersion(true);
170         httpConfiguration.setSendDateHeader(false);
171         return httpConfiguration;
172     }
173
174     @NotNull
175     private static ServletContextHandler getServletContextHandler(Properties provProps) {
176         ServletContextHandler servletContextHandler = new ServletContextHandler(0);
177         servletContextHandler.setContextPath("/");
178         servletContextHandler.addServlet(new ServletHolder(new FeedServlet()), "/feed/*");
179         servletContextHandler.addServlet(new ServletHolder(new FeedLogServlet()), "/feedlog/*");
180         servletContextHandler.addServlet(new ServletHolder(new PublishServlet()), "/publish/*");
181         servletContextHandler.addServlet(new ServletHolder(new SubscribeServlet()), "/subscribe/*");
182         servletContextHandler.addServlet(new ServletHolder(new StatisticsServlet()), "/statistics/*");
183         servletContextHandler.addServlet(new ServletHolder(new SubLogServlet()), "/sublog/*");
184         servletContextHandler.addServlet(new ServletHolder(new GroupServlet()), "/group/*");
185         servletContextHandler.addServlet(new ServletHolder(new SubscriptionServlet()), "/subs/*");
186         servletContextHandler.addServlet(new ServletHolder(new InternalServlet()), "/internal/*");
187         servletContextHandler.addServlet(new ServletHolder(new RouteServlet()), "/internal/route/*");
188         servletContextHandler.addServlet(new ServletHolder(new DRFeedsServlet()), "/");
189         return servletContextHandler;
190     }
191 }