DumpRawConnectorInMessage.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.services.connector.messages;

  21. import java.io.ByteArrayOutputStream;
  22. import java.io.File;
  23. import java.io.PrintWriter;
  24. import java.util.Date;
  25. import java.util.List;

  26. import org.openspcoop2.core.transazioni.constants.TipoMessaggio;
  27. import org.openspcoop2.message.OpenSPCoop2MessageParseResult;
  28. import org.openspcoop2.message.constants.MessageRole;
  29. import org.openspcoop2.message.constants.MessageType;
  30. import org.openspcoop2.message.exception.ParseExceptionUtils;
  31. import org.openspcoop2.message.soap.reader.OpenSPCoop2MessageSoapStreamReader;
  32. import org.openspcoop2.pdd.core.controllo_traffico.SogliaDimensioneMessaggio;
  33. import org.openspcoop2.pdd.core.controllo_traffico.SogliaReadTimeout;
  34. import org.openspcoop2.pdd.logger.MsgDiagnostico;
  35. import org.openspcoop2.pdd.services.connector.ConnectorException;
  36. import org.openspcoop2.protocol.sdk.Context;
  37. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  38. import org.openspcoop2.protocol.sdk.constants.IDService;
  39. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  40. import org.openspcoop2.protocol.sdk.state.URLProtocolContext;
  41. import org.openspcoop2.utils.LimitExceededIOException;
  42. import org.openspcoop2.utils.io.DumpByteArrayOutputStream;
  43. import org.openspcoop2.utils.io.notifier.NotifierInputStreamParams;
  44. import org.openspcoop2.utils.transport.Credential;
  45. import org.slf4j.Logger;

  46. /**
  47.  * DumpRawConnectorInMessage
  48.  *
  49.  * @author Poli Andrea (apoli@link.it)
  50.  * @author $Author$
  51.  * @version $Rev$, $Date$
  52.  */
  53. public class DumpRawConnectorInMessage implements ConnectorInMessage {

  54.     private ConnectorInMessage connectorInMessage;
  55.     private Logger log;
  56.     private DumpByteArrayOutputStream bout = null;
  57.     private OpenSPCoop2MessageParseResult parseResult = null;
  58.     private String contentType;
  59.     private Integer contentLength;

  60.     private Context context;
  61.     private String idTransazione;
  62.     private int soglia;
  63.     private File repositoryFile;
  64.    
  65.     @SuppressWarnings("unused")
  66.     private MsgDiagnostico msgDiagnostico;
  67.    
  68.     public DumpRawConnectorInMessage(Logger log,ConnectorInMessage connectorInMessage, Context context,
  69.             int soglia, File repositoryFile){
  70.         this.log = log;
  71.         this.connectorInMessage = connectorInMessage;
  72.        
  73.         this.context = context;
  74.         if(this.context!=null) {
  75.             this.idTransazione = (String) this.context.getObject(org.openspcoop2.core.constants.Costanti.ID_TRANSAZIONE);
  76.         }
  77.         this.soglia = soglia;
  78.         this.repositoryFile = repositoryFile;
  79.     }

  80.     @Override
  81.     public void setThresholdContext(Context context,
  82.             int soglia, File repositoryFile) {
  83.         // nop
  84.     }
  85.    
  86.     @Override
  87.     public void setRequestReadTimeout(SogliaReadTimeout timeout) {
  88.         // nop
  89.     }
  90.     @Override
  91.     public void disableReadTimeout() {
  92.         // nop
  93.     }
  94.    
  95.     @Override
  96.     public void setRequestLimitedStream(SogliaDimensioneMessaggio requestLimitSize) {
  97.         // nop
  98.     }
  99.     @Override
  100.     public void disableLimitedStream() {
  101.         // nop
  102.     }
  103.     @Override
  104.     public void checkContentLengthLimit() throws LimitExceededIOException {
  105.         this.connectorInMessage.checkContentLengthLimit(); // devo controllarlo sulla chiamata http
  106.     }
  107.    
  108.     @Override
  109.     public void setDiagnosticProducer(Context context, MsgDiagnostico msgDiag) {
  110.         if(this.context==null) {
  111.             this.context = context;
  112.         }
  113.         this.msgDiagnostico = msgDiag;
  114.     }
  115.    
  116.     public ConnectorInMessage getWrappedConnectorInMessage() {
  117.         return this.connectorInMessage;
  118.     }
  119.    
  120.     public DumpByteArrayOutputStream getDumpByteArrayOutputStream() {
  121.         if(this.bout!=null && this.bout.size()>0){
  122.             return this.bout;
  123.         }
  124.         return null;
  125.     }
  126. //  public byte[] getRequestAsByte(){
  127. //      if(this.bout!=null && this.bout.size()>0){
  128. //          return this.bout.toByteArray();
  129. //      }
  130. //      return null;
  131. //  }
  132. //  public String getRequestAsString(){
  133. //      if(this.bout!=null && this.bout.size()>0){
  134. //          return this.bout.toString();
  135. //      }
  136. //      return null;
  137. //  }
  138.     public boolean isParsingRequestError(){
  139.         return this.parseResult!=null && this.parseResult.getParseException()!=null;
  140.     }
  141.     public String getParsingRequestErrorAsString(){
  142.         if(this.parseResult!=null && this.parseResult.getParseException()!=null){
  143.             try{
  144.                 ByteArrayOutputStream bout = new ByteArrayOutputStream();
  145.                 PrintWriter pw = new PrintWriter(bout);
  146.                 this.parseResult.getParseException().getSourceException().printStackTrace(pw);
  147.                 pw.flush();
  148.                 bout.flush();
  149.                 pw.close();
  150.                 bout.close();
  151.                 return bout.toString();
  152.             }catch(Exception e){
  153.                 return "ParsingRequestError, serializazione eccezione non riuscita: "+e.getMessage();
  154.             }
  155.         }
  156.         return null;
  157.     }
  158.    
  159.     @Override
  160.     public OpenSPCoop2MessageSoapStreamReader getSoapReader() throws ConnectorException{
  161.         // wrapped method
  162.         return this.connectorInMessage.getSoapReader();
  163.     }
  164.    
  165.     @Override
  166.     public OpenSPCoop2MessageParseResult getRequest(NotifierInputStreamParams notifierInputStreamParams) throws ConnectorException {
  167.        
  168.         if(this.parseResult!=null){
  169.             return this.parseResult;
  170.         }

  171.         if(this.connectorInMessage instanceof HttpServletConnectorInMessage){
  172.             HttpServletConnectorInMessage http = (HttpServletConnectorInMessage) this.connectorInMessage;
  173.             this.bout = new DumpByteArrayOutputStream(this.soglia, this.repositoryFile, this.idTransazione,
  174.                     TipoMessaggio.RICHIESTA_INGRESSO_DUMP_BINARIO.getValue());
  175.             try{
  176.                 this.parseResult = http.getRequest(this.bout,notifierInputStreamParams); // il bout viene chiuso nel metodo interno
  177.                 if(this.bout.size()<=0){
  178.                     this.bout = null;
  179.                 }
  180.             }finally{
  181.                 try{
  182.                     if(this.bout!=null){
  183.                         this.bout.flush();
  184.                     }
  185.                 }catch(Throwable close){}
  186.                 try{
  187.                     if(this.bout!=null){
  188.                         this.bout.close();
  189.                     }
  190.                 }catch(Throwable close){}
  191.             }
  192.         }
  193.         else{
  194.             this.parseResult = this.connectorInMessage.getRequest(notifierInputStreamParams);
  195.             if(this.parseResult.getMessage()!=null){
  196.                 try{
  197.                     this.bout = new DumpByteArrayOutputStream(this.soglia, this.repositoryFile, this.idTransazione,
  198.                             TipoMessaggio.RICHIESTA_INGRESSO_DUMP_BINARIO.getValue());
  199.                     this.parseResult.getMessage().writeTo(this.bout, false);
  200.                 }catch(Throwable t){
  201.                     this.bout = null;
  202.                     OpenSPCoop2MessageParseResult result = new OpenSPCoop2MessageParseResult();
  203.                     if(this.parseResult.getMessage().getParseException()!=null){
  204.                         result.setParseException(this.parseResult.getMessage().getParseException());
  205.                     }
  206.                     else{
  207.                         result.setParseException(ParseExceptionUtils.buildParseException(t,MessageRole.REQUEST));
  208.                     }
  209.                     return result;
  210.                 }finally{
  211.                     try{
  212.                         if(this.bout!=null){
  213.                             this.bout.flush();
  214.                         }
  215.                     }catch(Throwable close){}
  216.                     try{
  217.                         if(this.bout!=null){
  218.                             this.bout.close();
  219.                         }
  220.                     }catch(Throwable close){}
  221.                 }
  222.             }
  223.         }
  224.        
  225.         return this.parseResult;
  226.        
  227.     }

  228.     @Override
  229.     public DumpByteArrayOutputStream getRequest() throws ConnectorException {
  230.         return this.getRequest(true);
  231.     }
  232.    
  233.     @Override
  234.     public DumpByteArrayOutputStream getRequest(boolean consume) throws ConnectorException {
  235.         if(this.bout!=null){
  236.             return this.bout;
  237.         }
  238.        
  239.         try{
  240.             DumpByteArrayOutputStream tmp = this.connectorInMessage.getRequest(consume);
  241.             if(tmp!=null){
  242.                 this.bout = tmp;
  243.             }
  244.         }catch(Throwable t){
  245.             try{
  246.                 this.bout = DumpByteArrayOutputStream.newInstance(("getRequest error: "+t.getMessage()).getBytes());
  247.             }catch(Throwable tWrite){}
  248.             this.log.error("getRequest error: "+t.getMessage(),t);
  249.         }finally{
  250.             try{
  251.                 if(this.bout!=null){
  252.                     this.bout.flush();
  253.                 }
  254.             }catch(Throwable close){}
  255.             try{
  256.                 if(this.bout!=null){
  257.                     this.bout.close();
  258.                 }
  259.             }catch(Throwable close){}
  260.         }
  261.        
  262.         if(this.bout!=null){
  263.             return this.bout;
  264.         }
  265.         else{
  266.             return null;
  267.         }
  268.     }
  269.    
  270.     @Override
  271.     public Date getDataIngressoRichiesta(){
  272.         return this.connectorInMessage.getDataIngressoRichiesta();
  273.     }
  274.    
  275.     @Override
  276.     public String getContentType() throws ConnectorException {
  277.         if(this.contentType!=null){
  278.             return this.contentType;
  279.         }
  280.         this.contentType = this.connectorInMessage.getContentType();
  281.         return this.contentType;
  282.     }
  283.     @Override
  284.     public int getContentLength() throws ConnectorException {
  285.         if(this.contentLength!=null){
  286.             return this.contentLength;
  287.         }
  288.         this.contentLength = this.connectorInMessage.getContentLength();
  289.         return this.contentLength;
  290.     }
  291.    
  292.    
  293.    
  294.     // Wrapped Only
  295.    
  296.     @Override
  297.     public MessageType getRequestMessageType() {
  298.         // wrapped method
  299.         return this.connectorInMessage.getRequestMessageType();
  300.     }
  301.    
  302.     @Override
  303.     public IDService getIdModuloAsIDService() {
  304.         // wrapped method
  305.         return this.connectorInMessage.getIdModuloAsIDService();
  306.     }

  307.     @Override
  308.     public String getIdModulo() {
  309.         // wrapped method
  310.         return this.connectorInMessage.getIdModulo();
  311.     }

  312.     @Override
  313.     public void updateRequestInfo(RequestInfo requestInfo) throws ConnectorException{
  314.         // wrapped method
  315.         this.connectorInMessage.updateRequestInfo(requestInfo);
  316.     }
  317.    
  318.     @Override
  319.     public RequestInfo getRequestInfo(){
  320.         // wrapped method
  321.         return this.connectorInMessage.getRequestInfo();
  322.     }
  323.    
  324.     @Override
  325.     public Object getAttribute(String key) throws ConnectorException {
  326.         // wrapped method
  327.         return this.connectorInMessage.getAttribute(key);
  328.     }

  329.     @Override
  330.     public List<String> getHeaderValues(String key) throws ConnectorException {
  331.         // wrapped method
  332.         return this.connectorInMessage.getHeaderValues(key);
  333.     }

  334.     @Override
  335.     public List<String> getParameterValues(String key) throws ConnectorException {
  336.         // wrapped method
  337.         return this.connectorInMessage.getParameterValues(key);
  338.     }

  339.     @Override
  340.     public IProtocolFactory<?> getProtocolFactory() throws ConnectorException {
  341.         // wrapped method
  342.         return this.connectorInMessage.getProtocolFactory();
  343.     }

  344.     @Override
  345.     public String getSOAPAction()
  346.             throws ConnectorException {
  347.         // wrapped method
  348.         return this.connectorInMessage.getSOAPAction();
  349.     }

  350.     @Override
  351.     public URLProtocolContext getURLProtocolContext() throws ConnectorException {
  352.         // wrapped method
  353.         return this.connectorInMessage.getURLProtocolContext();
  354.     }
  355.    
  356.     @Override
  357.     public Credential getCredential() throws ConnectorException {
  358.         // wrapped method
  359.         return this.connectorInMessage.getCredential();
  360.     }

  361.     @Override
  362.     public String getSource()
  363.             throws ConnectorException {
  364.         // wrapped method
  365.         return this.connectorInMessage.getSource();
  366.     }

  367.     @Override
  368.     public String getProtocol() throws ConnectorException {
  369.         // wrapped method
  370.         return this.connectorInMessage.getProtocol();
  371.     }

  372.    
  373.     @Override
  374.     public String getRemoteAddress() throws ConnectorException{
  375.         // wrapped method
  376.         return this.connectorInMessage.getRemoteAddress();
  377.     }
  378.    
  379.     @Override
  380.     public void close() throws ConnectorException {
  381.         // wrapped method
  382.         this.connectorInMessage.close();
  383.     }

  384. }