1 package org.apache.axis2.handlers.addressing; 2 3 import org.apache.axis2.addressing.*; 4 import org.apache.axis2.addressing.miheaders.RelatesTo; 5 import org.apache.axis2.context.MessageContext; 6 import org.apache.axis2.engine.AxisFault; 7 import org.apache.axis2.handlers.AbstractHandler; 8 import org.apache.axis2.om.OMAbstractFactory; 9 import org.apache.axis2.om.OMElement; 10 import org.apache.axis2.om.OMNamespace; 11 import org.apache.axis2.soap.SOAPHeader; 12 import org.apache.axis2.soap.SOAPHeaderBlock; 13 import org.apache.wsdl.WSDLConstants; 14 15 import javax.xml.namespace.QName ; 16 import java.util.Iterator ; 17 18 35 36 41 42 public class AddressingOutHandler 43 extends AbstractHandler 44 implements AddressingConstants { 45 46 49 50 private boolean isAddressingEnabled = true; 51 52 private boolean overrideINMessageInformation = false; 56 57 OMNamespace addressingNamespaceObject; 58 String addressingNamespace; 59 60 public void invoke(MessageContext msgContext) throws AxisFault { 61 62 if (!isAddressingEnabled) { 63 return; 64 } 65 66 if (msgContext.getOperationContext() != null) { 68 MessageContext inMessageContext = msgContext.getOperationContext().getMessageContext(WSDLConstants.MESSAGE_LABEL_IN); 69 if (inMessageContext == null) { 70 addressingNamespace = Submission.WSA_NAMESPACE; } else { 72 addressingNamespace = (String ) inMessageContext.getProperty(WS_ADDRESSING_VERSION); 73 if (addressingNamespace == null) { 74 addressingNamespace = Submission.WSA_NAMESPACE; } 76 } 77 } 78 79 if (addressingNamespace == null || "".equals(addressingNamespace)) { 80 addressingNamespace = Submission.WSA_NAMESPACE; 81 } 82 addressingNamespaceObject = OMAbstractFactory.getOMFactory().createOMNamespace(addressingNamespace, WSA_DEFAULT_PRFIX); 83 84 85 MessageInformationHeadersCollection messageInformationHeaders = 86 msgContext.getMessageInformationHeaders(); 87 SOAPHeader soapHeader = msgContext.getEnvelope().getHeader(); 88 89 90 EndpointReference epr = messageInformationHeaders.getTo(); 91 if (epr != null) { 92 93 String address = epr.getAddress(); 94 if (!"".equals(address) && address != null) { 95 SOAPHeaderBlock toHeaderBlock = soapHeader.addHeaderBlock(WSA_TO, addressingNamespaceObject); 96 toHeaderBlock.setText(address); 97 } 98 99 AnyContentType referenceParameters = epr.getReferenceParameters(); 100 if (referenceParameters != null) { 101 processAnyContentType(referenceParameters, soapHeader); 102 } 103 104 addToHeader(epr, soapHeader); 105 } 106 107 String action = messageInformationHeaders.getAction(); 108 if (action != null) { 109 processStringInfo(action, WSA_ACTION, soapHeader); 110 } 111 112 epr = messageInformationHeaders.getReplyTo(); 113 if (epr != null) { addToSOAPHeader(epr, AddressingConstants.WSA_REPLY_TO, soapHeader); 115 } 116 117 epr = messageInformationHeaders.getFrom(); 118 if (epr != null) { addToSOAPHeader(epr, AddressingConstants.WSA_FROM, soapHeader); 120 } 121 122 epr = messageInformationHeaders.getFaultTo(); 123 if (epr != null) { addToSOAPHeader(epr, AddressingConstants.WSA_FAULT_TO, soapHeader); 125 } 126 127 String messageID = messageInformationHeaders.getMessageId(); 128 if (messageID != null) { processStringInfo(messageID, WSA_MESSAGE_ID, soapHeader); 130 } 131 132 RelatesTo relatesTo = messageInformationHeaders.getRelatesTo(); 133 OMElement relatesToHeader = null; 134 135 if (relatesTo != null) { 136 relatesToHeader = processStringInfo(relatesTo.getValue(), WSA_RELATES_TO, soapHeader); 137 } 138 139 if (relatesToHeader != null) 140 if ("".equals(relatesTo.getRelationshipType())) { 141 relatesToHeader.addAttribute(WSA_RELATES_TO_RELATIONSHIP_TYPE, 142 Submission.WSA_RELATES_TO_RELATIONSHIP_TYPE_DEFAULT_VALUE, 143 addressingNamespaceObject); 144 } else { 145 relatesToHeader.addAttribute(WSA_RELATES_TO_RELATIONSHIP_TYPE, 146 relatesTo.getRelationshipType(), 147 addressingNamespaceObject); 148 } 149 } 150 151 152 private OMElement processStringInfo(String value, 153 String type, 154 SOAPHeader soapHeader) { 155 if (!"".equals(value) && value != null) { 156 SOAPHeaderBlock soapHeaderBlock = 157 soapHeader.addHeaderBlock(type, addressingNamespaceObject); 158 soapHeaderBlock.addChild(OMAbstractFactory.getOMFactory().createText(value)); 159 return soapHeaderBlock; 160 } 161 return null; 162 } 163 164 protected void addToSOAPHeader(EndpointReference epr, 165 String type, 166 SOAPHeader soapHeader) { 167 if (epr == null) { 168 return; 169 } 170 171 SOAPHeaderBlock soapHeaderBlock = 172 soapHeader.addHeaderBlock(type, addressingNamespaceObject); 173 174 String address = epr.getAddress(); 175 if (!"".equals(address) && address != null) { 176 OMElement addressElement = 177 OMAbstractFactory.getOMFactory().createOMElement(EPR_ADDRESS, 178 addressingNamespaceObject); 179 soapHeaderBlock.addChild(addressElement); 180 addressElement.setText(address); 181 } 182 183 addToHeader(epr, soapHeaderBlock); 184 185 186 AnyContentType referenceParameters = epr.getReferenceParameters(); 187 if (referenceParameters != null) { 188 OMElement reference = 189 OMAbstractFactory.getOMFactory().createOMElement(EPR_REFERENCE_PARAMETERS, addressingNamespaceObject); 190 soapHeaderBlock.addChild(reference); 191 processAnyContentType(referenceParameters, reference); 192 193 } 194 195 if (Submission.WSA_NAMESPACE.equals(addressingNamespace)) { 196 AnyContentType referenceProperties = epr.getReferenceProperties(); 197 if (referenceProperties != null) { 198 OMElement reference = 199 OMAbstractFactory.getOMFactory().createOMElement(Submission.EPR_REFERENCE_PROPERTIES, addressingNamespaceObject); 200 soapHeader.addChild(reference); 201 processAnyContentType(referenceParameters, reference); 202 } 203 204 } 205 206 } 207 208 private void addToHeader(EndpointReference epr, OMElement parentElement) { 209 210 211 QName interfaceQName = epr.getInterfaceName(); 212 if (interfaceQName != null) { 213 OMElement interfaceName = 214 OMAbstractFactory.getOMFactory().createOMElement(addressingNamespace.equals(Submission.WSA_NAMESPACE) ? Submission.EPR_PORT_TYPE : Final.WSA_INTERFACE_NAME, addressingNamespaceObject); 215 interfaceName.addChild(OMAbstractFactory.getOMFactory().createText(interfaceQName.getPrefix() + ":" + interfaceQName.getLocalPart())); 216 parentElement.addChild(interfaceName); 217 } 218 219 ServiceName serviceName = epr.getServiceName(); 220 if (serviceName != null) { 221 OMElement serviceNameElement = 222 OMAbstractFactory.getOMFactory().createOMElement(EPR_SERVICE_NAME, 223 addressingNamespaceObject); 224 serviceNameElement.addAttribute(addressingNamespace.equals(Submission.WSA_NAMESPACE) ? Submission.EPR_SERVICE_NAME_PORT_NAME : Final.WSA_SERVICE_NAME_ENDPOINT_NAME, 225 serviceName.getEndpointName(), 226 addressingNamespaceObject); 227 serviceNameElement.addChild(OMAbstractFactory.getOMFactory().createText(serviceName.getName().getPrefix() 228 + ":" 229 + serviceName.getName().getLocalPart())); 230 parentElement.addChild(serviceNameElement); 231 } 232 233 234 } 235 236 237 private void processAnyContentType 238 (AnyContentType 239 referenceValues, 240 OMElement 241 parentElement) { 242 if (referenceValues != null) { 243 Iterator iterator = referenceValues.getKeys(); 244 while (iterator.hasNext()) { 245 QName key = (QName ) iterator.next(); 246 String value = referenceValues.getReferenceValue(key); 247 OMElement omElement = 248 OMAbstractFactory.getOMFactory().createOMElement(key, parentElement); 249 if (Final.WSA_NAMESPACE.equals(addressingNamespace)) { 250 omElement.addAttribute(Final.WSA_IS_REFERENCE_PARAMETER_ATTRIBUTE, Final.WSA_TYPE_ATTRIBUTE_VALUE, addressingNamespaceObject); 251 252 } 253 omElement.setText(value); 254 } 255 } 256 } 257 } 258 259 | Popular Tags |