2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.fe.servlets;
23 import java.util.Enumeration;
25 import javax.servlet.ServletException;
26 import javax.servlet.http.HttpServletRequest;
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;
40 public abstract class SSLProxyServlet extends ProxyServlet {
42 private static final long serialVersionUID = 1L;
43 private static Logger log = LoggerFactory.getLogger(SSLProxyServlet.class.getName());
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,
59 proxyRequest.header(headerName, headerVal);
62 proxyRequest.getHeaders().remove(HttpHeader.HOST);
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();
73 client.setIdleTimeout(600000);
74 client.setStopTimeout(600000);
79 private HttpClient getSecureHttpClient() throws ServletException {
80 // Instantiate and configure the SslContextFactory
81 SslContextFactory sslContextFactory = new SslContextFactory(true);
83 // Instantiate HttpClient with the SslContextFactory
84 HttpClient httpClient = new HttpClient(sslContextFactory);
86 // Configure HttpClient, for example:
87 httpClient.setFollowRedirects(false);
94 } catch (Exception x) {
95 throw new ServletException(x);