Coverage Report - org.mule.providers.soap.axis.AxisMessageAdapter
 
Classes in this File Line Coverage Branch Coverage Complexity
AxisMessageAdapter
0%
0/45
0%
0/8
2.333
 
 1  
 /*
 2  
  * $Id: AxisMessageAdapter.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.providers.soap.axis;
 12  
 
 13  
 import org.mule.impl.ThreadSafeAccess;
 14  
 import org.mule.providers.AbstractMessageAdapter;
 15  
 import org.mule.providers.soap.MuleSoapHeaders;
 16  
 import org.mule.providers.soap.i18n.SoapMessages;
 17  
 import org.mule.transformers.simple.SerializableToByteArray;
 18  
 import org.mule.umo.MessagingException;
 19  
 import org.mule.umo.transformer.UMOTransformer;
 20  
 import org.mule.util.StringUtils;
 21  
 
 22  
 import java.util.Iterator;
 23  
 
 24  
 import javax.activation.DataHandler;
 25  
 import javax.xml.soap.SOAPException;
 26  
 import javax.xml.soap.SOAPMessage;
 27  
 
 28  
 import org.apache.axis.MessageContext;
 29  
 import org.apache.axis.attachments.AttachmentPart;
 30  
 
 31  
 /**
 32  
  * <code>AxisMessageAdapter</code> wraps a soap message. The payload of the adapter
 33  
  * is the raw message received from the transport, but you also have access to the
 34  
  * SOAPMessage object by using <code>adapter.getSOAPMessage()</code>
 35  
  */
 36  
 public class AxisMessageAdapter extends AbstractMessageAdapter
 37  
 {
 38  
     /**
 39  
      * Serial version
 40  
      */
 41  
     private static final long serialVersionUID = -923205879581370143L;
 42  
 
 43  
     private final Object payload;
 44  
     private final SOAPMessage message;
 45  0
     private UMOTransformer trans = new SerializableToByteArray();
 46  
 
 47  
     public AxisMessageAdapter(Object message) throws MessagingException
 48  0
     {
 49  0
         this.payload = message;
 50  
         try
 51  
         {
 52  0
             MessageContext ctx = MessageContext.getCurrentContext();
 53  
 
 54  0
             if (ctx != null)
 55  
             {
 56  0
                 MuleSoapHeaders header = new MuleSoapHeaders(ctx.getMessage().getSOAPPart().getEnvelope()
 57  
                     .getHeader());
 58  
 
 59  0
                 if (StringUtils.isNotBlank(header.getReplyTo()))
 60  
                 {
 61  0
                     setReplyTo(header.getReplyTo());
 62  
                 }
 63  
 
 64  0
                 if (StringUtils.isNotBlank(header.getCorrelationGroup()))
 65  
                 {
 66  0
                     setCorrelationGroupSize(Integer.parseInt(header.getCorrelationGroup()));
 67  
                 }
 68  0
                 if (StringUtils.isNotBlank(header.getCorrelationSequence()))
 69  
                 {
 70  0
                     setCorrelationSequence(Integer.parseInt(header.getCorrelationSequence()));
 71  
                 }
 72  0
                 if (StringUtils.isNotBlank(header.getCorrelationId()))
 73  
                 {
 74  0
                     setCorrelationId(header.getCorrelationId());
 75  
                 }
 76  
 
 77  0
                 this.message = ctx.getMessage();
 78  0
                 int x = 1;
 79  
                 try
 80  
                 {
 81  0
                     for (Iterator i = this.message.getAttachments(); i.hasNext(); x++)
 82  
                     {
 83  0
                         super.addAttachment(String.valueOf(x), ((AttachmentPart)i.next())
 84  
                             .getActivationDataHandler());
 85  
                     }
 86  
                 }
 87  0
                 catch (Exception e)
 88  
                 {
 89  
                     // this will not happen
 90  0
                     logger.fatal("Failed to read attachments", e);
 91  0
                 }
 92  
             }
 93  
             else
 94  
             {
 95  0
                 this.message = null;
 96  
             }
 97  
         }
 98  0
         catch (SOAPException e)
 99  
         {
 100  0
             throw new MessagingException(SoapMessages.failedToProcessSoapHeaders(), message, e);
 101  0
         }
 102  0
     }
 103  
 
 104  
     public AxisMessageAdapter(AxisMessageAdapter template)
 105  
     {
 106  0
         super(template);
 107  0
         payload = template.payload;
 108  0
         message = template.message;
 109  0
         trans = template.trans;
 110  0
     }
 111  
 
 112  
     /**
 113  
      * Converts the message implementation into a String representation
 114  
      * 
 115  
      * @param encoding The encoding to use when transforming the message (if
 116  
      *            necessary). The parameter is used when converting from a byte array
 117  
      * @return String representation of the message payload
 118  
      * @throws Exception Implementation may throw an endpoint specific exception
 119  
      */
 120  
     public String getPayloadAsString(String encoding) throws Exception
 121  
     {
 122  0
         return new String(getPayloadAsBytes(), encoding);
 123  
     }
 124  
 
 125  
     /**
 126  
      * Converts the payload implementation into a String representation
 127  
      * 
 128  
      * @return String representation of the payload
 129  
      * @throws Exception Implemetation may throw an endpoint specific exception
 130  
      */
 131  
     public byte[] getPayloadAsBytes() throws Exception
 132  
     {
 133  0
         return (byte[])trans.transform(payload);
 134  
     }
 135  
 
 136  
     /**
 137  
      * @return the current payload
 138  
      */
 139  
     public Object getPayload()
 140  
     {
 141  0
         return payload;
 142  
     }
 143  
 
 144  
     public SOAPMessage getSoapMessage()
 145  
     {
 146  0
         return message;
 147  
     }
 148  
 
 149  
     public void addAttachment(String name, DataHandler dataHandler) throws Exception
 150  
     {
 151  0
         if (null != message)
 152  
         {
 153  0
             message.addAttachmentPart(new AttachmentPart(dataHandler));
 154  
         }
 155  0
         super.addAttachment(name, dataHandler);
 156  0
     }
 157  
 
 158  
     public void removeAttachment(String name) throws Exception
 159  
     {
 160  0
         if ("all".equalsIgnoreCase(name))
 161  
         {
 162  0
             message.removeAllAttachments();
 163  0
             attachments.clear();
 164  
         }
 165  
         else
 166  
         {
 167  0
             throw new SOAPException(SoapMessages.cannotRemoveSingleAttachment().toString());
 168  
         }
 169  0
     }
 170  
 
 171  
     public ThreadSafeAccess newThreadCopy()
 172  
     {
 173  0
         return new AxisMessageAdapter(this);
 174  
     }
 175  
 
 176  
 }