KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > soap > skeleton > DocumentWrappedAction


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Emil Ong
28  */

29
30 package com.caucho.soap.skeleton;
31
32 import com.caucho.jaxb.JAXBContextImpl;
33 import com.caucho.jaxb.skeleton.Property;
34
35 import com.caucho.util.L10N;
36
37 import javax.jws.WebParam;
38 import javax.jws.WebResult;
39 import javax.xml.bind.JAXBException;
40 import javax.xml.bind.Marshaller;
41 import javax.xml.bind.Unmarshaller;
42 import javax.xml.namespace.QName JavaDoc;
43 import javax.xml.stream.XMLStreamException;
44 import javax.xml.stream.XMLStreamReader;
45 import javax.xml.stream.XMLStreamWriter;
46 import javax.xml.ws.Holder;
47 import javax.xml.ws.WebServiceException;
48 import java.io.IOException JavaDoc;
49 import java.lang.annotation.Annotation JavaDoc;
50 import java.lang.reflect.InvocationTargetException JavaDoc;
51 import java.lang.reflect.Method JavaDoc;
52 import java.util.ArrayList JavaDoc;
53 import java.util.HashMap JavaDoc;
54 import java.util.Map JavaDoc;
55 import java.util.logging.Logger JavaDoc;
56
57 /**
58  * Document wrapped action
59  */

60 public class DocumentWrappedAction extends AbstractAction {
61   private final static Logger JavaDoc log =
62     Logger.getLogger(DocumentWrappedAction.class.getName());
63   public static final L10N L = new L10N(DocumentWrappedAction.class);
64
65   private static final String JavaDoc TARGET_NAMESPACE_PREFIX = "m";
66
67   /*
68    * Document wrapped arguments are in an xsd:sequence -- in other words,
69    * there is a prescribed order in which the arguments are expected to be
70    * given. (Any arguments from the header are excepted; headers are
71    * key/value pairs.)
72    *
73    */

74
75   private HashMap JavaDoc<String JavaDoc,ParameterMarshal> _headerMap
76     = new HashMap JavaDoc<String JavaDoc,ParameterMarshal>();
77
78   private ParameterMarshal []_headerArgs;
79
80   private ParameterMarshal _returnMarshal;
81
82   public DocumentWrappedAction(Method JavaDoc method,
83                                JAXBContextImpl jaxbContext,
84                                String JavaDoc targetNamespace,
85                                Marshaller marshaller,
86                                Unmarshaller unmarshaller)
87     throws JAXBException, WebServiceException
88   {
89     super(method, jaxbContext, targetNamespace);
90
91     Class JavaDoc[] params = method.getParameterTypes();
92     Annotation JavaDoc[][] paramAnn = method.getParameterAnnotations();
93
94     ArrayList JavaDoc<ParameterMarshal> headerList = new ArrayList JavaDoc<ParameterMarshal>();
95     ArrayList JavaDoc<ParameterMarshal> bodyList = new ArrayList JavaDoc<ParameterMarshal>();
96     
97     for (int i = 0; i < params.length; i++) {
98       boolean isInput = true;
99       boolean isHeader = false;
100
101       String JavaDoc localName = "arg" + i; // As per JAX-WS spec
102

103       QName JavaDoc name = null;
104       WebParam.Mode mode = WebParam.Mode.IN;
105
106       for (Annotation JavaDoc ann : paramAnn[i]) {
107         if (ann instanceof WebParam) {
108           WebParam webParam = (WebParam) ann;
109
110           if (! "".equals(webParam.name()))
111             localName = webParam.name();
112
113           if ("".equals(webParam.targetNamespace()))
114             name = new QName JavaDoc(localName);
115           else
116             name = new QName JavaDoc(localName, webParam.targetNamespace());
117
118           if (params[i].equals(Holder.class)) {
119             mode = webParam.mode();
120
121             if (mode == WebParam.Mode.OUT)
122               isInput = false;
123           }
124         }
125       }
126
127       if (name == null)
128         name = new QName JavaDoc(localName);
129
130       Property property = _jaxbContext.createProperty(params[i]);
131
132       ParameterMarshal pMarshal
133         = ParameterMarshal.create(i, property, name, mode,
134                                   marshaller, unmarshaller);
135
136       if (isHeader) {
137         headerList.add(pMarshal);
138         _headerMap.put(localName, pMarshal);
139       }
140       else
141         bodyList.add(pMarshal);
142     }
143
144     _headerArgs = new ParameterMarshal[headerList.size()];
145     headerList.toArray(_headerArgs);
146
147     _bodyArgs = new ParameterMarshal[bodyList.size()];
148     bodyList.toArray(_bodyArgs);
149
150     if (! Void JavaDoc.class.equals(method.getReturnType()) &&
151         ! Void.TYPE.equals(method.getReturnType())) {
152       Property property = _jaxbContext.createProperty(method.getReturnType());
153
154       if (method.isAnnotationPresent(WebResult.class)) {
155         WebResult webResult = (WebResult) method.getAnnotation(WebResult.class);
156
157         String JavaDoc localName = webResult.name();
158
159         if ("".equals(localName))
160           localName = "return";
161
162         _resultName = new QName JavaDoc(webResult.targetNamespace(), localName);
163       }
164       else
165         _resultName = new QName JavaDoc("return");
166
167       _returnMarshal = ParameterMarshal.create(0, property, _resultName,
168                                                WebParam.Mode.OUT,
169                                                marshaller, unmarshaller);
170     }
171
172     //
173
// Exceptions -> Faults
174
//
175

176     // XXX
177
/*Class[] exceptions = getExceptionTypes();
178
179     for (Class exception : exceptions)
180       exceptionToFault(exception);
181       */

182   }
183
184   /**
185    * Invokes the request for a call.
186    */

187   public void invoke(Object JavaDoc service, XMLStreamReader in, XMLStreamWriter out)
188     throws IOException JavaDoc, XMLStreamException, Throwable JavaDoc
189   {
190     // We're starting out at the point in the input stream where the
191
// arguments are listed and the point in the output stream where
192
// the results are to be written.
193

194     Object JavaDoc[] args = new Object JavaDoc[_arity];
195
196     // document wrapped => everything must be in order
197
for (int i = 0; i < _bodyArgs.length; i++) {
198       if (_bodyArgs[i] instanceof InParameterMarshal)
199         _bodyArgs[i].deserializeCall(in, args);
200       else
201         _bodyArgs[i].deserializeCallDefault(args);
202     }
203
204     Object JavaDoc value = null;
205
206     try {
207       value = _method.invoke(service, args);
208     }
209     catch (IllegalAccessException JavaDoc e) {
210       throw new Throwable JavaDoc(e);
211     }
212     catch (InvocationTargetException JavaDoc e) {
213       throw new Throwable JavaDoc(e.getCause());
214     }
215
216     out.writeStartElement(TARGET_NAMESPACE_PREFIX,
217                           _responseName,
218                           _targetNamespace);
219     out.writeNamespace(TARGET_NAMESPACE_PREFIX, _targetNamespace);
220
221     if (_returnMarshal != null)
222       _returnMarshal.serializeReply(out, value);
223
224     for (int i = 0; i < _bodyArgs.length; i++)
225       _bodyArgs[i].serializeReply(out, args);
226
227     out.writeEndElement(); // response name
228
}
229
230   protected Object JavaDoc readResponse(XMLStreamReader in, Object JavaDoc []args)
231     throws IOException JavaDoc, XMLStreamException, JAXBException
232   {
233     Object JavaDoc ret = null;
234
235     if (in.nextTag() != XMLStreamReader.START_ELEMENT
236         || ! "Envelope".equals(in.getLocalName()))
237       throw expectStart("Envelope", in);
238
239     if (in.nextTag() != XMLStreamReader.START_ELEMENT)
240       throw expectStart("Header", in);
241
242     if ("Header".equals(in.getLocalName())) {
243       while (in.nextTag() == XMLStreamReader.START_ELEMENT) {
244         String JavaDoc tagName = in.getLocalName();
245
246         ParameterMarshal marshal = _headerMap.get(tagName);
247
248         if (marshal != null)
249           marshal.deserializeReply(in, args);
250         else {
251           int depth = 1;
252
253           while (depth > 0) {
254             switch (in.nextTag()) {
255               case XMLStreamReader.START_ELEMENT:
256                 depth++;
257                 break;
258               case XMLStreamReader.END_ELEMENT:
259                 depth--;
260                 break;
261               default:
262                 throw new IOException JavaDoc("expected </Header>");
263             }
264           }
265         }
266       }
267
268       if (! "Header".equals(in.getLocalName()))
269         throw expectEnd("Header", in);
270
271       if (in.nextTag() != XMLStreamReader.START_ELEMENT)
272         throw expectStart("Body", in);
273     }
274
275     if (! "Body".equals(in.getLocalName()))
276       throw expectStart("Body", in);
277
278     if (in.nextTag() != XMLStreamReader.START_ELEMENT &&
279         ! _responseName.equals(in.getLocalName()))
280       throw expectStart(_responseName, in);
281
282     in.nextTag();
283
284     if (_returnMarshal != null)
285       ret = _returnMarshal.deserializeReply(in);
286
287     // document wrapped => everything must be in order
288
for (int i = 0; i < _bodyArgs.length; i++) {
289       if (_bodyArgs[i] instanceof OutParameterMarshal)
290         _bodyArgs[i].deserializeReply(in, args);
291     }
292
293     if (in.getEventType() != XMLStreamReader.END_ELEMENT &&
294         ! _responseName.equals(in.getLocalName()))
295       throw expectEnd(_responseName, in);
296
297     if (in.nextTag() != XMLStreamReader.END_ELEMENT &&
298         ! "Body".equals(in.getLocalName()))
299       throw expectEnd("Body", in);
300
301     if (in.nextTag() != in.END_ELEMENT)
302       throw expectEnd("Envelope", in);
303
304     return ret;
305   }
306
307   private IOException JavaDoc expectStart(String JavaDoc expect, XMLStreamReader in)
308   {
309     return new IOException JavaDoc("expected <" + expect + "> at " + in.getName());
310   }
311
312   private IOException JavaDoc expectEnd(String JavaDoc expect, XMLStreamReader in)
313   {
314     return new IOException JavaDoc("expected </" + expect + "> at " + in.getName());
315   }
316 }
317
Popular Tags