Merge "Fixed Sonar Blocker in StatisticsServlet"
[dmaap/datarouter.git] / datarouter-subscriber / src / main / java / org / onap / dmaap / datarouter / subscriber / Subscriber.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
4  * * ===========================================================================
5  * * Copyright © 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
10  * *
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * *
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====================================================
19  * *
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23
24 package org.onap.dmaap.datarouter.subscriber;
25
26 import org.apache.log4j.Logger;
27 import org.eclipse.jetty.servlet.*;
28 import org.eclipse.jetty.util.ssl.*;
29 import org.eclipse.jetty.server.*;
30 import org.eclipse.jetty.http.HttpVersion;
31
32 import java.io.FileInputStream;
33 import java.io.IOException;
34 import java.util.Arrays;
35 import java.util.Properties;
36
37 public class Subscriber {
38
39     private static Logger logger = Logger.getLogger("org.onap.dmaap.datarouter.subscriber.Subscriber");
40
41     private static final String CONTEXT_PATH = "/";
42     private static final String URL_PATTERN = "/*";
43
44     static Properties props;
45
46     private static void loadProps() {
47         if (props == null) {
48             props = new Properties();
49             try {
50                 props.load(new FileInputStream(System.getProperty(
51                         "org.onap.dmaap.datarouter.subscriber.properties",
52                         "/opt/app/subscriber/etc/subscriber.properties")));
53             } catch (IOException e) {
54                 logger.fatal("SubServlet: Exception opening properties: " + e.getMessage());
55                 System.exit(1);
56             }
57         }
58     }
59
60     public static void main(String[] args) throws Exception {
61         //Load the properties
62         loadProps();
63
64         int httpsPort = Integer.parseInt(props.getProperty("org.onap.dmaap.datarouter.subscriber.https.port", "8443"));
65         int httpPort = Integer.parseInt(props.getProperty("org.onap.dmaap.datarouter.subscriber.http.port", "8080"));
66
67         Server server = new Server();
68         HttpConfiguration httpConfig = new HttpConfiguration();
69         httpConfig.setRequestHeaderSize(8192);
70
71         // HTTP connector
72         ServletContextHandler ctxt;
73         try (ServerConnector httpServerConnector = new ServerConnector(server,
74                 new HttpConnectionFactory(httpConfig))) {
75             httpServerConnector.setPort(httpPort);
76             httpServerConnector.setIdleTimeout(30000);
77
78             // SSL Context Factory
79             SslContextFactory sslContextFactory = new SslContextFactory();
80
81             // SSL HTTP Configuration
82             HttpConfiguration httpsConfig = new HttpConfiguration(httpConfig);
83             httpsConfig.addCustomizer(new SecureRequestCustomizer());
84
85             // SSL Connector
86             ServerConnector sslConnector = new ServerConnector(server,
87                     new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
88                     new HttpConnectionFactory(httpsConfig));
89             sslConnector.setPort(httpsPort);
90             server.addConnector(sslConnector);
91
92             /*Skip SSLv3 Fixes*/
93             sslContextFactory.addExcludeProtocols("SSLv3");
94             logger.info("Excluded protocols for Subscriber:" + Arrays.toString(sslContextFactory.getExcludeProtocols()));
95             /*End of SSLv3 Fixes*/
96
97             // HTTPS Configuration
98             try (ServerConnector https = new ServerConnector(server,
99                     new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
100                     new HttpConnectionFactory(httpsConfig))) {
101                 https.setPort(httpsPort);
102                 https.setIdleTimeout(30000);
103             }
104             server.setConnectors(new Connector[]{ httpServerConnector });
105         }
106         ctxt = new ServletContextHandler(0);
107         ctxt.setContextPath(CONTEXT_PATH);
108         server.setHandler(ctxt);
109
110         ctxt.addServlet(new ServletHolder(new SubscriberServlet()), URL_PATTERN);
111         try {
112             server.start();
113         } catch ( Exception e ) {
114             logger.info("Jetty failed to start. Reporting will be unavailable-"+e);
115         }
116         server.join();
117         logger.info("org.onap.dmaap.datarouter.subscriber.Subscriber started-"+ server.getState());
118
119     }
120 }