1 package org.apache.axis2.handlers.addressing; 2 3 import org.apache.axis2.addressing.AddressingConstants; 4 import org.apache.axis2.addressing.EndpointReference; 5 import org.apache.axis2.addressing.MessageInformationHeadersCollection; 6 import org.apache.axis2.addressing.miheaders.RelatesTo; 7 import org.apache.axis2.context.MessageContext; 8 import org.apache.axis2.engine.AxisFault; 9 import org.apache.axis2.handlers.AbstractHandler; 10 import org.apache.axis2.handlers.addressing.AddressingException; 11 import org.apache.axis2.om.OMAttribute; 12 import org.apache.axis2.om.OMElement; 13 import org.apache.axis2.soap.SOAPHeader; 14 import org.apache.axis2.soap.SOAPHeaderBlock; 15 import org.apache.commons.logging.Log; 16 import org.apache.commons.logging.LogFactory; 17 18 import javax.xml.namespace.QName ; 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 22 39 40 45 public class AddressingInHandler extends AbstractHandler implements AddressingConstants { 46 49 50 private boolean isAddressingOptional = true; 52 private String addressingNamespace = null; 53 54 private Log logger = LogFactory.getLog(getClass()); 55 56 57 public void invoke(MessageContext msgContext) throws AxisFault { 58 logger.debug("Starting Addressing IN Handler ........."); 59 SOAPHeader header = msgContext.getEnvelope().getHeader(); 60 if (header == null) { 61 return; 62 } 63 64 ArrayList addressingHeaders = null; 65 try { 66 addressingHeaders = header.getHeaderBolcksWithNSURI(Submission.WSA_NAMESPACE); 67 if (addressingHeaders != null) { 68 addressingNamespace = Submission.WSA_NAMESPACE; 69 extractAddressingSubmissionInformationFromHeaders(header, msgContext.getMessageInformationHeaders(), addressingHeaders); 70 } else { 71 addressingHeaders = header.getHeaderBolcksWithNSURI(Final.WSA_NAMESPACE); 72 if (addressingHeaders != null) { 73 addressingNamespace = Final.WSA_NAMESPACE; 74 extractAddressingFinalInformationFromHeaders(header, msgContext.getMessageInformationHeaders(), addressingHeaders); 75 extractReferenceParameters(header, msgContext.getMessageInformationHeaders()); 76 77 } else { 78 if (!isAddressingOptional) { 80 throw new AxisFault("Addressing Handlers should present, but doesn't present in the incoming message !!"); 81 } 82 logger.debug("No Addressing Headers present in the IN message. Addressing In Handler does nothing."); 83 } 84 } 85 msgContext.setProperty(WS_ADDRESSING_VERSION, addressingNamespace, true); 86 } catch (AddressingException e) { 87 logger.info("Exception occurred in Addressing Module"); 88 throw new AxisFault(e); 89 } 90 91 } 92 93 100 private void extractReferenceParameters(SOAPHeader header, MessageInformationHeadersCollection messageInformationHeaders) { 101 Iterator headerBlocks = header.getChildren(); 102 while (headerBlocks.hasNext()) { 103 SOAPHeaderBlock soapHeaderBlock = (SOAPHeaderBlock) headerBlocks.next(); 104 if (Final.WSA_TYPE_ATTRIBUTE_VALUE.equals(soapHeaderBlock.getFirstAttribute(new QName (Final.WSA_NAMESPACE, Final.WSA_IS_REFERENCE_PARAMETER_ATTRIBUTE)).getValue())) { 105 messageInformationHeaders.addReferenceParameter(soapHeaderBlock); 106 } 107 } 108 } 109 110 private void extractAddressingFinalInformationFromHeaders(SOAPHeader header, MessageInformationHeadersCollection messageInformationHeaders, ArrayList addressingHeaders) throws AddressingException { 111 extractCommonAddressingParameters(header, messageInformationHeaders, addressingHeaders, Final.WSA_NAMESPACE); 112 113 } 114 115 private void extractAddressingSubmissionInformationFromHeaders(SOAPHeader header, MessageInformationHeadersCollection messageInformationHeaders, ArrayList addressingHeaders) throws AddressingException { 116 extractCommonAddressingParameters(header, messageInformationHeaders, addressingHeaders, Submission.WSA_NAMESPACE); 117 118 } 119 120 public MessageInformationHeadersCollection extractCommonAddressingParameters(SOAPHeader header, MessageInformationHeadersCollection messageInformationHeadersCollection, ArrayList addressingHeaders, String addressingNamespace) { 121 if (messageInformationHeadersCollection == null) { 122 messageInformationHeadersCollection = new MessageInformationHeadersCollection(); 123 } 124 125 Iterator addressingHeadersIt = addressingHeaders.iterator(); 126 while (addressingHeadersIt.hasNext()) { 127 SOAPHeaderBlock soapHeaderBlock = (SOAPHeaderBlock) addressingHeadersIt.next(); 128 EndpointReference epr = null; 129 if (AddressingConstants.WSA_TO.equals(soapHeaderBlock.getLocalName())) { 130 epr = messageInformationHeadersCollection.getTo(); 131 if (epr == null) { 132 epr = new EndpointReference(AddressingConstants.WSA_TO, soapHeaderBlock.getText()); 133 messageInformationHeadersCollection.setTo(epr); 134 } 135 } else if (AddressingConstants.WSA_FROM.equals(soapHeaderBlock.getLocalName())) { 136 epr = messageInformationHeadersCollection.getFrom(); 137 if (epr == null) { 138 epr = new EndpointReference(AddressingConstants.WSA_FROM, ""); 139 messageInformationHeadersCollection.setFrom(epr); 140 } 141 extractEPRAddressInformation(soapHeaderBlock, epr, addressingNamespace); 142 } else if (AddressingConstants.WSA_REPLY_TO.equals(soapHeaderBlock.getLocalName())) { 143 epr = messageInformationHeadersCollection.getReplyTo(); 144 if (epr == null) { 145 epr = new EndpointReference(AddressingConstants.WSA_REPLY_TO, ""); 146 messageInformationHeadersCollection.setReplyTo(epr); 147 } 148 extractEPRAddressInformation(soapHeaderBlock, epr, addressingNamespace); 149 } else if (AddressingConstants.WSA_FAULT_TO.equals(soapHeaderBlock.getLocalName())) { 150 epr = messageInformationHeadersCollection.getFaultTo(); 151 if (epr == null) { 152 epr = new EndpointReference(AddressingConstants.WSA_FAULT_TO, ""); 153 messageInformationHeadersCollection.setFaultTo(epr); 154 } 155 extractEPRAddressInformation(soapHeaderBlock, epr, addressingNamespace); 156 } else if (AddressingConstants.WSA_MESSAGE_ID.equals(soapHeaderBlock.getLocalName())) { 157 messageInformationHeadersCollection.setMessageId(soapHeaderBlock.getText()); 158 } else if (AddressingConstants.WSA_ACTION.equals(soapHeaderBlock.getLocalName())) { 159 messageInformationHeadersCollection.setAction(soapHeaderBlock.getText()); 160 } else if (AddressingConstants.WSA_RELATES_TO.equals(soapHeaderBlock.getLocalName())) { 161 String address = soapHeaderBlock.getText(); 162 OMAttribute relationshipType = soapHeaderBlock.getFirstAttribute(new QName (AddressingConstants.WSA_RELATES_TO_RELATIONSHIP_TYPE)); 163 String relationshipTypeDefaultValue = Submission.WSA_NAMESPACE.equals(addressingNamespace) ? Submission.WSA_RELATES_TO_RELATIONSHIP_TYPE_DEFAULT_VALUE : Final.WSA_RELATES_TO_RELATIONSHIP_TYPE_DEFAULT_VALUE; 164 RelatesTo relatesTo = new RelatesTo(address, relationshipType == null ? relationshipTypeDefaultValue : relationshipType.getValue()); 165 messageInformationHeadersCollection.setRelatesTo(relatesTo); 166 } 167 } 168 169 return messageInformationHeadersCollection; 170 } 171 172 173 private void extractEPRAddressInformation(SOAPHeaderBlock headerBlock, EndpointReference epr, String addressingNamespace) { 174 OMElement address = headerBlock.getFirstChildWithName(new QName (addressingNamespace, AddressingConstants.EPR_ADDRESS)); 175 if (address != null) { 176 epr.setAddress(address.getText()); 177 } 178 179 180 } 181 } 182 183 | Popular Tags |