OpenSPCoop2Message_saaj_12_impl.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.message.soap;

  21. import java.io.IOException;
  22. import java.io.InputStream;

  23. import javax.xml.namespace.QName;
  24. import javax.xml.soap.MimeHeaders;
  25. import javax.xml.soap.SOAPConstants;
  26. import javax.xml.soap.SOAPException;
  27. import javax.xml.soap.SOAPFault;
  28. import javax.xml.soap.SOAPMessage;

  29. import org.apache.commons.io.input.CountingInputStream;
  30. import org.apache.commons.lang.StringUtils;
  31. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  32. import org.openspcoop2.message.constants.Costanti;
  33. import org.openspcoop2.message.exception.MessageException;
  34. import org.openspcoop2.utils.transport.http.ContentTypeUtilities;
  35. import org.openspcoop2.utils.transport.http.HttpConstants;

  36. import com.sun.xml.messaging.saaj.packaging.mime.internet.ContentType;

  37. /**
  38.  * Implementazione dell'OpenSPCoop2Message
  39.  *
  40.  * @author Lorenzo Nardi (nardi@link.it)
  41.  * @author $Author$
  42.  * @version $Rev$, $Date$
  43.  */

  44. public class OpenSPCoop2Message_saaj_12_impl extends AbstractOpenSPCoop2Message_saaj_impl
  45. {
  46.    
  47.     /* Costruttori */
  48.    
  49.     public OpenSPCoop2Message_saaj_12_impl(OpenSPCoop2MessageFactory messageFactory) {  
  50.         super(messageFactory, new Message1_2_FIX_Impl());
  51.     }
  52.    
  53.     public OpenSPCoop2Message_saaj_12_impl(OpenSPCoop2MessageFactory messageFactory, MimeHeaders mhs, InputStream is) throws SOAPException, IOException{
  54.         super(messageFactory, new Message1_2_FIX_Impl(mhs, new CountingInputStream(is)));
  55.     }
  56.    
  57.     public OpenSPCoop2Message_saaj_12_impl(OpenSPCoop2MessageFactory messageFactory, SOAPMessage msg) {
  58.         //TODO questo costruttore non funziona con messaggi con attachment.
  59.         //C'e' un bug nell'implementazione della sun che non copia gli attachment
  60.         //In particolare il parametro super.mimePart (protetto non accessibile).
  61.         // Per questo motivo essite la classe 1_2 FIX che utilizza direttamente il messaggio fornito
  62.         super(messageFactory, new Message1_2_FIX_Impl(msg));
  63.     }
  64.    

  65.     /* Initialize ed internal Message Impl */
  66.    
  67.     public void initialize(long overhead){
  68.         getMessage1_2_FIX_Impl().setLazyAttachments(false);
  69.         this.incomingsize = getMessage1_2_FIX_Impl().getCountingInputStream().getByteCount() - overhead;
  70.     }

  71.     private Message1_2_FIX_Impl getMessage1_2_FIX_Impl(){
  72.         return ((Message1_2_FIX_Impl)this._getSoapMessage());
  73.     }
  74.    
  75.    
  76.    
  77.     /* ContentType */
  78.    
  79.     @Override
  80.     protected String _super_getContentType() {
  81.         try {
  82.             return _getContentType(false);
  83.         } catch (Throwable e) {
  84.             // Non dovrebbe avvenire errori
  85.         }   return getMessage1_2_FIX_Impl().getContentType();
  86.     }
  87.    
  88.     @Override
  89.     public void setContentType(String type) throws MessageException{
  90.         getMessage1_2_FIX_Impl().setContentType(type);
  91.     }
  92.    
  93.     @Override
  94.     public String getContentType() throws MessageException{
  95.         return _getContentType(true);
  96.     }
  97.     private String _getContentType(boolean includeContentTypeParameters) throws MessageException{
  98.        
  99.         try {
  100.             String ct = getMessage1_2_FIX_Impl().getContentType();
  101.             ContentType cType = new ContentType(ct);
  102.             if(cType.getBaseType().equalsIgnoreCase(HttpConstants.CONTENT_TYPE_MULTIPART_RELATED)) {
  103.                 if(getMessage1_2_FIX_Impl().getMimeMultipart() != null)
  104.                     cType.setParameter(HttpConstants.CONTENT_TYPE_MULTIPART_PARAMETER_BOUNDARY,
  105.                             getMessage1_2_FIX_Impl().getMimeMultipart().getContentType().getParameter(HttpConstants.CONTENT_TYPE_MULTIPART_PARAMETER_BOUNDARY));
  106.             }
  107.             String soapActionValue = this.getSoapAction();
  108.             if(soapActionValue!=null){
  109.                 String pSoapAction = cType.getParameter(Costanti.SOAP12_OPTIONAL_CONTENT_TYPE_PARAMETER_SOAP_ACTION);
  110.                 if(!soapActionValue.equals(pSoapAction)) {
  111.                     if(soapActionValue.startsWith("\"") && soapActionValue.length()>1) {
  112.                         soapActionValue = soapActionValue.substring(1);
  113.                     }
  114.                     if(soapActionValue.endsWith("\"") && soapActionValue.length()>1) {
  115.                         soapActionValue = soapActionValue.substring(0,soapActionValue.length()-1);
  116.                     }
  117.                     if(StringUtils.isNotEmpty(soapActionValue)) {
  118.                         if(pSoapAction!=null){
  119.                             cType.getParameterList().remove(Costanti.SOAP12_OPTIONAL_CONTENT_TYPE_PARAMETER_SOAP_ACTION);
  120.                         }
  121.                         if(this.contentTypeParamaters!=null) {
  122.                             if(this.contentTypeParamaters.containsKey(Costanti.SOAP12_OPTIONAL_CONTENT_TYPE_PARAMETER_SOAP_ACTION)){
  123.                                 this.contentTypeParamaters.remove(Costanti.SOAP12_OPTIONAL_CONTENT_TYPE_PARAMETER_SOAP_ACTION);
  124.                             }
  125.                             this.contentTypeParamaters.put(Costanti.SOAP12_OPTIONAL_CONTENT_TYPE_PARAMETER_SOAP_ACTION, soapActionValue);
  126.                         }
  127.                         //System.out.println("NEW '"+soapActionValue+"'");
  128.                     }
  129.                 }
  130.                 //else {
  131.                 //  System.out.println("EQUALS");
  132.                 //}
  133.             }
  134.             if(includeContentTypeParameters) {
  135.                 return ContentTypeUtilities.buildContentType(cType.toString(),this.contentTypeParamaters);
  136.             }
  137.             else {
  138.                 return cType.toString();
  139.             }
  140.         } catch (Exception e) {
  141.             e.printStackTrace(System.err);
  142.             try{
  143.                 String ct = getMessage1_2_FIX_Impl().getContentType();
  144.                 if(includeContentTypeParameters) {
  145.                     return ContentTypeUtilities.buildContentType(ct,this.contentTypeParamaters);
  146.                 }
  147.                 else {
  148.                     return ct;
  149.                 }
  150.             }catch(Exception eInternal){
  151.                 throw new RuntimeException(eInternal.getMessage(),eInternal);
  152.             }
  153.         }
  154.     }
  155.    

  156.    
  157.     /* SOAP Utilities */
  158.    
  159.     @Override
  160.     public void setFaultCode(SOAPFault fault, SOAPFaultCode code, QName eccezioneName) throws MessageException {
  161.         try{
  162.             QName faultCode = new QName(SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE, code.toString());
  163.             fault.setFaultCode(faultCode);
  164.             fault.appendFaultSubcode(eccezioneName);
  165.         }catch(Exception e){
  166.             throw new MessageException(e.getMessage(),e);
  167.         }
  168.     }
  169. }