DirectVMConnectorInMessage.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.File;
  22. import java.util.Date;
  23. import java.util.HashMap;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import java.util.Map;

  27. import org.openspcoop2.core.constants.Costanti;
  28. import org.openspcoop2.core.transazioni.constants.TipoMessaggio;
  29. import org.openspcoop2.message.OpenSPCoop2Message;
  30. import org.openspcoop2.message.OpenSPCoop2MessageParseResult;
  31. import org.openspcoop2.message.config.ServiceBindingConfiguration;
  32. import org.openspcoop2.message.constants.MessageType;
  33. import org.openspcoop2.message.constants.ServiceBinding;
  34. import org.openspcoop2.message.soap.reader.OpenSPCoop2MessageSoapStreamReader;
  35. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  36. import org.openspcoop2.pdd.core.CostantiPdD;
  37. import org.openspcoop2.pdd.core.PdDContext;
  38. import org.openspcoop2.pdd.core.controllo_traffico.SogliaDimensioneMessaggio;
  39. import org.openspcoop2.pdd.core.controllo_traffico.SogliaReadTimeout;
  40. import org.openspcoop2.pdd.core.credenziali.Credenziali;
  41. import org.openspcoop2.pdd.logger.MsgDiagnostico;
  42. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  43. import org.openspcoop2.pdd.services.DirectVMProtocolInfo;
  44. import org.openspcoop2.pdd.services.connector.ConnectorException;
  45. import org.openspcoop2.pdd.services.connector.ConnectorUtils;
  46. import org.openspcoop2.protocol.engine.URLProtocolContextImpl;
  47. import org.openspcoop2.protocol.sdk.Context;
  48. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  49. import org.openspcoop2.protocol.sdk.constants.IDService;
  50. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  51. import org.openspcoop2.protocol.sdk.state.URLProtocolContext;
  52. import org.openspcoop2.utils.LimitExceededIOException;
  53. import org.openspcoop2.utils.LoggerWrapperFactory;
  54. import org.openspcoop2.utils.date.DateManager;
  55. import org.openspcoop2.utils.io.DumpByteArrayOutputStream;
  56. import org.openspcoop2.utils.io.notifier.NotifierInputStreamParams;
  57. import org.openspcoop2.utils.transport.Credential;
  58. import org.openspcoop2.utils.transport.TransportUtils;
  59. import org.slf4j.Logger;

  60. /**
  61.  * DirectVMConnectorInMessage
  62.  *
  63.  * @author Andrea Poli (apoli@link.it)
  64.  * @author $Author$
  65.  * @version $Rev$, $Date$
  66.  */
  67. public class DirectVMConnectorInMessage implements ConnectorInMessage {

  68.     private OpenSPCoop2Message message;
  69.     private Logger log;
  70.     private String idModulo;
  71.     private IDService idModuloAsIDService;
  72.     private RequestInfo requestInfo;
  73.     private IProtocolFactory<?> protocolFactory;
  74.     private String function;
  75.     private String url;
  76.     private Credenziali credenziali;
  77.     private String functionParameters;
  78.     private DirectVMProtocolInfo directVMProtocolInfo;
  79.     private PdDContext pddContext;
  80.     private Date dataIngressoRichiesta;
  81.    
  82.     protected OpenSPCoop2Properties openspcoopProperties;
  83.    
  84.     private Context context;
  85.     private String idTransazione;
  86.     private int soglia;
  87.     private File repositoryFile;
  88.    
  89.     @SuppressWarnings("unused")
  90.     private SogliaReadTimeout requestReadTimeout;
  91.     @SuppressWarnings("unused")
  92.     private SogliaDimensioneMessaggio requestLimitSize;
  93.    
  94.     @SuppressWarnings("unused")
  95.     private boolean useDiagnosticInputStream;
  96.     @SuppressWarnings("unused")
  97.     private MsgDiagnostico msgDiagnostico;
  98.    
  99.     public DirectVMConnectorInMessage(OpenSPCoop2Message msg,IDService idModuloAsIDService, String idModulo,
  100.             Map<String, List<String>> trasporto,
  101.             Map<String, List<String>> formUrl,
  102.             IProtocolFactory<?> protocolFactory,
  103.             String function, String url,
  104.             Credenziali credenziali,
  105.             String functionParameters,
  106.             ServiceBindingConfiguration serviceBindingConfiguration,
  107.             DirectVMProtocolInfo directVMProtocolInfo,
  108.             PdDContext pddContext) throws ConnectorException{
  109.         try{
  110.             this.message = msg;
  111.            
  112.             this.openspcoopProperties = OpenSPCoop2Properties.getInstance();
  113.            
  114.             this.log = OpenSPCoop2Logger.getLoggerOpenSPCoopCore();
  115.             if(this.log==null)
  116.                 this.log = LoggerWrapperFactory.getLogger(DirectVMConnectorInMessage.class);
  117.            
  118.             this.idModuloAsIDService = idModuloAsIDService;
  119.             this.idModulo = idModulo;
  120.            
  121.             if(trasporto!=null && !trasporto.isEmpty()) {
  122.                 Iterator<String> keys = trasporto.keySet().iterator();
  123.                 while (keys.hasNext()) {
  124.                     String key = (String) keys.next();
  125.                     this.headers.put(key, trasporto.get(key));
  126.                 }
  127.             }
  128.            
  129.             if(formUrl!=null && !formUrl.isEmpty()) {
  130.                 Iterator<String> keys = formUrl.keySet().iterator();
  131.                 while (keys.hasNext()) {
  132.                     String key = (String) keys.next();
  133.                     this.parameters.put(key, formUrl.get(key));
  134.                 }
  135.             }
  136.            
  137.             this.protocolFactory = protocolFactory;
  138.            
  139.             this.url = url;
  140.             this.function = function;
  141.            
  142.             this.credenziali = credenziali;
  143.            
  144.             this.functionParameters = functionParameters;
  145.            
  146.             this.directVMProtocolInfo = directVMProtocolInfo;
  147.            
  148.             this.pddContext = pddContext;
  149.            
  150.             if(this.pddContext!=null){
  151.                 this.setAttribute(CostantiPdD.OPENSPCOOP2_PDD_CONTEXT_HEADER_HTTP.getValue(),this.pddContext);
  152.             }
  153.                        
  154.             URLProtocolContext urlProtocolContext = new URLProtocolContextImpl(this.log);
  155.            
  156.             Map<String, List<String>> pFormBased = new HashMap<>();
  157.             pFormBased.putAll(this.parameters);
  158.             urlProtocolContext.setParameters(pFormBased);
  159.            
  160.             Map<String, List<String>> pTrasporto = new HashMap<>();
  161.             pTrasporto.putAll(this.headers);
  162.             urlProtocolContext.setHeaders(pTrasporto);
  163.            
  164.             urlProtocolContext.setFunction(this.function);
  165.            
  166.             urlProtocolContext.setProtocol(this.protocolFactory.getProtocol(),
  167.                     this.protocolFactory.getManifest().getWeb().getContextList().get(0).getName());
  168.            
  169.             urlProtocolContext.setRequestURI(this.url);
  170.            
  171.             urlProtocolContext.setWebContext("/openspcoop2");
  172.            
  173.             urlProtocolContext.setFunctionParameters(this.functionParameters);
  174.            
  175.             this.requestInfo = ConnectorUtils.getRequestInfo(this.protocolFactory, urlProtocolContext);
  176.            
  177.             if(this.pddContext!=null){
  178.                 this.setAttribute(Costanti.REQUEST_INFO.getValue(),this.requestInfo);
  179.             }
  180.            
  181.             if(this.openspcoopProperties!=null) {
  182.                 if(IDService.PORTA_APPLICATIVA.equals(idModuloAsIDService) || IDService.PORTA_APPLICATIVA_NIO.equals(idModuloAsIDService)){
  183.                     this.useDiagnosticInputStream = this.openspcoopProperties.isConnettoriUseDiagnosticInputStream_ricezioneBuste();
  184.                 }
  185.                 else {
  186.                     this.useDiagnosticInputStream = this.openspcoopProperties.isConnettoriUseDiagnosticInputStream_ricezioneContenutiApplicativi();
  187.                 }
  188.             }
  189.            
  190.         }catch(Exception e){
  191.             throw new ConnectorException(e.getMessage(),e);
  192.         }
  193.     }

  194.     @Override
  195.     public void setThresholdContext(Context context,
  196.             int soglia, File repositoryFile) {
  197.         this.context = context;
  198.         if(this.context!=null) {
  199.             this.idTransazione = (String) this.context.getObject(org.openspcoop2.core.constants.Costanti.ID_TRANSAZIONE);
  200.         }
  201.         this.soglia = soglia;
  202.         this.repositoryFile = repositoryFile;
  203.     }
  204.    
  205.     @Override
  206.     public void setRequestReadTimeout(SogliaReadTimeout timeout) {
  207.         this.requestReadTimeout = timeout;
  208.     }
  209.     @Override
  210.     public void disableReadTimeout() {
  211.         // nop
  212.     }
  213.    
  214.     @Override
  215.     public void setRequestLimitedStream(SogliaDimensioneMessaggio requestLimitSize) {
  216.         this.requestLimitSize = requestLimitSize;
  217.     }
  218.     @Override
  219.     public void disableLimitedStream() {
  220.         // nop
  221.     }
  222.     @Override
  223.     public void checkContentLengthLimit() throws LimitExceededIOException {
  224.         // nop
  225.     }
  226.    
  227.     @Override
  228.     public void setDiagnosticProducer(Context context, MsgDiagnostico msgDiag) {
  229.         if(this.context==null) {
  230.             this.context = context;
  231.         }
  232.         this.msgDiagnostico = msgDiag;
  233.     }
  234.    
  235.     public DirectVMProtocolInfo getDirectVMProtocolInfo() {
  236.         return this.directVMProtocolInfo;
  237.     }
  238.    
  239.     @Override
  240.     public IDService getIdModuloAsIDService(){
  241.         return this.idModuloAsIDService;
  242.     }
  243.    
  244.     @Override
  245.     public String getIdModulo(){
  246.         return this.idModulo;
  247.     }

  248.     @Override
  249.     public void updateRequestInfo(RequestInfo requestInfo) throws ConnectorException{
  250.         this.requestInfo = requestInfo;
  251.         if(this.pddContext!=null){
  252.             this.setAttribute(Costanti.REQUEST_INFO.getValue(),this.requestInfo);
  253.         }
  254.     }
  255.     @Override
  256.     public RequestInfo getRequestInfo(){
  257.         return this.requestInfo;
  258.     }
  259.    
  260.     @Override
  261.     public MessageType getRequestMessageType() {
  262.         return this.message.getMessageType();
  263.     }
  264.    
  265.     private Map<String, Object> attributes = new HashMap<>();
  266.     public void setAttribute(String key, Object object) throws ConnectorException {
  267.         this.attributes.put(key, object);
  268.     }
  269.     @Override
  270.     public Object getAttribute(String key) throws ConnectorException {
  271.         return this.attributes.get(key);
  272.     }
  273.    
  274.    
  275.     private Map<String, List<String>> headers = new HashMap<>();
  276.     public void addHeader(String key, String value) throws ConnectorException {
  277.         TransportUtils.addHeader(this.headers, key, value);
  278.     }
  279.     @Override
  280.     public List<String> getHeaderValues(String key) throws ConnectorException{
  281.         return TransportUtils.getRawObject(this.headers, key);
  282.     }
  283.    
  284.    
  285.     private Map<String, List<String>> parameters = new HashMap<>();
  286.     public void setParameter(String key, String value) throws ConnectorException {
  287.         TransportUtils.addParameter(this.parameters, key, value);
  288.     }
  289.     @Override
  290.     public List<String> getParameterValues(String key) throws ConnectorException{
  291.         return TransportUtils.getRawObject(this.parameters, key);
  292.     }

  293.    
  294.     @Override
  295.     public IProtocolFactory<?> getProtocolFactory() throws ConnectorException{
  296.         return this.protocolFactory;
  297.     }
  298.    
  299.     @Override
  300.     public String getContentType() throws ConnectorException{
  301.         try{
  302.             return this.message.getContentType();
  303.         }catch(Exception e){
  304.             throw new ConnectorException(e.getMessage(),e);
  305.         }
  306.     }
  307.    
  308.     @Override
  309.     public String getSOAPAction() throws ConnectorException{
  310.         try{
  311.             return this.message.castAsSoap().getSoapAction();
  312.         }catch(Exception e){
  313.             throw new ConnectorException(e.getMessage(),e);
  314.         }
  315.     }
  316.    
  317.     @Override
  318.     public OpenSPCoop2MessageSoapStreamReader getSoapReader() throws ConnectorException{
  319.         if(this.message!=null && ServiceBinding.SOAP.equals(this.message.getServiceBinding())) {
  320.             try{
  321.                 return this.message.castAsSoap().getSoapReader();
  322.             }catch(Exception e){
  323.                 throw new ConnectorException(e.getMessage(),e);
  324.             }  
  325.         }
  326.         return null;
  327.     }
  328.    
  329.     @Override
  330.     public OpenSPCoop2MessageParseResult getRequest(NotifierInputStreamParams notifierInputStreamParams) throws ConnectorException{
  331.         try{
  332.             OpenSPCoop2MessageParseResult pr = new OpenSPCoop2MessageParseResult();
  333.             pr.setMessage(this.message);
  334.             this.dataIngressoRichiesta = DateManager.getDate();
  335.             return pr;
  336.         }catch(Exception e){
  337.             throw new ConnectorException(e.getMessage(),e);
  338.         }  
  339.     }

  340.     @Override
  341.     public DumpByteArrayOutputStream getRequest() throws ConnectorException{
  342.         return this.getRequest(true);
  343.     }
  344.     @Override
  345.     public DumpByteArrayOutputStream getRequest(boolean consume) throws ConnectorException{
  346.         try{
  347.             this.dataIngressoRichiesta = DateManager.getDate();
  348.            
  349.             DumpByteArrayOutputStream bout = new DumpByteArrayOutputStream(this.soglia, this.repositoryFile, this.idTransazione,
  350.                     TipoMessaggio.RICHIESTA_INGRESSO_DUMP_BINARIO.getValue());
  351.             this.message.writeTo(bout, consume);
  352.             bout.flush();
  353.             bout.close();
  354.             return bout;
  355.         }catch(Exception e){
  356.             throw new ConnectorException(e.getMessage(),e);
  357.         }
  358.     }
  359.    
  360.     @Override
  361.     public Date getDataIngressoRichiesta(){
  362.         return this.dataIngressoRichiesta;
  363.     }
  364.    
  365.     @Override
  366.     public URLProtocolContext getURLProtocolContext() throws ConnectorException{
  367.         try{
  368.             return this.requestInfo.getProtocolContext();
  369.         }catch(Exception e){
  370.             throw new ConnectorException(e.getMessage(),e);
  371.         }
  372.     }
  373.    
  374.     @Override
  375.     public Credential getCredential() throws ConnectorException{
  376.         return this.credenziali;
  377.     }
  378.    
  379.     @Override
  380.     public String getSource() throws ConnectorException{
  381.         return "DirectVM_"+this.function;
  382.     }
  383.    
  384.     @Override
  385.     public String getProtocol() throws ConnectorException{
  386.         return "DirectVM";
  387.     }
  388.    
  389.     @Override
  390.     public int getContentLength() throws ConnectorException{
  391.         return -1; // devo gestire il chunked
  392.     }
  393.    
  394.     @Override
  395.     public String getRemoteAddress() throws ConnectorException{
  396.         return "DirectVM";
  397.     }
  398.    
  399.     @Override
  400.     public void close() throws ConnectorException{
  401.         // nop
  402.     }
  403.    
  404. }