Fix sonar issues and reporting
[dmaap/datarouter.git] / datarouter-subscriber / src / main / java / org / onap / dmaap / datarouter / subscriber / SubscriberMain.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 com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import java.util.Arrays;
29 import org.eclipse.jetty.http.HttpVersion;
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.ServletContextHandler;
38 import org.eclipse.jetty.servlet.ServletHolder;
39 import org.eclipse.jetty.util.ssl.SslContextFactory;
40
41
42 public class SubscriberMain {
43
44     private static EELFLogger logger = EELFManager.getInstance().getLogger(SubscriberMain.class);
45
46     /**
47      * Main class for Subscriber.
48      * @param args standard args array
49      * @throws Exception generic exception
50      */
51     public static void main(String[] args) throws Exception {
52         SubscriberProps props = SubscriberProps.getInstance(
53                 System.getProperty("org.onap.dmaap.datarouter.subscriber.properties", "subscriber.properties"));
54         int httpsPort = Integer.parseInt(props.getValue("org.onap.dmaap.datarouter.subscriber.https.port", "8443"));
55         int httpPort = Integer.parseInt(props.getValue("org.onap.dmaap.datarouter.subscriber.http.port", "8080"));
56
57         Server server = new Server();
58         HttpConfiguration httpConfig = new HttpConfiguration();
59         httpConfig.setRequestHeaderSize(8192);
60
61         // HTTP connector
62         ServletContextHandler ctxt;
63         try (ServerConnector httpServerConnector = new ServerConnector(server,
64                 new HttpConnectionFactory(httpConfig))) {
65             httpServerConnector.setPort(httpPort);
66             httpServerConnector.setIdleTimeout(30000);
67
68             // SSL Context Factory
69             SslContextFactory sslContextFactory = new SslContextFactory();
70
71             // SSL HTTP Configuration
72             HttpConfiguration httpsConfig = new HttpConfiguration(httpConfig);
73             httpsConfig.addCustomizer(new SecureRequestCustomizer());
74
75             // SSL Connector
76             ServerConnector sslConnector = new ServerConnector(server,
77                     new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
78                     new HttpConnectionFactory(httpsConfig));
79             sslConnector.setPort(httpsPort);
80             server.addConnector(sslConnector);
81
82             /*Skip SSLv3 Fixes*/
83             sslContextFactory.addExcludeProtocols("SSLv3");
84             logger.info("Excluded protocols for SubscriberMain:"
85                                 + Arrays.toString(sslContextFactory.getExcludeProtocols()));
86             /*End of SSLv3 Fixes*/
87
88             // HTTPS Configuration
89             try (ServerConnector https = new ServerConnector(server,
90                     new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
91                     new HttpConnectionFactory(httpsConfig))) {
92                 https.setPort(httpsPort);
93                 https.setIdleTimeout(30000);
94             }
95             server.setConnectors(new Connector[]{ httpServerConnector });
96         }
97         ctxt = new ServletContextHandler(0);
98         ctxt.setContextPath("/");
99         server.setHandler(ctxt);
100
101         ctxt.addServlet(new ServletHolder(new SampleSubscriberServlet()), "/*");
102         try {
103             server.start();
104         } catch ( Exception e ) {
105             logger.info("Jetty failed to start. Reporting will be unavailable-" + e);
106         }
107         server.join();
108         logger.info("org.onap.dmaap.datarouter.subscriber.SubscriberMain started-" + server.getState());
109
110     }
111 }