891bc4ae3472e477681f6b50a1a9f65e7bbd3233
[sdc.git] / catalog-fe / src / main / java / org / openecomp / sdc / fe / servlets / SSLProxyServlet.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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 package org.openecomp.sdc.fe.servlets;
21
22 import javax.servlet.ServletException;
23 import org.eclipse.jetty.client.HttpClient;
24 import org.eclipse.jetty.proxy.ProxyServlet;
25 import org.eclipse.jetty.util.ssl.SslContextFactory;
26 import org.openecomp.sdc.common.api.Constants;
27 import org.openecomp.sdc.fe.config.Configuration;
28 import org.openecomp.sdc.fe.config.ConfigurationManager;
29 import org.openecomp.sdc.fe.utils.BeProtocol;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public abstract class SSLProxyServlet extends ProxyServlet {
34
35     private static final long serialVersionUID = 1L;
36     private static final Logger log = LoggerFactory.getLogger(SSLProxyServlet.class);
37
38     @Override
39     protected HttpClient createHttpClient() throws ServletException {
40         Configuration config = ((ConfigurationManager) getServletConfig().getServletContext().getAttribute(Constants.CONFIGURATION_MANAGER_ATTR))
41             .getConfiguration();
42         boolean isSecureClient = !config.getBeProtocol().equals(BeProtocol.HTTP.getProtocolName());
43         HttpClient client = (isSecureClient) ? getSecureHttpClient() : super.createHttpClient();
44         int requestTimeout = config.getRequestTimeout() * 1000;
45         if (requestTimeout == 0) {
46             requestTimeout = 1200_000;
47         }
48         setTimeout(requestTimeout);
49         client.setIdleTimeout(requestTimeout);
50         client.setStopTimeout(requestTimeout);
51         return client;
52     }
53
54     private HttpClient getSecureHttpClient() throws ServletException {
55         // Instantiate HttpClient with the SslContextFactory
56         final var httpClient = new HttpClient(new SslContextFactory.Client(true));
57         // Configure HttpClient, for example:
58         httpClient.setFollowRedirects(false);
59         // Start HttpClient
60         try {
61             httpClient.start();
62         } catch (Exception x) {
63             log.error("Exception thrown while starting httpClient", x);
64             throw new ServletException(x);
65         }
66         return httpClient;
67     }
68 }