Message1_1_FIX_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.IOException;
  23. import java.io.InputStream;
  24. import java.io.OutputStream;
  25. import java.util.Iterator;

  26. import javax.xml.soap.AttachmentPart;
  27. import javax.xml.soap.MimeHeaders;
  28. import javax.xml.soap.SOAPElement;
  29. import javax.xml.soap.SOAPException;
  30. import javax.xml.soap.SOAPMessage;
  31. import javax.xml.soap.SOAPPart;

  32. import org.apache.commons.io.input.CountingInputStream;
  33. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  34. import org.openspcoop2.message.constants.MessageRole;
  35. import org.openspcoop2.message.constants.MessageType;

  36. import com.sun.xml.messaging.saaj.SOAPExceptionImpl;
  37. import com.sun.xml.messaging.saaj.packaging.mime.internet.ContentType;
  38. import com.sun.xml.messaging.saaj.packaging.mime.internet.MimeMultipart;

  39. /**
  40.  * Message1_1_FIX_Impl
  41.  *
  42.  *
  43.  * @author Poli Andrea (apoli@link.it)
  44.  * @author $Author$
  45.  * @version $Rev$, $Date$
  46.  */
  47. public class Message1_1_FIX_Impl extends com.sun.xml.messaging.saaj.soap.ver1_1.Message1_1Impl {
  48.    
  49.     // La classe Message1_1Impl presenta un problema per il costruttore con SOAPMessage
  50.     // Non funziona in presenza di messaggi con attachment.
  51.     // C'e' un bug nell'implementazione della sun che non copia gli attachment
  52.     // In particolare il parametro super.mimePart (protetto non accessibile).
  53.     // Per questo motivo essite la classe 1_1 FIX che utilizza direttamente il messaggio fornito
  54.    
  55.     private SOAPMessage msg; // messaggio fornito nel costruttore
  56.     private CountingInputStream cis;
  57.    
  58.    
  59.     // Costruttori
  60.    
  61.     public Message1_1_FIX_Impl() {
  62.         super();
  63.     }
  64.    
  65.     public Message1_1_FIX_Impl(boolean isFastInfoset, boolean acceptFastInfoset) {
  66.         super(isFastInfoset, acceptFastInfoset);
  67.     }

  68.     // !!Costruttore che presenta il problema!!
  69.     public Message1_1_FIX_Impl(SOAPMessage msg) {
  70.         try {
  71.             this.msg = (com.sun.xml.messaging.saaj.soap.ver1_1.Message1_1Impl) msg;
  72.         }catch(Throwable t) {
  73.             // Fix: java.lang.ClassCastException: com.sun.xml.messaging.saaj.soap.ver1_1.Message1_1Impl cannot be cast to com.sun.xml.messaging.saaj.soap.ver1_1.Message1_1Impl
  74.             // Versioni differenti tra saaj-impl nel software e quello nell'application server
  75.             if(t instanceof java.lang.ClassCastException) {
  76.                 try {
  77.                     ByteArrayOutputStream bout = new ByteArrayOutputStream();
  78.                     msg.writeTo(bout);
  79.                     bout.flush();
  80.                     bout.close();
  81.                     this.msg = OpenSPCoop2MessageFactory.getDefaultMessageFactory().createMessage(MessageType.SOAP_11, MessageRole.NONE,
  82.                             SoapUtils.getContentType(msg), bout.toByteArray(), null, null).getMessage_throwParseThrowable().castAsSoap().getSOAPMessage();
  83.                 }catch(Throwable tNew) {
  84.                     throw new RuntimeException(new org.openspcoop2.utils.UtilsMultiException(tNew, t));
  85.                 }
  86.             }
  87.             else {
  88.                 throw new RuntimeException(t);
  89.             }
  90.         }
  91.     }

  92.     public Message1_1_FIX_Impl(MimeHeaders headers, CountingInputStream in)
  93.         throws IOException, SOAPExceptionImpl {
  94.         super(headers, in);
  95.         this.cis = in;
  96.     }

  97.     public Message1_1_FIX_Impl(MimeHeaders headers, ContentType ct, int stat, InputStream in)
  98.         throws SOAPExceptionImpl {
  99.         super(headers,ct,stat,in);
  100.     }
  101.    
  102.    
  103.     // CountingInputStream
  104.    
  105.     public CountingInputStream getCountingInputStream(){
  106.         return this.cis;
  107.     }

  108.     // Wrapper
  109.    
  110.     public MimeMultipart getMimeMultipart(){
  111.         if (this.msg!= null) {
  112.             if(this.msg instanceof Message1_1_FIX_Impl)
  113.                 return ((Message1_1_FIX_Impl)this.msg).getMimeMultipart();
  114.             else
  115.                 return super.mmp;
  116.         }
  117.         else return super.mmp;
  118.     }
  119.    
  120.     // Metodi MessageImpl
  121.    
  122.     @Override
  123.     public String getContentType() {
  124.         if (this.msg!= null) return _getContentType(this.msg);  
  125.         else return super.getContentType();
  126.     }
  127.     private String _getContentType(SOAPMessage soapMsg) {
  128.         try {
  129.             com.sun.xml.messaging.saaj.soap.ver1_1.Message1_1Impl msg = (com.sun.xml.messaging.saaj.soap.ver1_1.Message1_1Impl) soapMsg;
  130.             return msg.getContentType();
  131.         }catch(Throwable t) {
  132.             // Fix: java.lang.ClassCastException: com.sun.xml.messaging.saaj.soap.ver1_1.Message1_1Impl cannot be cast to com.sun.xml.messaging.saaj.soap.ver1_1.Message1_1Impl
  133.             // Versioni differenti tra saaj-impl nel software e quello nell'application server
  134.             return SoapUtils.getContentType(this.msg);
  135.         }
  136.     }
  137.    
  138.     // Metodi SOAPMessage
  139.    
  140.     @Override
  141.     public void addAttachmentPart(AttachmentPart attachment) {
  142.         if (this.msg!= null) this.msg.addAttachmentPart(attachment);    
  143.         else super.addAttachmentPart(attachment);
  144.     }
  145.    
  146.     @Override
  147.     public int countAttachments() {
  148.         if (this.msg!= null) return this.msg.countAttachments();
  149.         else return super.countAttachments();
  150.     }
  151.    
  152.     @Override
  153.     public AttachmentPart createAttachmentPart() {
  154.         if (this.msg!= null) return this.msg.createAttachmentPart();
  155.         else return super.createAttachmentPart();
  156.     }
  157.    
  158.     @Override
  159.     public AttachmentPart getAttachment(SOAPElement element) throws SOAPException {
  160.         if (this.msg!= null) return this.msg.getAttachment(element);
  161.         else return super.getAttachment(element);
  162.     }
  163.    
  164.     @Override
  165.     public Iterator<AttachmentPart> getAttachments() {
  166.         if (this.msg!= null) return this.msg.getAttachments();
  167.         else return super.getAttachments();
  168.     }
  169.    
  170.     @Override
  171.     public Iterator<AttachmentPart> getAttachments(MimeHeaders headers) {
  172.         if (this.msg!= null) return this.msg.getAttachments(headers);
  173.         else return super.getAttachments(headers);
  174.     }
  175.    
  176.     @Override
  177.     public String getContentDescription() {
  178.         if (this.msg!= null) return this.msg.getContentDescription();
  179.         else return super.getContentDescription();
  180.     }
  181.    
  182.     @Override
  183.     public MimeHeaders getMimeHeaders() {
  184.         if (this.msg!= null) return this.msg.getMimeHeaders();
  185.         else return super.getMimeHeaders();
  186.     }
  187.    
  188.     @Override
  189.     public SOAPPart getSOAPPart() {
  190.         if (this.msg!= null) return this.msg.getSOAPPart();
  191.         else return super.getSOAPPart();
  192.     }
  193.    
  194.     @Override
  195.     public void removeAllAttachments() {
  196.         if (this.msg!= null) this.msg.removeAllAttachments();
  197.         else super.removeAllAttachments();
  198.     }
  199.    
  200.     @Override
  201.     public void removeAttachments(MimeHeaders headers) {
  202.         if (this.msg!= null) this.msg.removeAttachments(headers);
  203.         else super.removeAttachments(headers);
  204.     }
  205.    
  206.     @Override
  207.     public void saveChanges() throws SOAPException {
  208.         if (this.msg!= null) this.msg.saveChanges();
  209.         else super.saveChanges();
  210.     }
  211.    
  212.     @Override
  213.     public boolean saveRequired() {
  214.         if (this.msg!= null) return this.msg.saveRequired();
  215.         else return super.saveRequired();
  216.     }
  217.    
  218.     @Override
  219.     public void setContentDescription(String description) {
  220.         if (this.msg!= null) this.msg.setContentDescription(description);
  221.         else super.setContentDescription(description);
  222.     }
  223.    
  224.     @Override
  225.     public void writeTo(OutputStream out) throws SOAPException, IOException {
  226.         if (this.msg!= null) this.msg.writeTo(out);
  227.         else super.writeTo(out);
  228.     }
  229.        
  230. }