public void reverseDirection() {
    Boolean direction = (Boolean) _logicalContext.get(MESSAGE_OUTBOUND_PROPERTY);

    if (direction != null) {
      if (Boolean.TRUE.equals(direction)) {
        _logicalContext.put(MESSAGE_OUTBOUND_PROPERTY, Boolean.FALSE);
        _soapContext.put(MESSAGE_OUTBOUND_PROPERTY, Boolean.FALSE);
      } else {
        _logicalContext.put(MESSAGE_OUTBOUND_PROPERTY, Boolean.TRUE);
        _soapContext.put(MESSAGE_OUTBOUND_PROPERTY, Boolean.TRUE);
      }
    }
  }
  /** Invoke the handler chain for an outbound message. */
  public boolean invokeOutbound(Source source, Map<String, DataHandler> attachments)
      throws WebServiceException {
    _source = source;

    // Set the mandatory properties
    _logicalContext.put(MESSAGE_OUTBOUND_PROPERTY, Boolean.TRUE);
    _soapContext.put(MESSAGE_OUTBOUND_PROPERTY, Boolean.TRUE);

    _logicalContext.put(OUTBOUND_MESSAGE_ATTACHMENTS, attachments);
    _soapContext.put(OUTBOUND_MESSAGE_ATTACHMENTS, attachments);

    for (_i = 0; _i < _chain.size(); _i++) {
      boolean success = handleMessage(_i);

      if (_protocolException != null) return false;

      if (_runtimeException != null) return false;

      if (!success) return false;
    }

    return true;
  }
  /** Invoke the handler chain for an inbound message. */
  public boolean invokeInbound(InputStream in, Map<String, DataHandler> attachments)
      throws WebServiceException {
    _outbound = false;
    _source = null;

    try {
      DOMResult dom = new DOMResult();
      getTransformer().transform(new StreamSource(in), dom);

      // XXX The TCK seems to assume a source that will stand up to repeated
      // reads... meaning that StreamSource and SAXSource are out, so DOM
      // must be what they want.
      _source = new DOMSource(dom.getNode());
    } catch (Exception e) {
      throw new WebServiceException(e);
    }

    // Set the mandatory properties
    _logicalContext.put(MESSAGE_OUTBOUND_PROPERTY, Boolean.FALSE);
    _soapContext.put(MESSAGE_OUTBOUND_PROPERTY, Boolean.FALSE);

    _logicalContext.put(INBOUND_MESSAGE_ATTACHMENTS, attachments);
    _soapContext.put(INBOUND_MESSAGE_ATTACHMENTS, attachments);

    // NOTE: the order is reversed for inbound messages
    for (_i = _chain.size() - 1; _i >= 0; _i--) {
      boolean success = handleMessage(_i);

      if (_protocolException != null) return false;

      if (_runtimeException != null) return false;

      if (!success) return false;
    }

    return true;
  }
  /**
   * When a message direction is reversed within the chain, this method runs the message backwards
   * through the previous handlers. This method should only be invoked when a handler returns false,
   * but does not throw any kind of exception.
   */
  public boolean uninvokeInbound() throws WebServiceException {
    // Set the mandatory properties
    _logicalContext.put(MESSAGE_OUTBOUND_PROPERTY, Boolean.TRUE);
    _soapContext.put(MESSAGE_OUTBOUND_PROPERTY, Boolean.TRUE);

    for (_i++; _i < _chain.size(); _i++) {
      boolean success = handleMessage(_i);

      if (_protocolException != null) return false;

      if (_runtimeException != null) return false;

      if (!success) return false;
    }

    return true;
  }
  /**
   * When a message direction is reversed within the chain, this method runs the message backwards
   * through the previous handlers. This method should only be invoked when a handler returns false,
   * but does not throw any kind of exception.
   */
  public boolean uninvokeOutbound() throws WebServiceException {
    // Set the mandatory properties
    _logicalContext.put(MESSAGE_OUTBOUND_PROPERTY, Boolean.FALSE);
    _soapContext.put(MESSAGE_OUTBOUND_PROPERTY, Boolean.FALSE);

    // NOTE: the order is reversed for inbound messages
    for (_i--; _i >= 0; _i--) {
      boolean success = handleMessage(_i);

      if (_protocolException != null) return false;

      if (_runtimeException != null) return false;

      if (!success) return false;
    }

    return true;
  }