TransportRequestContext.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.utils.transport;

  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Set;
  24. import java.util.stream.Collectors;

  25. import org.openspcoop2.utils.LoggerWrapperFactory;
  26. import org.openspcoop2.utils.UtilsException;
  27. import org.openspcoop2.utils.transport.http.HttpConstants;
  28. import org.slf4j.Logger;

  29. /**
  30.  * RequestContext
  31.  *
  32.  * @author Poli Andrea (apoli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */


  36. public class TransportRequestContext implements java.io.Serializable {


  37.     /**
  38.      *
  39.      */
  40.     private static final long serialVersionUID = 1L;
  41.        
  42.     /* ---- Coppie nome/valori di invocazione form-based --- */
  43.     protected Map<String, List<String>> parameters;
  44.     /* ---- Coppie nome/valori di invocazione inserite nell'header del trasporto --- */
  45.     protected Map<String, List<String>> headers;
  46.     /* ---- Coppie nome/valori dei cookie --- */
  47.     protected Map<String, String> cookiesValue;
  48.     protected Map<String, Integer> cookiesMaxAge;
  49.    
  50.     protected Credential credential = null;
  51.    
  52.     protected String webContext = null; // es. openspcoop2
  53.     protected String requestURI = null;
  54.    
  55.     protected String requestType = null; // GET/POST/...
  56.    
  57.     protected String source = null; // location es. https://ip:socketPort
  58.    
  59.     protected String protocolName = null; // identificativo del protocollo
  60.     protected String protocolWebContext = null; // webcontext utilizzato per indirizzare il protocollo, ad esempio verra' assegnato api per trasparente.
  61.    
  62.     protected String function = null;
  63.     protected String functionParameters = null;
  64.     protected String interfaceName = null; // Nome PD o PA
  65.    
  66.     protected Logger log = null;
  67.    
  68.     public TransportRequestContext() throws UtilsException{
  69.         this(null);
  70.     }
  71.     public TransportRequestContext(Logger log) throws UtilsException{
  72.         if(log==null) {
  73.             this.log = LoggerWrapperFactory.getLogger(TransportRequestContext.class);
  74.         }
  75.         else {
  76.             this.log = log;
  77.         }
  78.     }
  79.    
  80.    
  81.     public Credential getCredential() {
  82.         return this.credential;
  83.     }
  84.     public String getWebContext() {
  85.         return this.webContext;
  86.     }
  87.     public String getRequestURI() {
  88.         return this.requestURI;
  89.     }
  90.     public String getSource() {
  91.         return this.source;
  92.     }
  93.     public String getProtocolName() {
  94.         return this.protocolName;
  95.     }
  96.     public String getProtocolWebContext() {
  97.         return this.protocolWebContext;
  98.     }
  99.     public String getFunction() {
  100.         return this.function;
  101.     }
  102.     public String getFunctionParameters() {
  103.         return this.functionParameters;
  104.     }
  105.     public String getRequestType() {
  106.         return this.requestType;
  107.     }
  108.     public String getInterfaceName() {
  109.         return this.interfaceName;
  110.     }
  111.        
  112.     @Deprecated
  113.     public Map<String, String> getParametersFormBased(){
  114.         return TransportUtils.convertToMapSingleValue(this.parameters);
  115.     }
  116.     public Map<String, List<String>> getParameters(){
  117.         return this.parameters;
  118.     }
  119.    
  120.     @Deprecated
  121.     public String getParameterFormBased(String name){
  122.         if(this.parameters==null){
  123.             return null;
  124.         }
  125.         return TransportUtils.getObjectAsString(this.parameters, name);
  126.     }
  127.     public String getParameter_compactMultipleValues(String name){
  128.         if(this.parameters==null){
  129.             return null;
  130.         }
  131.         return TransportUtils.getObjectAsString(this.parameters, name);
  132.     }
  133.     public String getParameterFirstValue(String name){
  134.         List<String> l = getParameterValues(name);
  135.         if(l!=null && !l.isEmpty()) {
  136.             return l.get(0);
  137.         }
  138.         return null;
  139.     }
  140.     public List<String> getParameterValues(String name){
  141.         if(this.parameters==null){
  142.             return null;
  143.         }
  144.         return TransportUtils.getRawObject(this.parameters, name);
  145.     }
  146.    
  147.     @Deprecated
  148.     public Object removeParameterFormBased(String name){
  149.         if(this.parameters==null){
  150.             return null;
  151.         }
  152.         return TransportUtils.removeObjectAsString(this.parameters, name);
  153.     }
  154.     public String removeParameter_compactMultipleValues(String name){
  155.         if(this.parameters==null){
  156.             return null;
  157.         }
  158.         return TransportUtils.removeObjectAsString(this.parameters, name);
  159.     }
  160.     public List<String> removeParameter(String name){
  161.         if(this.parameters==null){
  162.             return null;
  163.         }
  164.         return TransportUtils.removeRawObject(this.parameters, name);
  165.     }
  166.    
  167.     @Deprecated
  168.     public Map<String, String> getParametersTrasporto() {
  169.         return TransportUtils.convertToMapSingleValue(this.headers);
  170.     }
  171.     public Map<String, List<String>> getHeaders(){
  172.         return this.headers;
  173.     }
  174.    
  175.     @Deprecated
  176.     public String getParameterTrasporto(String name){
  177.         if(this.headers==null){
  178.             return null;
  179.         }
  180.         return TransportUtils.getObjectAsString(this.headers, name);
  181.     }
  182.     public String getHeader_compactMultipleValues(String name){
  183.         if(this.headers==null){
  184.             return null;
  185.         }
  186.         return TransportUtils.getObjectAsString(this.headers, name);
  187.     }
  188.     public String getHeaderFirstValue(String name){
  189.         List<String> l = getHeaderValues(name);
  190.         if(l!=null && !l.isEmpty()) {
  191.             return l.get(0);
  192.         }
  193.         return null;
  194.     }
  195.     public List<String> getHeaderValues(String name){
  196.         if(this.headers==null){
  197.             return null;
  198.         }
  199.         return TransportUtils.getRawObject(this.headers, name);
  200.     }
  201.    
  202.     @Deprecated
  203.     public Object removeParameterTrasporto(String name){
  204.         if(this.headers==null){
  205.             return null;
  206.         }
  207.         return TransportUtils.removeObjectAsString(this.headers, name);
  208.     }
  209.     public String removeHeader_compactMultipleValues(String name){
  210.         if(this.headers==null){
  211.             return null;
  212.         }
  213.         return TransportUtils.removeObjectAsString(this.headers, name);
  214.     }
  215.     public List<String> removeHeader(String name){
  216.         if(this.headers==null){
  217.             return null;
  218.         }
  219.         return TransportUtils.removeRawObject(this.headers, name);
  220.     }
  221.    
  222.    
  223.     public Set<String> getCookies(){
  224.         return this.cookiesValue.keySet();
  225.     }
  226.     public Map<String, String> getCookiesValue() {
  227.         return this.cookiesValue;
  228.     }
  229.     public String getCookieValue(String name){
  230.         if(this.cookiesValue==null){
  231.             return null;
  232.         }
  233.         return TransportUtils.getRawObject(this.cookiesValue, name);
  234.     }
  235.     public Map<String, Integer> getCookiesMaxAge() {
  236.         return this.cookiesMaxAge;
  237.     }
  238.     public Integer getCookieMaxAge(String name){
  239.         if(this.cookiesMaxAge==null){
  240.             return null;
  241.         }
  242.         return TransportUtils.getRawObject(this.cookiesMaxAge, name);
  243.     }
  244.    
  245.     public String getContentType(){
  246.         if(this.headers!=null){
  247.             return this.getHeaderFirstValue(HttpConstants.CONTENT_TYPE);
  248.         }
  249.         return null;
  250.     }
  251.     public String getContentType(boolean returnMsgErroreIfNotFound) throws Exception{

  252.         if(this.headers!=null &&
  253.                 TransportUtils.containsKey(this.headers, HttpConstants.CONTENT_TYPE)
  254.             ){
  255.             String ct = this.getHeaderFirstValue(HttpConstants.CONTENT_TYPE);
  256.             if(ct==null){
  257.                 if(returnMsgErroreIfNotFound)
  258.                     return HttpConstants.CONTENT_TYPE_NON_VALORIZZATO;
  259.                 else
  260.                     return null;
  261.             }
  262.             return ct;
  263.         }
  264.        
  265.         if(returnMsgErroreIfNotFound)
  266.             return HttpConstants.CONTENT_TYPE_NON_PRESENTE;
  267.         else
  268.             return null;
  269.     }


  270.    
  271.    
  272.     /**
  273.      * Ritorna l'url di invocazione
  274.      *
  275.      * @return url di invocazione
  276.      */
  277.     public String getUrlInvocazione_formBased() {
  278.         if(this.requestURI==null){
  279.             return null;
  280.         }
  281.         return TransportUtils.buildUrlWithParameters(this.parameters, this.requestURI, this.log);
  282.     }
  283.     public String getUrlInvocazioneWithParameters() {
  284.         return this.getUrlInvocazione_formBased();
  285.     }
  286.     public String getUrlInvocazioneWithoutParameters() {
  287.         return this.requestURI;
  288.     }

  289.     @Deprecated
  290.     public void setParametersFormBased(Map<String, String> parametersFormBased) {
  291.         this.parameters = TransportUtils.convertToMapListValues(parametersFormBased);
  292.     }
  293.     public void setParameters(Map<String, List<String>> parametersFormBased) {
  294.         this.parameters = parametersFormBased;
  295.     }

  296.     @Deprecated
  297.     public void setParametersTrasporto(Map<String, String> parametersTrasporto) {
  298.         this.headers = TransportUtils.convertToMapListValues(parametersTrasporto);
  299.     }
  300.     public void setHeaders(Map<String, List<String>> parametersFormBased) {
  301.         this.headers = parametersFormBased;
  302.     }

  303.     public void setCredentials(Credential credentials) {
  304.         this.credential = credentials;
  305.     }  
  306.    
  307.     public void setWebContext(String webContext) {
  308.         this.webContext = webContext;
  309.     }

  310.     public void setRequestURI(String requestURI) {
  311.         this.requestURI = requestURI;
  312.     }

  313.     public void setSource(String source) {
  314.         this.source = source;
  315.     }
  316.    
  317.     public void setProtocol(String protocolName,String protocolWebContext) {
  318.         this.protocolName = protocolName;
  319.         this.protocolWebContext = protocolWebContext;
  320.     }

  321.     public void setFunction(String function) {
  322.         this.function = function;
  323.     }

  324.     public void setFunctionParameters(String functionParameters) {
  325.         this.functionParameters = functionParameters;
  326.     }
  327.    
  328.     public void setRequestType(String requestType) {
  329.         this.requestType = requestType;
  330.     }
  331.    
  332.     public void setInterfaceName(String interfaceName) {
  333.         this.interfaceName = interfaceName;
  334.     }
  335.    
  336.    
  337.     @Override
  338.     public String toString() {
  339.         return this.toString("");
  340.     }
  341.     public String toString(String prefix) {
  342.         StringBuilder sb = new StringBuilder();
  343.         sb.append(prefix).append("DatiRichiesta");
  344.         if(this.webContext!=null) {
  345.             sb.append("\n").append(prefix).append("webContext: ").append(this.webContext);
  346.         }
  347.         if(this.requestURI!=null) {
  348.             sb.append("\n").append(prefix).append("requestURI: ").append(this.requestURI);
  349.         }
  350.         if(this.requestType!=null) {
  351.             sb.append("\n").append(prefix).append("requestType: ").append(this.requestType);
  352.         }
  353.         if(this.source!=null) {
  354.             sb.append("\n").append(prefix).append("source: ").append(this.source);
  355.         }
  356.         if(this.protocolName!=null) {
  357.             sb.append("\n").append(prefix).append("protocolName: ").append(this.protocolName);
  358.         }
  359.         if(this.protocolWebContext!=null) {
  360.             sb.append("\n").append(prefix).append("protocolWebContext: ").append(this.protocolWebContext);
  361.         }
  362.         if(this.function!=null) {
  363.             sb.append("\n").append(prefix).append("function: ").append(this.function);
  364.         }
  365.         if(this.functionParameters!=null) {
  366.             sb.append("\n").append(prefix).append("functionParameters: ").append(this.functionParameters);
  367.         }
  368.         if(this.interfaceName!=null) {
  369.             sb.append("\n").append(prefix).append("interfaceName: ").append(this.interfaceName);
  370.         }
  371.         if(this.credential!=null) {
  372.             sb.append("\n").append(prefix).append("credential: ").append(this.credential);
  373.         }
  374.         if(this.parameters!=null && !this.parameters.isEmpty()) {
  375.             sb.append("\n").append(prefix).append("parameters: ").append(this.convertWithStream(this.parameters));
  376.         }
  377.         if(this.headers!=null && !this.headers.isEmpty()) {
  378.             sb.append("\n").append(prefix).append("headers: ").append(this.convertWithStream(this.headers));
  379.         }
  380.         if(this.cookiesValue!=null && !this.cookiesValue.isEmpty()) {
  381.             sb.append("\n").append(prefix).append("cookiesValue: ").append(this.convertWithStream(this.cookiesValue));
  382.         }
  383.         if(this.cookiesMaxAge!=null && !this.cookiesMaxAge.isEmpty()) {
  384.             sb.append("\n").append(prefix).append("cookiesMaxAge: ").append(this.convertWithStream(this.cookiesMaxAge));
  385.         }
  386.         return sb.toString();
  387.     }
  388.    
  389.     private String convertWithStream(Map<?, ?> map) {
  390.         String mapAsString = map.keySet().stream()
  391.           .map(key -> key + "=" + map.get(key))
  392.           .collect(Collectors.joining(", ", "{", "}"));
  393.         return mapAsString;
  394.     }
  395.    
  396.    

  397. }