1 55 56 package org.jboss.axis.providers.java; 57 58 import org.jboss.axis.AxisFault; 59 import org.jboss.axis.Constants; 60 import org.jboss.axis.Message; 61 import org.jboss.axis.MessageContext; 62 import org.jboss.axis.MessagePart; 63 import org.jboss.axis.description.OperationDesc; 64 import org.jboss.axis.description.ParameterDesc; 65 import org.jboss.axis.description.ServiceDesc; 66 import org.jboss.axis.enums.Style; 67 import org.jboss.axis.enums.Use; 68 import org.jboss.axis.handlers.soap.SOAPService; 69 import org.jboss.axis.message.RPCElement; 70 import org.jboss.axis.message.RPCHeaderParam; 71 import org.jboss.axis.message.RPCParam; 72 import org.jboss.axis.message.RPCParamElementImpl; 73 import org.jboss.axis.message.SOAPBodyElementAxisImpl; 74 import org.jboss.axis.message.SOAPElementAxisImpl; 75 import org.jboss.axis.message.SOAPEnvelopeAxisImpl; 76 import org.jboss.axis.soap.SOAPConstants; 77 import org.jboss.axis.utils.JavaUtils; 78 import org.jboss.axis.utils.Messages; 79 import org.jboss.logging.Logger; 80 import org.w3c.dom.Element ; 81 82 import javax.activation.DataHandler ; 83 import javax.xml.namespace.QName ; 84 import javax.xml.rpc.JAXRPCException ; 85 import javax.xml.rpc.holders.Holder ; 86 import javax.xml.soap.AttachmentPart ; 87 import javax.xml.soap.SOAPHeaderElement ; 88 import java.util.ArrayList ; 89 import java.util.Arrays ; 90 import java.util.Iterator ; 91 import java.util.List ; 92 import java.util.Vector ; 93 94 100 public class RPCInvocation 101 { 102 103 private static Logger log = Logger.getLogger(RPCInvocation.class.getName()); 104 105 private RPCProvider rpcProvider; 106 private MessageContext messageContext; 107 private SOAPEnvelopeAxisImpl requestEnvelope; 108 private SOAPEnvelopeAxisImpl responseEnvelope; 109 private Object targetObject; 110 private OperationDesc operation; 111 private Object [] argValues; 112 private Object returnValue; 113 private RPCElement body; 114 private List outParams; 115 116 private boolean responsePrepared; 118 119 public RPCInvocation(RPCProvider rpcProvider, MessageContext messageContext, SOAPEnvelopeAxisImpl reqEnv, SOAPEnvelopeAxisImpl resEnv, Object targetObject) 120 { 121 this.rpcProvider = rpcProvider; 122 this.messageContext = messageContext; 123 this.requestEnvelope = reqEnv; 124 this.responseEnvelope = resEnv; 125 this.targetObject = targetObject; 126 } 127 128 131 public RPCInvocation(RPCInvocation invocation) 132 { 133 this.rpcProvider = invocation.rpcProvider; 134 this.messageContext = invocation.messageContext; 135 this.requestEnvelope = invocation.requestEnvelope; 136 this.responseEnvelope = invocation.responseEnvelope; 137 this.targetObject = invocation.targetObject; 138 this.responsePrepared = invocation.responsePrepared; 139 } 140 141 public RPCProvider getProvider() 142 { 143 return rpcProvider; 144 } 145 146 149 public void prepareFromRequestEnvelope() 150 { 151 152 log.debug("Enter: prepareFromRequestEnvelope\n" + requestEnvelope); 153 154 SOAPService service = messageContext.getService(); 155 ServiceDesc serviceDesc = service.getServiceDescription(); 156 operation = messageContext.getOperation(); 157 158 try 159 { 160 Vector bodies = requestEnvelope.getBodyElements(); 161 if (log.isDebugEnabled()) 162 { 163 log.debug(Messages.getMessage("bodyElems00", "" + bodies.size())); 164 log.debug(Messages.getMessage("bodyIs00", "" + bodies.get(0))); 165 } 166 167 for (int bNum = 0; body == null && bNum < bodies.size(); bNum++) 169 { 170 if (!(bodies.get(bNum) instanceof RPCElement)) 176 { 177 SOAPBodyElementAxisImpl bodyEl = (SOAPBodyElementAxisImpl)bodies.get(bNum); 178 if (bodyEl.isRoot() && operation != null && bodyEl.getID() == null) 182 { 183 ParameterDesc param = operation.getParameter(bNum); 184 if (param != null) 186 { 187 Object val = bodyEl.getValueAsType(param.getTypeQName()); 188 body = new RPCElement("", 189 operation.getName(), 190 new Object []{val}); 191 } 192 } 193 } 194 else 195 { 196 body = (RPCElement)bodies.get(bNum); 197 } 198 } 199 200 if (body == null) 203 { 204 if (!(serviceDesc.getStyle().equals(Style.DOCUMENT))) 206 { 207 throw new JAXRPCException (Messages.getMessage("noBody00")); 208 } 209 210 ArrayList ops = serviceDesc.getOperations(); 213 for (Iterator iterator = ops.iterator(); iterator.hasNext();) 214 { 215 OperationDesc desc = (OperationDesc)iterator.next(); 216 if (desc.getNumInParams() == 0) 217 { 218 messageContext.setOperation(desc); 220 body = new RPCElement(desc.getName()); 222 break; 224 } 225 } 226 227 if (body == null) 229 { 230 throw new JAXRPCException (Messages.getMessage("noBody00")); 231 } 232 } 233 234 String methodName = body.getMethodName(); 235 Vector args = body.getParams(); 236 int numArgs = args.size(); 237 238 operation = messageContext.getOperation(); 241 242 if (operation == null) 243 { 244 QName qname = new QName (body.getNamespaceURI(), 245 body.getName()); 246 operation = getOperation(serviceDesc, qname); 247 } 248 249 if (operation == null) 250 { 251 Vector headers = requestEnvelope.getHeaders(); 259 for (int i = 0; i < headers.size(); i++) 260 { 261 SOAPHeaderElement header = (SOAPHeaderElement )headers.elementAt(i); 262 if (header.getMustUnderstand() == true) 263 { 264 AxisFault fault = new AxisFault(Messages.getMessage("noSuchOperation", methodName)); 265 fault.setFaultCode(Constants.FAULT_MUSTUNDERSTAND); 266 267 throw new JAXRPCException (fault); 271 } 272 } 273 274 throw new JAXRPCException (Messages.getMessage("noSuchOperation", methodName)); 275 } 276 277 argValues = new Object [operation.getNumParams()]; 280 281 outParams = new ArrayList (); 283 284 boolean isGenericDocumentEndpoint = false; 287 Style style = serviceDesc.getStyle(); 288 Use use = serviceDesc.getUse(); 289 if (style.equals(Style.DOCUMENT) && use.equals(Use.LITERAL) && operation.getInParams().size() == 1) 290 { 291 ParameterDesc firstInParam = (ParameterDesc)operation.getInParams().get(0); 292 if (Element.class.isAssignableFrom(firstInParam.getJavaType())) 293 { 294 isGenericDocumentEndpoint = true; 295 argValues[0] = body; 296 } 297 } 298 299 if (isGenericDocumentEndpoint == false) 305 { 306 for (int i = 0; i < numArgs; i++) 307 { 308 RPCParam rpcParam = (RPCParam)args.get(i); 309 Object value = rpcParam.getValue(); 310 311 ParameterDesc paramDesc = rpcParam.getParamDesc(); 313 314 if (paramDesc != null && paramDesc.getJavaType() != null) 318 { 319 320 Class sigType = paramDesc.getJavaType(); 322 323 if (value instanceof AttachmentPart ) 325 { 326 if (sigType.equals(DataHandler .class)) 327 value = ((AttachmentPart )value).getDataHandler(); 328 else 329 value = ((AttachmentPart )value).getContent(); 330 } 331 332 value = JavaUtils.convert(value, sigType); 334 335 rpcParam.setValue(value); 336 if (paramDesc.getMode() == ParameterDesc.INOUT) 337 { 338 outParams.add(rpcParam); 339 } 340 } 341 342 if (paramDesc == null || paramDesc.getOrder() == -1) 345 { 346 argValues[i] = value; 347 } 348 else 349 { 350 argValues[paramDesc.getOrder()] = value; 351 } 352 353 if (log.isDebugEnabled()) 354 { 355 log.debug(" " + Messages.getMessage("value00", "" + argValues[i])); 356 } 357 } 358 } 359 360 String allowedMethods = (String )service.getOption("allowedMethods"); 363 rpcProvider.checkMethodName(messageContext, allowedMethods, operation.getName()); 364 365 int count = numArgs; 367 for (int i = 0; i < argValues.length; i++) 368 { 369 370 ParameterDesc param = operation.getParameter(i); 372 if (param.getMode() == ParameterDesc.IN) 373 continue; 374 375 Class holderClass = param.getJavaType(); 376 if (holderClass != null && 377 Holder .class.isAssignableFrom(holderClass)) 378 { 379 int index = count; 380 if (param.getMode() == ParameterDesc.OUT) 381 { 382 count++; 384 } 385 else if (param.getMode() == ParameterDesc.INOUT) 386 { 387 if (param.getOrder() != -1) 389 { 390 index = param.getOrder(); 391 } 392 else 393 { 394 count++; 395 } 396 if (argValues[index] != null) 398 { 399 continue; 400 } 401 } 402 argValues[index] = holderClass.newInstance(); 403 RPCParam p = new RPCParam(param.getQName(), 407 argValues[index]); 408 p.setParamDesc(param); 409 outParams.add(p); 410 } 411 else 412 { 413 String message = Messages.getMessage("badOutParameter00", "" + param.getQName(), operation.getName()); 414 log.error(message); 415 throw new JAXRPCException (message); 416 } 417 } 418 419 log.debug("Exit: prepareFromRequestEnvelope"); 420 } 421 catch (JAXRPCException e) 422 { 423 log.error(e.toString(), e); 424 throw e; 425 } 426 catch (Exception e) 427 { 428 log.error(e.toString(), e); 429 throw new JAXRPCException (e); 430 } 431 } 432 433 public OperationDesc getOperation(ServiceDesc serviceDesc, QName qname) 434 { 435 return serviceDesc.getOperationByElementQName(qname); 436 } 437 438 441 public void prepareResponseEnvelope(Object resObject) 442 { 443 444 returnValue = resObject; 445 log.debug("Enter: prepareResponseEnvelope: [resObject=" + resObject + "]"); 446 447 448 if (responsePrepared) 453 { 454 log.debug("Ignoring request to prepare response envelope"); 455 log.debug("Exit: prepareResponseEnvelope\n" + responseEnvelope); 456 return; 457 } 458 459 SOAPService service = messageContext.getService(); 460 ServiceDesc serviceDesc = service.getServiceDescription(); 461 462 ArrayList immutableElements = new ArrayList (); 463 464 try 465 { 466 String encodingStyle = messageContext.getEncodingStyle(); 470 String bodyEncStyle = responseEnvelope.getBody().getEncodingStyle(); 471 if (bodyEncStyle.equals(encodingStyle) == false) 472 { 473 if (bodyEncStyle.equals("")) 474 responseEnvelope.getBody().setEncodingStyle(encodingStyle); 475 else 476 log.warn("Mixed encoding styles are not supported: " + bodyEncStyle + "!=" + encodingStyle); 477 } 478 479 RPCElement resBody = null; 480 481 boolean isGenericDocumentReturn = false; 484 Style style = serviceDesc.getStyle(); 485 Use use = serviceDesc.getUse(); 486 if (style.equals(Style.DOCUMENT) && use.equals(Use.LITERAL) && resObject != null && Element.class.isAssignableFrom(resObject.getClass())) 487 { 488 Element resElement = (Element)resObject; 489 resBody = new RPCElement(resElement.getLocalName()); 490 resBody.setPrefix(resElement.getPrefix()); 491 resBody.setNamespaceURI(resElement.getNamespaceURI()); 492 immutableElements.add(resBody); 493 isGenericDocumentReturn = true; 494 } 495 496 497 498 if (isGenericDocumentReturn == false) 499 { 500 String methodName = operation.getMethod().getName(); 501 resBody = new RPCElement(methodName + "Response"); 502 resBody.setPrefix(body.getPrefix()); 503 resBody.setNamespaceURI(body.getNamespaceURI()); 504 immutableElements.add(resBody); 505 } 506 507 if (operation.getMethod().getReturnType() != Void.TYPE) 509 { 510 QName returnQName = operation.getReturnQName(); 511 if (returnQName == null) 512 { 513 String methodName = operation.getMethod().getName(); 514 returnQName = new QName (body.getNamespaceURI(), methodName + "Response"); 515 } 516 517 ParameterDesc retParamDesc = operation.getReturnParamDesc(); 518 QName retTypeQName = retParamDesc.getTypeQName(); 519 if (resObject != null && retTypeQName != null) 520 { 521 Class retType = messageContext.getTypeMapping().getClassForQName(retTypeQName); 522 if (retType != null && JavaUtils.isConvertable(resObject, retType)) 523 resObject = JavaUtils.convert(resObject, retType); 524 } 525 526 RPCParam param = new RPCParam(returnQName, resObject); 527 param.setParamDesc(retParamDesc); 528 param.setRPCCall(resBody); 529 530 if (operation.getUse().equals(Use.LITERAL)) 534 param.setXSITypeGeneration(Boolean.TRUE); 535 536 RPCParamElementImpl paramElement = new RPCParamElementImpl(param); 540 resBody.addChildElement(paramElement); 541 immutableElements.add(paramElement); 542 543 if (!operation.isReturnHeader()) 544 { 545 if (messageContext.getSOAPConstants() == SOAPConstants.SOAP12_CONSTANTS && 547 (serviceDesc.getStyle().equals(Style.RPC))) 548 { 549 RPCParam resultParam = new RPCParam(Constants.QNAME_RPC_RESULT, returnQName); 550 resultParam.setXSITypeGeneration(Boolean.FALSE); 551 resBody.addParam(resultParam); 552 } 553 resBody.addParam(param); 554 } 555 else 556 { 557 responseEnvelope.addHeader(new RPCHeaderParam(param)); 558 } 559 560 } 561 562 if (!outParams.isEmpty()) 564 { 565 for (Iterator i = outParams.iterator(); i.hasNext();) 566 { 567 RPCParam param = (RPCParam)i.next(); 569 Object value = param.getValue(); 570 if (value instanceof Holder ) 571 { 572 Holder holder = (Holder )value; 573 value = JavaUtils.getHolderValue(holder); 574 } 575 ParameterDesc paramDesc = param.getParamDesc(); 576 577 param.setValue(value); 578 if (paramDesc != null && paramDesc.isOutHeader()) 579 { 580 responseEnvelope.addHeader(new RPCHeaderParam(param)); 581 } 582 else 583 { 584 resBody.addParam(param); 585 } 586 } 587 } 588 589 responseEnvelope.addBodyElement(resBody); 590 responsePrepared = true; 591 592 Iterator it = immutableElements.iterator(); 594 while (it.hasNext()) 595 { 596 SOAPElementAxisImpl soapElement = (SOAPElementAxisImpl)it.next(); 597 soapElement.setImmutable(true); 598 } 599 600 messageContext.setPastPivot(true); 602 Message respMessage = (Message)messageContext.getMessage(); 603 ((MessagePart)respMessage.getSOAPPart()).setSOAPEnvelope(responseEnvelope); 604 605 log.debug("Exit: prepareResponseEnvelope\n" + responseEnvelope); 606 } 607 catch (JAXRPCException e) 608 { 609 throw e; 610 } 611 catch (Exception e) 612 { 613 throw new JAXRPCException (e); 614 } 615 } 616 617 public Object [] getArgValues() 618 { 619 return argValues; 620 } 621 622 public RPCElement getBody() 623 { 624 return body; 625 } 626 627 public MessageContext getMessageContext() 628 { 629 return messageContext; 630 } 631 632 public OperationDesc getOperation() 633 { 634 return operation; 635 } 636 637 public List getOutParams() 638 { 639 return outParams; 640 } 641 642 public SOAPEnvelopeAxisImpl getRequestEnvelope() 643 { 644 return requestEnvelope; 645 } 646 647 public SOAPEnvelopeAxisImpl getResponseEnvelope() 648 { 649 return responseEnvelope; 650 } 651 652 public Object getTargetObject() 653 { 654 return targetObject; 655 } 656 657 public String toString() 658 { 659 StringBuffer ret = new StringBuffer ("\nRPCInvocation:\n"); 660 ret.append("TargetObject=" + targetObject + "\n"); 661 ret.append("Operation=" + operation); 662 ret.append("ArgValues=" + (argValues != null ? Arrays.asList(argValues) : null) + "\n"); 663 ret.append("ReturnValue=" + returnValue + "\n"); 664 return ret.toString(); 665 } 666 } 667 | Popular Tags |