1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ws.addressing.handler;
17
18 import java.io.IOException;
19
20 import javax.xml.rpc.JAXRPCException;
21 import javax.xml.rpc.handler.MessageContext;
22 import javax.xml.rpc.handler.soap.SOAPMessageContext;
23 import javax.xml.soap.SOAPException;
24 import javax.xml.soap.SOAPMessage;
25
26 import org.apache.axis.message.addressing.Action;
27 import org.apache.axis.message.addressing.AddressingHeaders;
28 import org.apache.axis.message.addressing.Constants;
29 import org.apache.axis.message.addressing.EndpointReference;
30 import org.apache.axis.message.addressing.To;
31 import org.apache.axis.message.addressing.util.AddressingUtils;
32 import org.apache.axis.types.URI.MalformedURIException;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36
37
38
39
40
41
42
43
44
45 public class ClientSideAddressingHandler extends AbstractAddressingHandler {
46
47
48
49 private static final Log LOG = LogFactory.getLog(ClientSideAddressingHandler.class);
50
51
52
53
54 private static final String MSG_CONTEXT_PROP_SOAP_ACTION
55 = javax.xml.rpc.Call.SOAPACTION_URI_PROPERTY;
56
57
58
59
60 public boolean handleRequest(MessageContext msgContext) {
61 SOAPMessageContext soapMsgContext = (SOAPMessageContext) msgContext;
62 try {
63 AddressingHeaders headers = (AddressingHeaders) msgContext
64 .getProperty(Constants.ENV_ADDRESSING_REQUEST_HEADERS);
65 if (headers == null) {
66 headers = new AddressingHeaders();
67 }
68 headers.setSetMustUnderstand(isMustUnderstandEnabled(msgContext));
69 addMessageIdHeader(headers);
70 addToHeader(headers, msgContext);
71 addActionHeader(headers, msgContext);
72 addFromHeader(headers, msgContext);
73 addReplyToHeader(headers, msgContext);
74 if (headers.getFaultTo() == null
75 && msgContext.containsProperty(Constants.ENV_ADDRESSING_FAULTTO_URI)) {
76 headers.setFaultTo(new EndpointReference((String) msgContext
77 .getProperty(Constants.ENV_ADDRESSING_FAULTTO_URI)));
78 }
79
80 SOAPMessage msg = soapMsgContext.getMessage();
81 headers.toEnvelope(msg.getSOAPPart().getEnvelope(), getActor());
82 msgContext.setProperty(Constants.ENV_ADDRESSING_REQUEST_HEADERS, headers);
83 } catch (IOException e) {
84 if (LOG.isDebugEnabled()) {
85 LOG.debug("Unexpected error in handleRequest()", e);
86 }
87 throw new JAXRPCException("unexpected error in handleRequest()", e);
88 } catch (SOAPException e) {
89 if (LOG.isDebugEnabled()) {
90 LOG.debug("Unexpected error in handleRequest()", e);
91 }
92 throw new JAXRPCException("unexpected error in handleRequest()", e);
93 }
94 return CONTINUE_HANDLER_CHAIN_PROCESSING;
95 }
96
97
98
99
100 public boolean handleResponse(MessageContext msgContext) {
101 SOAPMessageContext soapMsgContext = (SOAPMessageContext) msgContext;
102 try {
103 SOAPMessage msg = soapMsgContext.getMessage();
104 if (msg == null) {
105 return CONTINUE_HANDLER_CHAIN_PROCESSING;
106 }
107
108 AddressingHeaders headers = new AddressingHeaders(
109 msg.getSOAPPart().getEnvelope(), getActor(), true, isRemoveHeadersEnabled(),
110 false, getReferencePropertyQNames());
111 msgContext.setProperty(Constants.ENV_ADDRESSING_RESPONSE_HEADERS, headers);
112 } catch (Exception e) {
113 if (LOG.isDebugEnabled()) {
114 e.printStackTrace();
115 }
116 throw new JAXRPCException("unexpected error in handleResponse(): " + e, e);
117 }
118 return CONTINUE_HANDLER_CHAIN_PROCESSING;
119 }
120
121
122
123
124 public boolean handleFault(MessageContext messageContext) {
125 return CONTINUE_HANDLER_CHAIN_PROCESSING;
126 }
127
128
129
130
131
132
133
134
135
136 protected String getSOAPAction(MessageContext msgContext) {
137 return (String) msgContext.getProperty(MSG_CONTEXT_PROP_SOAP_ACTION);
138 }
139
140
141
142
143
144
145
146
147 protected void setSOAPAction(MessageContext msgContext, String actionURI) {
148 msgContext.setProperty(MSG_CONTEXT_PROP_SOAP_ACTION, actionURI);
149 }
150
151
152
153
154
155
156
157
158
159
160 protected String getEndpointURL(MessageContext msgContext) {
161 return null;
162 }
163
164
165
166
167
168
169
170
171 private void addReplyToHeader(AddressingHeaders addrHeaders, MessageContext msgContext)
172 throws MalformedURIException {
173 if (isPropertyTrue(msgContext, Constants.ENV_ADDRESSING_SEND_REPLYTO)
174 && addrHeaders.getReplyTo() == null) {
175 if (msgContext.containsProperty(Constants.ENV_ADDRESSING_REPLYTO_URI)) {
176 addrHeaders.setReplyTo(new EndpointReference((String) msgContext
177 .getProperty(Constants.ENV_ADDRESSING_REPLYTO_URI)));
178 } else {
179 addrHeaders.setReplyTo(addrHeaders.getFrom());
180 }
181 }
182 }
183
184
185
186
187
188
189
190
191 private void addFromHeader(AddressingHeaders addrHeaders, MessageContext ctx)
192 throws MalformedURIException {
193 if (addrHeaders.getFrom() == null) {
194 String uri = (String)ctx.getProperty(Constants.ENV_ADDRESSING_FROM_URI);
195 if (uri == null) {
196 uri = AddressingUtils.getAnonymousRoleURI(ctx);
197 }
198 addrHeaders.setFrom(new EndpointReference(uri));
199 }
200 }
201
202
203
204
205
206
207
208 private void addMessageIdHeader(AddressingHeaders addrHeaders)
209 throws MalformedURIException {
210 if (addrHeaders.getMessageID() == null) {
211 addrHeaders.setMessageID(createMessageID());
212 }
213 }
214
215
216
217
218
219
220
221
222 private void addToHeader(AddressingHeaders addrHeaders, MessageContext msgContext)
223 throws MalformedURIException {
224 if (addrHeaders.getTo() == null) {
225 String endpointURL = getEndpointURL(msgContext);
226 addrHeaders.setTo(endpointURL != null ? new To(endpointURL) : null);
227 }
228 }
229
230
231
232
233
234
235
236
237 private void addActionHeader(AddressingHeaders addrHeaders, MessageContext msgContext)
238 throws MalformedURIException {
239 String actionURI = getSOAPAction(msgContext);
240 if (actionURI != null) {
241 addrHeaders.setAction(new Action(actionURI));
242 } else if (addrHeaders.getAction() != null) {
243 setSOAPAction(msgContext, addrHeaders.getAction().toString());
244 }
245 }
246 }