⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 soapclient.java

📁 jbpm-bpel-1.1.Beta3 JBoss jBPM Starters Kit  是一个综合包
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    }
    // obtain the definitions of parts which appear inside the body
    Message message = bindOperation.getOperation().getInput().getMessage();
    Collection parts = getParts(message, soapBody.getParts());
    // fill in part values
    Iterator partIt = parts.iterator();
    switch (parts.size()) {
    case 0:
      break;
    case 1: {
      Part part = (Part) partIt.next();
      if (part.getTypeName() != null) {
        // a schema type defines the content of the body
        XmlUtil.copy(envelope.getBody(),
            (Element) inputParts.get(part.getName()));
      }
      else {
        writeDocumentPart(part, inputParts, envelope);
      }
      break;
    }
    default:
      while (partIt.hasNext()) {
        Part part = (Part) partIt.next();
        writeDocumentPart(part, inputParts, envelope);
      }
    }
  }

  private static Collection getParts(Message message, List partNames) {
    return message.getOrderedParts(partNames);
  }

  private static void writeDocumentPart(Part part, Map inputParts,
      SOAPEnvelope envelope) throws SOAPException {
    QName elementName = part.getElementName();
    if (elementName == null) {
      throw new BpelException("only one part may be specified if a "
          + "type defines the contents of the body for document style");
    }
    String namespaceUri = elementName.getNamespaceURI();
    String localName = elementName.getLocalPart();
    // get part element from input message
    Element inputPartElem = (Element) inputParts.get(part.getName());
    // create part element directly under soap body
    Name bodyPartName = envelope.createName(localName,
        inputPartElem.getPrefix(), namespaceUri);
    SOAPElement bodyPartElem = envelope.getBody().addBodyElement(bodyPartName);
    XmlUtil.copy(bodyPartElem, inputPartElem);
  }

  public Map readMessage(String operation, SOAPEnvelope envelope)
      throws SOAPException {
    BindingOperation bindOperation = binding.getBindingOperation(operation,
        null, null);
    // obtain info on the operation as a whole
    SOAPOperation soapOperation = (SOAPOperation) WsdlUtil.getExtension(
        bindOperation.getExtensibilityElements(),
        SOAPConstants.Q_ELEM_SOAP_OPERATION);
    // determine whether the operation is rpc-oriented or document-oriented
    String style = soapOperation.getStyle();
    if (style == null) {
      // default to the value specified in the soap:binding element
      SOAPBinding soapBinding = (SOAPBinding) WsdlUtil.getExtension(
          binding.getExtensibilityElements(), SOAPConstants.Q_ELEM_SOAP_BINDING);
      style = soapBinding.getStyle();
      if (style == null) {
        // if the soap:binding element does not specify a style, 'document' is
        // assumed
        style = SoapBindConstants.DOCUMENT_STYLE;
      }
    }
    // read body element
    Map outputParts;
    if (SoapBindConstants.DOCUMENT_STYLE.equals(style)) {
      outputParts = readDocumentBody(bindOperation, envelope);
    }
    else {
      outputParts = readRpcBody(bindOperation, envelope);
    }
    return outputParts;
  }

  protected Map readRpcBody(BindingOperation bindOperation,
      SOAPEnvelope envelope) throws SOAPException {
    // obtain info on how message parts appear inside the body element
    List extensions = bindOperation.getBindingOutput()
        .getExtensibilityElements();
    javax.wsdl.extensions.soap.SOAPBody soapBody = (javax.wsdl.extensions.soap.SOAPBody) WsdlUtil.getExtension(
        extensions, SOAPConstants.Q_ELEM_SOAP_BODY);
    // exclude the use of encodings
    if (SoapBindConstants.ENCODED_USE.equals(soapBody.getUse())) {
      throw new BpelException("encoded use not supported");
    }
    // obtain the definitions of parts which appear inside the body
    Message message = bindOperation.getOperation().getOutput().getMessage();
    Collection parts = getParts(message, soapBody.getParts());
    // get the operation wrapper
    SOAPElement operationElem = XmlUtil.getElement(envelope.getBody());
    // create the output message instance
    Map outputParts = new HashMap();
    // fill in part values
    Iterator partIt = parts.iterator();
    while (partIt.hasNext()) {
      Part part = (Part) partIt.next();
      String partName = part.getName();
      // get part accessor from operation wrapper
      SOAPElement operPartAccessor = XmlUtil.getElement(operationElem, partName);
      // create part accessor in output message
      Element outputPartAccessor = XmlUtil.createElement(partName);
      outputParts.put(partName, outputPartAccessor);
      // copy operation part to output part
      XmlUtil.copy(outputPartAccessor, operPartAccessor);
    }
    return outputParts;
  }

  protected Map readDocumentBody(BindingOperation bindOperation,
      SOAPEnvelope envelope) throws SOAPException {
    // obtain info on how message parts appear inside the body element
    BindingOutput bindOutput = bindOperation.getBindingOutput();
    javax.wsdl.extensions.soap.SOAPBody soapBody = (javax.wsdl.extensions.soap.SOAPBody) WsdlUtil.getExtension(
        bindOutput.getExtensibilityElements(), SOAPConstants.Q_ELEM_SOAP_BODY);
    // exclude the use of encodings
    if (SoapBindConstants.ENCODED_USE.equals(soapBody.getUse())) {
      throw new BpelException("encoded use not supported");
    }
    // obtain the definitions of parts which appear inside the body
    Message message = bindOperation.getOperation().getOutput().getMessage();
    Collection parts = getParts(message, soapBody.getParts());
    // create the output message data
    Map outputParts = new HashMap();
    // fill in part values
    Iterator partIt = parts.iterator();
    switch (parts.size()) {
    case 0:
      break;
    case 1: {
      Part part = (Part) partIt.next();
      /*
       * the contents of a composite body can be defined using a type; in this
       * usage, only one part may be specified
       */
      if (part.getTypeName() != null) {
        // create part accessor in output message
        String partName = part.getName();
        Element outputPartAccessor = XmlUtil.createElement(partName);
        outputParts.put(partName, outputPartAccessor);
        // copy body to output part directly
        XmlUtil.copy(outputPartAccessor, envelope.getBody());
      }
      else {
        readDocumentPart(part, outputParts, envelope);
      }
      break;
    }
    default:
      while (partIt.hasNext()) {
        Part part = (Part) partIt.next();
        if (part.getTypeName() != null) {
          throw new BpelException(
              "only one part may be specified if a type defines the contents of a document-style body");
        }
        readDocumentPart(part, outputParts, envelope);
      }
    }
    return outputParts;
  }

  private static void readDocumentPart(Part part, Map outputParts,
      SOAPEnvelope envelope) throws SOAPException {
    // get part element from soap body
    QName elementName = part.getElementName();
    SOAPElement bodyPartElem = XmlUtil.getElement(envelope.getBody(),
        elementName.getNamespaceURI(), elementName.getLocalPart());
    // create part element under output message
    Element outputPartElem = XmlUtil.createElement(elementName);
    outputParts.put(part.getName(), outputPartElem);
    // copy body part to output part
    XmlUtil.copy(outputPartElem, bodyPartElem);
  }

  public FaultInstance readFault(String operation, SOAPEnvelope envelope)
      throws SOAPException {
    SOAPElement detail = XmlUtil.getElement(envelope.getBody().getFault(),
        "detail");
    if (detail == null) {
      throw new BpelException("soap fault does not include a detail element");
    }
    BindingOperation bindOperation = binding.getBindingOperation(operation,
        null, null);
    // look for a wsdl binding fault matching the contents of the soap fault
    // detail element
    Iterator faultIt = bindOperation.getOperation()
        .getFaults()
        .values()
        .iterator();
    while (faultIt.hasNext()) {
      Fault fault = (Fault) faultIt.next();
      // part definition
      Message message = fault.getMessage();
      Map parts = message.getParts();
      if (parts.size() != 1) {
        throw new BpelException("fault message must have a single part");
      }
      Part part = (Part) parts.values().iterator().next();
      QName elementName = part.getElementName();
      if (elementName == null) {
        throw new BpelException("fault message parts must reference an element");
      }
      // locate the element referenced by the part under the soap detail element
      SOAPElement detailPartValue = XmlUtil.getElement(detail,
          elementName.getNamespaceURI(), elementName.getLocalPart());
      if (detailPartValue != null) {
        // obtain info on the contents of the soap detail element
        BindingFault bindFault = bindOperation.getBindingFault(fault.getName());
        SOAPFault soapFault = (SOAPFault) WsdlUtil.getExtension(
            bindFault.getExtensibilityElements(),
            SOAPConstants.Q_ELEM_SOAP_FAULT);
        // exclude the use of encodings
        if (SoapBindConstants.ENCODED_USE.equals(soapFault.getUse())) {
          throw new BpelException("encoded use not supported");
        }
        // create the fault message
        MessageValue messageValue = new MessageValue(getMessageType(message));
        // create part element under fault message
        messageValue.setPart(part.getName(), detailPartValue);
        /*
         * a WSDL fault is identified by a qualified name formed by the target
         * namespace of the corresponding portType and the fault name local to
         * the operation
         */
        QName faultName = new QName(getBinding().getPortType()
            .getQName()
            .getNamespaceURI(), fault.getName());
        return new FaultInstance(faultName, messageValue);
      }
    }
    throw new BpelException(
        "no wsdl fault matches the contents of the soap detail element: "
            + detail);
  }

  private static MessageType getMessageType(Message message) {
    /*
     * create a transient message type for now,
     * org.jbpm.bpel.graph.scope.FaultActionHandler replaces it with the
     * corresponding persistent object
     */
    return new MessageType(message);
  }

  public String toString() {
    return new ToStringBuilder(this).append("address", address).append(
        "binding", binding.getQName()).toString();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -