MockHttpServletRequest.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.protocol.modipa.utils;

  21. import java.io.BufferedReader;
  22. import java.io.IOException;
  23. import java.io.InputStreamReader;
  24. import java.io.UnsupportedEncodingException;
  25. import java.nio.charset.StandardCharsets;
  26. import java.security.Principal;
  27. import java.util.Collection;
  28. import java.util.Collections;
  29. import java.util.Enumeration;
  30. import java.util.HashMap;
  31. import java.util.HashSet;
  32. import java.util.List;
  33. import java.util.Locale;
  34. import java.util.Map;
  35. import java.util.Objects;
  36. import java.util.Set;
  37. import java.util.stream.Collectors;

  38. import javax.servlet.AsyncContext;
  39. import javax.servlet.DispatcherType;
  40. import javax.servlet.RequestDispatcher;
  41. import javax.servlet.ServletContext;
  42. import javax.servlet.ServletException;
  43. import javax.servlet.ServletInputStream;
  44. import javax.servlet.ServletRequest;
  45. import javax.servlet.ServletResponse;
  46. import javax.servlet.http.Cookie;
  47. import javax.servlet.http.HttpServletRequest;
  48. import javax.servlet.http.HttpServletResponse;
  49. import javax.servlet.http.HttpSession;
  50. import javax.servlet.http.HttpUpgradeHandler;
  51. import javax.servlet.http.Part;

  52. /**
  53.  * Classe per simulare una richiesta servlet http in cui si possono sovrascrivere
  54.  * vari elementi
  55.  *
  56.  * @author Tommaso Burlon (tommaso.burlon@link.it)
  57.  * @author $Author$
  58.  * @version $Rev$, $Date$
  59.  */
  60. public class MockHttpServletRequest implements HttpServletRequest {

  61.     private AsyncContext asyncContext = null;
  62.     private Map<String, Object> attributes = null;
  63.     private Map<String, List<String>> headers = null;
  64.     private String characterEncoding = null;
  65.     private DispatcherType dispatcherType = null;
  66.     private HttpServletRequest req = null;
  67.     private MockServletInputStream is = null;
  68.    
  69.     public MockHttpServletRequest() {
  70.     }
  71.    
  72.     public MockHttpServletRequest(HttpServletRequest req) {
  73.         this.req = req;
  74.     }
  75.    
  76.     @Override
  77.     public AsyncContext getAsyncContext() {
  78.         if (this.asyncContext != null)
  79.             return this.asyncContext;
  80.         return this.req.getAsyncContext();
  81.     }
  82.     public void setAsyncContext(AsyncContext asyncContext) {
  83.         this.asyncContext = asyncContext;
  84.     }

  85.     @Override
  86.     public Object getAttribute(String arg0) {
  87.         if (this.attributes != null)
  88.             return this.attributes.get(arg0);
  89.         return this.req.getAttribute(arg0);
  90.     }
  91.     @Override
  92.     public Enumeration<String> getAttributeNames() {
  93.         if (this.attributes != null)
  94.             return Collections.enumeration(this.attributes.keySet());
  95.         return this.req.getAttributeNames();
  96.     }
  97.     public void setAttributes(Map<String, Object> attributes) {
  98.         this.attributes = attributes;
  99.     }

  100.     @Override
  101.     public String getCharacterEncoding() {
  102.         if (this.characterEncoding != null)
  103.             return this.characterEncoding;
  104.         return this.req.getCharacterEncoding();
  105.     }
  106.     public void setWrappedCharacterEncoding(String enc) {
  107.         this.characterEncoding = enc;
  108.     }

  109.     @Override
  110.     public int getContentLength() {
  111.         return this.getIntHeader("Content-Length");
  112.     }

  113.     @Override
  114.     public long getContentLengthLong() {
  115.         return this.getIntHeader("Content-Length");
  116.     }

  117.     @Override
  118.     public String getContentType() {
  119.         return this.getHeader("Content-Type");
  120.     }

  121.     @Override
  122.     public DispatcherType getDispatcherType() {
  123.         if (this.dispatcherType != null)
  124.             return this.dispatcherType;
  125.         return this.req.getDispatcherType();
  126.     }
  127.     public void setDispatcherType(DispatcherType dispatcherType) {
  128.         this.dispatcherType = dispatcherType;
  129.     }

  130.     @Override
  131.     public ServletInputStream getInputStream() throws IOException {
  132.         if (this.is != null)
  133.             return this.is;
  134.         return this.req.getInputStream();
  135.     }

  136.     @Override
  137.     public String getLocalAddr() {
  138.         return this.req.getLocalAddr();
  139.     }

  140.     @Override
  141.     public String getLocalName() {
  142.         return this.req.getLocalName();
  143.     }

  144.     @Override
  145.     public int getLocalPort() {
  146.         return this.req.getLocalPort();
  147.     }

  148.     @Override
  149.     public Locale getLocale() {
  150.         return this.req.getLocale();
  151.     }

  152.     @Override
  153.     public Enumeration<Locale> getLocales() {
  154.         return this.req.getLocales();
  155.     }

  156.     @Override
  157.     public String getParameter(String arg0) {
  158.         return this.req.getParameter(arg0);
  159.     }

  160.     @Override
  161.     public Map<String, String[]> getParameterMap() {
  162.         return this.req.getParameterMap();
  163.     }

  164.     @Override
  165.     public Enumeration<String> getParameterNames() {
  166.         return this.req.getParameterNames();
  167.     }

  168.     @Override
  169.     public String[] getParameterValues(String arg0) {
  170.         return this.req.getParameterValues(arg0);
  171.     }

  172.     @Override
  173.     public String getProtocol() {
  174.         return this.req.getProtocol();
  175.     }

  176.     @Override
  177.     public BufferedReader getReader() throws IOException {
  178.         if (this.is != null)
  179.             return new BufferedReader(new InputStreamReader(this.is, StandardCharsets.UTF_8));
  180.         return this.req.getReader();
  181.     }

  182.     /**
  183.      * @deprecated
  184.      */
  185.     @SuppressWarnings("deprecation")
  186.     @Deprecated(since="3.3")
  187.     @Override
  188.     public String getRealPath(String arg0) {
  189.         return this.req.getRealPath(arg0);
  190.     }

  191.     @Override
  192.     public String getRemoteAddr() {
  193.         return this.req.getRemoteAddr();
  194.     }

  195.     @Override
  196.     public String getRemoteHost() {
  197.         return this.req.getRemoteHost();
  198.     }

  199.     @Override
  200.     public int getRemotePort() {
  201.         return this.req.getRemotePort();
  202.     }

  203.     @Override
  204.     public RequestDispatcher getRequestDispatcher(String arg0) {
  205.         return this.req.getRequestDispatcher(arg0);
  206.     }

  207.     @Override
  208.     public String getScheme() {
  209.         return this.req.getScheme();
  210.     }

  211.     @Override
  212.     public String getServerName() {
  213.         return this.req.getServerName();
  214.     }

  215.     @Override
  216.     public int getServerPort() {
  217.         return this.req.getServerPort();
  218.     }

  219.     @Override
  220.     public ServletContext getServletContext() {
  221.         return this.req.getServletContext();
  222.     }

  223.     @Override
  224.     public boolean isAsyncStarted() {
  225.         return this.req.isAsyncStarted();
  226.     }

  227.     @Override
  228.     public boolean isAsyncSupported() {
  229.         return this.req.isAsyncSupported();
  230.     }

  231.     @Override
  232.     public boolean isSecure() {
  233.         return this.req.isSecure();
  234.     }

  235.     @Override
  236.     public void removeAttribute(String arg0) {
  237.         this.req.removeAttribute(arg0);
  238.     }

  239.     @Override
  240.     public void setAttribute(String arg0, Object arg1) {
  241.         this.req.setAttribute(arg0, arg1);

  242.     }

  243.     @Override
  244.     public void setCharacterEncoding(String arg0) throws UnsupportedEncodingException {
  245.         this.req.setCharacterEncoding(arg0);
  246.     }

  247.     @Override
  248.     public AsyncContext startAsync() throws IllegalStateException {
  249.         return this.req.startAsync();
  250.     }

  251.     @Override
  252.     public AsyncContext startAsync(ServletRequest arg0, ServletResponse arg1) throws IllegalStateException {
  253.         return this.req.startAsync(arg0, arg1);
  254.     }

  255.     @Override
  256.     public boolean authenticate(HttpServletResponse arg0) throws IOException, ServletException {
  257.         return this.req.authenticate(arg0);
  258.     }

  259.     @Override
  260.     public String changeSessionId() {
  261.         return this.req.changeSessionId();
  262.     }

  263.     @Override
  264.     public String getAuthType() {
  265.         return this.req.getAuthType();
  266.     }

  267.     @Override
  268.     public String getContextPath() {
  269.         return this.req.getContextPath();
  270.     }

  271.     @Override
  272.     public Cookie[] getCookies() {
  273.         return this.req.getCookies();
  274.     }

  275.     @Override
  276.     public long getDateHeader(String arg0) {
  277.         return Long.valueOf(this.getHeader(arg0));
  278.     }

  279.     private String preProcessHeaderName(String name) {
  280.         if (name != null) {
  281.             return name.toLowerCase();
  282.         }
  283.         return name;
  284.     }
  285.    
  286.     @Override
  287.     public String getHeader(String arg0) {
  288.         arg0 = preProcessHeaderName(arg0);
  289.         if (this.headers != null && this.headers.containsKey(arg0)) {
  290.             List<String> v = this.headers.get(arg0);
  291.             if(v!=null && !v.isEmpty()) {
  292.                 return String.join(",", this.headers.get(arg0));
  293.             }
  294.         }
  295.         return this.req.getHeader(arg0);
  296.     }

  297.     @Override
  298.     public Enumeration<String> getHeaderNames() {
  299.         Set<String> names = new HashSet<>();
  300.         if (this.headers != null)
  301.             names.addAll(this.headers.keySet());
  302.         if (this.req != null)
  303.             this.req.getHeaderNames().asIterator().forEachRemaining(name ->
  304.                 names.add(preProcessHeaderName(name))
  305.             );
  306.         return Collections.enumeration(names);
  307.     }

  308.     @Override
  309.     public Enumeration<String> getHeaders(String arg0) {
  310.         arg0 = preProcessHeaderName(arg0);
  311.         Set<String> values = new HashSet<>();
  312.         if (this.headers != null)
  313.             values.addAll(Objects.requireNonNullElse(this.headers.getOrDefault(arg0, List.of()), List.of()));
  314.         if (this.req != null && values.isEmpty())
  315.             this.req.getHeaders(arg0).asIterator().forEachRemaining(values::add);
  316.         return Collections.enumeration(values.stream().filter(o -> o != null).collect(Collectors.toList()));
  317.     }

  318.     @Override
  319.     public int getIntHeader(String arg0) {
  320.         return Integer.valueOf(this.getHeader(arg0));
  321.     }

  322.     @Override
  323.     public String getMethod() {
  324.         return this.req.getMethod();
  325.     }

  326.     @Override
  327.     public Part getPart(String arg0) throws IOException, ServletException {
  328.         return this.req.getPart(arg0);
  329.     }

  330.     @Override
  331.     public Collection<Part> getParts() throws IOException, ServletException {
  332.         return this.req.getParts();
  333.     }

  334.     @Override
  335.     public String getPathInfo() {
  336.         return this.req.getPathInfo();
  337.     }

  338.     @Override
  339.     public String getPathTranslated() {
  340.         return this.req.getPathTranslated();
  341.     }

  342.     @Override
  343.     public String getQueryString() {
  344.         return this.req.getQueryString();
  345.     }

  346.     @Override
  347.     public String getRemoteUser() {
  348.         return this.req.getRemoteUser();
  349.     }

  350.     @Override
  351.     public String getRequestURI() {
  352.         return this.req.getRequestURI();
  353.     }

  354.     @Override
  355.     public StringBuffer getRequestURL() {
  356.         return this.req.getRequestURL();
  357.     }

  358.     @Override
  359.     public String getRequestedSessionId() {
  360.         return null;
  361.     }

  362.     @Override
  363.     public String getServletPath() {
  364.         return this.req.getServletPath();
  365.     }

  366.     @Override
  367.     public HttpSession getSession() {
  368.         return this.req.getSession();
  369.     }

  370.     @Override
  371.     public HttpSession getSession(boolean arg0) {
  372.         return this.req.getSession(arg0);
  373.     }

  374.     @Override
  375.     public Principal getUserPrincipal() {
  376.         return this.req.getUserPrincipal();
  377.     }

  378.     @Override
  379.     public boolean isRequestedSessionIdFromCookie() {
  380.         return this.req.isRequestedSessionIdFromCookie();
  381.     }

  382.     @Override
  383.     public boolean isRequestedSessionIdFromURL() {
  384.         return this.req.isRequestedSessionIdFromURL();
  385.     }

  386.     /**
  387.      * @deprecated
  388.      */
  389.     @SuppressWarnings("deprecation")
  390.     @Deprecated(since="3.3")
  391.     @Override
  392.     public boolean isRequestedSessionIdFromUrl() {
  393.         return this.req.isRequestedSessionIdFromUrl();
  394.     }

  395.     @Override
  396.     public boolean isRequestedSessionIdValid() {
  397.         return this.req.isRequestedSessionIdValid();
  398.     }

  399.     @Override
  400.     public boolean isUserInRole(String arg0) {
  401.         return this.req.isUserInRole(arg0);
  402.     }

  403.     @Override
  404.     public void login(String arg0, String arg1) throws ServletException {
  405.         this.req.login(arg0, arg1);
  406.     }

  407.     @Override
  408.     public void logout() throws ServletException {
  409.         this.req.logout();
  410.     }

  411.     @Override
  412.     public <T extends HttpUpgradeHandler> T upgrade(Class<T> arg0) throws IOException, ServletException {
  413.         return this.req.upgrade(arg0);
  414.     }

  415.     public void setHeaders(Map<String, List<String>> headers) {
  416.         this.headers = new HashMap<>();
  417.         for (Map.Entry<String, List<String>> entry : headers.entrySet())
  418.             this.headers.put(preProcessHeaderName(entry.getKey()), entry.getValue());
  419.     }
  420.    
  421.     public void setHeader(String name, List<String> value) {
  422.         name = preProcessHeaderName(name);
  423.         if (this.headers == null)
  424.             this.headers = new HashMap<>();
  425.         this.headers.put(name, value);
  426.     }
  427.    
  428.     public void setInputStream(MockServletInputStream is) {
  429.         this.is = is;
  430.     }

  431. }