Esempio n. 1
0
  /**
   * Creates a DOM Document using the SOAP Envelope.
   *
   * @param env An org.apache.axiom.soap.SOAPEnvelope instance
   * @return Returns the DOM Document of the given SOAP Envelope.
   * @throws Exception
   */
  public static Document getDocumentFromSOAPEnvelope(SOAPEnvelope env, boolean useDoom)
      throws WSSecurityException {
    try {
      if (env instanceof Element) {
        return ((Element) env).getOwnerDocument();
      }

      if (useDoom) {
        env.build();

        // Workaround to prevent a bug in AXIOM where
        // there can be an incomplete OMElement as the first child body
        OMElement firstElement = env.getBody().getFirstElement();
        if (firstElement != null) {
          firstElement.build();
        }

        // Get processed headers
        SOAPHeader soapHeader = env.getHeader();
        ArrayList processedHeaderQNames = new ArrayList();
        if (soapHeader != null) {
          Iterator headerBlocs = soapHeader.getChildElements();
          while (headerBlocs.hasNext()) {
            SOAPHeaderBlock element = (SOAPHeaderBlock) headerBlocs.next();
            if (element.isProcessed()) {
              processedHeaderQNames.add(element.getQName());
            }
          }
        }

        // Check the namespace and find SOAP version and factory
        String nsURI = null;
        SOAPFactory factory;
        if (env.getNamespace()
            .getNamespaceURI()
            .equals(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI)) {
          nsURI = SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI;
          factory = DOOMAbstractFactory.getSOAP11Factory();
        } else {
          nsURI = SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI;
          factory = DOOMAbstractFactory.getSOAP12Factory();
        }

        StAXSOAPModelBuilder stAXSOAPModelBuilder =
            new StAXSOAPModelBuilder(env.getXMLStreamReader(), factory, nsURI);
        SOAPEnvelope envelope = (stAXSOAPModelBuilder).getSOAPEnvelope();
        ((OMNode) envelope.getParent()).build();

        // Set the processed flag of the processed headers
        SOAPHeader header = envelope.getHeader();
        for (Iterator iter = processedHeaderQNames.iterator(); iter.hasNext(); ) {
          QName name = (QName) iter.next();
          Iterator omKids = header.getChildrenWithName(name);
          if (omKids.hasNext()) {
            ((SOAPHeaderBlock) omKids.next()).setProcessed();
          }
        }

        Element envElem = (Element) envelope;
        return envElem.getOwnerDocument();
      } else {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        env.build();
        env.serialize(baos);
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        return factory.newDocumentBuilder().parse(bais);
      }
    } catch (Exception e) {
      throw new WSSecurityException("Error in converting SOAP Envelope to Document", e);
    }
  }
  public boolean mediate(MessageContext synCtx) {

    if (synCtx.getEnvironment().isDebugEnabled()) {
      if (super.divertMediationRoute(synCtx)) {
        return true;
      }
    }

    SynapseLog synLog = getLog(synCtx);

    if (synLog.isTraceOrDebugEnabled()) {
      synLog.traceOrDebug("Start : Fault mediator");

      if (synLog.isTraceTraceEnabled()) {
        synLog.traceTrace("Message : " + synCtx.getEnvelope());
      }
    }

    switch (soapVersion) {
      case SOAP11:
        makeSOAPFault(synCtx, SOAP11, synLog);
        break;
      case SOAP12:
        makeSOAPFault(synCtx, SOAP12, synLog);
        break;
      case POX:
        makePOXFault(synCtx, synLog);
        break;

      default:
        {
          // if this is a POX or REST message then make a POX fault
          if (synCtx.isDoingPOX() || synCtx.isDoingGET()) {

            makePOXFault(synCtx, synLog);

          } else {

            // determine from current message's SOAP envelope namespace
            SOAPEnvelope envelop = synCtx.getEnvelope();
            if (envelop != null) {

              if (SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(
                  envelop.getNamespace().getNamespaceURI())) {
                makeSOAPFault(synCtx, SOAP12, synLog);

              } else {
                makeSOAPFault(synCtx, SOAP11, synLog);
              }

            } else {
              // default to SOAP 11
              makeSOAPFault(synCtx, SOAP11, synLog);
            }
          }
        }
    }

    final Pipe pipe =
        (Pipe)
            (((Axis2MessageContext) synCtx))
                .getAxis2MessageContext()
                .getProperty(PassThroughConstants.PASS_THROUGH_PIPE);
    if (pipe != null) {
      // cleaning the OUTPUT PIPE with older references
      // if there is a [protocal violation when sending out message etc.]
      pipe.getBuffer().clear();
      pipe.resetOutputStream();
    }

    // if the message has to be marked as a response mark it as response
    if (markAsResponse) {
      synCtx.setResponse(true);
      synCtx.setTo(synCtx.getReplyTo());
    }

    return true;
  }