AbstractOpenSPCoop2Message_soap_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.ByteArrayOutputStream;
  22. import java.io.InputStream;
  23. import java.io.OutputStream;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import java.util.Locale;

  27. import javax.activation.DataHandler;
  28. import javax.xml.namespace.QName;
  29. import javax.xml.soap.AttachmentPart;
  30. import javax.xml.soap.MimeHeaders;
  31. import javax.xml.soap.SOAPBody;
  32. import javax.xml.soap.SOAPElement;
  33. import javax.xml.soap.SOAPEnvelope;
  34. import javax.xml.soap.SOAPFault;
  35. import javax.xml.soap.SOAPHeader;
  36. import javax.xml.soap.SOAPHeaderElement;
  37. import javax.xml.soap.SOAPMessage;
  38. import javax.xml.soap.SOAPPart;

  39. import org.openspcoop2.message.AbstractBaseOpenSPCoop2MessageDynamicContent;
  40. import org.openspcoop2.message.ForwardConfig;
  41. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  42. import org.openspcoop2.message.OpenSPCoop2MessageProperties;
  43. import org.openspcoop2.message.OpenSPCoop2SoapMessage;
  44. import org.openspcoop2.message.context.MessageContext;
  45. import org.openspcoop2.message.exception.MessageException;
  46. import org.openspcoop2.message.exception.MessageNotSupportedException;
  47. import org.openspcoop2.message.soap.dynamic.DynamicSOAPHeader;
  48. import org.openspcoop2.message.soap.dynamic.DynamicSOAPMessage;
  49. import org.openspcoop2.message.soap.dynamic.DynamicSOAPPart;
  50. import org.openspcoop2.message.soap.mtom.MTOMUtilities;
  51. import org.openspcoop2.message.soap.mtom.MtomXomPackageInfo;
  52. import org.openspcoop2.message.soap.mtom.MtomXomReference;
  53. import org.openspcoop2.message.soap.reader.OpenSPCoop2MessageSoapStreamReader;
  54. import org.openspcoop2.message.soap.reference.Reference;
  55. import org.openspcoop2.utils.UtilsRuntimeException;
  56. import org.openspcoop2.utils.transport.http.ContentTypeUtilities;
  57. import org.w3c.dom.Element;
  58. import org.w3c.dom.Node;
  59. import org.w3c.dom.NodeList;

  60. /**
  61.  * Implementazione dell'OpenSPCoop2Message utilizzabile per messaggi soap
  62.  *
  63.  * @author Andrea Poli (poli@link.it)
  64.  * @author $Author$
  65.  * @version $Rev$, $Date$
  66.  */
  67. public abstract class AbstractOpenSPCoop2Message_soap_impl<T extends AbstractOpenSPCoop2Message_saaj_impl> extends AbstractBaseOpenSPCoop2MessageDynamicContent<T> implements OpenSPCoop2SoapMessage {


  68.    
  69.     protected long overhead;
  70.     protected MimeHeaders mhs;
  71.    
  72.     /* OpenSPCoop2SoapMessageCore */
  73.     protected OpenSPCoop2SoapMessageCore soapCore;
  74.    

  75.     protected AbstractOpenSPCoop2Message_soap_impl(OpenSPCoop2MessageFactory messageFactory, MimeHeaders mhs, InputStream is, long overhead,
  76.             OpenSPCoop2MessageSoapStreamReader soapStreamReader) throws MessageException {
  77.         super(messageFactory, is, SoapUtils.getContentType(mhs), true, soapStreamReader);
  78.         this.overhead = overhead;
  79.         this.mhs = mhs;
  80.         this.soapCore = new OpenSPCoop2SoapMessageCore();
  81.     }
  82.        
  83.     @Override
  84.     protected String buildContentAsString() throws MessageException{
  85.         try{
  86.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  87.             this.serializeContent(bout, true);
  88.             bout.flush();
  89.             bout.close();
  90.             return bout.toString(this.contentTypeCharsetName);
  91.         }catch(Exception e){
  92.             throw new MessageException(e.getMessage(),e);
  93.         }
  94.     }
  95.    
  96.     @Override
  97.     protected byte[] buildContentAsByteArray() throws MessageException{
  98.         try{
  99.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  100.             this.serializeContent(bout, true);
  101.             bout.flush();
  102.             bout.close();
  103.             return bout.toByteArray();
  104.         }catch(Exception e){
  105.             throw new MessageException(e.getMessage(),e);
  106.         }
  107.     }
  108.    
  109.     @Override
  110.     protected void serializeContent(OutputStream os, boolean consume) throws MessageException {
  111.         try{
  112.             this.content.writeTo(os, consume);
  113.             os.flush();
  114.         }catch(Exception e){
  115.             throw new MessageException(e.getMessage(),e);
  116.         }
  117.     }
  118.    
  119.     protected void addSoapHeaderModifiedIntoSoapReader(SOAPHeader hdr, SOAPEnvelope envelope) throws MessageException {
  120.         try {
  121.             if(this.contentUpdatable && this.getSoapReader()!=null && this.getSoapReader().isSoapHeaderModified()) {
  122.                 SOAPHeader newHdr = this.getSoapReader().getHeader();
  123.                
  124.                 /**Node newHdrAdopted = envelope.getOwnerDocument().adoptNode(newHdr);*/
  125.                 if(hdr!=null) {
  126.                     /**org.w3c.dom.NodeList l = envelope.getChildNodes();
  127.                     for (int i = 0; i < l.getLength(); i++) {
  128.                         System.out.println("BEFORE ["+l.item(i).getClass().getName()+"] ["+l.item(i).getLocalName()+"]");
  129.                     }*/
  130.                    
  131.                     /**envelope.replaceChild(hdr, newHdrAdopted);*/
  132.                     // devo preservare i commenti, cdata ...
  133.                    
  134.                     /**
  135.                     In questa maniera creo un nuovo header e chi ha preso il riferimento se lo perde (es. sbustamento Modi, wsaddressing)
  136.                     Node newHdrAdopted = envelope.getOwnerDocument().importNode(newHdr, true);
  137.                     if((hdr.getPrefix()==null && newHdrAdopted.getPrefix()!=null) || (!hdr.getPrefix().equals(newHdrAdopted.getPrefix()))) {
  138.                         //System.out.println("ALLINEAO AD HEADER PREFIX ["+hdr.getPrefix()+"]");
  139.                         newHdrAdopted.setPrefix(hdr.getPrefix()); // allineo alla costruzione del DOM
  140.                     }
  141.                     envelope.insertBefore(newHdrAdopted, hdr);
  142.                     envelope.removeChild(hdr);
  143.                     */
  144.                    
  145.                     // rimpiazzo i figli
  146.                     hdr.removeContents();
  147.                     if(newHdr!=null) {
  148.                         NodeList list = newHdr.getChildNodes();
  149.                         /**System.out.println("LIST["+list.getLength()+"] ["+org.openspcoop2.utils.xml.XMLUtils.getInstance().toString(newHdr)+"]");*/
  150.                         if(list!=null && list.getLength()>0) {
  151.                             for (int i = 0; i < list.getLength(); i++) {
  152.                                 Node n = list.item(i);
  153.                                 /**System.out.println("i["+i+"] ["+n.getClass().getName()+"] ["+n.getLocalName()+"]");*/
  154.                                 Node newNodeAdopted = envelope.getOwnerDocument().importNode(n, true);
  155.                                 hdr.appendChild(newNodeAdopted);
  156.                             }
  157.                         }
  158.                     }
  159.                    
  160.                     /**l = envelope.getChildNodes();
  161.                     for (int i = 0; i < l.getLength(); i++) {
  162.                         System.out.println("AFTER ["+l.item(i).getClass().getName()+"] ["+l.item(i).getLocalName()+"]");
  163.                     }*/
  164.                 }
  165.                 else if(newHdr!=null){
  166.                     Node newHdrAdopted = envelope.getOwnerDocument().importNode(newHdr, true);
  167.                     if((envelope.getPrefix()==null && newHdrAdopted.getPrefix()!=null) || (!envelope.getPrefix().equals(newHdrAdopted.getPrefix()))) {
  168.                         /**System.out.println("ALLINEAO AD ENVELOPE PREFIX ["+envelope.getPrefix()+"]");*/
  169.                         newHdrAdopted.setPrefix(envelope.getPrefix()); // allineo alla costruzione del DOM
  170.                     }
  171.                     SOAPBody body = envelope.getBody();
  172.                     if(body!=null) {
  173.                         envelope.insertBefore(newHdrAdopted, body);
  174.                     }
  175.                     else {
  176.                         envelope.appendChild(newHdrAdopted);
  177.                     }
  178.                 }
  179.                
  180.                 // Rilascio memoria tanto verra usato sempre il DOM costruito
  181.                 /**if(this.contentUpdatable) {
  182.                 //System.out.println("ADD SOAP HEADER e RILASCIO RISORSE");*/
  183.                 this.getSoapReader().clearHeader();
  184.                 this.clearDynamicResources();
  185.                 /**}
  186.                 //else {
  187.                 //  System.out.println("ADD SOAP HEADER SENZA RILASCIARE RISORSE");
  188.                 //}*/
  189.                
  190.                 this.releaseSoapReader();
  191.             }
  192.            
  193.             // Rilascio SOAPReader tanto verra usato sempre il DOM costruito
  194.             /**if(this.contentUpdatable) {
  195.             //this.releaseSoapReader();
  196.             //}*/
  197.            
  198.         }catch(Exception e){
  199.             throw new MessageException(e.getMessage(),e);
  200.         }
  201.     }
  202.     @Override
  203.     protected void setUpdatableContent() throws MessageException{
  204.         try{
  205.            
  206.             if(
  207.                     //this.contentUpdatable &&
  208.                     this.getSoapReader()!=null && this.getSoapReader().isSoapHeaderModified()) {
  209.                 SOAPHeader hdr = this.content.getSOAPHeader();
  210.                 SOAPEnvelope envelope = this.content.getSOAPPart().getEnvelope();
  211.                 this.addSoapHeaderModifiedIntoSoapReader(hdr, envelope);
  212.             }
  213.            
  214.         }catch(Throwable t){
  215.             throw SoapUtils.buildMessageException("Unable to update envelope header: ",t);
  216.         }
  217.     }
  218.    
  219.    
  220.    
  221.    
  222.     // ---------- Metodi SAAJ re-implementati (in questi metodi l'accesso al contenuto avviene solo se precedentemente effettuato) ------------
  223.    
  224.     /* Copy Resources to another instance */
  225.    
  226.     @Override
  227.     public MessageContext serializeResourcesTo() throws MessageException{
  228. /**     if(this.isContentBuilded()) {
  229. //          return this.content.serializeResourcesTo();
  230. //      }
  231. //      else {*/
  232.         MessageContext messageContext = super.serializeResourcesTo();
  233.         return this.soapCore.serializeResourcesTo(messageContext);
  234. /**     }*/
  235.     }
  236.    
  237.     @Override
  238.     public void readResourcesFrom(MessageContext messageContext) throws MessageException{
  239. /**     if(this.isContentBuilded()) {
  240. //          this.content.readResourcesFrom(messageContext);
  241. //      }
  242. //      else {*/
  243.         super.readResourcesFrom(messageContext);
  244.         this.soapCore.readResourcesFrom(messageContext);
  245. /**     }*/
  246.     }
  247.    
  248.     /* SOAPAction */
  249.    
  250.     @Override
  251.     public String getSoapAction(){
  252. /**     if(this.isContentBuilded()) {
  253. //          return this.content.getSoapAction();
  254. //      }
  255. //      else {*/
  256.         return this.soapCore.getSoapAction();
  257. /**     }*/
  258.     }
  259.    
  260.     @Override
  261.     public void setSoapAction(String soapAction){
  262.         if(this.isContentBuilded()) {
  263.             this.content.setSoapAction(soapAction);
  264.         }
  265. /**     else {*/
  266.         this.soapCore.setSoapAction(soapAction);
  267. /**     }*/
  268.     }
  269.    
  270.     /* WSSecurity */
  271.    
  272.     @Override
  273.     public boolean isThrowExceptionIfFoundMoreSecurityHeader() {
  274. /**     if(this.isContentBuilded()) {
  275. //          return this.content.isThrowExceptionIfFoundMoreSecurityHeader();
  276. //      }
  277. //      else {*/
  278.         return this.soapCore.isThrowExceptionIfFoundMoreSecurityHeader();
  279. /**     }*/
  280.     }

  281.     @Override
  282.     public void setThrowExceptionIfFoundMoreSecurityHeader(boolean throwExceptionIfFoundMoreSecurityHeader) {
  283.         if(this.isContentBuilded()) {
  284.             this.content.setThrowExceptionIfFoundMoreSecurityHeader(throwExceptionIfFoundMoreSecurityHeader);
  285.         }
  286. /**     else {*/
  287.         this.soapCore.setThrowExceptionIfFoundMoreSecurityHeader(throwExceptionIfFoundMoreSecurityHeader);
  288. /**     }*/
  289.     }
  290.    
  291.     /* Trasporto */
  292.    
  293.     @Override
  294.     public OpenSPCoop2MessageProperties getForwardTransportHeader(ForwardConfig forwardConfig) throws MessageException{
  295.         OpenSPCoop2MessageProperties msg = super.getForwardTransportHeader(forwardConfig);
  296.         if(this.isContentBuilded()) {
  297.             return new OpenSPCoop2MessageMimeHeaderProperties(this.content._getSOAPMessage(),msg);
  298.         }
  299.         else {
  300.             return msg;
  301.         }
  302.     }
  303.    
  304.    
  305.     /* ContentType */
  306.    
  307.     @Override
  308.     public void addContentTypeParameter(String name,String value) throws MessageException{
  309.         if(this.isContentBuilded()) {
  310.             this.content.contentTypeParamaters.put(name, value);
  311.             if(!this.contentUpdatable) {
  312.                 // aggiorno anche struttura leggera
  313.                 super.addContentTypeParameter(name, value);
  314.             }
  315.         }
  316.         else {
  317.             super.addContentTypeParameter(name, value);
  318.         }
  319.     }
  320.    
  321.     @Override
  322.     public void removeContentTypeParameter(String name) throws MessageException{
  323.         if(this.isContentBuilded()) {
  324.             this.content.removeContentTypeParameter(name);
  325.             if(!this.contentUpdatable) {
  326.                 // aggiorno anche struttura leggera
  327.                 super.removeContentTypeParameter(name);
  328.             }
  329.         }
  330.         else {
  331.             super.removeContentTypeParameter(name);
  332.         }
  333.     }
  334.    
  335.     @Override
  336.     public void updateContentType() throws MessageException {
  337.         if(this.isContentBuilded()) {
  338.             this.content.updateContentType();
  339.             if(!this.contentUpdatable) {
  340.                 // aggiorno anche struttura leggera
  341.                 super.updateContentType();
  342.             }
  343.         }
  344.         else {
  345.             super.updateContentType();
  346.         }
  347.     }
  348.    
  349.     @Override
  350.     public void setContentType(String type) {
  351.         if(this.isContentBuilded()) {
  352.             try{
  353.                 this.content.setContentType(type);
  354.             }catch(Exception eInternal){
  355.                 throw new UtilsRuntimeException(eInternal.getMessage(),eInternal);
  356.             }
  357.             if(!this.contentUpdatable) {
  358.                 // aggiorno anche struttura leggera
  359.                 super.setContentType(type);
  360.             }
  361.         }
  362.         else {
  363.             super.setContentType(type);
  364.         }
  365.     }
  366.    
  367.     @Override
  368.     public String getContentType() {
  369.         if(this.contentBuffer!=null && !this.contentUpdatable) {
  370.             return _getContentType();
  371.         }
  372.         else if(this.isContentBuilded()) {
  373.             try{
  374.                 return this.content.getContentType();
  375.             }catch(Exception eInternal){
  376.                 throw new UtilsRuntimeException(eInternal.getMessage(),eInternal);
  377.             }
  378.         }
  379.         else {
  380.             return _getContentType();
  381.         }
  382.     }
  383.     protected String _getContentType() {
  384.         return super.getContentType();
  385.     }
  386.    
  387.     /* Save */
  388.    
  389.     @Override
  390.     public void saveChanges() throws MessageException{
  391.         if(this.isContentBuilded()) {
  392.             this.content.saveChanges();
  393.             if(!this.contentUpdatable) {
  394.                 // aggiorno anche struttura leggera
  395.                 super.saveChanges();
  396.             }
  397.         }
  398.         else {
  399.             super.saveChanges();
  400.         }
  401.     }
  402.    
  403.     @Override
  404.     public boolean saveRequired(){
  405.         if(this.contentBuffer!=null && !this.contentUpdatable) {
  406.             return super.saveRequired();
  407.         }
  408.         else if(this.isContentBuilded()) {
  409.             return this.content.saveRequired();
  410.         }
  411.         else {
  412.             return super.saveRequired();
  413.         }
  414.     }
  415.    
  416.     /* ContentID Attachments SOAP */
  417.    
  418.     @Override
  419.     public String createContentID(String ns) throws MessageException, MessageNotSupportedException {
  420.         if(this.isContentBuilded()) {
  421.             return this.content.createContentID(ns);
  422.         }
  423.         else {
  424.             return AbstractOpenSPCoop2Message_saaj_impl._createContentID(ns);
  425.         }
  426.     }
  427.    
  428.     /* SOAP Utilities */

  429.     @Override
  430.     public Element getFirstChildElement(SOAPElement element) throws MessageException, MessageNotSupportedException {
  431.         if(this.isContentBuilded()) {
  432.             return this.content.getFirstChildElement(element);
  433.         }
  434.         else {
  435.             return AbstractOpenSPCoop2Message_saaj_impl._getFirstChildElement(element);
  436.         }
  437.     }
  438.     @Override
  439.     public SOAPElement createSOAPElement(byte[] bytes) throws MessageException, MessageNotSupportedException {
  440.         if(this.isContentBuilded()) {
  441.             return this.content.createSOAPElement(bytes);
  442.         }
  443.         else {
  444.             return AbstractOpenSPCoop2Message_saaj_impl._createSOAPElement(bytes, this.getMessageType(), this.messageFactory);
  445.         }
  446.     }
  447.     @Override
  448.     public SOAPHeaderElement newSOAPHeaderElement(SOAPHeader hdr, QName name)
  449.             throws MessageException, MessageNotSupportedException {
  450.         if(this.isContentBuilded()) {
  451.             return this.content.newSOAPHeaderElement(hdr,name);
  452.         }
  453.         else {
  454.             return AbstractOpenSPCoop2Message_saaj_impl._newSOAPHeaderElement(hdr,name);
  455.         }
  456.     }
  457.     @Override
  458.     public void addHeaderElement(SOAPHeader hdr, SOAPHeaderElement hdrElement)
  459.             throws MessageException, MessageNotSupportedException {
  460.         if(this.isContentBuilded()) {
  461.             this.content.addHeaderElement(hdr, hdrElement);
  462.         }
  463.         else {
  464.             AbstractOpenSPCoop2Message_saaj_impl._addHeaderElement(hdr, hdrElement);
  465.         }
  466.     }
  467.     @Override
  468.     public void removeHeaderElement(SOAPHeader hdr, SOAPHeaderElement hdrElement)
  469.             throws MessageException, MessageNotSupportedException {
  470.         if(this.isContentBuilded()) {
  471.             this.content.removeHeaderElement(hdr, hdrElement);
  472.         }
  473.         else {
  474.             AbstractOpenSPCoop2Message_saaj_impl._removeHeaderElement(hdr, hdrElement);
  475.         }
  476.     }
  477.     @Override
  478.     public void setFaultCode(SOAPFault fault, SOAPFaultCode code, QName eccezioneName)
  479.             throws MessageException, MessageNotSupportedException {
  480.         if(this.isContentBuilded()) {
  481.             this.content.setFaultCode(fault, code, eccezioneName);
  482.         }
  483.         else {
  484.             AbstractOpenSPCoop2Message_saaj_impl._setFaultCode(fault, code, eccezioneName);
  485.         }
  486.     }
  487.     @Override
  488.     public void setFaultString(SOAPFault fault, String message) throws MessageException, MessageNotSupportedException {
  489.         if(this.isContentBuilded()) {
  490.             this.content.setFaultString(fault, message);
  491.         }
  492.         else {
  493.             AbstractOpenSPCoop2Message_saaj_impl._setFaultString(fault, message);
  494.         }
  495.     }
  496.     @Override
  497.     public void setFaultString(SOAPFault fault, String message, Locale locale)
  498.             throws MessageException, MessageNotSupportedException {
  499.         if(this.isContentBuilded()) {
  500.             this.content.setFaultString(fault, message, locale);
  501.         }
  502.         else {
  503.             AbstractOpenSPCoop2Message_saaj_impl._setFaultString(fault, message, locale);
  504.         }
  505.     }
  506.    
  507.     /* Ws Security (SoapBox) */
  508.    
  509.     @Override
  510.     public String getEncryptedDataHeaderBlockClass() {
  511.         if(this.isContentBuilded()) {
  512.             return this.content.getEncryptedDataHeaderBlockClass();
  513.         }
  514.         else {
  515.             return AbstractOpenSPCoop2Message_saaj_impl._getEncryptedDataHeaderBlockClass();
  516.         }
  517.     }
  518.     @Override
  519.     public String getProcessPartialEncryptedMessageClass() {
  520.         if(this.isContentBuilded()) {
  521.             return this.content.getProcessPartialEncryptedMessageClass();
  522.         }
  523.         else {
  524.             return AbstractOpenSPCoop2Message_saaj_impl._getProcessPartialEncryptedMessageClass();
  525.         }
  526.     }
  527.     @Override
  528.     public String getSignPartialMessageProcessorClass() {
  529.         if(this.isContentBuilded()) {
  530.             return this.content.getSignPartialMessageProcessorClass();
  531.         }
  532.         else {
  533.             return AbstractOpenSPCoop2Message_saaj_impl._getSignPartialMessageProcessorClass();
  534.         }
  535.     }
  536.    
  537.    
  538.    
  539.    
  540.     // ------------ Metodi SAAJ wrappati con la chiamata 'getContent' che fa si che venga costruito il messaggio --------------------
  541.    
  542.     public boolean isSoapHeaderOptimizable() {
  543.         OpenSPCoop2MessageSoapStreamReader soapReader = this.getSoapReader();
  544.         return ( (!this.isContentBuilded()) || !this.contentUpdatable )
  545.                 &&
  546.                 soapReader!=null && soapReader.isParsingComplete() && soapReader.isSoapHeaderOptimizable();
  547.     }
  548.    
  549.     /* Elementi SOAP */
  550.    
  551.     private DynamicSOAPMessage<T> dynamicSOAPMessage = null;
  552.     @Override
  553.     public SOAPMessage getSOAPMessage() throws MessageException, MessageNotSupportedException {
  554.         if(this.dynamicSOAPMessage==null &&
  555.             isSoapHeaderOptimizable()) {
  556.             this.dynamicSOAPMessage = new DynamicSOAPMessage<>(this);
  557.         }
  558.         if(this.dynamicSOAPMessage!=null) {
  559.             return this.dynamicSOAPMessage;
  560.         }
  561.         else {
  562.             return this.getContent().getSOAPMessage();
  563.         }
  564.     }
  565.    
  566.     private DynamicSOAPPart<T> dynamicSOAPPart = null;
  567.     @Override
  568.     public SOAPPart getSOAPPart() throws MessageException, MessageNotSupportedException {
  569.         if(this.dynamicSOAPPart==null &&
  570.             isSoapHeaderOptimizable()) {
  571.             this.dynamicSOAPPart = new DynamicSOAPPart<>(this);
  572.         }
  573.         if(this.dynamicSOAPPart!=null) {
  574.             return this.dynamicSOAPPart;
  575.         }
  576.         else {
  577.             T c = this.getContent();
  578.             return c!=null ? c.getSOAPPart() : null;
  579.         }
  580.     }
  581.    
  582.     private DynamicSOAPHeader<T> dynamicSOAPHeader = null;
  583.     @Override
  584.     public SOAPHeader getSOAPHeader() throws MessageException, MessageNotSupportedException {
  585.         if(this.dynamicSOAPHeader==null &&
  586.             isSoapHeaderOptimizable()) {
  587.             this.dynamicSOAPHeader = new DynamicSOAPHeader<>(this);
  588.         }
  589.         if(this.dynamicSOAPHeader!=null) {
  590.             return this.dynamicSOAPHeader;
  591.         }
  592.         else {
  593.             return this.getContent().getSOAPHeader();
  594.         }
  595.     }
  596.    
  597.     public void clearDynamicResources() {
  598.        
  599.         this.dynamicSOAPHeader = null;
  600.        
  601.         if(this.dynamicSOAPPart!=null) {
  602.             this.dynamicSOAPPart.clearDynamicResources();
  603.         }
  604.         this.dynamicSOAPPart=null;
  605.        
  606.         if(this.dynamicSOAPMessage!=null) {
  607.             this.dynamicSOAPMessage.clearDynamicResources();
  608.         }
  609.         this.dynamicSOAPMessage=null;
  610.     }
  611.    
  612.     @Override
  613.     public SOAPBody getSOAPBody() throws MessageException, MessageNotSupportedException {
  614.         return this.getContent().getSOAPBody();
  615.     }
  616.     @Override
  617.     protected SOAPBody _getSOAPBody_internalAnalyze()throws MessageException, MessageNotSupportedException {
  618.         // usato in isFault
  619.         SOAPBody body = null;
  620.         if(this.content!=null && this.contentBuffer!=null) {
  621.             // per lasciare readOnly
  622.             body = this.content.getSOAPBody();
  623.         }
  624.         else {
  625.             body = getSOAPBody();
  626.         }
  627.         return body;
  628.     }
  629.     @Override
  630.     public boolean hasSOAPFault() throws MessageException,MessageNotSupportedException{
  631.         OpenSPCoop2MessageSoapStreamReader soapReader = getSoapReader();
  632.         if(!this.isContentBuilded() && soapReader!=null && soapReader.isParsingComplete()) {
  633.             return soapReader.isFault();
  634.         }
  635.        
  636.         SOAPBody body = null;
  637.         if(this.content!=null && this.contentBuffer!=null) {
  638.             // per lasciare readOnly
  639.             body = this.content.getSOAPBody();
  640.         }
  641.         else {
  642.             body = getSOAPBody();
  643.         }
  644.         return body!=null && body.hasFault();
  645.     }
  646.     @Override
  647.     public boolean isSOAPBodyEmpty() throws MessageException,MessageNotSupportedException{
  648.         OpenSPCoop2MessageSoapStreamReader soapReader = getSoapReader();
  649.         if(!this.isContentBuilded() && soapReader!=null && soapReader.isParsingComplete()) {
  650.             return soapReader.isEmpty();
  651.         }
  652.        
  653.         SOAPBody body = null;
  654.         if(this.content!=null && this.contentBuffer!=null) {
  655.             // per lasciare readOnly
  656.             body = this.content.getSOAPBody();
  657.         }
  658.         else {
  659.             body = getSOAPBody();
  660.         }
  661.        
  662.         boolean hasContent = body!=null;
  663.         if(hasContent){
  664.             hasContent = SoapUtils.getFirstNotEmptyChildNode(this.messageFactory, body, false)!=null;
  665.         }
  666.         return !hasContent;
  667.     }
  668.    
  669.     /* Attachments SOAP */
  670.    
  671.     @Override
  672.     public void addAttachmentPart(AttachmentPart attachmentPart) throws MessageException, MessageNotSupportedException {
  673.         this.getContent().addAttachmentPart(attachmentPart);
  674.     }
  675.     @Override
  676.     public AttachmentPart createAttachmentPart(DataHandler dataHandler)
  677.             throws MessageException, MessageNotSupportedException {
  678.         return this.getContent().createAttachmentPart(dataHandler);
  679.     }
  680.     @Override
  681.     public AttachmentPart createAttachmentPart() throws MessageException, MessageNotSupportedException {
  682.         return this.getContent().createAttachmentPart();
  683.     }
  684.     @Override
  685.     public boolean hasAttachments() throws MessageException,MessageNotSupportedException{
  686.         if(this.isContentBuilded()){
  687.             return this.content.countAttachments()>0;
  688.         }
  689.         else {
  690.             String ct = this.getContentType();
  691.             try {
  692.                 return ct!=null && ContentTypeUtilities.isMultipartRelated(ct);
  693.             }catch(Exception e) {
  694.                 throw new MessageException(e.getMessage(),e);
  695.             }
  696.         }
  697.     }
  698.     @Override
  699.     public int countAttachments() throws MessageException, MessageNotSupportedException {
  700.         if(this.isContentBuilded()){
  701.             return this.content.countAttachments();
  702.         }
  703.         else {
  704.             return this.getContent().countAttachments();
  705.         }
  706.     }
  707.     @Override
  708.     public Iterator<?> getAttachments() throws MessageException, MessageNotSupportedException {
  709.         if(this.isContentBuilded()){
  710.             return this.content.getAttachments();
  711.         }
  712.         else {
  713.             return this.getContent().getAttachments();
  714.         }
  715.     }
  716.     @Override
  717.     public Iterator<?> getAttachments(MimeHeaders headers) throws MessageException, MessageNotSupportedException {
  718.         if(this.isContentBuilded()){
  719.             return this.content.getAttachments(headers);
  720.         }
  721.         else {
  722.             return this.getContent().getAttachments(headers);
  723.         }
  724.     }
  725.     @Override
  726.     public AttachmentPart getAttachment(SOAPElement element) throws MessageException, MessageNotSupportedException {
  727.         if(this.isContentBuilded()){
  728.             return this.content.getAttachment(element);
  729.         }
  730.         else {
  731.             return this.getContent().getAttachment(element);
  732.         }
  733.     }
  734.     @Override
  735.     public void removeAllAttachments() throws MessageException, MessageNotSupportedException {
  736.         this.getContent().removeAllAttachments();
  737.     }
  738.     @Override
  739.     public void removeAttachments(MimeHeaders mhs) throws MessageException, MessageNotSupportedException {
  740.         this.getContent().removeAttachments(mhs);
  741.     }
  742.     @Override
  743.     public void updateAttachmentPart(AttachmentPart ap, byte[] content, String contentType)
  744.             throws MessageException, MessageNotSupportedException {
  745.         this.getContent().updateAttachmentPart(ap, content, contentType);
  746.     }
  747.     @Override
  748.     public void updateAttachmentPart(AttachmentPart ap, String content, String contentType)
  749.             throws MessageException, MessageNotSupportedException {
  750.         this.getContent().updateAttachmentPart(ap, content, contentType);
  751.     }
  752.     @Override
  753.     public void updateAttachmentPart(AttachmentPart ap, DataHandler dh)
  754.             throws MessageException, MessageNotSupportedException {
  755.         this.getContent().updateAttachmentPart(ap, dh);
  756.     }

  757.     /* Ws Security */

  758.     @Override
  759.     public List<Reference> getWSSDirtyElements(String actor, boolean mustUnderstand)
  760.             throws MessageException, MessageNotSupportedException {    
  761.         if(this.isContentBuilded()){
  762.             return this.content.getWSSDirtyElements(actor, mustUnderstand);
  763.         }
  764.         else {
  765.             return this.getContent().getWSSDirtyElements(actor, mustUnderstand);
  766.         }
  767.     }
  768.     @Override
  769.     public void cleanWSSDirtyElements(String actor, boolean mustUnderstand, List<Reference> elementsToClean,
  770.             boolean detachHeaderWSSecurity, boolean removeAllIdRef)
  771.             throws MessageException, MessageNotSupportedException {
  772.         this.getContent().cleanWSSDirtyElements(actor, mustUnderstand, elementsToClean,
  773.                 detachHeaderWSSecurity, removeAllIdRef);
  774.     }
  775.    
  776.     /* MTOM */

  777.     @Override
  778.     public List<MtomXomReference> mtomUnpackaging() throws MessageException,MessageNotSupportedException{
  779.         return MTOMUtilities.unpackaging(this.getContent(), false, true);
  780.     }
  781.     @Override
  782.     public List<MtomXomReference> mtomPackaging( List<MtomXomPackageInfo> packageInfos) throws MessageException,MessageNotSupportedException{
  783.         return MTOMUtilities.packaging(this.getContent(), packageInfos, true);
  784.     }
  785.     @Override
  786.     public List<MtomXomReference> mtomVerify( List<MtomXomPackageInfo> packageInfos) throws MessageException,MessageNotSupportedException{
  787.         return MTOMUtilities.verify((this.isContentBuilded() ? this.content : this.getContent()), packageInfos, true);
  788.     }
  789.     @Override
  790.     public List<MtomXomReference> mtomFastUnpackagingForXSDConformance() throws MessageException,MessageNotSupportedException{
  791.         return MTOMUtilities.unpackaging(this.getContent(), true, true);
  792.     }
  793.     @Override
  794.     public void mtomRestoreAfterXSDConformance(List<MtomXomReference> references) throws MessageException,MessageNotSupportedException{
  795.         MTOMUtilities.restoreAfterFastUnpackaging(this.getContent(), references, true);
  796.     }
  797.    
  798. }