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