Create on boarding docker
[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
21 package org.openecomp.sdc.fe.servlets;
22
23 import java.util.Enumeration;
24
25 import javax.servlet.ServletException;
26 import javax.servlet.http.HttpServletRequest;
27
28 import org.eclipse.jetty.client.HttpClient;
29 import org.eclipse.jetty.client.api.Request;
30 import org.eclipse.jetty.http.HttpHeader;
31 import org.eclipse.jetty.proxy.ProxyServlet;
32 import org.eclipse.jetty.util.ssl.SslContextFactory;
33 import org.openecomp.sdc.common.api.Constants;
34 import org.openecomp.sdc.fe.config.Configuration;
35 import org.openecomp.sdc.fe.config.ConfigurationManager;
36 import org.openecomp.sdc.fe.utils.BeProtocol;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public abstract class SSLProxyServlet extends ProxyServlet {
41
42         private static final long serialVersionUID = 1L;
43         private static Logger log = LoggerFactory.getLogger(SSLProxyServlet.class.getName());
44
45
46
47         @Override
48         public void customizeProxyRequest(Request proxyRequest, HttpServletRequest request) {
49                 super.customizeProxyRequest(proxyRequest, request);
50                 // Add Missing Headers to proxy request
51                 @SuppressWarnings("unchecked")
52                 Enumeration<String> headerNames = request.getHeaderNames();
53                 while (headerNames.hasMoreElements()) {
54                         String headerName = headerNames.nextElement();
55                         if (!proxyRequest.getHeaders().containsKey(headerName)) {
56                                 String headerVal = request.getHeader(headerName);
57                                 log.debug("Adding missing header to request,  header name: {} , header value: {}", headerName,
58                                                 headerVal);
59                                 proxyRequest.header(headerName, headerVal);
60                         }
61                 }
62                 proxyRequest.getHeaders().remove(HttpHeader.HOST);
63
64         }
65
66         @Override
67         protected HttpClient createHttpClient() throws ServletException {
68                 Configuration config = ((ConfigurationManager) getServletConfig().getServletContext()
69                                 .getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).getConfiguration();
70                 boolean isSecureClient = !config.getBeProtocol().equals(BeProtocol.HTTP.getProtocolName());
71                 HttpClient client = (isSecureClient) ? getSecureHttpClient() : super.createHttpClient();
72                 setTimeout(600000);
73                 client.setIdleTimeout(600000);
74                 client.setStopTimeout(600000);
75
76                 return client;
77         }
78
79         private HttpClient getSecureHttpClient() throws ServletException {
80                 // Instantiate and configure the SslContextFactory
81                 SslContextFactory sslContextFactory = new SslContextFactory(true);
82
83                 // Instantiate HttpClient with the SslContextFactory
84                 HttpClient httpClient = new HttpClient(sslContextFactory);
85
86                 // Configure HttpClient, for example:
87                 httpClient.setFollowRedirects(false);
88
89                 // Start HttpClient
90                 try {
91                         httpClient.start();
92
93                         return httpClient;
94                 } catch (Exception x) {
95                         throw new ServletException(x);
96                 }
97         }
98 }