1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/client/dispatch/JAXBDispatch.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,135 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.ws.client.dispatch; 1.30 + 1.31 +import com.sun.xml.internal.bind.api.JAXBRIContext; 1.32 +import com.sun.xml.internal.ws.api.addressing.WSEndpointReference; 1.33 +import com.sun.xml.internal.ws.api.message.Header; 1.34 +import com.sun.xml.internal.ws.api.message.Headers; 1.35 +import com.sun.xml.internal.ws.api.message.Message; 1.36 +import com.sun.xml.internal.ws.api.message.Messages; 1.37 +import com.sun.xml.internal.ws.api.message.Packet; 1.38 +import com.sun.xml.internal.ws.api.pipe.Tube; 1.39 +import com.sun.xml.internal.ws.api.client.WSPortInfo; 1.40 +import com.sun.xml.internal.ws.binding.BindingImpl; 1.41 +import com.sun.xml.internal.ws.client.WSServiceDelegate; 1.42 +import com.sun.xml.internal.ws.message.jaxb.JAXBDispatchMessage; 1.43 +import com.sun.xml.internal.ws.spi.db.BindingContextFactory; 1.44 + 1.45 +import javax.xml.bind.JAXBContext; 1.46 +import javax.xml.bind.JAXBException; 1.47 +import javax.xml.bind.Unmarshaller; 1.48 +import javax.xml.namespace.QName; 1.49 +import javax.xml.transform.Source; 1.50 +import javax.xml.ws.Service; 1.51 +import javax.xml.ws.WebServiceException; 1.52 + 1.53 +/** 1.54 + * The <code>JAXBDispatch</code> class provides support 1.55 + * for the dynamic invocation of a service endpoint operation using 1.56 + * JAXB objects. The <code>javax.xml.ws.Service</code> 1.57 + * interface acts as a factory for the creation of <code>JAXBDispatch</code> 1.58 + * instances. 1.59 + * 1.60 + * @author WS Development Team 1.61 + * @version 1.0 1.62 + */ 1.63 +public class JAXBDispatch extends DispatchImpl<Object> { 1.64 + 1.65 + private final JAXBContext jaxbcontext; 1.66 + 1.67 + // We will support a JAXBContext parameter from an unknown JAXB 1.68 + // implementation by marshaling and unmarshaling directly from the 1.69 + // context object, as there is no Bond available. 1.70 + private final boolean isContextSupported; 1.71 + 1.72 + @Deprecated 1.73 + public JAXBDispatch(QName port, JAXBContext jc, Service.Mode mode, WSServiceDelegate service, Tube pipe, BindingImpl binding, WSEndpointReference epr) { 1.74 + super(port, mode, service, pipe, binding, epr); 1.75 + this.jaxbcontext = jc; 1.76 + this.isContextSupported = BindingContextFactory.isContextSupported(jc); 1.77 + } 1.78 + 1.79 + public JAXBDispatch(WSPortInfo portInfo, JAXBContext jc, Service.Mode mode, BindingImpl binding, WSEndpointReference epr) { 1.80 + super(portInfo, mode, binding, epr); 1.81 + this.jaxbcontext = jc; 1.82 + this.isContextSupported = BindingContextFactory.isContextSupported(jc); 1.83 + } 1.84 + 1.85 + Object toReturnValue(Packet response) { 1.86 + try { 1.87 + Unmarshaller unmarshaller = jaxbcontext.createUnmarshaller(); 1.88 + Message msg = response.getMessage(); 1.89 + switch (mode) { 1.90 + case PAYLOAD: 1.91 + return msg.<Object>readPayloadAsJAXB(unmarshaller); 1.92 + case MESSAGE: 1.93 + Source result = msg.readEnvelopeAsSource(); 1.94 + return unmarshaller.unmarshal(result); 1.95 + default: 1.96 + throw new WebServiceException("Unrecognized dispatch mode"); 1.97 + } 1.98 + } catch (JAXBException e) { 1.99 + throw new WebServiceException(e); 1.100 + } 1.101 + } 1.102 + 1.103 + 1.104 + Packet createPacket(Object msg) { 1.105 + assert jaxbcontext != null; 1.106 + 1.107 + Message message; 1.108 + if (mode == Service.Mode.MESSAGE) { 1.109 + message = isContextSupported ? 1.110 + new JAXBDispatchMessage(BindingContextFactory.create(jaxbcontext), msg, soapVersion) : 1.111 + new JAXBDispatchMessage(jaxbcontext, msg, soapVersion); 1.112 + } else { 1.113 + if (msg == null) { 1.114 + message = Messages.createEmpty(soapVersion); 1.115 + } else { 1.116 + message = isContextSupported ? 1.117 + Messages.create(jaxbcontext, msg, soapVersion) : 1.118 + Messages.createRaw(jaxbcontext, msg, soapVersion); 1.119 + } 1.120 + } 1.121 + 1.122 + return new Packet(message); 1.123 + 1.124 + } 1.125 + 1.126 + public void setOutboundHeaders(Object... headers) { 1.127 + if (headers == null) 1.128 + throw new IllegalArgumentException(); 1.129 + Header[] hl = new Header[headers.length]; 1.130 + for (int i = 0; i < hl.length; i++) { 1.131 + if (headers[i] == null) 1.132 + throw new IllegalArgumentException(); 1.133 + // TODO: handle any JAXBContext. 1.134 + hl[i] = Headers.create((JAXBRIContext) jaxbcontext, headers[i]); 1.135 + } 1.136 + super.setOutboundHeaders(hl); 1.137 + } 1.138 +}