ContentReader.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.ByteArrayOutputStream;

  22. import org.openspcoop2.message.OpenSPCoop2Message;
  23. import org.openspcoop2.message.constants.MessageRole;
  24. import org.openspcoop2.message.constants.MessageType;
  25. import org.openspcoop2.message.constants.ServiceBinding;
  26. import org.openspcoop2.message.rest.DumpRestMessageUtils;
  27. import org.openspcoop2.message.soap.DumpSoapMessageUtils;
  28. import org.openspcoop2.message.soap.TunnelSoapUtils;
  29. import org.openspcoop2.message.soap.reader.OpenSPCoop2MessageSoapStreamReader;
  30. import org.openspcoop2.message.utils.DumpMessaggio;
  31. import org.openspcoop2.message.utils.DumpMessaggioConfig;
  32. import org.openspcoop2.pdd.core.GestoreCorrelazioneApplicativa;
  33. import org.openspcoop2.protocol.sdk.Context;
  34. import org.openspcoop2.utils.digest.DigestEncoding;
  35. import org.slf4j.Logger;

  36. /**
  37.  * ContentReader
  38.  *
  39.  * @author Andrea Poli (apoli@link.it)
  40.  * @author $Author$
  41.  * @version $Rev$, $Date$
  42.  */
  43. public class ContentReader {

  44.     protected static final String FUNZIONALITA_RICHIEDE_SOAP = "Funzionalità utilizzabile solamente con service binding soap";
  45.     protected static final String FUNZIONALITA_RICHIEDE_JSON = "Funzionalità richiede un messaggio JSON";
  46.     protected static final String FUNZIONALITA_RICHIEDE_XML = "Funzionalità richiede un messaggio XML";
  47.    
  48.     protected Logger log;
  49.    
  50.     protected OpenSPCoop2Message message;
  51.    
  52.     protected Context pddContext;

  53.     public ContentReader(OpenSPCoop2Message message, Context pddContext, Logger log) {
  54.         this.message = message;
  55.         this.pddContext = pddContext;
  56.         this.log = log;
  57.     }


  58.     public String getApplicationMessageId() {
  59.         boolean request = this.message==null ||
  60.                 this.message.getMessageRole()==null ||
  61.                 MessageRole.NONE.equals(this.message.getMessageRole()) ||
  62.                 MessageRole.REQUEST.equals(this.message.getMessageRole());
  63.         if(request) {
  64.             if(this.pddContext!=null && this.pddContext.containsKey(GestoreCorrelazioneApplicativa.CONTEXT_CORRELAZIONE_APPLICATIVA_RICHIESTA)) {
  65.                 return (String) this.pddContext.get(GestoreCorrelazioneApplicativa.CONTEXT_CORRELAZIONE_APPLICATIVA_RICHIESTA);
  66.             }
  67.         }
  68.         else {
  69.             if(this.pddContext!=null && this.pddContext.containsKey(GestoreCorrelazioneApplicativa.CONTEXT_CORRELAZIONE_APPLICATIVA_RISPOSTA)) {
  70.                 return (String) this.pddContext.get(GestoreCorrelazioneApplicativa.CONTEXT_CORRELAZIONE_APPLICATIVA_RISPOSTA);
  71.             }
  72.         }
  73.         return null;
  74.     }
  75.    
  76.    
  77.     public OpenSPCoop2Message getMessage() {
  78.         return this.message; // questo metodo consente di attuare trasformazione più avanzate agendo sull'oggetto
  79.     }

  80.    
  81.     public boolean isSoap() {
  82.         if(this.message==null) {
  83.             return false;
  84.         }
  85.         return ServiceBinding.SOAP.equals(this.message.getServiceBinding());
  86.     }
  87.     public boolean isSoap11() {
  88.         if(this.message==null || this.message.getMessageType()==null) {
  89.             return false;
  90.         }
  91.         return MessageType.SOAP_11.equals(this.message.getMessageType());
  92.     }
  93.     public boolean isSoap12() {
  94.         if(this.message==null || this.message.getMessageType()==null) {
  95.             return false;
  96.         }
  97.         return MessageType.SOAP_12.equals(this.message.getMessageType());
  98.     }
  99.    
  100.     public boolean isRest() {
  101.         if(this.message==null) {
  102.             return false;
  103.         }
  104.         return ServiceBinding.REST.equals(this.message.getServiceBinding());
  105.     }
  106.     public boolean isRestXml() {
  107.         if(this.message==null || this.message.getMessageType()==null) {
  108.             return false;
  109.         }
  110.         return MessageType.XML.equals(this.message.getMessageType());
  111.     }
  112.     public boolean isRestJson() {
  113.         if(this.message==null || this.message.getMessageType()==null) {
  114.             return false;
  115.         }
  116.         return MessageType.JSON.equals(this.message.getMessageType());
  117.     }
  118.     public boolean isRestMultipart() {
  119.         if(this.message==null || this.message.getMessageType()==null) {
  120.             return false;
  121.         }
  122.         return MessageType.MIME_MULTIPART.equals(this.message.getMessageType());
  123.     }
  124.     public boolean isRestBinary() {
  125.         if(this.message==null || this.message.getMessageType()==null) {
  126.             return false;
  127.         }
  128.         return MessageType.BINARY.equals(this.message.getMessageType());
  129.     }
  130.    
  131.    
  132.     public boolean isHasContent() throws DynamicException {
  133.         // per invocazioni dinamiche
  134.         return this.hasContent();
  135.     }
  136.     public boolean hasContent() throws DynamicException {
  137.         try {
  138.             if(this.message==null) {
  139.                 return false;
  140.             }
  141.             if(ServiceBinding.SOAP.equals(this.message.getServiceBinding())) {
  142.                 return this.message.castAsSoap().getSOAPPart()!=null && this.message.castAsSoap().getSOAPPart().getEnvelope()!=null;
  143.             }
  144.             else {
  145.                 return this.message.castAsRest().hasContent();
  146.             }
  147.         }catch(Exception t) {
  148.             throw new DynamicException(t.getMessage(),t);
  149.         }
  150.     }
  151.     public byte[] getContent() throws DynamicException {
  152.         byte [] content = null;
  153.         if(!this.hasContent()) {
  154.             content = null;
  155.         }
  156.         else {
  157.             try {
  158.                 if(ServiceBinding.SOAP.equals(this.message.getServiceBinding())) {
  159.                     ByteArrayOutputStream bout = new ByteArrayOutputStream();
  160.                     this.message.castAsSoap().writeTo(bout, false);
  161.                     bout.flush();
  162.                     bout.close();
  163.                     content = bout.toByteArray();
  164.                 }
  165.                 else {
  166.                     /** non viene preservato il charset, es il test org.openspcoop2.pdd.core.trasformazioni.Test fallisce: Test [zip-json] (charset: ISO-8859-1) ... nei caratteri strani
  167.                     return this.message.castAsRest().getContentAsByteArray(); */
  168.                     content = this.message.castAsRest().getContentAsString().getBytes();
  169.                 }
  170.             }catch(Exception t) {
  171.                 throw new DynamicException(t.getMessage(),t);
  172.             }
  173.         }
  174.         return content;
  175.     }
  176.     public String getContentAsString() throws DynamicException {
  177.         if(!this.hasContent()) {
  178.             return null;
  179.         }
  180.         try {
  181.             if(ServiceBinding.SOAP.equals(this.message.getServiceBinding())) {
  182.                 ByteArrayOutputStream bout = new ByteArrayOutputStream();
  183.                 this.message.castAsSoap().writeTo(bout, false);
  184.                 bout.flush();
  185.                 bout.close();
  186.                 return bout.toString();
  187.             }
  188.             else {
  189.                 return this.message.castAsRest().getContentAsString();
  190.             }
  191.         }catch(Exception t) {
  192.             throw new DynamicException(t.getMessage(),t);
  193.         }
  194.     }

  195.     public String getContentBase64Digest(String algorithm) throws DynamicException{
  196.         return getContentDigest(algorithm, DigestEncoding.BASE64, false);
  197.     }
  198.     public String getContentBase64Digest(String algorithm, String rfc3230) throws DynamicException{
  199.         // // per invocazioni dinamiche
  200.         boolean v = false;
  201.         try {
  202.             v = Boolean.valueOf(rfc3230);
  203.         }catch(Exception e) {
  204.             throw new DynamicException(this.buildUncorrectBooleaValueMessage(rfc3230, e),e);
  205.         }
  206.         return getContentBase64Digest(algorithm, v);
  207.     }
  208.     public String getContentBase64Digest(String algorithm, boolean rfc3230) throws DynamicException{
  209.         return getContentDigest(algorithm, DigestEncoding.BASE64, rfc3230);
  210.     }
  211.    
  212.     public String getContentHexDigest(String algorithm) throws DynamicException{
  213.         return getContentDigest(algorithm, DigestEncoding.HEX, false);
  214.     }
  215.     public String getContentHexDigest(String algorithm, String rfc3230) throws DynamicException{
  216.         // // per invocazioni dinamiche
  217.         boolean v = false;
  218.         try {
  219.             v = Boolean.valueOf(rfc3230);
  220.         }catch(Exception e) {
  221.             throw new DynamicException(this.buildUncorrectBooleaValueMessage(rfc3230, e),e);
  222.         }
  223.         return getContentHexDigest(algorithm, v);
  224.     }
  225.     public String getContentHexDigest(String algorithm, boolean rfc3230) throws DynamicException{
  226.         return getContentDigest(algorithm, DigestEncoding.HEX, rfc3230);
  227.     }
  228.    
  229.     public String getContentDigest(String algorithm, String digestEncodingParam) throws DynamicException{
  230.         return getContentDigest(algorithm, digestEncodingParam, false);
  231.     }
  232.     public String getContentDigest(String algorithm, DigestEncoding digestEncoding) throws DynamicException{
  233.         return getContentDigest(algorithm, digestEncoding, false);
  234.     }
  235.     public String getContentDigest(String algorithm, String digestEncodingParam, String rfc3230) throws DynamicException{
  236.         // // per invocazioni dinamiche
  237.         boolean v = false;
  238.         try {
  239.             v = Boolean.valueOf(rfc3230);
  240.         }catch(Exception e) {
  241.             throw new DynamicException(this.buildUncorrectBooleaValueMessage(rfc3230, e),e);
  242.         }
  243.         return getContentDigest(algorithm, digestEncodingParam, v);
  244.     }
  245.     public String getContentDigest(String algorithm, String digestEncodingParam,
  246.             boolean rfc3230 // aggiunge prefisso algoritmo=
  247.             ) throws DynamicException{
  248.         DigestEncoding digestEncoding = null;
  249.         try {
  250.             digestEncoding = DigestEncoding.valueOf(digestEncodingParam);
  251.         }catch(Exception t) {
  252.             throw new DynamicException("DigestEncoding '"+digestEncodingParam+"' unsupported");
  253.         }
  254.         return getContentDigest(algorithm, digestEncoding, rfc3230);
  255.     }
  256.     public String getContentDigest(String algorithm, DigestEncoding digestEncoding, String rfc3230) throws DynamicException{
  257.         // // per invocazioni dinamiche
  258.         boolean v = false;
  259.         try {
  260.             v = Boolean.valueOf(rfc3230);
  261.         }catch(Exception e) {
  262.             throw new DynamicException(this.buildUncorrectBooleaValueMessage(rfc3230, e),e);
  263.         }
  264.         return getContentDigest(algorithm, digestEncoding, v);
  265.     }
  266.     public String getContentDigest(String algorithm, DigestEncoding digestEncoding,
  267.             boolean rfc3230 // aggiunge prefisso algoritmo=
  268.             ) throws DynamicException{
  269.         byte[] content = getContent();
  270.         if(content==null) {
  271.             throw new DynamicException("Content null");
  272.         }
  273.         try {
  274.             return org.openspcoop2.utils.digest.DigestUtils.getDigestValue(content, algorithm, digestEncoding, rfc3230);
  275.         }catch(Exception e) {
  276.             throw new DynamicException(e.getMessage(),e);
  277.         }
  278.     }
  279.    
  280.     public byte[] getContentSoapBody() throws DynamicException {
  281.         byte [] content = null;
  282.         if(!this.hasContent()) {
  283.             content = null;
  284.         }
  285.         else {
  286.             try {
  287.                 if(ServiceBinding.SOAP.equals(this.message.getServiceBinding())) {
  288.                     if(this.message.castAsSoap().getSOAPPart()==null || this.message.castAsSoap().getSOAPPart().getEnvelope()==null) {
  289.                         throw new DynamicException("Messaggio senza una busta SOAP");
  290.                     }
  291.                     content = TunnelSoapUtils.sbustamentoSOAPEnvelope(this.message.getFactory(), this.message.castAsSoap().getSOAPPart().getEnvelope(), false);
  292.                 }
  293.                 else {
  294.                     throw new DynamicException(FUNZIONALITA_RICHIEDE_SOAP);
  295.                 }
  296.             }catch(Exception t) {
  297.                 throw new DynamicException(t.getMessage(),t);
  298.             }
  299.         }
  300.         return content;
  301.     }
  302.    
  303.     public String getContentSoapBodyBase64Digest(String algorithm) throws DynamicException{
  304.         return getContentSoapBodyDigest(algorithm, DigestEncoding.BASE64, false);
  305.     }
  306.     public String getContentSoapBodyBase64Digest(String algorithm, String rfc3230) throws DynamicException{
  307.         // // per invocazioni dinamiche
  308.         boolean v = false;
  309.         try {
  310.             v = Boolean.valueOf(rfc3230);
  311.         }catch(Exception e) {
  312.             throw new DynamicException(this.buildUncorrectBooleaValueMessage(rfc3230, e),e);
  313.         }
  314.         return getContentSoapBodyBase64Digest(algorithm, v);
  315.     }
  316.     public String getContentSoapBodyBase64Digest(String algorithm, boolean rfc3230) throws DynamicException{
  317.         return getContentSoapBodyDigest(algorithm, DigestEncoding.BASE64, rfc3230);
  318.     }
  319.    
  320.     public String getContentSoapBodyHexDigest(String algorithm) throws DynamicException{
  321.         return getContentSoapBodyDigest(algorithm, DigestEncoding.HEX, false);
  322.     }
  323.     public String getContentSoapBodyHexDigest(String algorithm, String rfc3230) throws DynamicException{
  324.         // // per invocazioni dinamiche
  325.         boolean v = false;
  326.         try {
  327.             v = Boolean.valueOf(rfc3230);
  328.         }catch(Exception e) {
  329.             throw new DynamicException(this.buildUncorrectBooleaValueMessage(rfc3230, e),e);
  330.         }
  331.         return getContentSoapBodyHexDigest(algorithm, v);
  332.     }
  333.     public String getContentSoapBodyHexDigest(String algorithm, boolean rfc3230) throws DynamicException{
  334.         return getContentSoapBodyDigest(algorithm, DigestEncoding.HEX, rfc3230);
  335.     }
  336.    
  337.     public String getContentSoapBodyDigest(String algorithm, String digestEncodingParam) throws DynamicException{
  338.         return getContentSoapBodyDigest(algorithm, digestEncodingParam, false);
  339.     }
  340.     public String getContentSoapBodyDigest(String algorithm, DigestEncoding digestEncoding) throws DynamicException{
  341.         return getContentSoapBodyDigest(algorithm, digestEncoding, false);
  342.     }
  343.     public String getContentSoapBodyDigest(String algorithm, String digestEncodingParam, String rfc3230) throws DynamicException{
  344.         // // per invocazioni dinamiche
  345.         boolean v = false;
  346.         try {
  347.             v = Boolean.valueOf(rfc3230);
  348.         }catch(Exception e) {
  349.             throw new DynamicException(this.buildUncorrectBooleaValueMessage(rfc3230, e),e);
  350.         }
  351.         return getContentSoapBodyDigest(algorithm, digestEncodingParam, v);
  352.     }
  353.     public String getContentSoapBodyDigest(String algorithm, String digestEncodingParam,
  354.             boolean rfc3230 // aggiunge prefisso algoritmo=
  355.             ) throws DynamicException{
  356.         DigestEncoding digestEncoding = null;
  357.         try {
  358.             digestEncoding = DigestEncoding.valueOf(digestEncodingParam);
  359.         }catch(Exception t) {
  360.             throw new DynamicException("DigestEncoding '"+digestEncodingParam+"' unsupported");
  361.         }
  362.         return getContentSoapBodyDigest(algorithm, digestEncoding, rfc3230);
  363.     }
  364.     public String getContentSoapBodyDigest(String algorithm, DigestEncoding digestEncoding, String rfc3230) throws DynamicException{
  365.         // // per invocazioni dinamiche
  366.         boolean v = false;
  367.         try {
  368.             v = Boolean.valueOf(rfc3230);
  369.         }catch(Exception e) {
  370.             throw new DynamicException(this.buildUncorrectBooleaValueMessage(rfc3230, e),e);
  371.         }
  372.         return getContentSoapBodyDigest(algorithm, digestEncoding, v);
  373.     }
  374.     public String getContentSoapBodyDigest(String algorithm, DigestEncoding digestEncoding,
  375.             boolean rfc3230 // aggiunge prefisso algoritmo=
  376.             ) throws DynamicException{
  377.         byte[] content = getContentSoapBody();
  378.         if(content==null) {
  379.             throw new DynamicException("Content null");
  380.         }
  381.         try {
  382.             return org.openspcoop2.utils.digest.DigestUtils.getDigestValue(content, algorithm, digestEncoding, rfc3230);
  383.         }catch(Exception e) {
  384.             throw new DynamicException(e.getMessage(),e);
  385.         }
  386.     }
  387.    
  388.     private String buildUncorrectBooleaValueMessage(String rfc3230, Exception e) {
  389.         return "Uncorrect boolean value '"+rfc3230+"': "+e.getMessage();
  390.     }
  391.    
  392.    
  393.    
  394.     public boolean isSoapFault() throws DynamicException {
  395.         try {
  396.             if(ServiceBinding.SOAP.equals(this.message.getServiceBinding())) {
  397.                 return this.message.castAsSoap().hasSOAPFault();
  398.             }
  399.             else {
  400.                 throw new DynamicException(FUNZIONALITA_RICHIEDE_SOAP);
  401.             }
  402.         }catch(Exception t) {
  403.             throw new DynamicException(t.getMessage(),t);
  404.         }
  405.     }
  406.     public boolean isSoapBodyEmpty() throws DynamicException {
  407.         try {
  408.             if(ServiceBinding.SOAP.equals(this.message.getServiceBinding())) {
  409.                 return this.message.castAsSoap().isSOAPBodyEmpty();
  410.             }
  411.             else {
  412.                 throw new DynamicException(FUNZIONALITA_RICHIEDE_SOAP);
  413.             }
  414.         }catch(Exception t) {
  415.             throw new DynamicException(t.getMessage(),t);
  416.         }
  417.     }
  418.     public boolean isSoapWithAttachments() throws DynamicException {
  419.         try {
  420.             if(ServiceBinding.SOAP.equals(this.message.getServiceBinding())) {
  421.                 return this.message.castAsSoap().hasAttachments();
  422.             }
  423.             else {
  424.                 throw new DynamicException(FUNZIONALITA_RICHIEDE_SOAP);
  425.             }
  426.         }catch(Exception t) {
  427.             throw new DynamicException(t.getMessage(),t);
  428.         }
  429.     }
  430.     public OpenSPCoop2MessageSoapStreamReader getSoapReader() throws DynamicException {
  431.         try {
  432.             if(ServiceBinding.SOAP.equals(this.message.getServiceBinding())) {
  433.                 return this.message.castAsSoap().getSoapReader();
  434.             }
  435.             else {
  436.                 throw new DynamicException(FUNZIONALITA_RICHIEDE_SOAP);
  437.             }
  438.         }catch(Exception t) {
  439.             throw new DynamicException(t.getMessage(),t);
  440.         }
  441.     }
  442.    
  443.    
  444.     public DumpMessaggio getPart() throws DynamicException {
  445.         // per invocazioni dinamiche
  446.         return this.dumpMessage();
  447.     }
  448.     public DumpMessaggio getDumpMessage() throws DynamicException {
  449.         // per invocazioni dinamiche
  450.         return this.dumpMessage();
  451.     }
  452.     public DumpMessaggio dumpMessage() throws DynamicException {
  453.         if(this.dumpMessaggioInit==null) {
  454.             this.initDump();
  455.         }
  456.         return this.dumpMessaggio;
  457.     }
  458.    
  459.    
  460.     private DumpMessaggio dumpMessaggio = null;
  461.     private Boolean dumpMessaggioInit = null;
  462.     private synchronized void initDump() throws DynamicException {
  463.         if(this.dumpMessaggioInit==null) {
  464.            
  465.             try{
  466.                 if(this.hasContent()) {
  467.    
  468.                     DumpMessaggioConfig config = new DumpMessaggioConfig();
  469.                     config.setDumpAttachments(true);
  470.                     config.setDumpBody(true);
  471.                     config.setDumpHeaders(false);
  472.                     config.setDumpMultipartHeaders(true);
  473.                     if(ServiceBinding.SOAP.equals(this.message.getServiceBinding())) {
  474.                         this.dumpMessaggio = DumpSoapMessageUtils.dumpMessage(this.message.castAsSoap(), config, true);
  475.                     }
  476.                     else {
  477.                         this.dumpMessaggio = DumpRestMessageUtils.dumpMessage(this.message.castAsRest(), config, true);
  478.                     }
  479.                 }
  480.             }catch(Exception t) {
  481.                 throw new DynamicException(t.getMessage(),t);
  482.             }
  483.            
  484.             this.dumpMessaggioInit = true;
  485.         }
  486.     }

  487.    
  488. }