/**
   * Creates the WS-Security header necessary to send with an outgoing request.
   *
   * @param xmlWriter The XML writer to serialize the headers to.
   * @throws javax.xml.stream.XMLStreamException the xML stream exception
   */
  @Override
  public void serializeWSSecurityHeaders(XMLStreamWriter xmlWriter) throws XMLStreamException {
    EwsUtilities.EwsAssert(
        this.securityToken != null,
        "WSSecurityBasedCredentials.SerializeWSSecurityHeaders",
        "Security token cannot be null!");

    // <wsu:Timestamp wsu:Id="_timestamp">
    //   <wsu:Created>2007-09-20T01:13:10.468Z</wsu:Created>
    //   <wsu:Expires>2007-09-20T01:18:10.468Z</wsu:Expires>
    // </wsu:Timestamp>
    //
    String timestamp = null;
    if (this.addTimestamp) {
      Calendar utcNow = Calendar.getInstance();
      utcNow.add(Calendar.MINUTE, 5);
      timestamp = String.format(WSSecurityBasedCredentials.wsuTimeStampFormat, utcNow, utcNow);
    }

    // Format the WS-Security header based on all the information we have.
    String wsSecurityHeader =
        String.format(
            WSSecurityBasedCredentials.wsSecurityHeaderFormat, timestamp + this.securityToken);

    // And write the header out...
    xmlWriter.writeCharacters(wsSecurityHeader);
  }
  /**
   * Creates the WS-Addressing headers necessary to send with an outgoing request.
   *
   * @param xmlWriter The XML writer to serialize the headers to.
   * @param webMethodName The Web method being called.
   * @throws javax.xml.stream.XMLStreamException the xML stream exception
   */
  private void serializeWSAddressingHeaders(XMLStreamWriter xmlWriter, String webMethodName)
      throws XMLStreamException {
    EwsUtilities.EwsAssert(
        webMethodName != null,
        "WSSecurityBasedCredentials.SerializeWSAddressingHeaders",
        "Web method name cannot be null!");

    EwsUtilities.EwsAssert(
        this.ewsUrl != null,
        "WSSecurityBasedCredentials.SerializeWSAddressingHeaders",
        "EWS Url cannot be null!");

    // Format the WS-Addressing headers.
    String wsAddressingHeaders =
        String.format(
            WSSecurityBasedCredentials.wsAddressingHeadersFormat, webMethodName, this.ewsUrl);

    // And write them out...
    xmlWriter.writeCharacters(wsAddressingHeaders);
  }