InformazioniIntegrazione.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.pdd.core.dynamic;

  21. import java.io.Serializable;
  22. import java.util.ArrayList;
  23. import java.util.Collections;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;

  27. import org.apache.commons.lang.StringUtils;
  28. import org.openspcoop2.utils.io.Base64Utilities;
  29. import org.openspcoop2.utils.io.HexBinaryUtilities;
  30. import org.openspcoop2.utils.json.JSONUtils;
  31. import org.openspcoop2.utils.transport.TransportRequestContext;
  32. import org.openspcoop2.utils.transport.TransportResponseContext;
  33. import org.openspcoop2.utils.xml2json.JsonXmlPathExpressionEngine;
  34. import org.slf4j.Logger;

  35. import com.fasterxml.jackson.databind.JsonNode;

  36. /**    
  37.  * InformazioniIntegrazioneCustom
  38.  *
  39.  * @author Poli Andrea (poli@link.it)
  40.  * @author $Author$
  41.  * @version $Rev$, $Date$
  42.  */
  43. public class InformazioniIntegrazione extends org.openspcoop2.utils.beans.BaseBean implements Serializable {

  44.     /**
  45.      *
  46.      */
  47.     private static final long serialVersionUID = 1L;
  48.    
  49.     public InformazioniIntegrazione() {} // per serializzatore
  50.     public InformazioniIntegrazione(InformazioniIntegrazioneSorgente sourceType,  String sourceName, InformazioniIntegrazioneCodifica sourceEncodeType, boolean sourceRequired,
  51.             Logger log, TransportRequestContext transportRequestContext) throws Exception {
  52.         init(sourceType, sourceName, sourceEncodeType, sourceRequired,
  53.                 log, transportRequestContext, null);
  54.     }
  55.     public InformazioniIntegrazione(InformazioniIntegrazioneSorgente sourceType,  String sourceName, InformazioniIntegrazioneCodifica sourceEncodeType, boolean sourceRequired,
  56.             Logger log, TransportResponseContext transportResponseContext) throws Exception {
  57.         init(sourceType, sourceName, sourceEncodeType, sourceRequired,
  58.                 log, null, transportResponseContext);
  59.     }
  60.    
  61.     private static boolean logError = true;
  62.     public static boolean isLogError() {
  63.         return logError;
  64.     }
  65.     public static void setLogError(boolean logError) {
  66.         InformazioniIntegrazione.logError = logError;
  67.     }
  68.     private void init(InformazioniIntegrazioneSorgente sourceType,  String sourceName, InformazioniIntegrazioneCodifica sourceEncodeType, boolean sourceRequired,
  69.             Logger log, TransportRequestContext transportRequestContext, TransportResponseContext transportResponseContext) throws Exception {
  70.        
  71.         this.log = log;
  72.        
  73.         this.sourceType = sourceType;
  74.         this.sourceName = sourceName;
  75.         this.sourceEncodeType = sourceEncodeType;
  76.         this.sourceRequired = sourceRequired;
  77.        
  78.         if(this.sourceType==null) {
  79.             throw new Exception("Source type undefined");
  80.         }
  81.         if(this.sourceName==null) {
  82.             throw new Exception("Source name undefined");
  83.         }
  84.         if(this.sourceEncodeType==null) {
  85.             throw new Exception("Source encode type undefined");
  86.         }
  87.         if(transportRequestContext==null && transportResponseContext==null) {
  88.             throw new Exception("TransportContext undefined");
  89.         }
  90.        
  91.         List<String> values = null;
  92.         String debugName = null;
  93.         switch (this.sourceType) {
  94.         case http_header:
  95.             if(transportRequestContext!=null) {
  96.                 values = transportRequestContext.getHeaderValues(this.sourceName);
  97.             }
  98.             else if(transportResponseContext!=null) {
  99.                 values = transportResponseContext.getHeaderValues(this.sourceName);
  100.             }
  101.             debugName = "http header "+this.sourceName;
  102.             break;
  103.         case query_parameter:
  104.             if(transportRequestContext!=null) {
  105.                 values = transportRequestContext.getParameterValues(this.sourceName);
  106.             }
  107.             else {
  108.                 throw new Exception("QueryParameter unsupported for transport response context");
  109.             }
  110.             debugName = "query parameter "+this.sourceName;
  111.             break;
  112.         }
  113.        
  114.         if(values==null || values.isEmpty()) {
  115.             if(this.sourceRequired) {
  116.                 throw new Exception("Required " +debugName+" not found");
  117.             }
  118.             else {
  119.                 this.claims = new HashMap<>();
  120.                 this.integrationInfo = new HashMap<>();
  121.                 return;
  122.             }
  123.         }
  124.        
  125.         if(values.size()>1) {
  126.             throw new Exception("Found more than one "+debugName+" ("+values.size()+")");
  127.         }
  128.        
  129.         this.raw = values.get(0);
  130.         if(this.raw==null || StringUtils.isEmpty(this.raw)) {
  131.             throw new Exception("Found empty " +debugName);
  132.         }
  133.        
  134.         switch (this.sourceEncodeType) {
  135.         case plain:
  136.             this.rawDecoded = this.raw.getBytes();
  137.             break;
  138.         case base64:
  139.             try {
  140.                 this.rawDecoded = Base64Utilities.decode(this.raw);
  141.             }catch(Throwable t) {
  142.                 throw new Exception("Base64 decode " +debugName+" failed: "+t.getMessage(),t);
  143.             }
  144.             break;
  145.         case hex:
  146.             try {
  147.                 this.rawDecoded = HexBinaryUtilities.decode(this.raw);
  148.             }catch(Throwable t) {
  149.                 throw new Exception("HEX decode " +debugName+" failed: "+t.getMessage(),t);
  150.             }
  151.             break;
  152.         case jwt:
  153.             try {
  154.                 String [] split = this.raw.split("\\.");
  155.                 if(split==null || split.length<2) {
  156.                     throw new Exception("uncorrect jwt format");
  157.                 }
  158.                 String payload = split[1];
  159.                 if(payload==null || StringUtils.isEmpty(payload)) {
  160.                     throw new Exception("Found empty jwt payload in " +debugName);
  161.                 }
  162.                 this.rawDecoded = Base64Utilities.decode(payload);
  163.             }catch(Throwable t) {
  164.                 throw new Exception("JWT decode " +debugName+" failed: "+t.getMessage(),t);
  165.             }
  166.             break;
  167.         }

  168.         //System.out.println("JSON ["+new String(this.rawDecoded)+"]");
  169.                
  170.         JSONUtils jsonUtils = JSONUtils.getInstance();
  171.        
  172.         JsonNode root = null;
  173.         if(logError) {
  174.             try {
  175.                 root = jsonUtils.getAsNode(this.rawDecoded);
  176.             }catch(Throwable t) {
  177.                 throw new Exception("Content in " +debugName+" isn't a json: "+t.getMessage(),t);
  178.             }
  179.         }
  180.         else {
  181.             if(jsonUtils.isJson(this.rawDecoded)) {
  182.                 root = jsonUtils.getAsNode(this.rawDecoded);
  183.             }
  184.         }
  185.        
  186.         if(root!=null) {
  187.             Map<String, Serializable> readClaims = jsonUtils.convertToSimpleMap(root);
  188.             if(readClaims!=null && readClaims.size()>0) {
  189.                 this.claims.putAll(readClaims);
  190.             }
  191.         }
  192.        
  193.         this.integrationInfo = jsonUtils.convertToMap(log, debugName, this.rawDecoded);
  194.        
  195.     }

  196.        
  197.     // NOTA: l'ordine stabilisce come viene serializzato nell'oggetto json
  198.    
  199.     private transient Logger log;
  200.    
  201.     // Informazioni
  202.     private Map<String, Serializable> integrationInfo;
  203.    
  204.     // Claims
  205.     private Map<String,Object> claims = new HashMap<>();
  206.    
  207.     // NOTA: l'ordine stabilisce come viene serializzato nell'oggetto json
  208.        
  209.     // Raw
  210.     private String raw;
  211.     private byte[] rawDecoded;
  212.    
  213.     // Nome dell'header HTTP dove sono stati reperite le informazioni
  214.     private InformazioniIntegrazioneSorgente sourceType;
  215.     private String sourceName;
  216.     private InformazioniIntegrazioneCodifica sourceEncodeType;
  217.     private boolean sourceRequired;
  218.    
  219.    
  220.     public Map<String, Serializable> getIntegrationInfo() {
  221.         return this.integrationInfo;
  222.     }
  223.     public Map<String, Serializable> getInfo() {
  224.         return this.integrationInfo;
  225.     }
  226.     public void setIntegrationInfo(Map<String, Serializable> integrationInfo) {
  227.         this.integrationInfo = integrationInfo;
  228.     }
  229.     public List<String> getInfoNames(){
  230.         return getIntegrationInfoNames();
  231.     }
  232.     public List<String> getIntegrationInfoNames(){
  233.         if(this.integrationInfo!=null && !this.integrationInfo.isEmpty()) {
  234.             List<String> attributesNames = new ArrayList<>();
  235.             for (String attrName : this.integrationInfo.keySet()) {
  236.                 attributesNames.add(attrName);
  237.             }
  238.             Collections.sort(attributesNames);
  239.             return attributesNames;
  240.         }
  241.         return null;
  242.     }
  243.    
  244.     public String getClaim(String name) throws Exception {
  245.         if(name==null) {
  246.             throw new Exception("Claim name is null");
  247.         }
  248.         String pattern ="$.."+name;
  249.         return JsonXmlPathExpressionEngine.extractAndConvertResultAsString(getRawDecodedAsString(), pattern, this.log);
  250.     }
  251.    
  252.     public Map<String, Object> getClaims() {
  253.         return this.claims;
  254.     }
  255.     public void setClaims(Map<String, Object> claims) {
  256.         this.claims = claims;
  257.     }
  258.            
  259.     public String getRaw() {
  260.         return this.raw;
  261.     }
  262.     public void setRaw(String raw) {
  263.         this.raw = raw;
  264.     }
  265.     public void setRawDecoded(byte[] rawDecoded) {
  266.         this.rawDecoded = rawDecoded;
  267.     }
  268.     public byte[] getRawDecoded() {
  269.         return this.rawDecoded;
  270.     }
  271.     public String getRawDecodedAsString() {
  272.         return new String(this.rawDecoded);
  273.     }
  274.    
  275.     public InformazioniIntegrazioneSorgente getSourceType() {
  276.         return this.sourceType;
  277.     }
  278.     public void setSourceType(InformazioniIntegrazioneSorgente sourceType) {
  279.         this.sourceType = sourceType;
  280.     }
  281.     public void setSourceName(String sourceName) {
  282.         this.sourceName = sourceName;
  283.     }
  284.     public void setSourceEncodeType(InformazioniIntegrazioneCodifica sourceEncodeType) {
  285.         this.sourceEncodeType = sourceEncodeType;
  286.     }
  287.     public void setSourceRequired(boolean sourceRequired) {
  288.         this.sourceRequired = sourceRequired;
  289.     }
  290.     public String getSourceName() {
  291.         return this.sourceName;
  292.     }
  293.     public InformazioniIntegrazioneCodifica getSourceEncodeType() {
  294.         return this.sourceEncodeType;
  295.     }
  296.     public boolean isSourceRequired() {
  297.         return this.sourceRequired;
  298.     }
  299.    
  300. }