OpenSPCoop2Message_soap12_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.ByteArrayInputStream;
  22. import java.io.FileInputStream;
  23. import java.io.InputStream;

  24. import javax.xml.soap.MimeHeaders;
  25. import javax.xml.soap.SOAPEnvelope;
  26. import javax.xml.soap.SOAPHeader;

  27. import org.apache.commons.lang.StringUtils;
  28. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  29. import org.openspcoop2.message.constants.Costanti;
  30. import org.openspcoop2.message.exception.MessageException;
  31. import org.openspcoop2.message.soap.reader.OpenSPCoop2MessageSoapStreamReader;
  32. import org.openspcoop2.utils.io.DumpByteArrayOutputStream;
  33. import org.openspcoop2.utils.transport.http.ContentTypeUtilities;

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

  35. /**
  36.  * Implementazione dell'OpenSPCoop2Message utilizzabile per messaggi SOAP 12
  37.  *
  38.  * @author Andrea Poli (poli@link.it)
  39.  * @author $Author$
  40.  * @version $Rev$, $Date$
  41.  */
  42. public class OpenSPCoop2Message_soap12_impl extends AbstractOpenSPCoop2Message_soap_impl<OpenSPCoop2Message_saaj_12_impl> {

  43.     public OpenSPCoop2Message_soap12_impl(OpenSPCoop2MessageFactory messageFactory, MimeHeaders mhs, InputStream is, long overhead,
  44.             OpenSPCoop2MessageSoapStreamReader soapStreamReader) throws MessageException {
  45.         super(messageFactory, mhs, is, overhead, soapStreamReader);
  46.     }
  47.    
  48.     @Override
  49.     protected OpenSPCoop2Message_saaj_12_impl buildContent() throws MessageException{
  50.         try{
  51.             return buildContent(this._getInputStream());
  52.         }finally{
  53.             try{
  54.                 this._getInputStream().close();
  55.             }catch(Exception eClose){
  56.                 // close
  57.             }
  58.         }
  59.     }
  60.     @Override
  61.     protected OpenSPCoop2Message_saaj_12_impl buildContent(DumpByteArrayOutputStream contentBuffer) throws MessageException{
  62.         try{
  63.             if(contentBuffer.isSerializedOnFileSystem()) {
  64.                 try(InputStream is = new FileInputStream(contentBuffer.getSerializedFile())){
  65.                     return buildContent(is);
  66.                 }
  67.             }
  68.             else {
  69.                 try(InputStream is = new ByteArrayInputStream(contentBuffer.toByteArray())){
  70.                     return buildContent(is);
  71.                 }
  72.             }
  73.         }
  74.         catch(MessageException me) {
  75.             throw me;
  76.         }
  77.         catch(Exception e){
  78.             throw new MessageException(e.getMessage(),e);
  79.         }
  80.     }

  81.     protected OpenSPCoop2Message_saaj_12_impl buildContent(InputStream is) throws MessageException{
  82.         try{
  83.             //System.out.println("BUILD 12");
  84.             OpenSPCoop2Message_saaj_12_impl msg = new OpenSPCoop2Message_saaj_12_impl(this.messageFactory, this.mhs, is);
  85.             msg.initialize(this.overhead);
  86.             msg.copyResourceFrom(this.soapCore);
  87.             msg.setMessageRole(this.messageRole);
  88.             msg.setMessageType(this.messageType);
  89.            
  90.             // Verifica struttura (in AbstractBaseOpenSPCoop2MessageDynamicContent verrĂ  collezionato l'errore di parsing)
  91.             // Servono tutti e 3 i comandi per far leggere tutto lo stream
  92.             // Se si levano alcuni test falliscono
  93.             SOAPHeader hdr = msg.getSOAPHeader();
  94.             SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
  95.             msg.countAttachments();
  96.            
  97.             this.addSoapHeaderModifiedIntoSoapReader(hdr, envelope);
  98.            
  99.             return msg;
  100.         }catch(Throwable t){
  101.             throw SoapUtils.buildMessageException("Unable to create envelope from given source: ",t);
  102.         }
  103.     }

  104.     @Override
  105.     protected String _getContentType() {
  106.         String ct = super._getContentType();
  107.         try {
  108.             ContentType cType = new ContentType(ct);
  109.             String soapActionValue = this.getSoapAction();
  110.             if(soapActionValue!=null && StringUtils.isNotEmpty(soapActionValue)){
  111.                 String pSoapAction = cType.getParameter(Costanti.SOAP12_OPTIONAL_CONTENT_TYPE_PARAMETER_SOAP_ACTION);
  112.                 //System.out.println("PARAM["+pSoapAction+"] presente["+soapActionValue+"]");
  113.                 if(soapActionValue.equals(pSoapAction)) {
  114.                     //System.out.println("EQUALS");
  115.                     return ct;
  116.                 }
  117.                 else {
  118.                     if(soapActionValue.startsWith("\"") && soapActionValue.length()>1) {
  119.                         soapActionValue = soapActionValue.substring(1);
  120.                     }
  121.                     if(soapActionValue.endsWith("\"") && soapActionValue.length()>1) {
  122.                         soapActionValue = soapActionValue.substring(0,soapActionValue.length()-1);
  123.                     }
  124.                     if(StringUtils.isEmpty(soapActionValue)) {
  125.                         return ct; // dopo l'eliminazione degli "
  126.                     }
  127.                     if(pSoapAction!=null){
  128.                         cType.getParameterList().remove(Costanti.SOAP12_OPTIONAL_CONTENT_TYPE_PARAMETER_SOAP_ACTION);
  129.                     }
  130.                     if(this.contentTypeParamaters!=null) {
  131.                         if(this.contentTypeParamaters.containsKey(Costanti.SOAP12_OPTIONAL_CONTENT_TYPE_PARAMETER_SOAP_ACTION)){
  132.                             this.contentTypeParamaters.remove(Costanti.SOAP12_OPTIONAL_CONTENT_TYPE_PARAMETER_SOAP_ACTION);
  133.                         }
  134.                         this.contentTypeParamaters.put(Costanti.SOAP12_OPTIONAL_CONTENT_TYPE_PARAMETER_SOAP_ACTION, soapActionValue);
  135.                     }
  136.                     //System.out.println("NEW '"+soapActionValue+"'");
  137.                     return ContentTypeUtilities.buildContentType(cType.toString(),this.contentTypeParamaters);
  138.                 }
  139.             }
  140.             else {
  141.                 return ct;
  142.             }
  143.         } catch (Exception e) {
  144.             e.printStackTrace(System.err);
  145.             return ct;
  146.         }
  147.     }
  148. }