Fully HTTPS support in the catalog-fe
[sdc.git] / catalog-fe / src / main / java / org / openecomp / sdc / fe / listen / FEAppContextListener.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (c) 2019 Samsung
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdc.fe.listen;
23
24 import java.security.KeyStoreException;
25 import java.security.NoSuchAlgorithmException;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Executors;
28
29 import javax.net.ssl.SSLContext;
30 import javax.net.ssl.SSLException;
31 import javax.servlet.ServletContextEvent;
32 import javax.servlet.ServletContextListener;
33
34 import org.apache.http.config.Registry;
35 import org.apache.http.config.RegistryBuilder;
36 import org.apache.http.conn.socket.ConnectionSocketFactory;
37 import org.apache.http.conn.socket.PlainConnectionSocketFactory;
38 import org.apache.http.conn.ssl.NoopHostnameVerifier;
39 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
40 import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
41 import org.apache.http.impl.client.CloseableHttpClient;
42 import org.apache.http.impl.client.HttpClients;
43 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
44 import org.apache.http.ssl.SSLContextBuilder;
45 import org.openecomp.sdc.common.api.Constants;
46 import org.openecomp.sdc.common.impl.ExternalConfiguration;
47 import org.openecomp.sdc.common.listener.AppContextListener;
48 import org.openecomp.sdc.fe.config.ConfigurationManager;
49 import org.openecomp.sdc.fe.impl.PluginStatusBL;
50 import org.openecomp.sdc.fe.monitoring.FeMonitoringService;
51 import org.openecomp.sdc.fe.servlets.HealthCheckService;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 public class FEAppContextListener extends AppContextListener implements ServletContextListener {
56
57     private static final int HEALTH_CHECK_INTERVAL = 5;
58     private static final int PROBE_INTERVAL = 15;
59     private static Logger log = LoggerFactory.getLogger(FEAppContextListener.class.getName());
60
61     public void contextInitialized(ServletContextEvent context) {
62
63         super.contextInitialized(context);
64
65         ConfigurationManager configurationManager = new ConfigurationManager(
66                 ExternalConfiguration.getConfigurationSource());
67         log.debug("loading configuration from configDir:{} appName:{}", ExternalConfiguration.getConfigDir(),
68                 ExternalConfiguration.getAppName());
69         context.getServletContext().setAttribute(Constants.CONFIGURATION_MANAGER_ATTR, configurationManager);
70
71         try {
72             PluginStatusBL pbl = new PluginStatusBL(buildRestClient());
73             context.getServletContext().setAttribute(Constants.PLUGIN_BL_COMPONENT, pbl);
74         } catch (SSLException e) {
75             log.debug("ERROR: Build rest client failed because ", e);
76             return;
77         }
78
79         // Health Check service
80         HealthCheckService hcs = new HealthCheckService(context.getServletContext());
81         hcs.start(configurationManager.getConfiguration().getHealthCheckIntervalInSeconds(HEALTH_CHECK_INTERVAL));
82         context.getServletContext().setAttribute(Constants.HEALTH_CHECK_SERVICE_ATTR, hcs);
83
84         // Monitoring service
85         FeMonitoringService fms = new FeMonitoringService(context.getServletContext());
86         fms.start(configurationManager.getConfiguration().getSystemMonitoring().getProbeIntervalInSeconds(PROBE_INTERVAL));
87
88         if (configurationManager.getConfiguration() == null) {
89             log.debug("ERROR: configuration was not properly loaded");
90             return;
91         }
92
93         ExecutorService executorPool = Executors
94                 .newFixedThreadPool(configurationManager.getConfiguration().getThreadpoolSize());
95         context.getServletContext().setAttribute(Constants.THREAD_EXECUTOR_ATTR, executorPool);
96
97         log.debug("After executing {}", this.getClass());
98     }
99
100     public void contextDestroyed(ServletContextEvent context) {
101
102         ExecutorService executorPool = (ExecutorService) context.getServletContext()
103                 .getAttribute(Constants.THREAD_EXECUTOR_ATTR);
104         if (executorPool != null) {
105             executorPool.shutdown();
106         }
107
108         super.contextDestroyed(context);
109
110     }
111
112     private CloseableHttpClient buildRestClient() throws SSLException {
113         SSLContextBuilder builder = new SSLContextBuilder();
114         try {
115             builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
116             SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
117                 SSLContext.getDefault(), NoopHostnameVerifier.INSTANCE);
118             Registry<ConnectionSocketFactory> registry =
119                 RegistryBuilder.<ConnectionSocketFactory>create()
120                     .register("http", new PlainConnectionSocketFactory()).register("https", sslsf)
121                     .build();
122             PoolingHttpClientConnectionManager cm =
123                 new PoolingHttpClientConnectionManager(registry);
124             return HttpClients.custom().setSSLSocketFactory(sslsf).setConnectionManager(cm).build();
125         } catch (NoSuchAlgorithmException | KeyStoreException e) {
126             throw new SSLException(e);
127         }
128     }
129 }