/**
   * Read items from XML.
   *
   * @param reader The reader
   * @param propertySet The property set
   * @param destinationList The list in which to add the read items.
   * @throws javax.xml.stream.XMLStreamException the xML stream exception
   * @throws ServiceXmlDeserializationException the service xml deserialization exception
   * @throws Exception the exception
   */
  private void internalReadItemsFromXml(
      EwsServiceXmlReader reader, PropertySet propertySet, List<TItem> destinationList)
      throws XMLStreamException, ServiceXmlDeserializationException, Exception {
    EwsUtilities.EwsAssert(
        destinationList != null,
        "FindItemResponse.InternalReadItemsFromXml",
        "destinationList is null.");

    reader.readStartElement(XmlNamespace.Types, XmlElementNames.Items);
    if (!reader.isEmptyElement()) {
      do {
        reader.read();

        if (reader.getNodeType().nodeType == XmlNodeType.START_ELEMENT) {
          Item item =
              EwsUtilities.createEwsObjectFromXmlElementName(
                  Item.class, reader.getService(), reader.getLocalName());

          if (item == null) {
            reader.skipCurrentElement();
          } else {
            item.loadFromXml(
                reader, true, /* clearPropertyBag */ propertySet, true /* summaryPropertiesOnly */);

            destinationList.add((TItem) item);
          }
        }
      } while (!reader.isEndElement(XmlNamespace.Types, XmlElementNames.Items));
    } else {
      reader.read();
    }
  }
  /**
   * * Writes elements and content to XML.
   *
   * @param writer the writer
   * @throws Exception the exception
   */
  @Override
  protected void writeElementsToXml(EwsServiceXmlWriter writer) throws Exception {
    super.writeElementsToXml(writer);
    // ExchangeVersion ev=writer.getService().getRequestedServerVersion();
    if (writer.getService().getRequestedServerVersion().ordinal()
        > ExchangeVersion.Exchange2007_SP1.ordinal()) {
      writer.writeElementValue(
          XmlNamespace.Types, XmlElementNames.IsContactPhoto, this.isContactPhoto);
    }

    writer.writeStartElement(XmlNamespace.Types, XmlElementNames.Content);

    if (!(this.fileName == null || this.fileName.isEmpty())) {
      File fileStream = new File(this.fileName);
      FileInputStream fis = null;
      try {
        fis = new FileInputStream(fileStream);
        writer.writeBase64ElementValue(fis);
      } finally {
        if (fis != null) {
          fis.close();
        }
      }

    } else if (this.contentStream != null) {
      writer.writeBase64ElementValue(this.contentStream);
    } else if (this.content != null) {
      writer.writeBase64ElementValue(this.content);
    } else {
      EwsUtilities.EwsAssert(
          false, "FileAttachment.WriteElementsToXml", "The attachment's content is not set.");
    }

    writer.writeEndElement();
  }
  /**
   * 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
  protected 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);
  }
  /**
   * Initializes a new instance of the FindItemResponse class.
   *
   * @param isGrouped if set to true if grouped.
   * @param propertySet The property Set
   */
  protected FindItemResponse(boolean isGrouped, PropertySet propertySet) {
    super();
    this.isGrouped = isGrouped;
    this.propertySet = propertySet;

    EwsUtilities.EwsAssert(
        this.propertySet != null, "FindItemResponse.ctor", "PropertySet should not be null");
  }
  /**
   * Initializes a new instance of the class.
   *
   * @param referenceItem the reference item
   * @throws Exception the exception
   */
  protected SuppressReadReceipt(Item referenceItem) throws Exception {
    super(referenceItem.getService());
    EwsUtilities.EwsAssert(
        referenceItem != null, "SuppressReadReceipt.ctor", "referenceItem is null");

    referenceItem.throwIfThisIsNew();
    this.referenceItem = referenceItem;
  }
  /**
   * Initializes a new instance of the class.
   *
   * @param userConfiguration the user configuration
   */
  protected GetUserConfigurationResponse(UserConfiguration userConfiguration) {
    super();
    EwsUtilities.EwsAssert(
        userConfiguration != null,
        "GetUserConfigurationResponse.ctor",
        "userConfiguration is null");

    this.userConfiguration = userConfiguration;
  }
  /**
   * 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);
  }
  /**
   * GetXmlElementName retrieves the XmlElementName of this type based on the EwsObjectDefinition
   * attribute that decorates it, if present.
   *
   * @return The XML element name associated with this type.
   */
  protected String getXmlElementName() {
    if (this.isNullOrEmpty(this.xmlElementName)) {
      this.xmlElementName = this.getXmlElementNameOverride();
      if (this.isNullOrEmpty(this.xmlElementName)) {
        synchronized (this.lockObject) {
          ServiceObjectDefinition annotation =
              this.getClass().getAnnotation(ServiceObjectDefinition.class);
          if (null != annotation) {
            this.xmlElementName = annotation.xmlElementName();
          }
        }
      }
    }
    EwsUtilities.EwsAssert(
        !isNullOrEmpty(this.xmlElementName),
        "EwsObject.GetXmlElementName",
        String.format(
            "The class %s does not have an " + "associated XML element name.",
            this.getClass().getName()));

    return this.xmlElementName;
  }