1 10 11 package org.mule.providers.soap; 12 13 import org.dom4j.Namespace; 14 import org.dom4j.QName; 15 import org.dom4j.dom.DOMElement; 16 import org.mule.config.MuleProperties; 17 import org.mule.umo.UMOEvent; 18 import org.w3c.dom.Element ; 19 import org.w3c.dom.Node ; 20 21 import javax.xml.soap.Name ; 22 import javax.xml.soap.SOAPElement ; 23 import javax.xml.soap.SOAPEnvelope ; 24 import javax.xml.soap.SOAPException ; 25 import javax.xml.soap.SOAPHeader ; 26 import javax.xml.soap.SOAPHeaderElement ; 27 import java.util.Iterator ; 28 29 33 public class MuleSoapHeaders 34 { 35 private String replyTo; 36 private String correlationId; 37 private String correlationGroup; 38 private String correlationSequence; 39 40 public static final String MULE_10_ACTOR = "http://www.muleumo.org/providers/soap/1.0"; 41 public static final String MULE_NAMESPACE = "mule"; 42 public static final String MULE_HEADER = "header"; 43 public static final String ENV_REQUEST_HEADERS = "MULE_REQUEST_HEADERS"; 44 45 50 public MuleSoapHeaders(UMOEvent event) 51 { 52 setCorrelationId(event.getMessage().getCorrelationId()); 53 setCorrelationGroup(String.valueOf(event.getMessage().getCorrelationGroupSize())); 54 setCorrelationSequence(String.valueOf(event.getMessage().getCorrelationSequence())); 55 setReplyTo((String )event.getMessage().getReplyTo()); 56 } 57 58 63 public MuleSoapHeaders(SOAPHeader soapHeader) 64 { 65 Iterator iter = soapHeader.examineHeaderElements(MULE_10_ACTOR); 66 SOAPHeaderElement headerElement; 67 while (iter.hasNext()) 68 { 69 headerElement = (SOAPHeaderElement )iter.next(); 70 71 if (org.mule.util.StringUtils.equals(MULE_10_ACTOR, headerElement.getNamespaceURI())) 73 { 74 Iterator iter2 = headerElement.getChildElements(); 75 readElements(iter2); 76 } 77 } 78 } 79 80 public MuleSoapHeaders(Iterator elements) 81 { 82 readElements(elements); 83 } 84 85 protected void readElements(Iterator elements) 86 { 87 88 SOAPElement element; 89 90 while (elements.hasNext()) 91 { 92 93 Object elementObject = elements.next(); 94 95 if (elementObject instanceof SOAPElement ) 97 { 100 element = (SOAPElement )elementObject; 101 String localName = element.getLocalName(); 102 String elementValue = getStringValue(element); 103 104 if (MuleProperties.MULE_CORRELATION_ID_PROPERTY.equals(localName)) 105 { 106 correlationId = elementValue; 107 } 108 else if (MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY.equals(localName)) 109 { 110 correlationGroup = elementValue; 111 } 112 else if (MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY.equals(localName)) 113 { 114 correlationSequence = elementValue; 115 } 116 else if (MuleProperties.MULE_REPLY_TO_PROPERTY.equals(localName)) 117 { 118 replyTo = elementValue; 119 } 120 121 } 122 } 123 } 124 125 private String getStringValue(Element e) 126 { 127 String value = e.getNodeValue(); 128 if (value == null && e.hasChildNodes()) 129 { 130 value = e.getFirstChild().getNodeValue(); 132 if (value != null) 133 { 134 } 136 } 137 return value; 138 } 139 140 146 public void addHeaders(SOAPEnvelope env) throws Exception 147 { 148 SOAPHeader header = env.getHeader(); 149 SOAPHeaderElement muleHeader; 150 if (correlationId != null || replyTo != null) 151 { 152 if (header == null) 153 { 154 header = env.addHeader(); 155 } 156 Name muleHeaderName = env.createName(MULE_HEADER, MULE_NAMESPACE, MULE_10_ACTOR); 157 muleHeader = header.addHeaderElement(muleHeaderName); 158 muleHeader.setActor(MULE_10_ACTOR); 159 } 160 else 161 { 162 return; 163 } 164 165 if (correlationId != null) 166 { 167 SOAPElement e = muleHeader.addChildElement(MuleProperties.MULE_CORRELATION_ID_PROPERTY, 168 MULE_NAMESPACE); 169 e.addTextNode(correlationId); 170 e = muleHeader.addChildElement(MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY, 171 MULE_NAMESPACE); 172 e.addTextNode(correlationGroup); 173 e = muleHeader.addChildElement(MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY, MULE_NAMESPACE); 174 e.addTextNode(correlationSequence); 175 } 176 if (replyTo != null) 177 { 178 SOAPElement e = muleHeader.addChildElement(MuleProperties.MULE_REPLY_TO_PROPERTY, MULE_NAMESPACE); 179 e.addTextNode(replyTo); 182 } 183 } 184 185 public Element createHeaders() throws Exception 186 { 187 Element muleHeader = null; 188 189 if (correlationId != null || replyTo != null) 190 { 191 muleHeader = new DOMElement(new QName(MULE_HEADER, new Namespace(MULE_NAMESPACE, MULE_10_ACTOR))); 192 } 193 else 194 { 195 return null; 196 } 197 198 if (correlationId != null) 199 { 200 Node e = muleHeader.appendChild(new DOMElement(new QName( 201 MuleProperties.MULE_CORRELATION_ID_PROPERTY, new Namespace(MULE_NAMESPACE, MULE_10_ACTOR)))); 202 e.setNodeValue(correlationId); 203 204 e = muleHeader.appendChild(new DOMElement(new QName( 205 MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY, new Namespace(MULE_NAMESPACE, 206 MULE_10_ACTOR)))); 207 e.setNodeValue(correlationGroup); 208 209 e = muleHeader.appendChild(new DOMElement(new QName( 210 MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY, new Namespace(MULE_NAMESPACE, 211 MULE_10_ACTOR)))); 212 e.setNodeValue(correlationSequence); 213 } 214 if (replyTo != null) 215 { 216 217 Node e = muleHeader.appendChild(new DOMElement(new QName(MuleProperties.MULE_REPLY_TO_PROPERTY, 218 new Namespace(MULE_NAMESPACE, MULE_10_ACTOR)))); 219 e.setNodeValue(replyTo); 220 } 221 return muleHeader; 222 } 223 224 public String getReplyTo() 225 { 226 return replyTo; 227 } 228 229 public void setReplyTo(String replyTo) 230 { 231 this.replyTo = replyTo; 232 } 233 234 public String getCorrelationId() 235 { 236 return correlationId; 237 } 238 239 public void setCorrelationId(String correlationId) 240 { 241 this.correlationId = correlationId; 242 } 243 244 public String getCorrelationGroup() 245 { 246 return correlationGroup; 247 } 248 249 public void setCorrelationGroup(String correlationGroup) 250 { 251 this.correlationGroup = correlationGroup; 252 } 253 254 public String getCorrelationSequence() 255 { 256 return correlationSequence; 257 } 258 259 public void setCorrelationSequence(String correlationSequence) 260 { 261 this.correlationSequence = correlationSequence; 262 } 263 } 264 | Popular Tags |