private void setFaultReason(
      MessageContext synCtx, SOAPFactory factory, SOAPFault fault, int soapVersion) {
    String reasonString = null;

    if (faultReasonValue == null && faultReasonExpr == null) {
      handleException("A valid fault reason value or expression is required", synCtx);
    } else if (faultReasonValue != null) {
      reasonString = faultReasonValue;
    } else {
      reasonString = faultReasonExpr.stringValueOf(synCtx);
    }

    SOAPFaultReason reason = factory.createSOAPFaultReason();
    switch (soapVersion) {
      case SOAP11:
        reason.setText(reasonString);
        break;
      case SOAP12:
        SOAPFaultText text = factory.createSOAPFaultText();
        text.setText(reasonString);
        text.setLang("en");
        reason.addSOAPText(text);
        break;
    }
    fault.setReason(reason);
  }
  private void setFaultCode(
      MessageContext synCtx, SOAPFactory factory, SOAPFault fault, int soapVersion) {

    QName fault_code = null;

    if (faultCodeValue == null && faultCodeExpr == null) {
      handleException("A valid fault code QName value or expression is required", synCtx);
    } else if (faultCodeValue != null) {
      fault_code = faultCodeValue;
    } else {
      String codeStr = faultCodeExpr.stringValueOf(synCtx);
      fault_code = new QName(fault.getNamespace().getNamespaceURI(), codeStr);
    }

    SOAPFaultCode code = factory.createSOAPFaultCode();
    switch (soapVersion) {
      case SOAP11:
        code.setText(fault_code);
        break;
      case SOAP12:
        SOAPFaultValue value = factory.createSOAPFaultValue(code);
        value.setText(fault_code);
        break;
    }
    fault.setCode(code);
  }
 private void setFaultRole(SOAPFactory factory, SOAPFault fault) {
   if (faultRole != null) {
     SOAPFaultRole soapFaultRole = factory.createSOAPFaultRole();
     soapFaultRole.setRoleValue(faultRole.toString());
     fault.setRole(soapFaultRole);
   }
 }
 private void setFaultNode(SOAPFactory factory, SOAPFault fault) {
   if (faultNode != null) {
     SOAPFaultNode soapfaultNode = factory.createSOAPFaultNode();
     soapfaultNode.setNodeValue(faultNode.toString());
     fault.setNode(soapfaultNode);
   }
 }
 private void setFaultDetail(MessageContext synCtx, SOAPFactory factory, SOAPFault fault) {
   if (faultDetail != null) {
     SOAPFaultDetail soapFaultDetail = factory.createSOAPFaultDetail();
     soapFaultDetail.setText(faultDetail);
     fault.setDetail(soapFaultDetail);
   } else if (faultDetailExpr != null) {
     SOAPFaultDetail soapFaultDetail = factory.createSOAPFaultDetail();
     Object result = null;
     try {
       result = faultDetailExpr.evaluate(synCtx);
     } catch (JaxenException e) {
       handleException(
           "Evaluation of the XPath expression " + this.toString() + " resulted in an error", e);
     }
     if (result instanceof List) {
       List list = (List) result;
       for (Object obj : list) {
         if (obj instanceof OMNode) {
           soapFaultDetail.addChild((OMNode) obj);
         }
       }
     } else {
       soapFaultDetail.setText(faultDetailExpr.stringValueOf(synCtx));
     }
     fault.setDetail(soapFaultDetail);
   } else if (!faultDetailElements.isEmpty()) {
     SOAPFaultDetail soapFaultDetail = factory.createSOAPFaultDetail();
     for (OMElement faultDetailElement : faultDetailElements) {
       soapFaultDetail.addChild(faultDetailElement.cloneOMElement());
     }
     fault.setDetail(soapFaultDetail);
   } else if (fault.getDetail() != null) {
     // work around for a rampart issue in the following thread
     // http://www.nabble.com/Access-to-validation-error-message-tf4498668.html#a13284520
     fault.getDetail().detach();
   }
 }
  /**
   * Actual transformation of the current message into a fault message
   *
   * @param synCtx the current message context
   * @param soapVersion SOAP version of the resulting fault desired
   * @param synLog the Synapse log to use
   */
  private void makeSOAPFault(MessageContext synCtx, int soapVersion, SynapseLog synLog) {

    if (synLog.isTraceOrDebugEnabled()) {
      synLog.traceOrDebug("Creating a SOAP " + (soapVersion == SOAP11 ? "1.1" : "1.2") + " fault");
    }

    // get the correct SOAP factory to be used
    SOAPFactory factory =
        (soapVersion == SOAP11
            ? OMAbstractFactory.getSOAP11Factory()
            : OMAbstractFactory.getSOAP12Factory());

    // create the SOAP fault document and envelope
    OMDocument soapFaultDocument = factory.createOMDocument();
    SOAPEnvelope faultEnvelope = factory.getDefaultFaultEnvelope();
    soapFaultDocument.addChild(faultEnvelope);

    // create the fault element  if it is need
    SOAPFault fault = faultEnvelope.getBody().getFault();
    if (fault == null) {
      fault = factory.createSOAPFault();
    }

    // populate it
    setFaultCode(synCtx, factory, fault, soapVersion);
    setFaultReason(synCtx, factory, fault, soapVersion);
    setFaultNode(factory, fault);
    setFaultRole(factory, fault);
    setFaultDetail(synCtx, factory, fault);

    // set the all headers of original SOAP Envelope to the Fault Envelope
    if (synCtx.getEnvelope() != null) {
      SOAPHeader soapHeader = synCtx.getEnvelope().getHeader();
      if (soapHeader != null) {
        for (Iterator iter = soapHeader.examineAllHeaderBlocks(); iter.hasNext(); ) {
          Object o = iter.next();
          if (o instanceof SOAPHeaderBlock) {
            SOAPHeaderBlock header = (SOAPHeaderBlock) o;
            faultEnvelope.getHeader().addChild(header);
          } else if (o instanceof OMElement) {
            faultEnvelope.getHeader().addChild((OMElement) o);
          }
        }
      }
    }

    if (synLog.isTraceOrDebugEnabled()) {
      String msg =
          "Original SOAP Message : "
              + synCtx.getEnvelope().toString()
              + "Fault Message created : "
              + faultEnvelope.toString();
      if (synLog.isTraceTraceEnabled()) {
        synLog.traceTrace(msg);
      }
      if (log.isTraceEnabled()) {
        log.trace(msg);
      }
    }

    // overwrite current message envelope with new fault envelope
    try {
      synCtx.setEnvelope(faultEnvelope);
    } catch (AxisFault af) {
      handleException(
          "Error replacing current SOAP envelope " + "with the fault envelope", af, synCtx);
    }

    if (synCtx.getFaultTo() != null) {
      synCtx.setTo(synCtx.getFaultTo());
    } else if (synCtx.getReplyTo() != null) {
      synCtx.setTo(synCtx.getReplyTo());
    } else {
      synCtx.setTo(null);
    }

    // set original messageID as relatesTo
    if (synCtx.getMessageID() != null) {
      RelatesTo relatesTo = new RelatesTo(synCtx.getMessageID());
      synCtx.setRelatesTo(new RelatesTo[] {relatesTo});
    }

    synLog.traceOrDebug("End : Fault mediator");
  }