KickJava   Java API By Example, From Geeks To Geeks.

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


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

35
36 /**
37  * Author : Deepal Jayasinghe
38  * Date: May 10, 2005
39  * Time: 12:03:17 PM
40  */

41
42 public class AddressingOutHandler
43         extends AbstractHandler
44         implements AddressingConstants {
45
46     /**
47      * Eran Chinthaka (chinthaka@apache.org)
48      */

49
50     private boolean isAddressingEnabled = true;
51
52     // IN message, if any, has messageId and replyTo and faultTo addresses that needs to be used
53
// in the OUT message. User may sometimes override these values, at his discretion .The following
54
// boolean variable will create room for that.
55
private boolean overrideINMessageInformation = false;
56
57     OMNamespace addressingNamespaceObject;
58     String JavaDoc addressingNamespace;
59
60     public void invoke(MessageContext msgContext) throws AxisFault {
61
62         if (!isAddressingEnabled) {
63             return;
64         }
65
66         // check for a IN message context, else default to WSA Submission
67
if (msgContext.getOperationContext() != null) {
68             MessageContext inMessageContext = msgContext.getOperationContext().getMessageContext(WSDLConstants.MESSAGE_LABEL_IN);
69             if (inMessageContext == null) {
70                 addressingNamespace = Submission.WSA_NAMESPACE; // setting Submission version as the default addressing namespace
71
} else {
72                 addressingNamespace = (String JavaDoc) inMessageContext.getProperty(WS_ADDRESSING_VERSION);
73                 if (addressingNamespace == null) {
74                     addressingNamespace = Submission.WSA_NAMESPACE; // Addressing version has not been set in the IN path
75
}
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 JavaDoc 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 JavaDoc action = messageInformationHeaders.getAction();
108         if (action != null) {
109             processStringInfo(action, WSA_ACTION, soapHeader);
110         }
111
112         epr = messageInformationHeaders.getReplyTo();
113         if (epr != null) {//optional
114
addToSOAPHeader(epr, AddressingConstants.WSA_REPLY_TO, soapHeader);
115         }
116
117         epr = messageInformationHeaders.getFrom();
118         if (epr != null) {//optional
119
addToSOAPHeader(epr, AddressingConstants.WSA_FROM, soapHeader);
120         }
121
122         epr = messageInformationHeaders.getFaultTo();
123         if (epr != null) {//optional
124
addToSOAPHeader(epr, AddressingConstants.WSA_FAULT_TO, soapHeader);
125         }
126
127         String JavaDoc messageID = messageInformationHeaders.getMessageId();
128         if (messageID != null) {//optional
129
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 JavaDoc value,
153                                         String JavaDoc 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 JavaDoc type,
166                                    SOAPHeader soapHeader) {
167         if (epr == null) {
168             return;
169         }
170
171         SOAPHeaderBlock soapHeaderBlock =
172                 soapHeader.addHeaderBlock(type, addressingNamespaceObject);
173
174         String JavaDoc 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 JavaDoc 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 JavaDoc iterator = referenceValues.getKeys();
244             while (iterator.hasNext()) {
245                 QName JavaDoc key = (QName JavaDoc) iterator.next();
246                 String JavaDoc 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