Coverage Report - org.mule.providers.soap.xfire.XFireMessageAdapter
 
Classes in this File Line Coverage Branch Coverage Complexity
XFireMessageAdapter
0%
0/50
0%
0/9
2
 
 1  
 /*
 2  
  * $Id: XFireMessageAdapter.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.xfire;
 12  
 
 13  
 import org.mule.config.MuleProperties;
 14  
 import org.mule.impl.ThreadSafeAccess;
 15  
 import org.mule.providers.AbstractMessageAdapter;
 16  
 import org.mule.providers.soap.MuleSoapHeaders;
 17  
 import org.mule.transformers.simple.SerializableToByteArray;
 18  
 import org.mule.umo.transformer.UMOTransformer;
 19  
 
 20  
 import java.util.Iterator;
 21  
 
 22  
 import javax.activation.DataHandler;
 23  
 
 24  
 import org.codehaus.xfire.MessageContext;
 25  
 import org.codehaus.xfire.attachments.Attachment;
 26  
 import org.codehaus.xfire.attachments.Attachments;
 27  
 import org.codehaus.xfire.attachments.SimpleAttachment;
 28  
 import org.jdom.Element;
 29  
 import org.jdom.Namespace;
 30  
 
 31  
 /**
 32  
  * <code>XFireMessageAdapter</code> wraps an XFire MessageContext, reading
 33  
  * attachments and Mule headers.
 34  
  */
 35  
 public class XFireMessageAdapter extends AbstractMessageAdapter
 36  
 {
 37  
     /**
 38  
      * Serial version
 39  
      */
 40  
     private static final long serialVersionUID = 419878758858206446L;
 41  
 
 42  
     private final Object payload;
 43  
     private MessageContext messageContext;
 44  
 
 45  0
     private UMOTransformer trans = new SerializableToByteArray();
 46  
 
 47  
     public XFireMessageAdapter(Object message)
 48  0
     {
 49  0
         this.payload = message;
 50  0
     }
 51  
 
 52  
     protected XFireMessageAdapter(XFireMessageAdapter template)
 53  
     {
 54  0
         super(template);
 55  0
         payload = template.payload;
 56  0
         messageContext = template.messageContext;
 57  0
     }
 58  
 
 59  
     /**
 60  
      * Converts the message implementation into a String representation
 61  
      * 
 62  
      * @param encoding The encoding to use when transforming the message (if
 63  
      *            necessary). The parameter is used when converting from a byte array
 64  
      * @return String representation of the message payload
 65  
      * @throws Exception Implementation may throw an endpoint specific exception
 66  
      */
 67  
     public String getPayloadAsString(String encoding) throws Exception
 68  
     {
 69  0
         return new String(getPayloadAsBytes(), encoding);
 70  
     }
 71  
 
 72  
     /**
 73  
      * Converts the payload implementation into a String representation
 74  
      * 
 75  
      * @return String representation of the payload
 76  
      * @throws Exception Implemetation may throw an endpoint specific exception
 77  
      */
 78  
     public byte[] getPayloadAsBytes() throws Exception
 79  
     {
 80  0
         return (byte[]) trans.transform(payload);
 81  
     }
 82  
 
 83  
     /**
 84  
      * @return the current payload
 85  
      */
 86  
     public Object getPayload()
 87  
     {
 88  0
         return payload;
 89  
     }
 90  
 
 91  
     public void addAttachment(String name, DataHandler dataHandler) throws Exception
 92  
     {
 93  0
         messageContext.getInMessage().getAttachments().addPart(new SimpleAttachment(name, dataHandler));
 94  0
         super.addAttachment(name, dataHandler);
 95  0
     }
 96  
 
 97  
     public void removeAttachment(String name) throws Exception
 98  
     {
 99  0
         throw new UnsupportedOperationException("XFIRE: removeAttachment");
 100  
         // TODO unable to remove an attachment from XFire Attachments
 101  
     }
 102  
 
 103  
     public MessageContext getMessageContext()
 104  
     {
 105  0
         return messageContext;
 106  
     }
 107  
 
 108  
     public void setMessageContext(MessageContext messageContext)
 109  
     {
 110  0
         this.messageContext = messageContext;
 111  0
         initHeaders();
 112  
         // TODO what is the expense of reading attachments??
 113  0
         initAttachments();
 114  0
     }
 115  
 
 116  
     protected void initHeaders()
 117  
     {
 118  0
         if (messageContext.getInMessage() != null)
 119  
         {
 120  0
             Element header = messageContext.getInMessage().getHeader();
 121  0
             if (header == null)
 122  
             {
 123  0
                 return;
 124  
             }
 125  
 
 126  0
             Namespace ns = Namespace.getNamespace(MuleSoapHeaders.MULE_NAMESPACE,
 127  
                 MuleSoapHeaders.MULE_10_ACTOR);
 128  0
             Element muleHeaders = header.getChild(MuleSoapHeaders.MULE_HEADER, ns);
 129  0
             if (muleHeaders != null)
 130  
             {
 131  0
                 Element child = muleHeaders.getChild(MuleProperties.MULE_CORRELATION_ID_PROPERTY, ns);
 132  0
                 if (child != null)
 133  
                 {
 134  0
                     setCorrelationId(child.getText());
 135  
                 }
 136  0
                 child = muleHeaders.getChild(MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY, ns);
 137  0
                 if (child != null)
 138  
                 {
 139  0
                     setCorrelationGroupSize(Integer.valueOf(child.getText()).intValue());
 140  
                 }
 141  0
                 child = muleHeaders.getChild(MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY, ns);
 142  0
                 if (child != null)
 143  
                 {
 144  0
                     setCorrelationSequence(Integer.valueOf(child.getText()).intValue());
 145  
                 }
 146  0
                 child = muleHeaders.getChild(MuleProperties.MULE_REPLY_TO_PROPERTY, ns);
 147  0
                 if (child != null)
 148  
                 {
 149  0
                     setReplyTo(child.getText());
 150  
                 }
 151  
             }
 152  
         }
 153  
 
 154  0
     }
 155  
 
 156  
     protected void initAttachments()
 157  
     {
 158  
         try
 159  
         {
 160  0
             Attachments atts = this.messageContext.getInMessage().getAttachments();
 161  0
             if (atts != null)
 162  
             {
 163  0
                 for (Iterator i = atts.getParts(); i.hasNext();)
 164  
                 {
 165  0
                     Attachment att = ((Attachment) i.next());
 166  0
                     super.addAttachment(att.getId(), att.getDataHandler());
 167  
                 }
 168  
             }
 169  
         }
 170  0
         catch (Exception e)
 171  
         {
 172  
             // this will not happen
 173  0
             logger.fatal("Failed to read attachments", e);
 174  0
         }
 175  0
     }
 176  
 
 177  
     public ThreadSafeAccess newThreadCopy()
 178  
     {
 179  0
         return new XFireMessageAdapter(this);
 180  
     }
 181  
 
 182  
 }