IntegrationManagerUtility.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.skeleton;

  21. import org.openspcoop2.core.id.IDSoggetto;
  22. import org.openspcoop2.message.OpenSPCoop2SoapMessage;
  23. import org.openspcoop2.protocol.engine.ProtocolFactoryManager;
  24. import org.openspcoop2.protocol.manifest.Openspcoop2;
  25. import org.openspcoop2.protocol.sdk.AbstractEccezioneBuilderParameter;
  26. import org.openspcoop2.protocol.sdk.EccezioneIntegrazioneBuilderParameters;
  27. import org.openspcoop2.protocol.sdk.EccezioneProtocolloBuilderParameters;
  28. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  29. import org.openspcoop2.protocol.sdk.ProtocolException;
  30. import org.openspcoop2.protocol.sdk.constants.IntegrationFunctionError;
  31. import org.openspcoop2.protocol.utils.ErroriProperties;
  32. import org.w3c.dom.Node;

  33. /**
  34.  * IntegrationManagerUtility
  35.  *
  36.  * @author apoli@link.it
  37.  * @author $Author$
  38.  * @version $Rev$, $Date$
  39.  */
  40. public class IntegrationManagerUtility {

  41.     public static void readAndSetProtocol(javax.servlet.http.HttpServletRequest req,String pathinfo) throws IntegrationManagerException{
  42.        
  43.         //System.out.println("PATH INFO["+pathinfo+"]");
  44.         String protocol = (String) req.getAttribute(org.openspcoop2.core.constants.Costanti.PROTOCOL_NAME.getValue());
  45.         //System.out.println("PROTOCOLLO ["+protocol+"]");
  46.         if(protocol!=null){
  47.            
  48.             //System.out.println("INIZIALIZZO IM con INPUT PROTOCOL ["+protocol+"]");
  49.             // verifico che sia un protocollo
  50.             try{
  51.                 ProtocolFactoryManager.getInstance().getProtocolFactoryByName(protocol);
  52.             } catch(ProtocolException e) {
  53.                
  54.                 // vedo se quello che mi è errivato è un contesto altrimeni rilancio la prima eccezione
  55.                 boolean ok = false;
  56.                 try{
  57.                     Openspcoop2 manifest = ProtocolFactoryManager.getInstance().getProtocolManifest(protocol);
  58.                     req.setAttribute(org.openspcoop2.core.constants.Costanti.PROTOCOL_NAME.getValue(), manifest.getProtocol().getName());
  59.                     ok = true;
  60.                 } catch(ProtocolException eInternal) {}
  61.                
  62.                 if(ok==false){
  63.                     throw new RuntimeException(e);
  64.                 }
  65.             }
  66.            
  67.         }
  68.         else  {
  69.        
  70.             // rimuovo il nome del contesto, rimane protocol/IntegrationManager
  71.             pathinfo = pathinfo.substring(pathinfo.indexOf("/", 1) + 1);
  72.             // prendo la prima parola: protocol
  73.             String protocolName = pathinfo;
  74.             if(pathinfo.indexOf("/") != -1){
  75.                 try{
  76.                     String servletPath = pathinfo.substring(0, pathinfo.indexOf("/"));
  77.                     if(servletPath.equals("PA") || servletPath.equals("PD") || servletPath.equals("PDtoSOAP") || servletPath.equals("IntegrationManager")) {
  78.                         servletPath = "@EMPTY@";//CostantiPdD.OPENSPCOOP_PROTOCOL_EMPTY_CONTEXT;
  79.                     }
  80.                     //System.out.println("INIZIALIZZO IM con SERVLET PATH ["+servletPath+"]");
  81.                     Openspcoop2 manifest = ProtocolFactoryManager.getInstance().getProtocolManifest(servletPath);
  82.                     protocolName = manifest.getProtocol().getName();
  83.                 } catch(ProtocolException e) {
  84.                     throw new RuntimeException(e);
  85.                 }
  86.             }
  87.            
  88.             req.setAttribute(org.openspcoop2.core.constants.Costanti.PROTOCOL_NAME.getValue(), protocolName);
  89.            
  90.         }
  91.        
  92.     }
  93.    
  94.    
  95.     /**
  96.      * Mappa una risposta di errore applicativo XML in una eccezione IntegrationManagerException
  97.      *
  98.      * @param xml XML su cui effettuare il mapping
  99.      * @return la protocol exception
  100.      *
  101.      */
  102.     public static IntegrationManagerException mapXMLIntoProtocolException(IProtocolFactory<?> protocolFactory,String xml,String prefixCodiceErroreApplicativoIntegrazione,
  103.             IntegrationFunctionError functionError, ErroriProperties erroriProperties) throws Exception{
  104.         org.openspcoop2.message.xml.MessageXMLUtils xmlUtils = org.openspcoop2.message.xml.MessageXMLUtils.DEFAULT;
  105.         org.w3c.dom.Document document = xmlUtils.newDocument(xml.getBytes());
  106.         return IntegrationManagerUtility.mapXMLIntoProtocolException(protocolFactory,document.getFirstChild(),prefixCodiceErroreApplicativoIntegrazione,
  107.                 functionError, erroriProperties);
  108.     }
  109.    
  110.     public static IntegrationManagerException mapXMLIntoProtocolException(IProtocolFactory<?> protocolFactory,Node xml,String prefixCodiceErroreApplicativoIntegrazione,
  111.             IntegrationFunctionError functionError, ErroriProperties erroriProperties) throws Exception{
  112.        
  113.         AbstractEccezioneBuilderParameter eccezione =
  114.                 protocolFactory.createErroreApplicativoBuilder().readErroreApplicativo(xml, prefixCodiceErroreApplicativoIntegrazione);
  115.         IntegrationManagerException exc = null;
  116.         if(eccezione instanceof EccezioneProtocolloBuilderParameters){
  117.             EccezioneProtocolloBuilderParameters eccBusta = (EccezioneProtocolloBuilderParameters) eccezione;
  118.             exc = new IntegrationManagerException(protocolFactory, eccBusta.getEccezioneProtocollo(),
  119.                     functionError, erroriProperties);
  120.         }
  121.         else{
  122.             EccezioneIntegrazioneBuilderParameters eccIntegrazione = (EccezioneIntegrazioneBuilderParameters) eccezione;
  123.             exc = new IntegrationManagerException(protocolFactory, eccIntegrazione.getErroreIntegrazione(),
  124.                     functionError, erroriProperties);
  125.         }
  126.        
  127.         exc.setOraRegistrazione(protocolFactory.createTraduttore().getDate_protocolFormat(eccezione.getOraRegistrazione()));
  128.         exc.setIdentificativoFunzione(eccezione.getIdFunzione());
  129.         exc.setIdentificativoPorta(eccezione.getDominioPorta().getCodicePorta());

  130.         return exc;

  131.     }
  132.    
  133.    
  134.     public static IntegrationManagerException mapMessageIntoProtocolException(OpenSPCoop2SoapMessage message, String faultCode, String faultString, IDSoggetto identitaPdD, String identificativoFunzione) throws Exception {
  135.    
  136.         Object govwayPrefixCodeInContextProperty = message.getContextProperty(org.openspcoop2.message.constants.Costanti.ERRORE_GOVWAY_PREFIX_CODE);
  137.         Object govwayCodeInContextProperty = message.getContextProperty(org.openspcoop2.message.constants.Costanti.ERRORE_GOVWAY_CODE);
  138.         if(govwayPrefixCodeInContextProperty!=null && govwayCodeInContextProperty!=null){
  139.             return new IntegrationManagerException(message, faultCode, faultString, identitaPdD, identificativoFunzione);
  140.         }
  141.        
  142.         return null;

  143.     }
  144. }