|
1 /* |
|
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.xml.internal.ws.client.dispatch; |
|
27 |
|
28 import com.sun.istack.internal.NotNull; |
|
29 import com.sun.istack.internal.Nullable; |
|
30 import com.sun.xml.internal.ws.api.BindingID; |
|
31 import com.sun.xml.internal.ws.api.SOAPVersion; |
|
32 import com.sun.xml.internal.ws.api.WSBinding; |
|
33 import com.sun.xml.internal.ws.api.addressing.AddressingVersion; |
|
34 import com.sun.xml.internal.ws.api.addressing.WSEndpointReference; |
|
35 import com.sun.xml.internal.ws.api.client.WSPortInfo; |
|
36 import com.sun.xml.internal.ws.api.message.Attachment; |
|
37 import com.sun.xml.internal.ws.api.message.AttachmentSet; |
|
38 import com.sun.xml.internal.ws.api.message.Message; |
|
39 import com.sun.xml.internal.ws.api.message.Packet; |
|
40 import com.sun.xml.internal.ws.api.pipe.Fiber; |
|
41 import com.sun.xml.internal.ws.api.pipe.Tube; |
|
42 import com.sun.xml.internal.ws.api.pipe.FiberContextSwitchInterceptor; |
|
43 import com.sun.xml.internal.ws.binding.BindingImpl; |
|
44 import com.sun.xml.internal.ws.client.*; |
|
45 import com.sun.xml.internal.ws.encoding.soap.DeserializationException; |
|
46 import com.sun.xml.internal.ws.fault.SOAPFaultBuilder; |
|
47 import com.sun.xml.internal.ws.message.AttachmentSetImpl; |
|
48 import com.sun.xml.internal.ws.message.DataHandlerAttachment; |
|
49 import com.sun.xml.internal.ws.resources.DispatchMessages; |
|
50 |
|
51 import javax.activation.DataHandler; |
|
52 import javax.xml.bind.JAXBException; |
|
53 import javax.xml.namespace.QName; |
|
54 import javax.xml.transform.Source; |
|
55 import javax.xml.ws.AsyncHandler; |
|
56 import javax.xml.ws.BindingProvider; |
|
57 import javax.xml.ws.Dispatch; |
|
58 import javax.xml.ws.Response; |
|
59 import javax.xml.ws.Service; |
|
60 import javax.xml.ws.Service.Mode; |
|
61 import javax.xml.ws.WebServiceException; |
|
62 import javax.xml.ws.handler.MessageContext; |
|
63 import javax.xml.ws.http.HTTPBinding; |
|
64 import javax.xml.ws.soap.SOAPBinding; |
|
65 import javax.xml.ws.soap.SOAPFaultException; |
|
66 import javax.xml.ws.WebServiceClient; |
|
67 import javax.xml.ws.WebEndpoint; |
|
68 import java.net.MalformedURLException; |
|
69 import java.net.URI; |
|
70 import java.net.URISyntaxException; |
|
71 import java.net.URL; |
|
72 import java.util.ArrayList; |
|
73 import java.util.HashMap; |
|
74 import java.util.List; |
|
75 import java.util.Map; |
|
76 import java.util.concurrent.Callable; |
|
77 import java.util.concurrent.Executor; |
|
78 import java.util.concurrent.ExecutorService; |
|
79 import java.util.concurrent.Future; |
|
80 import java.util.concurrent.TimeUnit; |
|
81 import java.lang.reflect.Method; |
|
82 import java.util.logging.Level; |
|
83 import java.util.logging.Logger; |
|
84 |
|
85 |
|
86 /** |
|
87 * The <code>DispatchImpl</code> abstract class provides support |
|
88 * for the dynamic invocation of a service endpoint operation using XML |
|
89 * constructs, JAXB objects or <code>SOAPMessage</code>. The <code>javax.xml.ws.Service</code> |
|
90 * interface acts as a factory for the creation of <code>DispatchImpl</code> |
|
91 * instances. |
|
92 * |
|
93 * @author WS Development Team |
|
94 * @version 1.0 |
|
95 */ |
|
96 public abstract class DispatchImpl<T> extends Stub implements Dispatch<T> { |
|
97 |
|
98 private static final Logger LOGGER = Logger.getLogger(DispatchImpl.class.getName()); |
|
99 |
|
100 final Service.Mode mode; |
|
101 final SOAPVersion soapVersion; |
|
102 final boolean allowFaultResponseMsg; |
|
103 static final long AWAIT_TERMINATION_TIME = 800L; |
|
104 |
|
105 /** |
|
106 * |
|
107 * @param port dispatch instance is associated with this wsdl port qName |
|
108 * @param mode Service.mode associated with this Dispatch instance - Service.mode.MESSAGE or Service.mode.PAYLOAD |
|
109 * @param owner Service that created the Dispatch |
|
110 * @param pipe Master pipe for the pipeline |
|
111 * @param binding Binding of this Dispatch instance, current one of SOAP/HTTP or XML/HTTP |
|
112 */ |
|
113 @Deprecated |
|
114 protected DispatchImpl(QName port, Service.Mode mode, WSServiceDelegate owner, Tube pipe, BindingImpl binding, @Nullable WSEndpointReference epr) { |
|
115 super(port, owner, pipe, binding, (owner.getWsdlService() != null)? owner.getWsdlService().get(port) : null , owner.getEndpointAddress(port), epr); |
|
116 this.mode = mode; |
|
117 this.soapVersion = binding.getSOAPVersion(); |
|
118 this.allowFaultResponseMsg = false; |
|
119 } |
|
120 |
|
121 /** |
|
122 * @param portInfo dispatch instance is associated with this portInfo |
|
123 * @param mode Service.mode associated with this Dispatch instance - Service.mode.MESSAGE or Service.mode.PAYLOAD |
|
124 * @param binding Binding of this Dispatch instance, current one of SOAP/HTTP or XML/HTTP |
|
125 */ |
|
126 protected DispatchImpl(WSPortInfo portInfo, Service.Mode mode, BindingImpl binding, @Nullable WSEndpointReference epr) { |
|
127 this(portInfo, mode, binding, epr, false); |
|
128 } |
|
129 |
|
130 /** |
|
131 * @param portInfo dispatch instance is associated with this portInfo |
|
132 * @param mode Service.mode associated with this Dispatch instance - Service.mode.MESSAGE or Service.mode.PAYLOAD |
|
133 * @param binding Binding of this Dispatch instance, current one of SOAP/HTTP or XML/HTTP |
|
134 * @param allowFaultResponseMsg A packet containing a SOAP fault message is allowed as the response to a request on this dispatch instance. |
|
135 */ |
|
136 protected DispatchImpl(WSPortInfo portInfo, Service.Mode mode, BindingImpl binding, @Nullable WSEndpointReference epr, boolean allowFaultResponseMsg) { |
|
137 this(portInfo, mode, binding, null, epr, allowFaultResponseMsg); |
|
138 } |
|
139 |
|
140 /** |
|
141 * @param portInfo dispatch instance is associated with this portInfo |
|
142 * @param mode Service.mode associated with this Dispatch instance - Service.mode.MESSAGE or Service.mode.PAYLOAD |
|
143 * @param binding Binding of this Dispatch instance, current one of SOAP/HTTP or XML/HTTP |
|
144 * @param pipe Master pipe for the pipeline |
|
145 * @param allowFaultResponseMsg A packet containing a SOAP fault message is allowed as the response to a request on this dispatch instance. |
|
146 */ |
|
147 protected DispatchImpl(WSPortInfo portInfo, Service.Mode mode, BindingImpl binding, Tube pipe, @Nullable WSEndpointReference epr, boolean allowFaultResponseMsg) { |
|
148 super(portInfo, binding, pipe, portInfo.getEndpointAddress(), epr); |
|
149 this.mode = mode; |
|
150 this.soapVersion = binding.getSOAPVersion(); |
|
151 this.allowFaultResponseMsg = allowFaultResponseMsg; |
|
152 } |
|
153 /** |
|
154 * |
|
155 * @param port dispatch instance is associated with this wsdl port qName |
|
156 * @param mode Service.mode associated with this Dispatch instance - Service.mode.MESSAGE or Service.mode.PAYLOAD |
|
157 * @param owner Service that created the Dispatch |
|
158 * @param pipe Master pipe for the pipeline |
|
159 * @param binding Binding of this Dispatch instance, current one of SOAP/HTTP or XML/HTTP |
|
160 * @param allowFaultResponseMsg A packet containing a SOAP fault message is allowed as the response to a request on this dispatch instance. |
|
161 */ |
|
162 protected DispatchImpl(WSPortInfo portInfo, Service.Mode mode, Tube pipe, BindingImpl binding, @Nullable WSEndpointReference epr, boolean allowFaultResponseMsg) { |
|
163 super(portInfo, binding, pipe, portInfo.getEndpointAddress(), epr); |
|
164 this.mode = mode; |
|
165 this.soapVersion = binding.getSOAPVersion(); |
|
166 this.allowFaultResponseMsg = allowFaultResponseMsg; |
|
167 } |
|
168 |
|
169 /** |
|
170 * Abstract method that is implemented by each concrete Dispatch class |
|
171 * @param msg message passed in from the client program on the invocation |
|
172 * @return The Message created returned as the Interface in actuallity a |
|
173 * concrete Message Type |
|
174 */ |
|
175 abstract Packet createPacket(T msg); |
|
176 |
|
177 /** |
|
178 * Obtains the value to return from the response message. |
|
179 */ |
|
180 abstract T toReturnValue(Packet response); |
|
181 |
|
182 public final Response<T> invokeAsync(T param) { |
|
183 if (LOGGER.isLoggable(Level.FINE)) { |
|
184 dumpParam(param, "invokeAsync(T)"); |
|
185 } |
|
186 AsyncInvoker invoker = new DispatchAsyncInvoker(param); |
|
187 AsyncResponseImpl<T> ft = new AsyncResponseImpl<T>(invoker,null); |
|
188 invoker.setReceiver(ft); |
|
189 ft.run(); |
|
190 return ft; |
|
191 } |
|
192 |
|
193 private void dumpParam(T param, String method) { |
|
194 if (param instanceof Packet) { |
|
195 Packet message = (Packet)param; |
|
196 |
|
197 String action; |
|
198 String msgId; |
|
199 if (LOGGER.isLoggable(Level.FINE)) { |
|
200 AddressingVersion av = DispatchImpl.this.getBinding().getAddressingVersion(); |
|
201 SOAPVersion sv = DispatchImpl.this.getBinding().getSOAPVersion(); |
|
202 action = |
|
203 av != null && message.getMessage() != null ? |
|
204 message.getMessage().getHeaders().getAction(av, sv) : null; |
|
205 msgId = |
|
206 av != null && message.getMessage() != null ? |
|
207 message.getMessage().getHeaders().getMessageID(av, sv) : null; |
|
208 LOGGER.fine("In DispatchImpl." + method + " for message with action: " + action + " and msg ID: " + msgId + " msg: " + message.getMessage()); |
|
209 |
|
210 if (message.getMessage() == null) { |
|
211 LOGGER.fine("Dispatching null message for action: " + action + " and msg ID: " + msgId); |
|
212 } |
|
213 } |
|
214 } |
|
215 } |
|
216 public final Future<?> invokeAsync(T param, AsyncHandler<T> asyncHandler) { |
|
217 if (LOGGER.isLoggable(Level.FINE)) { |
|
218 dumpParam(param, "invokeAsync(T, AsyncHandler<T>)"); |
|
219 } |
|
220 AsyncInvoker invoker = new DispatchAsyncInvoker(param); |
|
221 AsyncResponseImpl<T> ft = new AsyncResponseImpl<T>(invoker,asyncHandler); |
|
222 invoker.setReceiver(ft); |
|
223 invoker.setNonNullAsyncHandlerGiven(asyncHandler != null); |
|
224 |
|
225 ft.run(); |
|
226 return ft; |
|
227 } |
|
228 |
|
229 /** |
|
230 * Synchronously invokes a service. |
|
231 * |
|
232 * See {@link #process(Packet, RequestContext, ResponseContextReceiver)} on |
|
233 * why it takes a {@link RequestContext} and {@link ResponseContextReceiver} as a parameter. |
|
234 */ |
|
235 public final T doInvoke(T in, RequestContext rc, ResponseContextReceiver receiver){ |
|
236 Packet response = null; |
|
237 try { |
|
238 try { |
|
239 checkNullAllowed(in, rc, binding, mode); |
|
240 |
|
241 Packet message = createPacket(in); |
|
242 resolveEndpointAddress(message, rc); |
|
243 setProperties(message,true); |
|
244 response = process(message,rc,receiver); |
|
245 Message msg = response.getMessage(); |
|
246 |
|
247 // REVIEW: eliminate allowFaultResponseMsg, but make that behavior default for MessageDispatch, PacketDispatch |
|
248 if(msg != null && msg.isFault() && |
|
249 !allowFaultResponseMsg) { |
|
250 SOAPFaultBuilder faultBuilder = SOAPFaultBuilder.create(msg); |
|
251 // passing null means there is no checked excpetion we're looking for all |
|
252 // it will get back to us is a protocol exception |
|
253 throw (SOAPFaultException)faultBuilder.createException(null); |
|
254 } |
|
255 } catch (JAXBException e) { |
|
256 //TODO: i18nify |
|
257 throw new DeserializationException(DispatchMessages.INVALID_RESPONSE_DESERIALIZATION(),e); |
|
258 } catch(WebServiceException e){ |
|
259 //it could be a WebServiceException or a ProtocolException |
|
260 throw e; |
|
261 } catch(Throwable e){ |
|
262 // it could be a RuntimeException resulting due to some internal bug or |
|
263 // its some other exception resulting from user error, wrap it in |
|
264 // WebServiceException |
|
265 throw new WebServiceException(e); |
|
266 } |
|
267 |
|
268 return toReturnValue(response); |
|
269 } finally { |
|
270 // REVIEW: Move to AsyncTransportProvider |
|
271 if (response != null && response.transportBackChannel != null) |
|
272 response.transportBackChannel.close(); |
|
273 } |
|
274 } |
|
275 |
|
276 public final T invoke(T in) { |
|
277 if (LOGGER.isLoggable(Level.FINE)) { |
|
278 dumpParam(in, "invoke(T)"); |
|
279 } |
|
280 |
|
281 return doInvoke(in,requestContext,this); |
|
282 } |
|
283 |
|
284 public final void invokeOneWay(T in) { |
|
285 if (LOGGER.isLoggable(Level.FINE)) { |
|
286 dumpParam(in, "invokeOneWay(T)"); |
|
287 } |
|
288 |
|
289 try { |
|
290 checkNullAllowed(in, requestContext, binding, mode); |
|
291 |
|
292 Packet request = createPacket(in); |
|
293 setProperties(request,false); |
|
294 Packet response = process(request,requestContext,this); |
|
295 } catch(WebServiceException e){ |
|
296 //it could be a WebServiceException or a ProtocolException |
|
297 throw e; |
|
298 } catch(Throwable e){ |
|
299 // it could be a RuntimeException resulting due to some internal bug or |
|
300 // its some other exception resulting from user error, wrap it in |
|
301 // WebServiceException |
|
302 throw new WebServiceException(e); |
|
303 } |
|
304 } |
|
305 |
|
306 void setProperties(Packet packet, boolean expectReply) { |
|
307 packet.expectReply = expectReply; |
|
308 } |
|
309 |
|
310 static boolean isXMLHttp(@NotNull WSBinding binding) { |
|
311 return binding.getBindingId().equals(BindingID.XML_HTTP); |
|
312 } |
|
313 |
|
314 static boolean isPAYLOADMode(@NotNull Service.Mode mode) { |
|
315 return mode == Service.Mode.PAYLOAD; |
|
316 } |
|
317 |
|
318 static void checkNullAllowed(@Nullable Object in, RequestContext rc, WSBinding binding, Service.Mode mode) { |
|
319 |
|
320 if (in != null) |
|
321 return; |
|
322 |
|
323 //With HTTP Binding a null invocation parameter can not be used |
|
324 //with HTTP Request Method == POST |
|
325 if (isXMLHttp(binding)){ |
|
326 if (methodNotOk(rc)) |
|
327 throw new WebServiceException(DispatchMessages.INVALID_NULLARG_XMLHTTP_REQUEST_METHOD(HTTP_REQUEST_METHOD_POST, HTTP_REQUEST_METHOD_GET)); |
|
328 } else { //soapBinding |
|
329 if (mode == Service.Mode.MESSAGE ) |
|
330 throw new WebServiceException(DispatchMessages.INVALID_NULLARG_SOAP_MSGMODE(mode.name(), Service.Mode.PAYLOAD.toString())); |
|
331 } |
|
332 } |
|
333 |
|
334 static boolean methodNotOk(@NotNull RequestContext rc) { |
|
335 String requestMethod = (String)rc.get(MessageContext.HTTP_REQUEST_METHOD); |
|
336 String request = (requestMethod == null)? HTTP_REQUEST_METHOD_POST: requestMethod; |
|
337 // if method == post or put with a null invocation parameter in xml/http binding this is not ok |
|
338 return HTTP_REQUEST_METHOD_POST.equalsIgnoreCase(request) || HTTP_REQUEST_METHOD_PUT.equalsIgnoreCase(request); |
|
339 } |
|
340 |
|
341 public static void checkValidSOAPMessageDispatch(WSBinding binding, Service.Mode mode) { |
|
342 // Dispatch<SOAPMessage> is only valid for soap binding and in Service.Mode.MESSAGE |
|
343 if (DispatchImpl.isXMLHttp(binding)) |
|
344 throw new WebServiceException(DispatchMessages.INVALID_SOAPMESSAGE_DISPATCH_BINDING(HTTPBinding.HTTP_BINDING, SOAPBinding.SOAP11HTTP_BINDING + " or " + SOAPBinding.SOAP12HTTP_BINDING)); |
|
345 if (DispatchImpl.isPAYLOADMode(mode)) |
|
346 throw new WebServiceException(DispatchMessages.INVALID_SOAPMESSAGE_DISPATCH_MSGMODE(mode.name(), Service.Mode.MESSAGE.toString())); |
|
347 } |
|
348 |
|
349 public static void checkValidDataSourceDispatch(WSBinding binding, Service.Mode mode) { |
|
350 // Dispatch<DataSource> is only valid with xml/http binding and in Service.Mode.MESSAGE |
|
351 if (!DispatchImpl.isXMLHttp(binding)) |
|
352 throw new WebServiceException(DispatchMessages.INVALID_DATASOURCE_DISPATCH_BINDING("SOAP/HTTP", HTTPBinding.HTTP_BINDING)); |
|
353 if (DispatchImpl.isPAYLOADMode(mode)) |
|
354 throw new WebServiceException(DispatchMessages.INVALID_DATASOURCE_DISPATCH_MSGMODE(mode.name(), Service.Mode.MESSAGE.toString())); |
|
355 } |
|
356 |
|
357 public final @NotNull QName getPortName() { |
|
358 return portname; |
|
359 } |
|
360 |
|
361 void resolveEndpointAddress(@NotNull Packet message, @NotNull RequestContext requestContext) { |
|
362 //resolve endpoint look for query parameters, pathInfo |
|
363 String endpoint = (String) requestContext.get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY); |
|
364 if (endpoint == null) |
|
365 endpoint = message.endpointAddress.toString(); |
|
366 |
|
367 String pathInfo = null; |
|
368 String queryString = null; |
|
369 if (requestContext.get(MessageContext.PATH_INFO) != null) |
|
370 pathInfo = (String) requestContext.get(MessageContext.PATH_INFO); |
|
371 |
|
372 if (requestContext.get(MessageContext.QUERY_STRING) != null) |
|
373 queryString = (String) requestContext.get(MessageContext.QUERY_STRING); |
|
374 |
|
375 |
|
376 String resolvedEndpoint = null; |
|
377 if (pathInfo != null || queryString != null) { |
|
378 pathInfo = checkPath(pathInfo); |
|
379 queryString = checkQuery(queryString); |
|
380 if (endpoint != null) { |
|
381 try { |
|
382 final URI endpointURI = new URI(endpoint); |
|
383 resolvedEndpoint = resolveURI(endpointURI, pathInfo, queryString); |
|
384 } catch (URISyntaxException e) { |
|
385 throw new WebServiceException(DispatchMessages.INVALID_URI(endpoint)); |
|
386 } |
|
387 } |
|
388 requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, resolvedEndpoint); |
|
389 //message.endpointAddress = EndpointAddress.create(resolvedEndpoint); |
|
390 } |
|
391 } |
|
392 |
|
393 protected @NotNull String resolveURI(@NotNull URI endpointURI, @Nullable String pathInfo, @Nullable String queryString) { |
|
394 String query = null; |
|
395 String fragment = null; |
|
396 if (queryString != null) { |
|
397 final URI result; |
|
398 try { |
|
399 URI tp = new URI(null, null, endpointURI.getPath(), queryString, null); |
|
400 result = endpointURI.resolve(tp); |
|
401 } catch (URISyntaxException e) { |
|
402 throw new WebServiceException(DispatchMessages.INVALID_QUERY_STRING(queryString)); |
|
403 } |
|
404 query = result.getQuery(); |
|
405 fragment = result.getFragment(); |
|
406 } |
|
407 |
|
408 final String path = (pathInfo != null) ? pathInfo : endpointURI.getPath(); |
|
409 try { |
|
410 //final URI temp = new URI(null, null, path, query, fragment); |
|
411 //return endpointURI.resolve(temp).toURL().toExternalForm(); |
|
412 // Using the following HACK instead of the above to avoid double encoding of |
|
413 // the query. Application's QUERY_STRING is encoded using URLEncoder.encode(). |
|
414 // If we use that query in URI's constructor, it is encoded again. |
|
415 // URLEncoder's encoding is not the same as URI's encoding of the query. |
|
416 // See {@link URL} |
|
417 StringBuilder spec = new StringBuilder(); |
|
418 if (path != null) { |
|
419 spec.append(path); |
|
420 } |
|
421 if (query != null) { |
|
422 spec.append("?"); |
|
423 spec.append(query); |
|
424 } |
|
425 if (fragment != null) { |
|
426 spec.append("#"); |
|
427 spec.append(fragment); |
|
428 } |
|
429 return new URL(endpointURI.toURL(), spec.toString()).toExternalForm(); |
|
430 } catch (MalformedURLException e) { |
|
431 throw new WebServiceException(DispatchMessages.INVALID_URI_RESOLUTION(path)); |
|
432 } |
|
433 } |
|
434 |
|
435 private static String checkPath(@Nullable String path) { |
|
436 //does it begin with / |
|
437 return (path == null || path.startsWith("/")) ? path : "/" + path; |
|
438 } |
|
439 |
|
440 private static String checkQuery(@Nullable String query) { |
|
441 if (query == null) return null; |
|
442 |
|
443 if (query.indexOf('?') == 0) |
|
444 throw new WebServiceException(DispatchMessages.INVALID_QUERY_LEADING_CHAR(query)); |
|
445 return query; |
|
446 } |
|
447 |
|
448 |
|
449 protected AttachmentSet setOutboundAttachments() { |
|
450 HashMap<String, DataHandler> attachments = (HashMap<String, DataHandler>) |
|
451 getRequestContext().get(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS); |
|
452 |
|
453 if (attachments != null) { |
|
454 List<Attachment> alist = new ArrayList(); |
|
455 for (Map.Entry<String, DataHandler> att : attachments.entrySet()) { |
|
456 DataHandlerAttachment dha = new DataHandlerAttachment(att.getKey(), att.getValue()); |
|
457 alist.add(dha); |
|
458 } |
|
459 return new AttachmentSetImpl(alist); |
|
460 } |
|
461 return new AttachmentSetImpl(); |
|
462 } |
|
463 |
|
464 /* private void getInboundAttachments(Message msg) { |
|
465 AttachmentSet attachments = msg.getAttachments(); |
|
466 if (!attachments.isEmpty()) { |
|
467 Map<String, DataHandler> in = new HashMap<String, DataHandler>(); |
|
468 for (Attachment attachment : attachments) |
|
469 in.put(attachment.getContentId(), attachment.asDataHandler()); |
|
470 getResponseContext().put(MessageContext.INBOUND_MESSAGE_ATTACHMENTS, in); |
|
471 } |
|
472 |
|
473 } |
|
474 */ |
|
475 |
|
476 |
|
477 /** |
|
478 * Calls {@link DispatchImpl#doInvoke(Object,RequestContext,ResponseContextReceiver)}. |
|
479 */ |
|
480 private class Invoker implements Callable { |
|
481 private final T param; |
|
482 // snapshot the context now. this is necessary to avoid concurrency issue, |
|
483 // and is required by the spec |
|
484 private final RequestContext rc = requestContext.copy(); |
|
485 |
|
486 /** |
|
487 * Because of the object instantiation order, |
|
488 * we can't take this as a constructor parameter. |
|
489 */ |
|
490 private ResponseContextReceiver receiver; |
|
491 |
|
492 Invoker(T param) { |
|
493 this.param = param; |
|
494 } |
|
495 |
|
496 public T call() throws Exception { |
|
497 if (LOGGER.isLoggable(Level.FINE)) { |
|
498 dumpParam(param, "call()"); |
|
499 } |
|
500 return doInvoke(param,rc,receiver); |
|
501 } |
|
502 |
|
503 void setReceiver(ResponseContextReceiver receiver) { |
|
504 this.receiver = receiver; |
|
505 } |
|
506 } |
|
507 |
|
508 /** |
|
509 * |
|
510 */ |
|
511 private class DispatchAsyncInvoker extends AsyncInvoker { |
|
512 private final T param; |
|
513 // snapshot the context now. this is necessary to avoid concurrency issue, |
|
514 // and is required by the spec |
|
515 private final RequestContext rc = requestContext.copy(); |
|
516 |
|
517 DispatchAsyncInvoker(T param) { |
|
518 this.param = param; |
|
519 } |
|
520 |
|
521 public void do_run () { |
|
522 checkNullAllowed(param, rc, binding, mode); |
|
523 final Packet message = createPacket(param); |
|
524 message.nonNullAsyncHandlerGiven = this.nonNullAsyncHandlerGiven; |
|
525 resolveEndpointAddress(message, rc); |
|
526 setProperties(message,true); |
|
527 |
|
528 String action = null; |
|
529 String msgId = null; |
|
530 if (LOGGER.isLoggable(Level.FINE)) { |
|
531 AddressingVersion av = DispatchImpl.this.getBinding().getAddressingVersion(); |
|
532 SOAPVersion sv = DispatchImpl.this.getBinding().getSOAPVersion(); |
|
533 action = |
|
534 av != null && message.getMessage() != null ? |
|
535 message.getMessage().getHeaders().getAction(av, sv) : null; |
|
536 msgId = |
|
537 av != null&& message.getMessage() != null ? |
|
538 message.getMessage().getHeaders().getMessageID(av, sv) : null; |
|
539 LOGGER.fine("In DispatchAsyncInvoker.do_run for async message with action: " + action + " and msg ID: " + msgId); |
|
540 } |
|
541 |
|
542 final String actionUse = action; |
|
543 final String msgIdUse = msgId; |
|
544 |
|
545 Fiber.CompletionCallback callback = new Fiber.CompletionCallback() { |
|
546 public void onCompletion(@NotNull Packet response) { |
|
547 |
|
548 if (LOGGER.isLoggable(Level.FINE)) { |
|
549 LOGGER.fine("Done with processAsync in DispatchAsyncInvoker.do_run, and setting response for async message with action: " + actionUse + " and msg ID: " + msgIdUse); |
|
550 } |
|
551 |
|
552 Message msg = response.getMessage(); |
|
553 |
|
554 if (LOGGER.isLoggable(Level.FINE)) { |
|
555 LOGGER.fine("Done with processAsync in DispatchAsyncInvoker.do_run, and setting response for async message with action: " + actionUse + " and msg ID: " + msgIdUse + " msg: " + msg); |
|
556 } |
|
557 |
|
558 try { |
|
559 if(msg != null && msg.isFault() && |
|
560 !allowFaultResponseMsg) { |
|
561 SOAPFaultBuilder faultBuilder = SOAPFaultBuilder.create(msg); |
|
562 // passing null means there is no checked excpetion we're looking for all |
|
563 // it will get back to us is a protocol exception |
|
564 throw (SOAPFaultException)faultBuilder.createException(null); |
|
565 } |
|
566 responseImpl.setResponseContext(new ResponseContext(response)); |
|
567 responseImpl.set(toReturnValue(response), null); |
|
568 } catch (JAXBException e) { |
|
569 //TODO: i18nify |
|
570 responseImpl.set(null, new DeserializationException(DispatchMessages.INVALID_RESPONSE_DESERIALIZATION(),e)); |
|
571 } catch(WebServiceException e){ |
|
572 //it could be a WebServiceException or a ProtocolException |
|
573 responseImpl.set(null, e); |
|
574 } catch(Throwable e){ |
|
575 // It could be any RuntimeException resulting due to some internal bug. |
|
576 // or its some other exception resulting from user error, wrap it in |
|
577 // WebServiceException |
|
578 responseImpl.set(null, new WebServiceException(e)); |
|
579 } |
|
580 } |
|
581 public void onCompletion(@NotNull Throwable error) { |
|
582 if (LOGGER.isLoggable(Level.FINE)) { |
|
583 LOGGER.fine("Done with processAsync in DispatchAsyncInvoker.do_run, and setting response for async message with action: " + actionUse + " and msg ID: " + msgIdUse + " Throwable: " + error.toString()); |
|
584 } |
|
585 if (error instanceof WebServiceException) { |
|
586 responseImpl.set(null, error); |
|
587 |
|
588 } else { |
|
589 //its RuntimeException or some other exception resulting from user error, wrap it in |
|
590 // WebServiceException |
|
591 responseImpl.set(null, new WebServiceException(error)); |
|
592 } |
|
593 } |
|
594 }; |
|
595 processAsync(responseImpl,message,rc, callback); |
|
596 } |
|
597 } |
|
598 |
|
599 public void setOutboundHeaders(Object... headers) { |
|
600 throw new UnsupportedOperationException(); |
|
601 } |
|
602 |
|
603 static final String HTTP_REQUEST_METHOD_GET="GET"; |
|
604 static final String HTTP_REQUEST_METHOD_POST="POST"; |
|
605 static final String HTTP_REQUEST_METHOD_PUT="PUT"; |
|
606 |
|
607 @Deprecated |
|
608 public static Dispatch<Source> createSourceDispatch(QName port, Mode mode, WSServiceDelegate owner, Tube pipe, BindingImpl binding, WSEndpointReference epr) { |
|
609 if(isXMLHttp(binding)) |
|
610 return new RESTSourceDispatch(port,mode,owner,pipe,binding,epr); |
|
611 else |
|
612 return new SOAPSourceDispatch(port,mode,owner,pipe,binding,epr); |
|
613 } |
|
614 |
|
615 public static Dispatch<Source> createSourceDispatch(WSPortInfo portInfo, Mode mode, BindingImpl binding, WSEndpointReference epr) { |
|
616 if (isXMLHttp(binding)) |
|
617 return new RESTSourceDispatch(portInfo, mode, binding, epr); |
|
618 else |
|
619 return new SOAPSourceDispatch(portInfo, mode, binding, epr); |
|
620 } |
|
621 } |