From 3a9c18a098c3f5c519fb0dbed36384af6ccaf151 Mon Sep 17 00:00:00 2001 From: avigaffa Date: Wed, 15 Aug 2018 18:52:07 +0300 Subject: Fix duplicate headers in webseal-simulator webseal-simulator add user related headers multiple time for requests. Change-Id: Iaaaaaf0c6431cbbef5e64dc72b72c6563b7055c3 Issue-ID: SDC-1652 Signed-off-by: avigaffa Signed-off-by: vempo --- .../openecomp/sdc/webseal/simulator/SdcProxy.java | 175 +++++++++++---------- 1 file changed, 96 insertions(+), 79 deletions(-) (limited to 'utils/webseal-simulator/src/main/java') diff --git a/utils/webseal-simulator/src/main/java/org/openecomp/sdc/webseal/simulator/SdcProxy.java b/utils/webseal-simulator/src/main/java/org/openecomp/sdc/webseal/simulator/SdcProxy.java index 2580b439c2..7a3b9bc692 100644 --- a/utils/webseal-simulator/src/main/java/org/openecomp/sdc/webseal/simulator/SdcProxy.java +++ b/utils/webseal-simulator/src/main/java/org/openecomp/sdc/webseal/simulator/SdcProxy.java @@ -1,33 +1,44 @@ package org.openecomp.sdc.webseal.simulator; -import org.apache.commons.httpclient.Header; -import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.httpclient.HttpException; -import org.apache.commons.httpclient.HttpMethodBase; -import org.apache.commons.httpclient.methods.*; -import org.apache.commons.httpclient.protocol.Protocol; -import org.openecomp.sdc.webseal.simulator.SSL.DummySSLProtocolSocketFactory; -import org.openecomp.sdc.webseal.simulator.conf.Conf; - -import javax.net.ssl.X509TrustManager; -import javax.servlet.RequestDispatcher; -import javax.servlet.ServletConfig; -import javax.servlet.ServletException; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.*; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.ArrayList; +import java.util.Arrays; import java.util.Enumeration; import java.util.List; import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; import java.util.zip.GZIPInputStream; +import javax.net.ssl.X509TrustManager; +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.apache.commons.httpclient.Header; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HttpException; +import org.apache.commons.httpclient.HttpMethodBase; +import org.apache.commons.httpclient.methods.DeleteMethod; +import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.methods.InputStreamRequestEntity; +import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.commons.httpclient.methods.PutMethod; +import org.apache.commons.httpclient.protocol.Protocol; +import org.openecomp.sdc.webseal.simulator.SSL.DummySSLProtocolSocketFactory; +import org.openecomp.sdc.webseal.simulator.conf.Conf; public class SdcProxy extends HttpServlet { @@ -35,7 +46,7 @@ public class SdcProxy extends HttpServlet { private URL url; private HttpClient proxy; private Conf conf; - + private final String SDC1 = "/sdc1"; private final String ONBOARDING = "/onboarding/"; private final String SCRIPTS = "/scripts"; @@ -43,6 +54,8 @@ public class SdcProxy extends HttpServlet { private final String LANGUAGES = "/languages"; private final String CONFIGURATIONS = "/configurations"; + private static final Set RESERVED_HEADERS = Arrays.stream(ReservedHeaders.values()).map(h -> h.name()).collect(Collectors.toSet()); + public void init(ServletConfig config) throws ServletException { super.init(config); conf = Conf.getInstance(); @@ -105,8 +118,6 @@ public class SdcProxy extends HttpServlet { System.out.print(request.getRequestURI() + " -> "); String userIdHeader = getUseridFromRequest(request); - //System.out.print(" (userIdHeader=" + userIdHeader + ") "); - User user = getUser(userIdHeader); // new request - forward to login page if (userIdHeader == null) { @@ -116,17 +127,18 @@ public class SdcProxy extends HttpServlet { } String uri = getUri(request, requestParameters); - HttpMethodBase proxyMethod = getMethod(request, methodEnum, uri); + HttpMethodBase proxyMethod = createHttpMethod(request, methodEnum, uri); System.out.println(uri); - addHeaders(user, proxyMethod); - addHeaders(request, proxyMethod); + + User user = getUser(userIdHeader); + addHeadersToMethod(proxyMethod, user, request); this.proxy.executeMethod(proxyMethod); response.setStatus(proxyMethod.getStatusCode()); - + if (request.getRequestURI().indexOf(".svg") > -1) { response.setContentType("image/svg+xml"); } - + InputStream responseBodyStream = proxyMethod.getResponseBodyAsStream(); Header contentEncodingHeader = proxyMethod.getResponseHeader("Content-Encoding"); if (contentEncodingHeader != null && contentEncodingHeader.getValue().equalsIgnoreCase("gzip")) { @@ -135,18 +147,6 @@ public class SdcProxy extends HttpServlet { write(responseBodyStream, response.getOutputStream()); } - private void addHeaders(HttpServletRequest request, HttpMethodBase proxyMethod) { - Enumeration headerNames = request.getHeaderNames(); - while (headerNames.hasMoreElements()) { - String headerName = headerNames.nextElement(); - Enumeration headers = request.getHeaders(headerName); - while (headers.hasMoreElements()) { - String headerValue = headers.nextElement(); - proxyMethod.addRequestHeader(headerName, headerValue); - } - } - } - private User getUser(String userId, String password) { User user = getUser(userId); if (user.getPassword().equals(password)) { @@ -159,16 +159,16 @@ public class SdcProxy extends HttpServlet { return conf.getUsers().get(userId); } - + private List getContextPaths(){ - List contextPaths = new ArrayList<>(); - contextPaths.add(SDC1); - contextPaths.add(ONBOARDING); - contextPaths.add(STYLES); - contextPaths.add(SCRIPTS); - contextPaths.add(LANGUAGES); - contextPaths.add(CONFIGURATIONS); - return contextPaths; + List contextPaths = new ArrayList<>(); + contextPaths.add(SDC1); + contextPaths.add(ONBOARDING); + contextPaths.add(STYLES); + contextPaths.add(SCRIPTS); + contextPaths.add(LANGUAGES); + contextPaths.add(CONFIGURATIONS); + return contextPaths; } private String getUri(HttpServletRequest request, Map requestParameters) throws UnsupportedEncodingException { @@ -181,23 +181,23 @@ public class SdcProxy extends HttpServlet { return uri; } - private HttpMethodBase getMethod(HttpServletRequest request, MethodEnum methodEnum, String uri) throws IOException { + private HttpMethodBase createHttpMethod(HttpServletRequest request, MethodEnum methodEnum, String uri) throws IOException { HttpMethodBase proxyMethod = null; switch (methodEnum) { - case GET: - proxyMethod = new GetMethod(uri); - break; - case POST: - proxyMethod = new PostMethod(uri); - ((PostMethod) proxyMethod).setRequestEntity(new InputStreamRequestEntity(request.getInputStream())); - break; - case PUT: - proxyMethod = new PutMethod(uri); - ((PutMethod) proxyMethod).setRequestBody(getBody(request)); - break; - case DELETE: - proxyMethod = new DeleteMethod(uri); - break; + case GET: + proxyMethod = new GetMethod(uri); + break; + case POST: + proxyMethod = new PostMethod(uri); + ((PostMethod) proxyMethod).setRequestEntity(new InputStreamRequestEntity(request.getInputStream())); + break; + case PUT: + proxyMethod = new PutMethod(uri); + ((PutMethod) proxyMethod).setRequestBody(getBody(request)); + break; + case DELETE: + proxyMethod = new DeleteMethod(uri); + break; } return proxyMethod; } @@ -217,41 +217,54 @@ public class SdcProxy extends HttpServlet { if (cookies != null){ for (int i=0; i headerNames = request.getHeaderNames(); + while (headerNames.hasMoreElements()) { + String headerName = headerNames.nextElement(); + if (!RESERVED_HEADERS.contains(headerName)) { + Enumeration headers = request.getHeaders(headerName); + while (headers.hasMoreElements()) { + String headerValue = headers.nextElement(); + proxyMethod.addRequestHeader(headerName, headerValue); + } + } + } } private String alignUrlProxy(String requestURI) { - + int i = requestURI.indexOf(ONBOARDING); if (-1 != i){ return requestURI.substring(i); } - + i = requestURI.indexOf(SDC1+SDC1); if (-1 != i){ return requestURI.substring(SDC1.length()); } - + i = requestURI.indexOf(SDC1); if (-1 != i){ return requestURI; } - + return SDC1+requestURI; } @@ -284,8 +297,8 @@ public class SdcProxy extends HttpServlet { public String getServletInfo() { return "Http Proxy Servlet"; } - - + + public String getBody(HttpServletRequest request) throws IOException { String body = null; @@ -320,6 +333,10 @@ public class SdcProxy extends HttpServlet { return body; } + private enum ReservedHeaders { + HTTP_IV_USER, USER_ID, HTTP_CSP_FIRSTNAME, HTTP_CSP_EMAIL, HTTP_CSP_LASTNAME, HTTP_IV_REMOTE_ADDRESS, HTTP_CSP_WSTYPE + } + private class DefaultTrustManager implements X509TrustManager { @Override -- cgit 1.2.3-korg