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

📄 soaphandler.java

📁 jbpm-bpel-1.1.Beta3 JBoss jBPM Starters Kit  是一个综合包
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    }

    throw new BpelException(
        "current message context does not have a servlet context");
  }

  protected ObjectMessage sendRequest(SOAPMessage soapMessage,
      Session jmsSession, JbpmContext jbpmContext,
      IntegrationControl integrationControl) throws SOAPException, JMSException {
    // create a jms message to carry the incoming message
    ObjectMessage jmsRequest = jmsSession.createObjectMessage();

    // stamp the partner link on behalf of which this request comes
    PartnerLinkEntry partnerLinkEntry = integrationControl.getPartnerLinkEntry(partnerLinkHandle);
    long partnerLinkId = partnerLinkEntry.getId();
    jmsRequest.setLongProperty(IntegrationConstants.PARTNER_LINK_ID_PROP,
        partnerLinkId);

    /*
     * determine the operation to perform - the operation wrapper should be the
     * soap body's only child
     */
    SOAPBody soapBody = soapMessage.getSOAPBody();
    SOAPElement operationWrapper = XmlUtil.getElement(soapBody);
    String operationName = operationWrapper.getLocalName();
    // stamp the operation name
    jmsRequest.setStringProperty(IntegrationConstants.OPERATION_NAME_PROP,
        operationName);

    // get the operation definition
    PartnerLinkDefinition partnerLink = IntegrationSession.getInstance(
        jbpmContext).loadPartnerLinkDefinition(partnerLinkId);
    Operation operation = partnerLink.getMyRole().getPortType().getOperation(
        operationName, null, null);
    log.debug("received request: partnerLink="
        + partnerLink
        + ", operation="
        + operationName);

    // extract message content
    javax.wsdl.Message wsdlRequest = operation.getInput().getMessage();
    Map requestParts = getRequestParts(operationWrapper, wsdlRequest);
    jmsRequest.setObject((Serializable) requestParts);

    // fill message properties
    BpelDefinition process = integrationControl.getAppDescriptor()
        .findProcessDefinition(jbpmContext);
    MessageType requestType = process.getImports().getMessageType(
        wsdlRequest.getQName());
    fillCorrelationProperties(requestParts, jmsRequest,
        requestType.getPropertyAliases());

    // set up producer
    MessageProducer producer = jmsSession.createProducer(partnerLinkEntry.getDestination());
    try {
      // is the exchange pattern request/response?
      if (operation.getStyle().equals(OperationType.REQUEST_RESPONSE)) {
        TemporaryQueue replyTo = jmsSession.createTemporaryQueue();
        jmsRequest.setJMSReplyTo(replyTo);
        // have jms discard request message if response timeout expires
        producer.setTimeToLive(responseTimeout);
      }
      else {
        // have jms discard message if one-way timeout expires
        producer.setTimeToLive(oneWayTimeout);
      }

      // send request message
      producer.send(jmsRequest);
      log.debug("sent request: " + RequestListener.messageToString(jmsRequest));

      return jmsRequest;
    }
    finally {
      // release producer resources
      producer.close();
    }
  }

  private static Map getRequestParts(SOAPElement operationWrapper,
      javax.wsdl.Message requestMessage) throws JMSException, SOAPException {
    HashMap inputParts = new HashMap();
    SOAPEnvelope envelope = (SOAPEnvelope) operationWrapper.getParentElement()
        .getParentElement();
    Name nilName = envelope.createName(BpelConstants.ATTR_NIL, null,
        BpelConstants.NS_XML_SCHEMA_INSTANCE);

    // iterate through input message parts
    Iterator partIt = requestMessage.getParts().values().iterator();
    while (partIt.hasNext()) {
      Part requestPart = (Part) partIt.next();
      // get part accessor from operation wrapper
      String partName = requestPart.getName();
      SOAPElement partAccessor = XmlUtil.getElement(operationWrapper, partName);

      // BPEL-68 check for xsi:nil
      String nilValue = partAccessor.getAttributeValue(nilName);
      if (nilValue == null
          || DatatypeUtil.parseBoolean(nilValue) != Boolean.TRUE) {
        SOAPElement partValue = getPartValue(partAccessor, requestPart);
        // create a dom element with the same name as the operation part
        Element inputPart = XmlUtil.createElement(partValue.getNamespaceURI(),
            partValue.getLocalName());
        // add part to input message
        inputParts.put(partName, inputPart);
        // copy operation part to input part
        XmlUtil.copy(inputPart, partValue);
      }
      // else: do not copy the operation part
    }
    return inputParts;
  }

  private static SOAPElement getPartValue(SOAPElement partAccessor,
      Part requestPart) {
    SOAPElement partValue;
    // check referenced schema definition
    QName elementName = requestPart.getElementName();
    if (elementName != null) {
      // an element defines the part, take that element out of the part accessor
      partValue = XmlUtil.getElement(partAccessor,
          elementName.getNamespaceURI(), elementName.getLocalPart());
    }
    else {
      // a schema type defines the part, the part accessor is of that type
      partValue = partAccessor;
    }
    return partValue;
  }

  /**
   * Gets the values of message properties from the request message parts and
   * sets them in the property fields of the JMS message.
   * @param requestParts the parts extracted from the request SOAP message
   * @param jmsRequest the JMS message whose properties will be set
   * @param propertyAliases the property aliases associated with the request
   *        message type
   * @throws JMSException
   */
  private static void fillCorrelationProperties(Map requestParts,
      ObjectMessage jmsRequest, Map propertyAliases) throws JMSException {
    // easy way out: no property aliases
    if (propertyAliases == null)
      return;
    // iterate through the property aliases associated with the message type
    Iterator aliasEntryIt = propertyAliases.entrySet().iterator();
    while (aliasEntryIt.hasNext()) {
      Entry aliasEntry = (Entry) aliasEntryIt.next();
      QName propertyName = (QName) aliasEntry.getKey();
      PropertyAlias alias = (PropertyAlias) aliasEntry.getValue();
      // get part accessor from operation wrapper
      String partName = alias.getPart();
      Object value = requestParts.get(partName);
      if (value == null) {
        log.debug("message part is missing, cannot get property: property="
            + propertyName
            + ", part="
            + partName);
        continue;
      }
      // evaluate the query against the part value, if any
      Query query = alias.getQuery();
      if (query != null) {
        try {
          value = query.getEvaluator().evaluate((Element) value);
        }
        catch (BpelFaultException e) {
          // selection failures may arise from missing locations, don't set
          // those properties
          log.debug(
              "could not select message location, cannot get property: property="
                  + propertyName
                  + ", part="
                  + partName
                  + ", query="
                  + query.getText(), e);
          continue;
        }
      }
      // set the value in a jms message property field
      jmsRequest.setObjectProperty(propertyName.getLocalPart(),
          value instanceof Node ? XmlUtil.getStringValue((Node) value) : value);
    }
  }

  protected ObjectMessage receiveResponse(Session jmsSession,
      TemporaryQueue replyTo) throws JMSException, SOAPFaultException {
    // set up consumer
    MessageConsumer consumer = jmsSession.createConsumer(replyTo);
    try {
      // receive response message
      log.debug("listening for response: " + replyTo.getQueueName());
      ObjectMessage jmsResponse = (ObjectMessage) consumer.receive(responseTimeout);
      // did a message arrive in time?
      if (jmsResponse != null) {
        log.debug("received response: "
            + RequestListener.messageToString(jmsResponse));
        jmsResponse.acknowledge();
        return jmsResponse;
      }
      else {
        log.debug("response timeout expired: " + replyTo.getQueueName());
        throw new SOAPFaultException(SERVER_FAULTCODE, TIMEOUT_FAULTSTRING,
            null, null);
      }
    }
    finally {
      // release consumer resources
      consumer.close();
    }
  }

  protected void writeResponse(SOAPMessage soapMessage, Map responseParts,
      JbpmContext jbpmContext, IntegrationControl integrationControl)
      throws SOAPException {
    SOAPEnvelope envelope = soapMessage.getSOAPPart().getEnvelope();

    // remove existing body, it might be immutable
    SOAPBody body = envelope.getBody();
    body.detachNode();
    Name responseName = XmlUtil.getElement(body).getElementName();
    // re-create body
    body = envelope.addBody();
    SOAPBodyElement operationWrapper = body.addBodyElement(responseName);

    // retrieve partner link definition
    PartnerLinkEntry partnerLinkEntry = integrationControl.getPartnerLinkEntry(partnerLinkHandle);
    PartnerLinkDefinition partnerLink = IntegrationSession.getInstance(
        jbpmContext).loadPartnerLinkDefinition(partnerLinkEntry.getId());
    // get current operation
    String operationName = responseName.getLocalName();
    operationName = operationName.substring(0,
        operationName.indexOf("Response"));
    Operation operation = partnerLink.getMyRole().getPortType().getOperation(
        operationName, null, null);

    // iterate through the output parts
    Iterator partIt = operation.getOutput()
        .getMessage()
        .getParts()
        .values()
        .iterator();
    while (partIt.hasNext()) {
      Part part = (Part) partIt.next();
      writeResponsePart(part, responseParts, operationWrapper);
    }
  }

  private static void writeResponsePart(Part part, Map responseParts,
      SOAPElement operationWrapper) throws SOAPException {
    String partName = part.getName();
    // get part from response data
    Element responsePart = (Element) responseParts.get(partName);
    // create part accessor under parts wrapper
    SOAPElement partAccessor = XmlUtil.addChildElement(operationWrapper,
        partName);
    // check the referenced schema definition
    if (part.getElementName() != null) {
      // an element defines the part, write that element as a child of the part
      // accessor
      XmlUtil.copyChildElement(partAccessor, responsePart);
    }
    else {
      // a schema type defines the part, write on the part accessor itself
      XmlUtil.copy(partAccessor, responsePart);
    }
  }

  protected void writeFault(SOAPMessage soapMessage, Map faultParts,
      SOAPFaultException faultException) throws SOAPException {
    faultWriter.writeFault(soapMessage.getSOAPPart().getEnvelope(),
        faultException, faultParts);
  }

  private static SoapFaultWriter createSoapFaultWriter() {
    /*
     * obtain the group of saaj implementations with known issues and their
     * associated fault writers from the configuration
     */
    Map soapFaultWriters = (Map) JbpmConfiguration.Configs.getObject("jbpm.bpel.soap.fault.writers");
    if (soapFaultWriters != null) {
      try {
        /*
         * find out whether the concrete soap factory class is in the group of
         * saaj implementations with issues
         */
        String soapFactoryClassName = SOAPFactory.newInstance()
            .getClass()
            .getName();
        SoapFaultWriter faultWriter = (SoapFaultWriter) soapFaultWriters.get(soapFactoryClassName);

        if (faultWriter != null) {
          log.debug("using custom fault writer: soapFactory="
              + soapFactoryClassName
              + ", faultWriter="
              + faultWriter.getClass().getName());

          return faultWriter;
        }
      }
      catch (SOAPException e) {
        log.debug("could not instantiate soap factory", e);
      }
    }

    return new DefaultSoapFaultWriter();
  }
}

⌨️ 快捷键说明

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