1 10 11 package org.mule.providers.soap.xfire; 12 13 import java.util.Iterator ; 14 15 import javax.activation.DataHandler ; 16 17 import org.codehaus.xfire.MessageContext; 18 import org.codehaus.xfire.attachments.Attachment; 19 import org.codehaus.xfire.attachments.Attachments; 20 import org.codehaus.xfire.attachments.SimpleAttachment; 21 import org.jdom.Element; 22 import org.jdom.Namespace; 23 import org.mule.config.MuleProperties; 24 import org.mule.providers.AbstractMessageAdapter; 25 import org.mule.providers.soap.MuleSoapHeaders; 26 import org.mule.transformers.simple.SerializableToByteArray; 27 import org.mule.umo.transformer.UMOTransformer; 28 29 33 public class XFireMessageAdapter extends AbstractMessageAdapter 34 { 35 38 private static final long serialVersionUID = 419878758858206446L; 39 40 private final Object payload; 41 private MessageContext messageContext; 42 43 private UMOTransformer trans = new SerializableToByteArray(); 44 45 public XFireMessageAdapter(Object message) 46 { 47 this.payload = message; 48 } 49 50 58 public String getPayloadAsString(String encoding) throws Exception 59 { 60 return new String (getPayloadAsBytes(), encoding); 61 } 62 63 69 public byte[] getPayloadAsBytes() throws Exception 70 { 71 return (byte[])trans.transform(payload); 72 } 73 74 77 public Object getPayload() 78 { 79 return payload; 80 } 81 82 public void addAttachment(String name, DataHandler dataHandler) throws Exception 83 { 84 messageContext.getInMessage().getAttachments().addPart(new SimpleAttachment(name, dataHandler)); 85 super.addAttachment(name, dataHandler); 86 } 87 88 public void removeAttachment(String name) throws Exception 89 { 90 throw new UnsupportedOperationException ("XFIRE: removeAttachment"); 91 } 93 94 public MessageContext getMessageContext() 95 { 96 return messageContext; 97 } 98 99 public void setMessageContext(MessageContext messageContext) 100 { 101 this.messageContext = messageContext; 102 initHeaders(); 103 initAttachments(); 105 } 106 107 protected void initHeaders() 108 { 109 if (messageContext.getInMessage() != null) 110 { 111 Element header = messageContext.getInMessage().getHeader(); 112 if (header == null) return; 113 114 Namespace ns = Namespace.getNamespace(MuleSoapHeaders.MULE_NAMESPACE, 115 MuleSoapHeaders.MULE_10_ACTOR); 116 Element muleHeaders = header.getChild(MuleSoapHeaders.MULE_HEADER, ns); 117 if (muleHeaders != null) 118 { 119 Element child = muleHeaders.getChild(MuleProperties.MULE_CORRELATION_ID_PROPERTY, ns); 120 if (child != null) 121 { 122 setCorrelationId(child.getText()); 123 } 124 child = muleHeaders.getChild(MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY, ns); 125 if (child != null) 126 { 127 setCorrelationGroupSize(Integer.valueOf(child.getText()).intValue()); 128 } 129 child = muleHeaders.getChild(MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY, ns); 130 if (child != null) 131 { 132 setCorrelationSequence(Integer.valueOf(child.getText()).intValue()); 133 } 134 child = muleHeaders.getChild(MuleProperties.MULE_REPLY_TO_PROPERTY, ns); 135 if (child != null) 136 { 137 setReplyTo(child.getText()); 138 } 139 } 140 } 141 142 } 143 144 protected void initAttachments() 145 { 146 try 147 { 148 Attachments atts = this.messageContext.getInMessage().getAttachments(); 149 if (atts != null) 150 { 151 for (Iterator i = atts.getParts(); i.hasNext();) 152 { 153 Attachment att = ((Attachment)i.next()); 154 super.addAttachment(att.getId(), att.getDataHandler()); 155 } 156 } 157 } 158 catch (Exception e) 159 { 160 logger.fatal("Failed to read attachments", e); 162 } 163 } 164 165 } 166 | Popular Tags |