1 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 ; 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 ; 49 import java.lang.annotation.Annotation ; 50 import java.lang.reflect.InvocationTargetException ; 51 import java.lang.reflect.Method ; 52 import java.util.ArrayList ; 53 import java.util.HashMap ; 54 import java.util.Map ; 55 import java.util.logging.Logger ; 56 57 60 public class DocumentWrappedAction extends AbstractAction { 61 private final static Logger log = 62 Logger.getLogger(DocumentWrappedAction.class.getName()); 63 public static final L10N L = new L10N(DocumentWrappedAction.class); 64 65 private static final String TARGET_NAMESPACE_PREFIX = "m"; 66 67 74 75 private HashMap <String ,ParameterMarshal> _headerMap 76 = new HashMap <String ,ParameterMarshal>(); 77 78 private ParameterMarshal []_headerArgs; 79 80 private ParameterMarshal _returnMarshal; 81 82 public DocumentWrappedAction(Method method, 83 JAXBContextImpl jaxbContext, 84 String targetNamespace, 85 Marshaller marshaller, 86 Unmarshaller unmarshaller) 87 throws JAXBException, WebServiceException 88 { 89 super(method, jaxbContext, targetNamespace); 90 91 Class [] params = method.getParameterTypes(); 92 Annotation [][] paramAnn = method.getParameterAnnotations(); 93 94 ArrayList <ParameterMarshal> headerList = new ArrayList <ParameterMarshal>(); 95 ArrayList <ParameterMarshal> bodyList = new ArrayList <ParameterMarshal>(); 96 97 for (int i = 0; i < params.length; i++) { 98 boolean isInput = true; 99 boolean isHeader = false; 100 101 String localName = "arg" + i; 103 QName name = null; 104 WebParam.Mode mode = WebParam.Mode.IN; 105 106 for (Annotation 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 (localName); 115 else 116 name = new QName (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 (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 .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 localName = webResult.name(); 158 159 if ("".equals(localName)) 160 localName = "return"; 161 162 _resultName = new QName (webResult.targetNamespace(), localName); 163 } 164 else 165 _resultName = new QName ("return"); 166 167 _returnMarshal = ParameterMarshal.create(0, property, _resultName, 168 WebParam.Mode.OUT, 169 marshaller, unmarshaller); 170 } 171 172 176 182 } 183 184 187 public void invoke(Object service, XMLStreamReader in, XMLStreamWriter out) 188 throws IOException , XMLStreamException, Throwable 189 { 190 194 Object [] args = new Object [_arity]; 195 196 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 value = null; 205 206 try { 207 value = _method.invoke(service, args); 208 } 209 catch (IllegalAccessException e) { 210 throw new Throwable (e); 211 } 212 catch (InvocationTargetException e) { 213 throw new Throwable (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(); } 229 230 protected Object readResponse(XMLStreamReader in, Object []args) 231 throws IOException , XMLStreamException, JAXBException 232 { 233 Object 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 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 ("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 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 expectStart(String expect, XMLStreamReader in) 308 { 309 return new IOException ("expected <" + expect + "> at " + in.getName()); 310 } 311 312 private IOException expectEnd(String expect, XMLStreamReader in) 313 { 314 return new IOException ("expected </" + expect + "> at " + in.getName()); 315 } 316 } 317 | Popular Tags |