KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > handlers > addressing > AddressingInHandler


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 JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21
22 /*
23  * Copyright 2004,2005 The Apache Software Foundation.
24  *
25  * Licensed under the Apache License, Version 2.0 (the "License");
26  * you may not use this file except in compliance with the License.
27  * You may obtain a copy of the License at
28  *
29  * http://www.apache.org/licenses/LICENSE-2.0
30  *
31  * Unless required by applicable law or agreed to in writing, software
32  * distributed under the License is distributed on an "AS IS" BASIS,
33  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34  * See the License for the specific language governing permissions and
35  * limitations under the License.
36  *
37  *
38  */

39
40 /**
41  * Author : Deepal Jayasinghe
42  * Date: May 10, 2005
43  * Time: 11:53:20 AM
44  */

45 public class AddressingInHandler extends AbstractHandler implements AddressingConstants {
46     /**
47      * Eran Chinthaka (chinthaka@apache.org) Date : 03-04-2005 Time : 14:42
48      */

49
50     // this parameter has to be set by the module deployer.
51
private boolean isAddressingOptional = true;
52     private String JavaDoc 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 JavaDoc 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                     // Addressing headers are not present in the SOAP message
79
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     /**
94      * WSA 1.0 specification mandates all the reference parameters to have a attribute as wsa:Type=�parameter�. So
95      * here this will check for header blocks with the above attribute and will put them in message information header collection
96      *
97      * @param header
98      * @param messageInformationHeaders
99      */

100     private void extractReferenceParameters(SOAPHeader header, MessageInformationHeadersCollection messageInformationHeaders) {
101         Iterator JavaDoc 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 JavaDoc(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 JavaDoc addressingHeaders) throws AddressingException {
111         extractCommonAddressingParameters(header, messageInformationHeaders, addressingHeaders, Final.WSA_NAMESPACE);
112
113     }
114
115     private void extractAddressingSubmissionInformationFromHeaders(SOAPHeader header, MessageInformationHeadersCollection messageInformationHeaders, ArrayList JavaDoc addressingHeaders) throws AddressingException {
116         extractCommonAddressingParameters(header, messageInformationHeaders, addressingHeaders, Submission.WSA_NAMESPACE);
117
118     }
119
120     public MessageInformationHeadersCollection extractCommonAddressingParameters(SOAPHeader header, MessageInformationHeadersCollection messageInformationHeadersCollection, ArrayList JavaDoc addressingHeaders, String JavaDoc addressingNamespace) {
121         if (messageInformationHeadersCollection == null) {
122             messageInformationHeadersCollection = new MessageInformationHeadersCollection();
123         }
124
125         Iterator JavaDoc 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 JavaDoc address = soapHeaderBlock.getText();
162                 OMAttribute relationshipType = soapHeaderBlock.getFirstAttribute(new QName JavaDoc(AddressingConstants.WSA_RELATES_TO_RELATIONSHIP_TYPE));
163                 String JavaDoc 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 JavaDoc addressingNamespace) {
174         OMElement address = headerBlock.getFirstChildWithName(new QName JavaDoc(addressingNamespace, AddressingConstants.EPR_ADDRESS));
175         if (address != null) {
176             epr.setAddress(address.getText());
177         }
178
179
180     }
181 }
182
183
Popular Tags