View Javadoc
1   /*
2    * Copyright (c) 1997, 2012, 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.xml.internal.bind.api.JAXBRIContext;
29  import com.sun.xml.internal.ws.api.addressing.WSEndpointReference;
30  import com.sun.xml.internal.ws.api.message.Header;
31  import com.sun.xml.internal.ws.api.message.Headers;
32  import com.sun.xml.internal.ws.api.message.Message;
33  import com.sun.xml.internal.ws.api.message.Messages;
34  import com.sun.xml.internal.ws.api.message.Packet;
35  import com.sun.xml.internal.ws.api.pipe.Tube;
36  import com.sun.xml.internal.ws.api.client.WSPortInfo;
37  import com.sun.xml.internal.ws.binding.BindingImpl;
38  import com.sun.xml.internal.ws.client.WSServiceDelegate;
39  import com.sun.xml.internal.ws.message.jaxb.JAXBDispatchMessage;
40  import com.sun.xml.internal.ws.spi.db.BindingContextFactory;
41  
42  import javax.xml.bind.JAXBContext;
43  import javax.xml.bind.JAXBException;
44  import javax.xml.bind.Unmarshaller;
45  import javax.xml.namespace.QName;
46  import javax.xml.transform.Source;
47  import javax.xml.ws.Service;
48  import javax.xml.ws.WebServiceException;
49  
50  /**
51   * The <code>JAXBDispatch</code> class provides support
52   * for the dynamic invocation of a service endpoint operation using
53   * JAXB objects. The <code>javax.xml.ws.Service</code>
54   * interface acts as a factory for the creation of <code>JAXBDispatch</code>
55   * instances.
56   *
57   * @author WS Development Team
58   * @version 1.0
59   */
60  public class JAXBDispatch extends DispatchImpl<Object> {
61  
62      private final JAXBContext jaxbcontext;
63  
64      // We will support a JAXBContext parameter from an unknown JAXB
65      // implementation by marshaling and unmarshaling directly from the
66      // context object, as there is no Bond available.
67      private final boolean isContextSupported;
68  
69      @Deprecated
70      public JAXBDispatch(QName port, JAXBContext jc, Service.Mode mode, WSServiceDelegate service, Tube pipe, BindingImpl binding, WSEndpointReference epr) {
71          super(port, mode, service, pipe, binding, epr);
72          this.jaxbcontext = jc;
73          this.isContextSupported = BindingContextFactory.isContextSupported(jc);
74      }
75  
76      public JAXBDispatch(WSPortInfo portInfo, JAXBContext jc, Service.Mode mode, BindingImpl binding, WSEndpointReference epr) {
77          super(portInfo, mode, binding, epr);
78          this.jaxbcontext = jc;
79          this.isContextSupported = BindingContextFactory.isContextSupported(jc);
80      }
81  
82      Object toReturnValue(Packet response) {
83          try {
84              Unmarshaller unmarshaller = jaxbcontext.createUnmarshaller();
85              Message msg = response.getMessage();
86              switch (mode) {
87                  case PAYLOAD:
88                      return msg.<Object>readPayloadAsJAXB(unmarshaller);
89                  case MESSAGE:
90                      Source result = msg.readEnvelopeAsSource();
91                      return unmarshaller.unmarshal(result);
92                  default:
93                      throw new WebServiceException("Unrecognized dispatch mode");
94              }
95          } catch (JAXBException e) {
96              throw new WebServiceException(e);
97          }
98      }
99  
100 
101     Packet createPacket(Object msg) {
102         assert jaxbcontext != null;
103 
104         Message message;
105         if (mode == Service.Mode.MESSAGE) {
106             message = isContextSupported ?
107                     new JAXBDispatchMessage(BindingContextFactory.create(jaxbcontext), msg, soapVersion) :
108                     new JAXBDispatchMessage(jaxbcontext, msg, soapVersion);
109         } else {
110             if (msg == null) {
111                 message = Messages.createEmpty(soapVersion);
112             } else {
113                 message = isContextSupported ?
114                         Messages.create(jaxbcontext, msg, soapVersion) :
115                         Messages.createRaw(jaxbcontext, msg, soapVersion);
116             }
117         }
118 
119         return new Packet(message);
120 
121     }
122 
123     public void setOutboundHeaders(Object... headers) {
124         if (headers == null)
125             throw new IllegalArgumentException();
126         Header[] hl = new Header[headers.length];
127         for (int i = 0; i < hl.length; i++) {
128             if (headers[i] == null)
129                 throw new IllegalArgumentException();
130             // TODO: handle any JAXBContext.
131             hl[i] = Headers.create((JAXBRIContext) jaxbcontext, headers[i]);
132         }
133         super.setOutboundHeaders(hl);
134     }
135 }