aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.ws.client.dispatch; aoqi@0: aoqi@0: import com.sun.istack.internal.NotNull; aoqi@0: import com.sun.istack.internal.Nullable; aoqi@0: import com.sun.xml.internal.ws.api.BindingID; aoqi@0: import com.sun.xml.internal.ws.api.SOAPVersion; aoqi@0: import com.sun.xml.internal.ws.api.WSBinding; aoqi@0: import com.sun.xml.internal.ws.api.addressing.AddressingVersion; aoqi@0: import com.sun.xml.internal.ws.api.addressing.WSEndpointReference; aoqi@0: import com.sun.xml.internal.ws.api.client.WSPortInfo; aoqi@0: import com.sun.xml.internal.ws.api.message.AddressingUtils; aoqi@0: import com.sun.xml.internal.ws.api.message.Attachment; aoqi@0: import com.sun.xml.internal.ws.api.message.AttachmentSet; aoqi@0: import com.sun.xml.internal.ws.api.message.Message; aoqi@0: import com.sun.xml.internal.ws.api.message.Packet; aoqi@0: import com.sun.xml.internal.ws.api.pipe.Fiber; aoqi@0: import com.sun.xml.internal.ws.api.pipe.Tube; aoqi@0: import com.sun.xml.internal.ws.api.server.Container; aoqi@0: import com.sun.xml.internal.ws.api.server.ContainerResolver; aoqi@0: import com.sun.xml.internal.ws.binding.BindingImpl; aoqi@0: import com.sun.xml.internal.ws.client.*; aoqi@0: import com.sun.xml.internal.ws.encoding.soap.DeserializationException; aoqi@0: import com.sun.xml.internal.ws.fault.SOAPFaultBuilder; aoqi@0: import com.sun.xml.internal.ws.message.AttachmentSetImpl; aoqi@0: import com.sun.xml.internal.ws.message.DataHandlerAttachment; aoqi@0: import com.sun.xml.internal.ws.resources.DispatchMessages; aoqi@0: aoqi@0: import javax.activation.DataHandler; aoqi@0: import javax.xml.bind.JAXBException; aoqi@0: import javax.xml.namespace.QName; aoqi@0: import javax.xml.transform.Source; aoqi@0: import javax.xml.ws.AsyncHandler; aoqi@0: import javax.xml.ws.BindingProvider; aoqi@0: import javax.xml.ws.Dispatch; aoqi@0: import javax.xml.ws.Response; aoqi@0: import javax.xml.ws.Service; aoqi@0: import javax.xml.ws.Service.Mode; aoqi@0: import javax.xml.ws.WebServiceException; aoqi@0: import javax.xml.ws.handler.MessageContext; aoqi@0: import javax.xml.ws.http.HTTPBinding; aoqi@0: import javax.xml.ws.soap.SOAPBinding; aoqi@0: import javax.xml.ws.soap.SOAPFaultException; aoqi@0: import java.net.MalformedURLException; aoqi@0: import java.net.URI; aoqi@0: import java.net.URISyntaxException; aoqi@0: import java.net.URL; aoqi@0: import java.util.ArrayList; aoqi@0: import java.util.HashMap; aoqi@0: import java.util.List; aoqi@0: import java.util.Map; aoqi@0: import java.util.concurrent.Callable; aoqi@0: import java.util.concurrent.Future; aoqi@0: import java.util.logging.Level; aoqi@0: import java.util.logging.Logger; aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * The DispatchImpl abstract class provides support aoqi@0: * for the dynamic invocation of a service endpoint operation using XML aoqi@0: * constructs, JAXB objects or SOAPMessage. The javax.xml.ws.Service aoqi@0: * interface acts as a factory for the creation of DispatchImpl aoqi@0: * instances. aoqi@0: * aoqi@0: * @author WS Development Team aoqi@0: * @version 1.0 aoqi@0: */ aoqi@0: public abstract class DispatchImpl extends Stub implements Dispatch { aoqi@0: aoqi@0: private static final Logger LOGGER = Logger.getLogger(DispatchImpl.class.getName()); aoqi@0: aoqi@0: final Service.Mode mode; aoqi@0: final SOAPVersion soapVersion; aoqi@0: final boolean allowFaultResponseMsg; aoqi@0: static final long AWAIT_TERMINATION_TIME = 800L; aoqi@0: aoqi@0: /** aoqi@0: * aoqi@0: * @param port dispatch instance is associated with this wsdl port qName aoqi@0: * @param mode Service.mode associated with this Dispatch instance - Service.mode.MESSAGE or Service.mode.PAYLOAD aoqi@0: * @param owner Service that created the Dispatch aoqi@0: * @param pipe Master pipe for the pipeline aoqi@0: * @param binding Binding of this Dispatch instance, current one of SOAP/HTTP or XML/HTTP aoqi@0: */ aoqi@0: @Deprecated aoqi@0: protected DispatchImpl(QName port, Service.Mode mode, WSServiceDelegate owner, Tube pipe, BindingImpl binding, @Nullable WSEndpointReference epr) { aoqi@0: super(port, owner, pipe, binding, (owner.getWsdlService() != null)? owner.getWsdlService().get(port) : null , owner.getEndpointAddress(port), epr); aoqi@0: this.mode = mode; aoqi@0: this.soapVersion = binding.getSOAPVersion(); aoqi@0: this.allowFaultResponseMsg = false; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * @param portInfo dispatch instance is associated with this portInfo aoqi@0: * @param mode Service.mode associated with this Dispatch instance - Service.mode.MESSAGE or Service.mode.PAYLOAD aoqi@0: * @param binding Binding of this Dispatch instance, current one of SOAP/HTTP or XML/HTTP aoqi@0: */ aoqi@0: protected DispatchImpl(WSPortInfo portInfo, Service.Mode mode, BindingImpl binding, @Nullable WSEndpointReference epr) { aoqi@0: this(portInfo, mode, binding, epr, false); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * @param portInfo dispatch instance is associated with this portInfo aoqi@0: * @param mode Service.mode associated with this Dispatch instance - Service.mode.MESSAGE or Service.mode.PAYLOAD aoqi@0: * @param binding Binding of this Dispatch instance, current one of SOAP/HTTP or XML/HTTP aoqi@0: * @param allowFaultResponseMsg A packet containing a SOAP fault message is allowed as the response to a request on this dispatch instance. aoqi@0: */ aoqi@0: protected DispatchImpl(WSPortInfo portInfo, Service.Mode mode, BindingImpl binding, @Nullable WSEndpointReference epr, boolean allowFaultResponseMsg) { aoqi@0: this(portInfo, mode, binding, null, epr, allowFaultResponseMsg); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * @param portInfo dispatch instance is associated with this portInfo aoqi@0: * @param mode Service.mode associated with this Dispatch instance - Service.mode.MESSAGE or Service.mode.PAYLOAD aoqi@0: * @param binding Binding of this Dispatch instance, current one of SOAP/HTTP or XML/HTTP aoqi@0: * @param pipe Master pipe for the pipeline aoqi@0: * @param allowFaultResponseMsg A packet containing a SOAP fault message is allowed as the response to a request on this dispatch instance. aoqi@0: */ aoqi@0: protected DispatchImpl(WSPortInfo portInfo, Service.Mode mode, BindingImpl binding, Tube pipe, @Nullable WSEndpointReference epr, boolean allowFaultResponseMsg) { aoqi@0: super(portInfo, binding, pipe, portInfo.getEndpointAddress(), epr); aoqi@0: this.mode = mode; aoqi@0: this.soapVersion = binding.getSOAPVersion(); aoqi@0: this.allowFaultResponseMsg = allowFaultResponseMsg; aoqi@0: } aoqi@0: /** aoqi@0: * aoqi@0: * @param portportInfo dispatch instance is associated with this wsdl port qName aoqi@0: * @param mode Service.mode associated with this Dispatch instance - Service.mode.MESSAGE or Service.mode.PAYLOAD aoqi@0: * @param pipe Master pipe for the pipeline aoqi@0: * @param binding Binding of this Dispatch instance, current one of SOAP/HTTP or XML/HTTP aoqi@0: * @param allowFaultResponseMsg A packet containing a SOAP fault message is allowed as the response to a request on this dispatch instance. aoqi@0: */ aoqi@0: protected DispatchImpl(WSPortInfo portInfo, Service.Mode mode, Tube pipe, BindingImpl binding, @Nullable WSEndpointReference epr, boolean allowFaultResponseMsg) { aoqi@0: super(portInfo, binding, pipe, portInfo.getEndpointAddress(), epr); aoqi@0: this.mode = mode; aoqi@0: this.soapVersion = binding.getSOAPVersion(); aoqi@0: this.allowFaultResponseMsg = allowFaultResponseMsg; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Abstract method that is implemented by each concrete Dispatch class aoqi@0: * @param msg message passed in from the client program on the invocation aoqi@0: * @return The Message created returned as the Interface in actuallity a aoqi@0: * concrete Message Type aoqi@0: */ aoqi@0: abstract Packet createPacket(T msg); aoqi@0: aoqi@0: /** aoqi@0: * Obtains the value to return from the response message. aoqi@0: */ aoqi@0: abstract T toReturnValue(Packet response); aoqi@0: aoqi@0: public final Response invokeAsync(T param) { aoqi@0: Container old = ContainerResolver.getDefault().enterContainer(owner.getContainer()); aoqi@0: try { aoqi@0: if (LOGGER.isLoggable(Level.FINE)) { aoqi@0: dumpParam(param, "invokeAsync(T)"); aoqi@0: } aoqi@0: AsyncInvoker invoker = new DispatchAsyncInvoker(param); aoqi@0: AsyncResponseImpl ft = new AsyncResponseImpl(invoker,null); aoqi@0: invoker.setReceiver(ft); aoqi@0: ft.run(); aoqi@0: return ft; aoqi@0: } finally { aoqi@0: ContainerResolver.getDefault().exitContainer(old); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private void dumpParam(T param, String method) { aoqi@0: if (param instanceof Packet) { aoqi@0: Packet message = (Packet)param; aoqi@0: aoqi@0: String action; aoqi@0: String msgId; aoqi@0: if (LOGGER.isLoggable(Level.FINE)) { aoqi@0: AddressingVersion av = DispatchImpl.this.getBinding().getAddressingVersion(); aoqi@0: SOAPVersion sv = DispatchImpl.this.getBinding().getSOAPVersion(); aoqi@0: action = aoqi@0: av != null && message.getMessage() != null ? aoqi@0: AddressingUtils.getAction(message.getMessage().getHeaders(), av, sv) : null; aoqi@0: msgId = aoqi@0: av != null && message.getMessage() != null ? aoqi@0: AddressingUtils.getMessageID(message.getMessage().getHeaders(), av, sv) : null; aoqi@0: LOGGER.fine("In DispatchImpl." + method + " for message with action: " + action + " and msg ID: " + msgId + " msg: " + message.getMessage()); aoqi@0: aoqi@0: if (message.getMessage() == null) { aoqi@0: LOGGER.fine("Dispatching null message for action: " + action + " and msg ID: " + msgId); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: public final Future invokeAsync(T param, AsyncHandler asyncHandler) { aoqi@0: Container old = ContainerResolver.getDefault().enterContainer(owner.getContainer()); aoqi@0: try { aoqi@0: if (LOGGER.isLoggable(Level.FINE)) { aoqi@0: dumpParam(param, "invokeAsync(T, AsyncHandler)"); aoqi@0: } aoqi@0: AsyncInvoker invoker = new DispatchAsyncInvoker(param); aoqi@0: AsyncResponseImpl ft = new AsyncResponseImpl(invoker,asyncHandler); aoqi@0: invoker.setReceiver(ft); aoqi@0: invoker.setNonNullAsyncHandlerGiven(asyncHandler != null); aoqi@0: aoqi@0: ft.run(); aoqi@0: return ft; aoqi@0: } finally { aoqi@0: ContainerResolver.getDefault().exitContainer(old); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Synchronously invokes a service. aoqi@0: * aoqi@0: * See {@link #process(Packet, RequestContext, ResponseContextReceiver)} on aoqi@0: * why it takes a {@link RequestContext} and {@link ResponseContextReceiver} as a parameter. aoqi@0: */ aoqi@0: public final T doInvoke(T in, RequestContext rc, ResponseContextReceiver receiver){ aoqi@0: Packet response = null; aoqi@0: try { aoqi@0: try { aoqi@0: checkNullAllowed(in, rc, binding, mode); aoqi@0: aoqi@0: Packet message = createPacket(in); aoqi@0: message.setState(Packet.State.ClientRequest); aoqi@0: resolveEndpointAddress(message, rc); aoqi@0: setProperties(message,true); aoqi@0: response = process(message,rc,receiver); aoqi@0: Message msg = response.getMessage(); aoqi@0: aoqi@0: // REVIEW: eliminate allowFaultResponseMsg, but make that behavior default for MessageDispatch, PacketDispatch aoqi@0: if(msg != null && msg.isFault() && aoqi@0: !allowFaultResponseMsg) { aoqi@0: SOAPFaultBuilder faultBuilder = SOAPFaultBuilder.create(msg); aoqi@0: // passing null means there is no checked excpetion we're looking for all aoqi@0: // it will get back to us is a protocol exception aoqi@0: throw (SOAPFaultException)faultBuilder.createException(null); aoqi@0: } aoqi@0: } catch (JAXBException e) { aoqi@0: //TODO: i18nify aoqi@0: throw new DeserializationException(DispatchMessages.INVALID_RESPONSE_DESERIALIZATION(),e); aoqi@0: } catch(WebServiceException e){ aoqi@0: //it could be a WebServiceException or a ProtocolException aoqi@0: throw e; aoqi@0: } catch(Throwable e){ aoqi@0: // it could be a RuntimeException resulting due to some internal bug or aoqi@0: // its some other exception resulting from user error, wrap it in aoqi@0: // WebServiceException aoqi@0: throw new WebServiceException(e); aoqi@0: } aoqi@0: aoqi@0: return toReturnValue(response); aoqi@0: } finally { aoqi@0: // REVIEW: Move to AsyncTransportProvider aoqi@0: if (response != null && response.transportBackChannel != null) aoqi@0: response.transportBackChannel.close(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public final T invoke(T in) { aoqi@0: Container old = ContainerResolver.getDefault().enterContainer(owner.getContainer()); aoqi@0: try { aoqi@0: if (LOGGER.isLoggable(Level.FINE)) { aoqi@0: dumpParam(in, "invoke(T)"); aoqi@0: } aoqi@0: aoqi@0: return doInvoke(in,requestContext,this); aoqi@0: } finally { aoqi@0: ContainerResolver.getDefault().exitContainer(old); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public final void invokeOneWay(T in) { aoqi@0: Container old = ContainerResolver.getDefault().enterContainer(owner.getContainer()); aoqi@0: try { aoqi@0: if (LOGGER.isLoggable(Level.FINE)) { aoqi@0: dumpParam(in, "invokeOneWay(T)"); aoqi@0: } aoqi@0: aoqi@0: try { aoqi@0: checkNullAllowed(in, requestContext, binding, mode); aoqi@0: aoqi@0: Packet request = createPacket(in); aoqi@0: request.setState(Packet.State.ClientRequest); aoqi@0: setProperties(request,false); aoqi@0: process(request,requestContext,this); aoqi@0: } catch(WebServiceException e){ aoqi@0: //it could be a WebServiceException or a ProtocolException aoqi@0: throw e; aoqi@0: } catch(Throwable e){ aoqi@0: // it could be a RuntimeException resulting due to some internal bug or aoqi@0: // its some other exception resulting from user error, wrap it in aoqi@0: // WebServiceException aoqi@0: throw new WebServiceException(e); aoqi@0: } aoqi@0: } finally { aoqi@0: ContainerResolver.getDefault().exitContainer(old); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void setProperties(Packet packet, boolean expectReply) { aoqi@0: packet.expectReply = expectReply; aoqi@0: } aoqi@0: aoqi@0: static boolean isXMLHttp(@NotNull WSBinding binding) { aoqi@0: return binding.getBindingId().equals(BindingID.XML_HTTP); aoqi@0: } aoqi@0: aoqi@0: static boolean isPAYLOADMode(@NotNull Service.Mode mode) { aoqi@0: return mode == Service.Mode.PAYLOAD; aoqi@0: } aoqi@0: aoqi@0: static void checkNullAllowed(@Nullable Object in, RequestContext rc, WSBinding binding, Service.Mode mode) { aoqi@0: aoqi@0: if (in != null) aoqi@0: return; aoqi@0: aoqi@0: //With HTTP Binding a null invocation parameter can not be used aoqi@0: //with HTTP Request Method == POST aoqi@0: if (isXMLHttp(binding)){ aoqi@0: if (methodNotOk(rc)) aoqi@0: throw new WebServiceException(DispatchMessages.INVALID_NULLARG_XMLHTTP_REQUEST_METHOD(HTTP_REQUEST_METHOD_POST, HTTP_REQUEST_METHOD_GET)); aoqi@0: } else { //soapBinding aoqi@0: if (mode == Service.Mode.MESSAGE ) aoqi@0: throw new WebServiceException(DispatchMessages.INVALID_NULLARG_SOAP_MSGMODE(mode.name(), Service.Mode.PAYLOAD.toString())); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: static boolean methodNotOk(@NotNull RequestContext rc) { aoqi@0: String requestMethod = (String)rc.get(MessageContext.HTTP_REQUEST_METHOD); aoqi@0: String request = (requestMethod == null)? HTTP_REQUEST_METHOD_POST: requestMethod; aoqi@0: // if method == post or put with a null invocation parameter in xml/http binding this is not ok aoqi@0: return HTTP_REQUEST_METHOD_POST.equalsIgnoreCase(request) || HTTP_REQUEST_METHOD_PUT.equalsIgnoreCase(request); aoqi@0: } aoqi@0: aoqi@0: public static void checkValidSOAPMessageDispatch(WSBinding binding, Service.Mode mode) { aoqi@0: // Dispatch is only valid for soap binding and in Service.Mode.MESSAGE aoqi@0: if (DispatchImpl.isXMLHttp(binding)) aoqi@0: throw new WebServiceException(DispatchMessages.INVALID_SOAPMESSAGE_DISPATCH_BINDING(HTTPBinding.HTTP_BINDING, SOAPBinding.SOAP11HTTP_BINDING + " or " + SOAPBinding.SOAP12HTTP_BINDING)); aoqi@0: if (DispatchImpl.isPAYLOADMode(mode)) aoqi@0: throw new WebServiceException(DispatchMessages.INVALID_SOAPMESSAGE_DISPATCH_MSGMODE(mode.name(), Service.Mode.MESSAGE.toString())); aoqi@0: } aoqi@0: aoqi@0: public static void checkValidDataSourceDispatch(WSBinding binding, Service.Mode mode) { aoqi@0: // Dispatch is only valid with xml/http binding and in Service.Mode.MESSAGE aoqi@0: if (!DispatchImpl.isXMLHttp(binding)) aoqi@0: throw new WebServiceException(DispatchMessages.INVALID_DATASOURCE_DISPATCH_BINDING("SOAP/HTTP", HTTPBinding.HTTP_BINDING)); aoqi@0: if (DispatchImpl.isPAYLOADMode(mode)) aoqi@0: throw new WebServiceException(DispatchMessages.INVALID_DATASOURCE_DISPATCH_MSGMODE(mode.name(), Service.Mode.MESSAGE.toString())); aoqi@0: } aoqi@0: aoqi@0: public final @NotNull QName getPortName() { aoqi@0: return portname; aoqi@0: } aoqi@0: aoqi@0: void resolveEndpointAddress(@NotNull final Packet message, @NotNull final RequestContext requestContext) { aoqi@0: final boolean p = message.packetTakesPriorityOverRequestContext; aoqi@0: aoqi@0: //resolve endpoint look for query parameters, pathInfo aoqi@0: String endpoint; aoqi@0: if (p && message.endpointAddress != null) { aoqi@0: endpoint = message.endpointAddress.toString(); aoqi@0: } else { aoqi@0: endpoint = (String) requestContext.get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY); aoqi@0: } aoqi@0: // This is existing before packetTakesPriorityOverRequestContext so leaving in place. aoqi@0: if (endpoint == null) { aoqi@0: if (message.endpointAddress == null) throw new WebServiceException(DispatchMessages.INVALID_NULLARG_URI()); aoqi@0: endpoint = message.endpointAddress.toString(); aoqi@0: } aoqi@0: aoqi@0: String pathInfo = null; aoqi@0: String queryString = null; aoqi@0: if (p && message.invocationProperties.get(MessageContext.PATH_INFO) != null) { aoqi@0: pathInfo = (String) message.invocationProperties.get(MessageContext.PATH_INFO); aoqi@0: } else if (requestContext.get(MessageContext.PATH_INFO) != null) { aoqi@0: pathInfo = (String) requestContext.get(MessageContext.PATH_INFO); aoqi@0: } aoqi@0: aoqi@0: if (p && message.invocationProperties.get(MessageContext.QUERY_STRING) != null) { aoqi@0: queryString = (String) message.invocationProperties.get(MessageContext.QUERY_STRING); aoqi@0: } else if (requestContext.get(MessageContext.QUERY_STRING) != null) { aoqi@0: queryString = (String) requestContext.get(MessageContext.QUERY_STRING); aoqi@0: } aoqi@0: aoqi@0: if (pathInfo != null || queryString != null) { aoqi@0: pathInfo = checkPath(pathInfo); aoqi@0: queryString = checkQuery(queryString); aoqi@0: if (endpoint != null) { aoqi@0: try { aoqi@0: final URI endpointURI = new URI(endpoint); aoqi@0: endpoint = resolveURI(endpointURI, pathInfo, queryString); aoqi@0: } catch (URISyntaxException e) { aoqi@0: throw new WebServiceException(DispatchMessages.INVALID_URI(endpoint)); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: // These two lines used to be inside the above if. It is outside so: aoqi@0: // - in cases where there is no setting of address on a Packet before invocation or no pathInfo/queryString aoqi@0: // this will just put back what it found in the requestContext - basically a noop. aoqi@0: // - but when info is in the Packet this will update so it will get used later. aoqi@0: // Remember - we are operating on a copied RequestContext at this point - not the sticky one in the Stub. aoqi@0: requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpoint); aoqi@0: // This is not necessary because a later step will copy the resolvedEndpoint put above into message. aoqi@0: //message.endpointAddress = EndpointAddress.create(endpoint); aoqi@0: } aoqi@0: aoqi@0: protected @NotNull String resolveURI(@NotNull URI endpointURI, @Nullable String pathInfo, @Nullable String queryString) { aoqi@0: String query = null; aoqi@0: String fragment = null; aoqi@0: if (queryString != null) { aoqi@0: final URI result; aoqi@0: try { aoqi@0: URI tp = new URI(null, null, endpointURI.getPath(), queryString, null); aoqi@0: result = endpointURI.resolve(tp); aoqi@0: } catch (URISyntaxException e) { aoqi@0: throw new WebServiceException(DispatchMessages.INVALID_QUERY_STRING(queryString)); aoqi@0: } aoqi@0: query = result.getQuery(); aoqi@0: fragment = result.getFragment(); aoqi@0: } aoqi@0: aoqi@0: final String path = (pathInfo != null) ? pathInfo : endpointURI.getPath(); aoqi@0: try { aoqi@0: //final URI temp = new URI(null, null, path, query, fragment); aoqi@0: //return endpointURI.resolve(temp).toURL().toExternalForm(); aoqi@0: // Using the following HACK instead of the above to avoid double encoding of aoqi@0: // the query. Application's QUERY_STRING is encoded using URLEncoder.encode(). aoqi@0: // If we use that query in URI's constructor, it is encoded again. aoqi@0: // URLEncoder's encoding is not the same as URI's encoding of the query. aoqi@0: // See {@link URL} aoqi@0: StringBuilder spec = new StringBuilder(); aoqi@0: if (path != null) { aoqi@0: spec.append(path); aoqi@0: } aoqi@0: if (query != null) { aoqi@0: spec.append("?"); aoqi@0: spec.append(query); aoqi@0: } aoqi@0: if (fragment != null) { aoqi@0: spec.append("#"); aoqi@0: spec.append(fragment); aoqi@0: } aoqi@0: return new URL(endpointURI.toURL(), spec.toString()).toExternalForm(); aoqi@0: } catch (MalformedURLException e) { aoqi@0: throw new WebServiceException(DispatchMessages.INVALID_URI_RESOLUTION(path)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private static String checkPath(@Nullable String path) { aoqi@0: //does it begin with / aoqi@0: return (path == null || path.startsWith("/")) ? path : "/" + path; aoqi@0: } aoqi@0: aoqi@0: private static String checkQuery(@Nullable String query) { aoqi@0: if (query == null) return null; aoqi@0: aoqi@0: if (query.indexOf('?') == 0) aoqi@0: throw new WebServiceException(DispatchMessages.INVALID_QUERY_LEADING_CHAR(query)); aoqi@0: return query; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: protected AttachmentSet setOutboundAttachments() { aoqi@0: HashMap attachments = (HashMap) aoqi@0: getRequestContext().get(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS); aoqi@0: aoqi@0: if (attachments != null) { aoqi@0: List alist = new ArrayList(); aoqi@0: for (Map.Entry att : attachments.entrySet()) { aoqi@0: DataHandlerAttachment dha = new DataHandlerAttachment(att.getKey(), att.getValue()); aoqi@0: alist.add(dha); aoqi@0: } aoqi@0: return new AttachmentSetImpl(alist); aoqi@0: } aoqi@0: return new AttachmentSetImpl(); aoqi@0: } aoqi@0: aoqi@0: /* private void getInboundAttachments(Message msg) { aoqi@0: AttachmentSet attachments = msg.getAttachments(); aoqi@0: if (!attachments.isEmpty()) { aoqi@0: Map in = new HashMap(); aoqi@0: for (Attachment attachment : attachments) aoqi@0: in.put(attachment.getContentId(), attachment.asDataHandler()); aoqi@0: getResponseContext().put(MessageContext.INBOUND_MESSAGE_ATTACHMENTS, in); aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: */ aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Calls {@link DispatchImpl#doInvoke(Object,RequestContext,ResponseContextReceiver)}. aoqi@0: */ aoqi@0: private class Invoker implements Callable { aoqi@0: private final T param; aoqi@0: // snapshot the context now. this is necessary to avoid concurrency issue, aoqi@0: // and is required by the spec aoqi@0: private final RequestContext rc = requestContext.copy(); aoqi@0: aoqi@0: /** aoqi@0: * Because of the object instantiation order, aoqi@0: * we can't take this as a constructor parameter. aoqi@0: */ aoqi@0: private ResponseContextReceiver receiver; aoqi@0: aoqi@0: Invoker(T param) { aoqi@0: this.param = param; aoqi@0: } aoqi@0: aoqi@0: public T call() throws Exception { aoqi@0: if (LOGGER.isLoggable(Level.FINE)) { aoqi@0: dumpParam(param, "call()"); aoqi@0: } aoqi@0: return doInvoke(param,rc,receiver); aoqi@0: } aoqi@0: aoqi@0: void setReceiver(ResponseContextReceiver receiver) { aoqi@0: this.receiver = receiver; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * aoqi@0: */ aoqi@0: private class DispatchAsyncInvoker extends AsyncInvoker { aoqi@0: private final T param; aoqi@0: // snapshot the context now. this is necessary to avoid concurrency issue, aoqi@0: // and is required by the spec aoqi@0: private final RequestContext rc = requestContext.copy(); aoqi@0: aoqi@0: DispatchAsyncInvoker(T param) { aoqi@0: this.param = param; aoqi@0: } aoqi@0: aoqi@0: public void do_run () { aoqi@0: checkNullAllowed(param, rc, binding, mode); aoqi@0: final Packet message = createPacket(param); aoqi@0: message.setState(Packet.State.ClientRequest); aoqi@0: message.nonNullAsyncHandlerGiven = this.nonNullAsyncHandlerGiven; aoqi@0: resolveEndpointAddress(message, rc); aoqi@0: setProperties(message,true); aoqi@0: aoqi@0: String action = null; aoqi@0: String msgId = null; aoqi@0: if (LOGGER.isLoggable(Level.FINE)) { aoqi@0: AddressingVersion av = DispatchImpl.this.getBinding().getAddressingVersion(); aoqi@0: SOAPVersion sv = DispatchImpl.this.getBinding().getSOAPVersion(); aoqi@0: action = aoqi@0: av != null && message.getMessage() != null ? aoqi@0: AddressingUtils.getAction(message.getMessage().getHeaders(), av, sv) : null; aoqi@0: msgId = aoqi@0: av != null&& message.getMessage() != null ? aoqi@0: AddressingUtils.getMessageID(message.getMessage().getHeaders(), av, sv) : null; aoqi@0: LOGGER.fine("In DispatchAsyncInvoker.do_run for async message with action: " + action + " and msg ID: " + msgId); aoqi@0: } aoqi@0: aoqi@0: final String actionUse = action; aoqi@0: final String msgIdUse = msgId; aoqi@0: aoqi@0: Fiber.CompletionCallback callback = new Fiber.CompletionCallback() { aoqi@0: public void onCompletion(@NotNull Packet response) { aoqi@0: aoqi@0: if (LOGGER.isLoggable(Level.FINE)) { aoqi@0: LOGGER.fine("Done with processAsync in DispatchAsyncInvoker.do_run, and setting response for async message with action: " + actionUse + " and msg ID: " + msgIdUse); aoqi@0: } aoqi@0: aoqi@0: Message msg = response.getMessage(); aoqi@0: aoqi@0: if (LOGGER.isLoggable(Level.FINE)) { aoqi@0: LOGGER.fine("Done with processAsync in DispatchAsyncInvoker.do_run, and setting response for async message with action: " + actionUse + " and msg ID: " + msgIdUse + " msg: " + msg); aoqi@0: } aoqi@0: aoqi@0: try { aoqi@0: if(msg != null && msg.isFault() && aoqi@0: !allowFaultResponseMsg) { aoqi@0: SOAPFaultBuilder faultBuilder = SOAPFaultBuilder.create(msg); aoqi@0: // passing null means there is no checked excpetion we're looking for all aoqi@0: // it will get back to us is a protocol exception aoqi@0: throw (SOAPFaultException)faultBuilder.createException(null); aoqi@0: } aoqi@0: responseImpl.setResponseContext(new ResponseContext(response)); aoqi@0: responseImpl.set(toReturnValue(response), null); aoqi@0: } catch (JAXBException e) { aoqi@0: //TODO: i18nify aoqi@0: responseImpl.set(null, new DeserializationException(DispatchMessages.INVALID_RESPONSE_DESERIALIZATION(),e)); aoqi@0: } catch(WebServiceException e){ aoqi@0: //it could be a WebServiceException or a ProtocolException aoqi@0: responseImpl.set(null, e); aoqi@0: } catch(Throwable e){ aoqi@0: // It could be any RuntimeException resulting due to some internal bug. aoqi@0: // or its some other exception resulting from user error, wrap it in aoqi@0: // WebServiceException aoqi@0: responseImpl.set(null, new WebServiceException(e)); aoqi@0: } aoqi@0: } aoqi@0: public void onCompletion(@NotNull Throwable error) { aoqi@0: if (LOGGER.isLoggable(Level.FINE)) { aoqi@0: 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()); aoqi@0: } aoqi@0: if (error instanceof WebServiceException) { aoqi@0: responseImpl.set(null, error); aoqi@0: aoqi@0: } else { aoqi@0: //its RuntimeException or some other exception resulting from user error, wrap it in aoqi@0: // WebServiceException aoqi@0: responseImpl.set(null, new WebServiceException(error)); aoqi@0: } aoqi@0: } aoqi@0: }; aoqi@0: processAsync(responseImpl,message,rc, callback); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void setOutboundHeaders(Object... headers) { aoqi@0: throw new UnsupportedOperationException(); aoqi@0: } aoqi@0: aoqi@0: static final String HTTP_REQUEST_METHOD_GET="GET"; aoqi@0: static final String HTTP_REQUEST_METHOD_POST="POST"; aoqi@0: static final String HTTP_REQUEST_METHOD_PUT="PUT"; aoqi@0: aoqi@0: @Deprecated aoqi@0: public static Dispatch createSourceDispatch(QName port, Mode mode, WSServiceDelegate owner, Tube pipe, BindingImpl binding, WSEndpointReference epr) { aoqi@0: if(isXMLHttp(binding)) aoqi@0: return new RESTSourceDispatch(port,mode,owner,pipe,binding,epr); aoqi@0: else aoqi@0: return new SOAPSourceDispatch(port,mode,owner,pipe,binding,epr); aoqi@0: } aoqi@0: aoqi@0: public static Dispatch createSourceDispatch(WSPortInfo portInfo, Mode mode, BindingImpl binding, WSEndpointReference epr) { aoqi@0: if (isXMLHttp(binding)) aoqi@0: return new RESTSourceDispatch(portInfo, mode, binding, epr); aoqi@0: else aoqi@0: return new SOAPSourceDispatch(portInfo, mode, binding, epr); aoqi@0: } aoqi@0: }