private void serializeRuntimeException() throws WebServiceException {
    try {
      MessageFactory factory = _soapContext.getMessageFactory();
      SOAPMessage message = factory.createMessage();

      QName faultcode =
          new QName(
              message.getSOAPBody().getNamespaceURI(), "Server", message.getSOAPBody().getPrefix());

      message.getSOAPBody().addFault(faultcode, _runtimeException.getMessage());

      _soapContext.setMessage(message);
      _source = _soapContext.getMessage().getSOAPPart().getContent();
    } catch (SOAPException se) {
      throw new WebServiceException(se);
    }
  }
  private boolean handleFault(int i) {
    Handler handler = _chain.get(i);

    boolean success = false;
    _invoked[i] = true;

    try {
      if (handler instanceof LogicalHandler) {
        _logicalContext.getMessage().setPayload(_source);
        success = handler.handleFault(_logicalContext);
        _source = _logicalContext.getMessage().getPayload();
      } else if (handler instanceof SOAPHandler) {
        try {
          _soapContext.setMessage(_source);
          success = handler.handleFault(_soapContext);
          _source = _soapContext.getMessage().getSOAPPart().getContent();
        } catch (SOAPException e) {
          throw new WebServiceException(e);
        }
      } else {
        throw new WebServiceException(
            L.l("Unsupported Handler type: {0}", handler.getClass().getName()));
      }

      _protocolException = null;
    } catch (ProtocolException e) {
      _protocolException = e;
      serializeProtocolException();
    } catch (RuntimeException e) {
      _runtimeException = e;
      serializeRuntimeException();
    }

    return success;
  }
  private void serializeProtocolException() throws WebServiceException {
    if (_protocolException instanceof SOAPFaultException) {
      SOAPFaultException sfe = (SOAPFaultException) _protocolException;
      SOAPFault fault = sfe.getFault();

      try {
        MessageFactory factory = _soapContext.getMessageFactory();
        SOAPMessage message = factory.createMessage();
        message.getSOAPBody().addChildElement(fault);
        _soapContext.setMessage(message);
        _source = _soapContext.getMessage().getSOAPPart().getContent();
      } catch (SOAPException se) {
        throw new WebServiceException(se);
      }
    } else {
      throw new WebServiceException(L.l("Unsupported ProtocolException: {0}", _protocolException));
    }
  }
  private void close(int i) {
    Handler handler = _chain.get(i);

    if (!_invoked[i]) return;

    _invoked[i] = false;

    if (handler instanceof LogicalHandler) {
      _logicalContext.getMessage().setPayload(_source);
      handler.close(_logicalContext);
      _source = _logicalContext.getMessage().getPayload();
    } else if (handler instanceof SOAPHandler) {
      try {
        _soapContext.setMessage(_source);
        handler.close(_soapContext);
        _source = _soapContext.getMessage().getSOAPPart().getContent();
      } catch (SOAPException e) {
        throw new WebServiceException(e);
      }
    }
  }
 protected QName getOpQName(Exchange ex, Object data) {
   SOAPMessageContextImpl sm = (SOAPMessageContextImpl) data;
   try {
     SOAPMessage msg = sm.getMessage();
     if (msg == null) {
       return null;
     }
     SOAPBody body = msg.getSOAPBody();
     if (body == null) {
       return null;
     }
     org.w3c.dom.Node nd = body.getFirstChild();
     while (nd != null && !(nd instanceof org.w3c.dom.Element)) {
       nd = nd.getNextSibling();
     }
     if (nd != null) {
       return new QName(nd.getNamespaceURI(), nd.getLocalName());
     }
   } catch (SOAPException e) {
     // ignore, nothing we can do
   }
   return null;
 }
  @Override
  protected MessageContext createProtocolMessageContext(SoapMessage message) {
    SOAPMessageContextImpl sm = new SOAPMessageContextImpl(message);

    Exchange exch = message.getExchange();
    setupBindingOperationInfo(exch, sm);
    SOAPMessage msg = sm.getMessage();
    try {
      List<SOAPElement> params = new ArrayList<SOAPElement>();
      message.put(MessageContext.REFERENCE_PARAMETERS, params);
      SOAPHeader head = msg.getSOAPHeader();
      if (head != null) {
        Iterator<Node> it = CastUtils.cast(head.getChildElements());
        while (it != null && it.hasNext()) {
          Node nd = it.next();
          if (nd instanceof SOAPElement) {
            SOAPElement el = (SOAPElement) nd;
            if (el.hasAttributeNS(Names.WSA_NAMESPACE_NAME, "IsReferenceParameter")
                && ("1".equals(el.getAttributeNS(Names.WSA_NAMESPACE_NAME, "IsReferenceParameter"))
                    || Boolean.parseBoolean(
                        el.getAttributeNS(Names.WSA_NAMESPACE_NAME, "IsReferenceParameter")))) {
              params.add(el);
            }
          }
        }
      }
      if (msg.getSOAPPart().getEnvelope().getBody() != null
          && msg.getSOAPPart().getEnvelope().getBody().hasFault()) {
        return null;
      }
    } catch (SOAPException e) {
      throw new Fault(e);
    }

    return sm;
  }