1 57 58 package org.apache.soap.rpc; 59 60 import java.io.*; 61 import java.util.*; 62 import org.w3c.dom.*; 63 import org.apache.soap.util.*; 64 import org.apache.soap.util.xml.*; 65 import org.apache.soap.*; 66 import org.apache.soap.encoding.*; 67 import org.apache.soap.server.*; 68 69 77 public class RPCMessage implements Serializer { 78 protected String targetObjectURI; 79 protected String fullTargetObjectURI; 80 protected String methodName; 81 protected Vector params; 82 protected Header header; 83 protected String encodingStyleURI; 84 protected SOAPContext ctx; 85 86 protected RPCMessage(String targetObjectURI, String methodName, 87 Vector params, Header header, 88 String encodingStyleURI, SOAPContext ctx) { 89 setTargetObjectURI(targetObjectURI); 90 this.methodName = methodName; 91 this.params = params; 92 this.header = header; 93 this.encodingStyleURI = encodingStyleURI; 94 this.ctx = ctx; 95 } 96 97 public void setTargetObjectURI(String targetObjectURI) { 98 this.fullTargetObjectURI = targetObjectURI; 100 101 this.targetObjectURI = StringUtils.parseFullTargetObjectURI( 104 targetObjectURI); 105 } 106 107 public String getTargetObjectURI() { 108 return targetObjectURI; 109 } 110 111 public void setFullTargetObjectURI(String targetObjectURI) { 112 setTargetObjectURI(targetObjectURI); 113 } 114 115 public String getFullTargetObjectURI() { 116 return fullTargetObjectURI; 117 } 118 119 public void setMethodName(String methodName) { 120 this.methodName = methodName; 121 } 122 123 public String getMethodName() { 124 return methodName; 125 } 126 127 public void setParams(Vector params) { 128 this.params = params; 129 } 130 131 public Vector getParams() { 132 return params; 133 } 134 135 public void setHeader(Header header) { 136 this.header = header; 137 } 138 139 public Header getHeader() { 140 return header; 141 } 142 143 public void setEncodingStyleURI(String encodingStyleURI) { 144 this.encodingStyleURI = encodingStyleURI; 145 } 146 147 public String getEncodingStyleURI() { 148 return encodingStyleURI; 149 } 150 151 protected void setSOAPContext(SOAPContext ctx) { 152 this.ctx = ctx; 153 } 154 155 public SOAPContext getSOAPContext() { 156 return ctx; 157 } 158 159 protected Envelope buildEnvelope(boolean isResponse) { 160 Envelope env = new Envelope(); 162 Body body = new Body(); 163 Vector bodyEntries = new Vector(); 164 165 bodyEntries.addElement(new Bean( 166 (isResponse ? Response.class : Call.class), 167 this)); 168 body.setBodyEntries(bodyEntries); 169 env.setBody(body); 170 env.setHeader(header); 171 172 return env; 173 } 174 175 protected static RPCMessage extractFromEnvelope(Envelope env, 176 ServiceManager svcMgr, 177 boolean isResponse, 178 SOAPMappingRegistry respSMR, 179 SOAPContext ctx) 180 throws IllegalArgumentException { 181 Body body = env.getBody(); 182 Vector bodyEntries = body.getBodyEntries(); 183 RPCMessage msg = null; 184 185 if (bodyEntries.size() > 0) { 187 Element msgEl = (Element)bodyEntries.elementAt(0); 188 Class toClass = isResponse ? Response.class : Call.class; 189 String declEnvEncStyle = env.getAttribute(new QName( 190 Constants.NS_URI_SOAP_ENV, Constants.ATTR_ENCODING_STYLE)); 191 String declBodyEncStyle = body.getAttribute(new QName( 192 Constants.NS_URI_SOAP_ENV, Constants.ATTR_ENCODING_STYLE)); 193 String actualEncStyle = declBodyEncStyle != null 194 ? declBodyEncStyle 195 : declEnvEncStyle; 196 197 msg = RPCMessage.unmarshall(actualEncStyle, msgEl, toClass, svcMgr, 198 respSMR, ctx); 199 msg.setHeader(env.getHeader()); 200 201 return msg; 202 } else { 203 throw new IllegalArgumentException ("An '" + Constants.Q_ELEM_BODY + 204 "' element must contain a " + 205 "child element."); 206 } 207 } 208 209 public void marshall(String inScopeEncStyle, Class javaType, Object src, 210 Object context, Writer sink, NSStack nsStack, 211 XMLJavaMappingRegistry xjmr, SOAPContext ctx) 212 throws IllegalArgumentException , IOException { 213 nsStack.pushScope(); 214 215 RPCMessage msg = (RPCMessage)src; 216 boolean isResponse = (javaType == Response.class); 217 String targetObjectURI = Utils.cleanString( 218 msg.getFullTargetObjectURI()); 219 String methodName = msg.getMethodName(); 220 Vector params = msg.getParams(); 221 String suffix = isResponse 222 ? RPCConstants.RESPONSE_SUFFIX 223 : ""; 224 String declMsgEncStyle = msg.getEncodingStyleURI(); 225 String actualMsgEncStyle = declMsgEncStyle != null 226 ? declMsgEncStyle 227 : inScopeEncStyle; 228 229 if (isResponse) { 231 Response resp = (Response)msg; 232 233 if (!resp.generatedFault()) { 234 StringWriter nsDeclSW = new StringWriter(); 236 String targetObjectNSPrefix = nsStack.getPrefixFromURI( 237 targetObjectURI, nsDeclSW); 238 239 sink.write('<' + targetObjectNSPrefix + ':' + 240 methodName + suffix + nsDeclSW); 241 242 String soapEnvNSPrefix = nsStack.getPrefixFromURI( 245 Constants.NS_URI_SOAP_ENV, sink); 246 247 if (declMsgEncStyle != null 248 && !declMsgEncStyle.equals(inScopeEncStyle)) { 249 sink.write(' ' + soapEnvNSPrefix + ':' + 250 Constants.ATTR_ENCODING_STYLE + "=\"" + 251 declMsgEncStyle + '\"'); 252 } 253 254 sink.write('>' + StringUtils.lineSeparator); 255 256 Parameter ret = resp.getReturnValue(); 258 259 if (ret != null) { 260 String declParamEncStyle = ret.getEncodingStyleURI(); 261 String actualParamEncStyle = declParamEncStyle != null 262 ? declParamEncStyle 263 : actualMsgEncStyle; 264 Serializer ser = xjmr.querySerializer(Parameter.class, 265 actualParamEncStyle); 266 267 ser.marshall(actualMsgEncStyle, Parameter.class, ret, null, 268 sink, nsStack, xjmr, ctx); 269 270 sink.write(StringUtils.lineSeparator); 271 } 272 273 serializeParams(params, actualMsgEncStyle, sink, nsStack, xjmr, ctx); 274 275 sink.write("</" + targetObjectNSPrefix + ':' + 276 methodName + suffix + '>' + 277 StringUtils.lineSeparator); 278 } else { 279 Fault fault = resp.getFault(); 281 282 fault.marshall(actualMsgEncStyle, sink, nsStack, xjmr, ctx); 283 } 284 } else { 285 StringWriter nsDeclSW = new StringWriter(); 287 String targetObjectNSPrefix = nsStack.getPrefixFromURI(targetObjectURI, 288 nsDeclSW); 289 290 sink.write('<' + targetObjectNSPrefix + ':' + 291 methodName + suffix + nsDeclSW); 292 293 String soapEnvNSPrefix = nsStack.getPrefixFromURI( 296 Constants.NS_URI_SOAP_ENV, sink); 297 298 if (declMsgEncStyle != null 299 && !declMsgEncStyle.equals(inScopeEncStyle)) { 300 sink.write(' ' + soapEnvNSPrefix + ':' + 301 Constants.ATTR_ENCODING_STYLE + "=\"" + 302 declMsgEncStyle + '\"'); 303 } 304 305 sink.write('>' + StringUtils.lineSeparator); 306 307 serializeParams(params, actualMsgEncStyle, sink, nsStack, xjmr, ctx); 308 309 sink.write("</" + targetObjectNSPrefix + ':' + 310 methodName + suffix + '>'); 311 } 312 313 nsStack.popScope(); 314 } 315 316 private void serializeParams(Vector params, String inScopeEncStyle, 317 Writer sink, NSStack nsStack, 318 XMLJavaMappingRegistry xjmr, SOAPContext ctx) 319 throws IOException { 320 if (params != null) { 322 int size = params.size(); 323 324 for (int i = 0; i < size; i++) { 325 Parameter param = (Parameter)params.elementAt(i); 326 String declParamEncStyle = param.getEncodingStyleURI(); 327 String actualParamEncStyle = declParamEncStyle != null 328 ? declParamEncStyle 329 : inScopeEncStyle; 330 Serializer ser = xjmr.querySerializer(Parameter.class, 331 actualParamEncStyle); 332 333 ser.marshall(inScopeEncStyle, Parameter.class, param, null, sink, 334 nsStack, xjmr, ctx); 335 336 sink.write(StringUtils.lineSeparator); 337 } 338 } 339 } 340 341 public static RPCMessage unmarshall(String inScopeEncStyle, Node src, 342 Class toClass, ServiceManager svcMgr, 343 SOAPMappingRegistry respSMR, 344 SOAPContext ctx) 345 throws IllegalArgumentException { 346 SOAPMappingRegistry smr = null; 347 Element root = (Element)src; 348 boolean isResponse = (toClass == Response.class); 349 String fullTargetObjectURI = null; 350 String targetObjectURI = null; 351 String methodName = null; 352 Parameter returnValue = null; 353 Fault fault = null; 354 Vector params = null; 355 String declMsgEncStyle = DOMUtils.getAttributeNS(root, 356 Constants.NS_URI_SOAP_ENV, Constants.ATTR_ENCODING_STYLE); 357 String actualMsgEncStyle = declMsgEncStyle != null 358 ? declMsgEncStyle 359 : inScopeEncStyle; 360 361 if (isResponse && Constants.Q_ELEM_FAULT.matches(root)) { 363 fault = Fault.unmarshall(actualMsgEncStyle, root, respSMR, ctx); 364 } else { 365 String tagName = root.getLocalName(); 367 368 fullTargetObjectURI = root.getNamespaceURI(); 370 targetObjectURI = StringUtils.parseFullTargetObjectURI( 371 fullTargetObjectURI); 372 373 if (!isResponse) { 376 DeploymentDescriptor dd = null; 378 try { 379 dd = svcMgr.query(targetObjectURI); 380 smr = DeploymentDescriptor.buildSOAPMappingRegistry(dd, ctx); 381 } catch (SOAPException e) { 382 throw new IllegalArgumentException ("Unable to resolve " + 383 "targetObjectURI '" + 384 targetObjectURI + "'."); 385 } 386 } else { 387 smr = respSMR; 389 } 390 391 smr.setDefaultEncodingStyle(Constants.NS_URI_SOAP_ENC); 394 395 methodName = tagName; 396 397 403 if (isResponse && methodName.endsWith(RPCConstants.RESPONSE_SUFFIX)) { 404 methodName = methodName.substring(0, methodName.length() - 8); 406 } 407 408 Element tempEl = DOMUtils.getFirstChildElement(root); 409 410 if (isResponse && tempEl != null) { 412 String declParamEncStyle = DOMUtils.getAttributeNS(tempEl, 413 Constants.NS_URI_SOAP_ENV, Constants.ATTR_ENCODING_STYLE); 414 String actualParamEncStyle = declParamEncStyle != null 415 ? declParamEncStyle 416 : actualMsgEncStyle; 417 Bean returnBean = smr.unmarshall(actualParamEncStyle, 418 RPCConstants.Q_ELEM_PARAMETER, 419 tempEl, ctx); 420 421 returnValue = (Parameter)returnBean.value; 422 423 if (declParamEncStyle != null) 424 { 425 returnValue.setEncodingStyleURI(declParamEncStyle); 426 } 427 428 tempEl = DOMUtils.getNextSiblingElement(tempEl); 429 } 430 431 if (tempEl != null) { 433 for (params = new Vector(); 434 tempEl != null; 435 tempEl = DOMUtils.getNextSiblingElement(tempEl)) { 436 String declParamEncStyle = DOMUtils.getAttributeNS(tempEl, 437 Constants.NS_URI_SOAP_ENV, Constants.ATTR_ENCODING_STYLE); 438 String actualParamEncStyle = declParamEncStyle != null 439 ? declParamEncStyle 440 : actualMsgEncStyle; 441 Bean paramBean = smr.unmarshall(actualParamEncStyle, 442 RPCConstants.Q_ELEM_PARAMETER, 443 tempEl, ctx); 444 Parameter param = (Parameter)paramBean.value; 445 446 if (declParamEncStyle != null) { 447 param.setEncodingStyleURI(declParamEncStyle); 448 } 449 450 params.addElement(param); 451 } 452 } 453 } 454 455 RPCMessage msg = isResponse 456 ? (fault == null 457 ? (RPCMessage)new Response(fullTargetObjectURI, methodName, 458 returnValue, params, null, 459 declMsgEncStyle, ctx) 460 : (RPCMessage)new Response(fullTargetObjectURI, methodName, 461 fault, params, null, 462 declMsgEncStyle, ctx)) 463 : (RPCMessage)new Call(fullTargetObjectURI, methodName, 464 params, null, actualMsgEncStyle, ctx); 465 466 if (msg instanceof Call) { 467 ((Call)msg).setSOAPMappingRegistry(smr); 468 } 469 470 return msg; 471 } 472 473 public String toString() { 474 StringWriter sw = new StringWriter(); 475 PrintWriter pw = new PrintWriter(sw); 476 boolean isResponse = this instanceof Response; 477 478 pw.print("[Header=" + header + "] " + 479 "[methodName=" + methodName + "] " + 480 "[targetObjectURI=" + targetObjectURI + "] " + 481 "[encodingStyleURI=" + encodingStyleURI + "] " + 482 "[SOAPContext=" + ctx + "] "); 483 484 if (isResponse) { 485 Response res = (Response)this; 486 487 if (res.generatedFault()) { 488 pw.print("[fault=" + res.getFault() + "] "); 489 } else { 490 pw.println("[return=" + res.getReturnValue() + "] "); 491 } 492 } 493 494 pw.print("[Params={"); 495 496 if (params != null) { 497 for (int i = 0; i < params.size(); i++) { 498 if (i > 0) { 499 pw.print(", "); 500 } 501 502 pw.print("[" + params.elementAt(i) + "]"); 503 } 504 } 505 506 pw.print("}]"); 507 508 return sw.toString(); 509 } 510 } 511 | Popular Tags |