ConnectorDispatcherUtils.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;

  21. import java.io.ByteArrayOutputStream;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.util.HashMap;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import java.util.Map;

  28. import javax.servlet.http.HttpServletRequest;
  29. import javax.servlet.http.HttpServletResponse;

  30. import org.openspcoop2.message.OpenSPCoop2Message;
  31. import org.openspcoop2.message.constants.ServiceBinding;
  32. import org.openspcoop2.message.exception.ParseException;
  33. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  34. import org.openspcoop2.pdd.core.CostantiPdD;
  35. import org.openspcoop2.pdd.core.integrazione.HeaderIntegrazione;
  36. import org.openspcoop2.pdd.core.integrazione.UtilitiesIntegrazione;
  37. import org.openspcoop2.pdd.services.connector.messages.ConnectorOutMessage;
  38. import org.openspcoop2.pdd.services.error.RicezioneBusteExternalErrorGenerator;
  39. import org.openspcoop2.pdd.services.error.RicezioneContenutiApplicativiInternalErrorGenerator;
  40. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  41. import org.openspcoop2.protocol.sdk.constants.ErroreIntegrazione;
  42. import org.openspcoop2.protocol.sdk.constants.ErroriIntegrazione;
  43. import org.openspcoop2.protocol.sdk.constants.IDService;
  44. import org.openspcoop2.protocol.sdk.constants.IntegrationFunctionError;
  45. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  46. import org.openspcoop2.utils.CopyStream;
  47. import org.openspcoop2.utils.transport.TransportUtils;
  48. import org.openspcoop2.utils.transport.http.HttpConstants;
  49. import org.openspcoop2.utils.transport.http.HttpRequestMethod;
  50. import org.slf4j.Logger;
  51. import org.w3c.dom.Document;

  52. /**
  53.  * RicezioneContenutiApplicativiIntegrationManagerService
  54.  *
  55.  * @author Poli Andrea (apoli@link.it)
  56.  * @author $Author$
  57.  * @version $Rev$, $Date$
  58.  */
  59. public class ConnectorDispatcherUtils {

  60.     public static final boolean CLIENT_ERROR = true;
  61.     public static final boolean GENERAL_ERROR = false;
  62.    
  63.     public static void doServiceBindingNotSupported(HttpServletRequest req, HttpServletResponse res, HttpRequestMethod httpMethod, ServiceBinding serviceBinding, IDService idService) throws IOException{
  64.        
  65.         OpenSPCoop2Properties op2Properties = OpenSPCoop2Properties.getInstance();
  66.        
  67.         // messaggio di errore
  68.         boolean errore404 = false;
  69.         if(op2Properties!=null){
  70.             if(IDService.PORTA_DELEGATA.equals(idService)){
  71.                 if(op2Properties.isGenerazioneErroreHttpMethodUnsupportedPortaDelegataEnabled()==false){
  72.                     errore404 = true;
  73.                 }
  74.             }
  75.             else if(IDService.PORTA_DELEGATA_XML_TO_SOAP.equals(idService)){
  76.                 if(op2Properties.isGenerazioneErroreHttpMethodUnsupportedPortaDelegataImbustamentoSOAPEnabled()==false){
  77.                     errore404 = true;
  78.                 }
  79.             }
  80.             else if(IDService.PORTA_APPLICATIVA.equals(idService)){
  81.                 if(op2Properties.isGenerazioneErroreHttpMethodUnsupportedPortaDelegataEnabled()==false){
  82.                     errore404 = true;
  83.                 }
  84.             }
  85.             else{
  86.                 throw new IOException("Service ["+idService+"] not supported");
  87.             }
  88.         }
  89.        
  90.         if(errore404){
  91.             res.sendError(404,ConnectorUtils.generateError404Message(ConnectorUtils.getFullCodeServiceBindingNotSupported(idService, serviceBinding)));
  92.         }
  93.         else{
  94.        
  95.             res.setStatus(500);
  96.            
  97.             ConnectorUtils.generateErrorMessage(idService,httpMethod,req,res, ConnectorUtils.getMessageServiceBindingNotSupported(serviceBinding), false, true);
  98.            
  99.             try{
  100.                 res.getOutputStream().flush();
  101.             }catch(Exception eClose){
  102.                 // ignore
  103.             }
  104.             try{
  105.                 res.getOutputStream().close();
  106.             }catch(Exception eClose){
  107.                 // ignore
  108.             }
  109.            
  110.         }
  111.     }
  112.    
  113.     public static void doMethodNotSupported(HttpServletRequest req, HttpServletResponse res, HttpRequestMethod method, IDService idService) throws IOException{
  114.        
  115.         OpenSPCoop2Properties op2Properties = OpenSPCoop2Properties.getInstance();
  116.        
  117.         // messaggio di errore
  118.         boolean errore404 = false;
  119.         if(op2Properties!=null){
  120.             if(IDService.PORTA_DELEGATA.equals(idService)){
  121.                 if(op2Properties.isGenerazioneErroreHttpMethodUnsupportedPortaDelegataEnabled()==false){
  122.                     errore404 = true;
  123.                 }
  124.             }
  125.             else if(IDService.PORTA_DELEGATA_XML_TO_SOAP.equals(idService)){
  126.                 if(op2Properties.isGenerazioneErroreHttpMethodUnsupportedPortaDelegataImbustamentoSOAPEnabled()==false){
  127.                     errore404 = true;
  128.                 }
  129.             }
  130.             else if(IDService.PORTA_APPLICATIVA.equals(idService)){
  131.                 if(op2Properties.isGenerazioneErroreHttpMethodUnsupportedPortaDelegataEnabled()==false){
  132.                     errore404 = true;
  133.                 }
  134.             }
  135.             else{
  136.                 throw new IOException("Service ["+idService+"] not supported");
  137.             }
  138.         }
  139.        
  140.         if(errore404){
  141.             res.sendError(404,ConnectorUtils.generateError404Message(ConnectorUtils.getFullCodeHttpMethodNotSupported(idService, method)));
  142.         }
  143.         else{
  144.        
  145.             res.setStatus(500);
  146.            
  147.             ConnectorUtils.generateErrorMessage(idService,method,req,res, ConnectorUtils.getMessageHttpMethodNotSupported(method), false, true);
  148.            
  149.             try{
  150.                 res.getOutputStream().flush();
  151.             }catch(Exception eClose){
  152.                 // ignore
  153.             }
  154.             try{
  155.                 res.getOutputStream().close();
  156.             }catch(Exception eClose){
  157.                 // ignore
  158.             }
  159.            
  160.         }
  161.     }
  162.    
  163.    
  164.     public static void doWsdl(HttpServletRequest req, HttpServletResponse res, HttpRequestMethod method, IDService idService ) throws IOException{
  165.        
  166.         OpenSPCoop2Properties op2Properties = OpenSPCoop2Properties.getInstance();
  167.         String versione = CostantiPdD.OPENSPCOOP2_PRODUCT_VERSION;
  168.         if(op2Properties!=null){
  169.             versione = op2Properties.getPddDetailsForServices();
  170.         }
  171.        
  172.        
  173.         InputStream is =null;
  174.         try{
  175.            
  176.             String wsdl = null;
  177.             boolean generazioneWsdlEnabled = false;
  178.             if(IDService.PORTA_DELEGATA.equals(idService)){
  179.                 wsdl = "/PD.wsdl";
  180.                 if(op2Properties!=null){
  181.                     generazioneWsdlEnabled = op2Properties.isGenerazioneWsdlPortaDelegataEnabled();
  182.                 }
  183.             }
  184.             else if(IDService.PORTA_APPLICATIVA.equals(idService)){
  185.                 wsdl = "/PA.wsdl";
  186.                 if(op2Properties!=null){
  187.                     generazioneWsdlEnabled = op2Properties.isGenerazioneWsdlPortaApplicativaEnabled();
  188.                 }
  189.             }
  190.             else{
  191.                 throw new Exception("Service ["+idService+"] not supported");
  192.             }
  193.            
  194.             is = RicezioneContenutiApplicativiConnector.class.getResourceAsStream(wsdl);
  195.             ByteArrayOutputStream bout = new ByteArrayOutputStream();

  196.             if(is!=null){
  197. //              int letti = 0;
  198. //              byte [] buffer = new byte[Utilities.DIMENSIONE_BUFFER];
  199. //              while( (letti=is.read(buffer)) != -1 ){
  200. //                  bout.write(buffer, 0, letti);
  201. //              }
  202.                 CopyStream.copy(is, bout);
  203.                 bout.flush();
  204.                 bout.close();
  205.             }
  206.            
  207.             if(generazioneWsdlEnabled){
  208.            
  209.                 if(bout.size()<=0){
  210.                     throw new Exception("WSDL Not Found");
  211.                 }
  212.                
  213.                 byte[] b = bout.toByteArray();
  214.                 org.openspcoop2.message.xml.MessageXMLUtils xmlUtils = org.openspcoop2.message.xml.MessageXMLUtils.DEFAULT;
  215.                 Document d = xmlUtils.newDocument(b);
  216.                 d.getFirstChild().appendChild(d.createComment(versione));
  217.                 xmlUtils.writeTo(d, res.getOutputStream());
  218.                
  219.             }
  220.             else{
  221.                
  222.                 res.sendError(404, ConnectorUtils.generateError404Message(ConnectorUtils.getFullCodeWsdlUnsupported(idService)));
  223.                
  224.             }
  225.            
  226.         }catch(Exception e){                    
  227.             res.setStatus(500);
  228.            
  229.             ConnectorUtils.generateErrorMessage(idService,method,req,res, "Generazione WSDL non riuscita", false, true);
  230.            
  231.             ConnectorUtils.getErrorLog().error("Generazione WSDL "+idService+" non riuscita",e);    
  232.         }finally{
  233.             try{
  234.                 res.getOutputStream().flush();
  235.             }catch(Exception eClose){
  236.                 // ignore
  237.             }
  238.             try{
  239.                 res.getOutputStream().close();
  240.             }catch(Exception eClose){
  241.                 // ignore
  242.             }
  243.             try{
  244.                 if(is!=null)
  245.                     is.close();
  246.             }catch(Exception eClose){
  247.                 // ignore
  248.             }
  249.         }
  250.         return;
  251.     }
  252.    
  253.     public static void doError(RequestInfo requestInfo,
  254.             RicezioneContenutiApplicativiInternalErrorGenerator generatoreErrore, ErroreIntegrazione erroreIntegrazione,
  255.             IntegrationFunctionError integrationFunctionError, Throwable e, HttpServletResponse res, Logger log){
  256.        
  257.         IProtocolFactory<?> protocolFactory = requestInfo.getProtocolFactory();
  258.                
  259.         if(generatoreErrore!=null){
  260.             OpenSPCoop2Message msgErrore = generatoreErrore.build(null, integrationFunctionError, erroreIntegrazione, e, null);
  261.             if(msgErrore.getForcedResponseCode()!=null){
  262.                 res.setStatus(Integer.parseInt(msgErrore.getForcedResponseCode()));
  263.             }
  264.             try{
  265.                 msgErrore.writeTo(res.getOutputStream(), true);
  266.             }catch(Exception eWriteTo){
  267.                 log.error("Errore durante la serializzazione dell'errore: "+e.getMessage(),e);
  268.                 try{
  269.                     res.sendError(500);
  270.                 }catch(Exception eInternal){
  271.                     throw new RuntimeException(eInternal.getMessage(),eInternal);
  272.                 }
  273.             }
  274.         }
  275.         else{
  276.             try{
  277.                 res.sendError(500,erroreIntegrazione.getDescrizione(protocolFactory));
  278.             }catch(Exception eInternal){
  279.                 throw new RuntimeException(eInternal.getMessage(),eInternal);
  280.             }
  281.         }
  282.        
  283.     }
  284.    
  285.     public static ConnectorDispatcherErrorInfo doError(RequestInfo requestInfo,
  286.             RicezioneContenutiApplicativiInternalErrorGenerator generatoreErrore, ErroreIntegrazione erroreIntegrazione,
  287.             IntegrationFunctionError integrationFunctionError, Throwable e, ParseException parseException,
  288.             ConnectorOutMessage res, Logger log, boolean clientError) throws ConnectorException{
  289.        
  290.         IProtocolFactory<?> protocolFactory = requestInfo.getProtocolFactory();
  291.        
  292.         if(generatoreErrore!=null){
  293.            
  294.             OpenSPCoop2Message msgErrore = generatoreErrore.build(null, integrationFunctionError, erroreIntegrazione, e, parseException);
  295.            
  296.             return _doError(requestInfo, res, log, true, erroreIntegrazione, integrationFunctionError, e, parseException, msgErrore, clientError);
  297.            
  298.         }
  299.         else{
  300.             try{
  301.                 String errore = erroreIntegrazione.getDescrizione(protocolFactory);
  302.                 throw new ConnectorException(errore);
  303.             }catch(Throwable eInternal){
  304.                 throw new ConnectorException(eInternal.getMessage(),eInternal);
  305.             }
  306.         }
  307.        
  308.     }
  309.    
  310.     public static void doError(RequestInfo requestInfo,
  311.             RicezioneBusteExternalErrorGenerator generatoreErrore, ErroreIntegrazione erroreIntegrazione,
  312.             IntegrationFunctionError integrationFunctionError, Throwable e, HttpServletResponse res, Logger log){
  313.        
  314.         IProtocolFactory<?> protocolFactory = requestInfo.getProtocolFactory();
  315.                
  316.         if(generatoreErrore!=null){
  317.             OpenSPCoop2Message msgErrore = generatoreErrore.buildErroreProcessamento(null, integrationFunctionError, erroreIntegrazione, e);
  318.             if(msgErrore.getForcedResponseCode()!=null){
  319.                 res.setStatus(Integer.parseInt(msgErrore.getForcedResponseCode()));
  320.             }
  321.             try{
  322.                 msgErrore.writeTo(res.getOutputStream(), true);
  323.             }catch(Exception eWriteTo){
  324.                 log.error("Errore durante la serializzazione dell'errore: "+e.getMessage(),e);
  325.                 try{
  326.                     res.sendError(500);
  327.                 }catch(Exception eInternal){
  328.                     throw new RuntimeException(eInternal.getMessage(),eInternal);
  329.                 }
  330.             }
  331.         }
  332.         else{
  333.             try{
  334.                 res.sendError(500,erroreIntegrazione.getDescrizione(protocolFactory));
  335.             }catch(Exception eInternal){
  336.                 throw new RuntimeException(eInternal.getMessage(),eInternal);
  337.             }
  338.         }
  339.        
  340.     }
  341.    
  342.     public static ConnectorDispatcherErrorInfo doError(RequestInfo requestInfo,
  343.             RicezioneBusteExternalErrorGenerator generatoreErrore, ErroreIntegrazione erroreIntegrazione,
  344.             IntegrationFunctionError integrationFunctionError, Throwable e, ParseException parseException,
  345.             ConnectorOutMessage res, Logger log, boolean clientError) throws ConnectorException{
  346.        
  347.         IProtocolFactory<?> protocolFactory = requestInfo.getProtocolFactory();
  348.                
  349.         if(generatoreErrore!=null){
  350.        
  351.             OpenSPCoop2Message msgErrore = generatoreErrore.buildErroreProcessamento(null, integrationFunctionError, erroreIntegrazione, e);
  352.            
  353.             return _doError(requestInfo, res, log, false, erroreIntegrazione, integrationFunctionError, e, parseException, msgErrore, clientError);
  354.            
  355.         }
  356.         else{
  357.             try{
  358.                 String errore = erroreIntegrazione.getDescrizione(protocolFactory);
  359.                 throw new ConnectorException(errore);
  360.             }catch(Throwable eInternal){
  361.                 throw new ConnectorException(eInternal.getMessage(),eInternal);
  362.             }
  363.         }
  364.        
  365.     }
  366.    
  367.    
  368.    
  369.     private static ConnectorDispatcherErrorInfo _doError(RequestInfo requestInfo,ConnectorOutMessage res,Logger log,boolean portaDelegata,
  370.             ErroreIntegrazione erroreIntegrazione,
  371.             IntegrationFunctionError integrationFunctionError, Throwable e, ParseException parseException,
  372.             OpenSPCoop2Message msg, boolean clientError) throws ConnectorException{
  373.        
  374.         if(requestInfo==null) {
  375.             throw new ConnectorException("RequestInfo param is null");
  376.         }
  377.        
  378.         IProtocolFactory<?> protocolFactory = requestInfo.getProtocolFactory();
  379.        
  380.         Map<String, List<String>> trasporto = new HashMap<>();
  381.         try {
  382.             UtilitiesIntegrazione utilitiesIntegrazione = null;
  383.             if(portaDelegata) {
  384.                 utilitiesIntegrazione = UtilitiesIntegrazione.getInstancePDResponse(log);
  385.             }
  386.             else {
  387.                 utilitiesIntegrazione = UtilitiesIntegrazione.getInstancePAResponse(log);
  388.             }
  389.             if(requestInfo!=null && requestInfo.getIdTransazione()!=null) {
  390.                 HeaderIntegrazione hdr = new HeaderIntegrazione(requestInfo.getIdTransazione());
  391.                 utilitiesIntegrazione.setTransportProperties(hdr,trasporto,null);
  392.             }
  393.             else {
  394.                 utilitiesIntegrazione.setInfoProductTransportProperties(trasporto);
  395.             }
  396.             utilitiesIntegrazione.setSecurityHeaders(msg!=null ? msg.getServiceBinding() : ServiceBinding.REST, requestInfo, trasporto, null);
  397.             if(trasporto.size()>0){
  398.                 Iterator<String> keys = trasporto.keySet().iterator();
  399.                 while (keys.hasNext()) {
  400.                     String key = (String) keys.next();
  401.                     List<String> values = trasporto.get(key);
  402.                     if(values!=null && !values.isEmpty()) {
  403.                         for (String value : values) {
  404.                             res.addHeader(key,value);
  405.                         }
  406.                     }
  407.                 }  
  408.             }
  409.            
  410.         }catch(Throwable error){
  411.             log.error("Errore durante la serializzazione degli headers: "+error.getMessage(),error);
  412.             try{
  413.                 throw new ConnectorException(erroreIntegrazione.getDescrizione(protocolFactory),e);
  414.             }catch(Throwable eInternal){
  415.                 // rilancio eccezione originale
  416.                 throw new ConnectorException(e.getMessage(),e);
  417.             }
  418.         }
  419.        
  420.         int status = 200;
  421.         String contentTypeRisposta = null;
  422.         if(msg!=null && msg.getForcedResponseCode()!=null){
  423.             status = Integer.parseInt(msg.getForcedResponseCode());
  424.             res.setStatus(status);
  425.         }
  426.        
  427.         // content type
  428.         // Alcune implementazioni richiedono di aggiornare il Content-Type
  429.         try{
  430.             if(msg!=null) {
  431.                 msg.updateContentType();
  432.                 contentTypeRisposta = msg.getContentType();
  433.                 if (contentTypeRisposta != null) {
  434.                     res.setContentType(contentTypeRisposta);
  435.                     TransportUtils.setHeader(trasporto,HttpConstants.CONTENT_TYPE, contentTypeRisposta);
  436.                 }
  437.             }
  438.         }catch(Throwable error){
  439.             log.error("Errore durante la serializzazione del contentType: "+error.getMessage(),error);
  440.             try{
  441.                 throw new ConnectorException(erroreIntegrazione.getDescrizione(protocolFactory),e);
  442.             }catch(Throwable eInternal){
  443.                 // rilancio eccezione originale
  444.                 throw new ConnectorException(e.getMessage(),e);
  445.             }
  446.         }
  447.        
  448.         ConnectorDispatcherErrorInfo errorInfo = null;
  449.         try{
  450.             if(clientError) {
  451.                 errorInfo = ConnectorDispatcherErrorInfo.getClientError(msg, status, contentTypeRisposta, trasporto, requestInfo, protocolFactory);
  452.             }else{
  453.                 errorInfo = ConnectorDispatcherErrorInfo.getGenericError(msg, status, contentTypeRisposta, trasporto, requestInfo, protocolFactory);    
  454.             }
  455.         }catch(Throwable error){
  456.             log.error("Errore durante la generazione delle informazioni di errore: "+error.getMessage(),error);
  457.             try{
  458.                 throw new ConnectorException(erroreIntegrazione.getDescrizione(protocolFactory),e);
  459.             }catch(Throwable eInternal){
  460.                 // rilancio eccezione originale
  461.                 throw new ConnectorException(e.getMessage(),e);
  462.             }
  463.         }

  464.         boolean consume = false; // può essere usato nel post out response handler
  465.         try{
  466.             res.sendResponse(msg, consume);
  467.         }catch(Throwable error){
  468.             log.error("Errore durante la serializzazione dell'errore: "+error.getMessage(),error);
  469.             try{
  470.                 throw new ConnectorException(erroreIntegrazione.getDescrizione(protocolFactory),e);
  471.             }catch(Throwable eInternal){
  472.                 // rilancio eccezione originale
  473.                 throw new ConnectorException(e.getMessage(),e);
  474.             }
  475.         }finally {
  476.             res.flush(false);
  477.             res.close(false);
  478.         }
  479.        
  480.         return errorInfo;
  481.     }
  482.    
  483.    
  484.     public static void doInfo(ConnectorDispatcherInfo info, RequestInfo requestInfo,ConnectorOutMessage res, Logger log, boolean portaDelegata) throws ConnectorException{
  485.        
  486.         if(requestInfo==null) {
  487.             throw new ConnectorException("RequestInfo param is null");
  488.         }
  489.        
  490.         IProtocolFactory<?> protocolFactory = requestInfo.getProtocolFactory();
  491.        
  492.         OpenSPCoop2Message msg = info.getMessage();
  493.        
  494.         Map<String, List<String>> trasporto = info.getTrasporto();
  495.         try {
  496.             UtilitiesIntegrazione utilitiesIntegrazione = null;
  497.             if(portaDelegata) {
  498.                 utilitiesIntegrazione = UtilitiesIntegrazione.getInstancePDResponse(log);
  499.             }
  500.             else {
  501.                 utilitiesIntegrazione = UtilitiesIntegrazione.getInstancePAResponse(log);
  502.             }
  503.             if(requestInfo!=null && requestInfo.getIdTransazione()!=null) {
  504.                 HeaderIntegrazione hdr = new HeaderIntegrazione(requestInfo.getIdTransazione());
  505.                 utilitiesIntegrazione.setTransportProperties(hdr,trasporto,null);
  506.             }
  507.             else {
  508.                 utilitiesIntegrazione.setInfoProductTransportProperties(trasporto);
  509.             }
  510.             utilitiesIntegrazione.setSecurityHeaders(msg!=null ? msg.getServiceBinding() : ServiceBinding.REST, requestInfo, trasporto, null);
  511.             if(trasporto.size()>0){
  512.                 Iterator<String> keys = trasporto.keySet().iterator();
  513.                 while (keys.hasNext()) {
  514.                     String key = (String) keys.next();
  515.                     List<String> values = trasporto.get(key);
  516.                     if(values!=null && !values.isEmpty()) {
  517.                         for (String value : values) {
  518.                             res.addHeader(key,value);
  519.                         }
  520.                     }
  521.                 }  
  522.             }
  523.            
  524.         }catch(Throwable error){
  525.             log.error("Errore durante la serializzazione degli headers: "+error.getMessage(),error);
  526.             try{
  527.                 throw new ConnectorException(ErroriIntegrazione.ERRORE_5XX_GENERICO_PROCESSAMENTO_MESSAGGIO.get5XX_ErroreProcessamento(error.getMessage())
  528.                         .getDescrizione(protocolFactory),error);
  529.             }catch(Throwable eInternal){
  530.                 // rilancio eccezione originale
  531.                 throw new ConnectorException(error.getMessage(),error);
  532.             }
  533.         }
  534.        
  535.         int status = 200;
  536.         String contentTypeRisposta = null;
  537.         if(msg!=null && msg.getForcedResponseCode()!=null){
  538.             status = Integer.parseInt(msg.getForcedResponseCode());
  539.             res.setStatus(status);
  540.         }
  541.         else if(info.getStatus()>0) {
  542.             status = info.getStatus();
  543.             res.setStatus(status);
  544.         }
  545.        
  546.         // content type
  547.         // Alcune implementazioni richiedono di aggiornare il Content-Type
  548.         try{
  549.             if(msg!=null) {
  550.                 msg.updateContentType();
  551.                 contentTypeRisposta = msg.getContentType();
  552.                 if (contentTypeRisposta != null) {
  553.                     res.setContentType(contentTypeRisposta);
  554.                     TransportUtils.setHeader(trasporto,HttpConstants.CONTENT_TYPE, contentTypeRisposta);
  555.                 }
  556.             }
  557.         }catch(Throwable error){
  558.             log.error("Errore durante la serializzazione del contentType: "+error.getMessage(),error);
  559.             try{
  560.                 throw new ConnectorException(ErroriIntegrazione.ERRORE_5XX_GENERICO_PROCESSAMENTO_MESSAGGIO.get5XX_ErroreProcessamento(error.getMessage())
  561.                         .getDescrizione(protocolFactory),error);
  562.             }catch(Throwable eInternal){
  563.                 // rilancio eccezione originale
  564.                 throw new ConnectorException(error.getMessage(),error);
  565.             }
  566.         }
  567.    
  568.         boolean consume = false; // può essere usato nel post out response handler
  569.         try{
  570.             res.sendResponse(msg, consume);
  571.         }catch(Throwable error){
  572.             log.error("Errore durante la serializzazione dell'errore: "+error.getMessage(),error);
  573.             try{
  574.                 throw new ConnectorException(ErroriIntegrazione.ERRORE_5XX_GENERICO_PROCESSAMENTO_MESSAGGIO.get5XX_ErroreProcessamento(error.getMessage())
  575.                         .getDescrizione(protocolFactory),error);
  576.             }catch(Throwable eInternal){
  577.                 // rilancio eccezione originale
  578.                 throw new ConnectorException(error.getMessage(),error);
  579.             }
  580.         }finally {
  581.             res.flush(false);
  582.             res.close(false);
  583.         }
  584.        
  585.     }
  586. }