001/*
002 * The MIT License
003 * Copyright (c) 2012 Microsoft Corporation
004 *
005 * Permission is hereby granted, free of charge, to any person obtaining a copy
006 * of this software and associated documentation files (the "Software"), to deal
007 * in the Software without restriction, including without limitation the rights
008 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
009 * copies of the Software, and to permit persons to whom the Software is
010 * furnished to do so, subject to the following conditions:
011 *
012 * The above copyright notice and this permission notice shall be included in
013 * all copies or substantial portions of the Software.
014 *
015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
016 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
017 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
019 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
020 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
021 * THE SOFTWARE.
022 */
023
024package microsoft.exchange.webservices.data.core.response;
025
026import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
027import microsoft.exchange.webservices.data.core.XmlElementNames;
028import microsoft.exchange.webservices.data.core.request.HangingServiceRequestBase;
029import microsoft.exchange.webservices.data.core.enumeration.misc.HangingRequestDisconnectReason;
030import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
031import microsoft.exchange.webservices.data.notification.GetStreamingEventsResults;
032import microsoft.exchange.webservices.data.security.XmlNodeType;
033
034import java.util.ArrayList;
035import java.util.List;
036
037/**
038 * Represents the response to a subscription event retrieval operation.
039 */
040public final class GetStreamingEventsResponse extends ServiceResponse {
041
042  private GetStreamingEventsResults results = new GetStreamingEventsResults();
043  private HangingServiceRequestBase request;
044
045
046  /**
047   * Enumeration of ConnectionStatus that can be returned by the server.
048   */
049  private enum ConnectionStatus {
050    /**
051     * Simple heartbeat
052     */
053    OK,
054
055    /**
056     * Server is closing the connection.
057     */
058    Closed
059  }
060
061  /**
062   * Initializes a new instance of the GetStreamingEventsResponse class.
063   *
064   * @param request The request
065   *                Request to disconnect when we get a close message.
066   */
067  public GetStreamingEventsResponse(HangingServiceRequestBase request) {
068    super();
069    List<String> string = new ArrayList<String>();
070    this.setErrorSubscriptionIds(string);
071    this.request = request;
072  }
073
074  /**
075   * Reads response elements from XML.
076   *
077   * @throws Exception
078   */
079  @Override
080  protected void readElementsFromXml(EwsServiceXmlReader reader)
081      throws Exception {
082    super.readElementsFromXml(reader);
083
084    reader.read();
085
086    if (reader.getLocalName().equals(XmlElementNames.Notifications)) {
087      this.results.loadFromXml(reader);
088    } else if (reader.getLocalName().equals(XmlElementNames.ConnectionStatus)) {
089      String connectionStatus = reader.readElementValue(XmlNamespace.
090          Messages, XmlElementNames.ConnectionStatus);
091
092      if (connectionStatus.equals(ConnectionStatus.Closed.toString())) {
093        this.request.disconnect(
094            HangingRequestDisconnectReason.Clean, null);
095      }
096    }
097  }
098
099  /**
100   * Loads extra error details from XML
101   *
102   * @throws Exception
103   */
104  @Override
105  protected boolean loadExtraErrorDetailsFromXml(EwsServiceXmlReader reader,
106      String xmlElementName) throws Exception {
107    boolean baseReturnVal = super.
108        loadExtraErrorDetailsFromXml(reader, xmlElementName);
109
110    if (reader.isStartElement(XmlNamespace.Messages, XmlElementNames.ErrorSubscriptionIds)) {
111      do {
112        reader.read();
113
114        if (reader.getNodeType().getNodeType() == XmlNodeType.START_ELEMENT &&
115            reader.getLocalName().equals(XmlElementNames.SubscriptionId)) {
116          this.getErrorSubscriptionIds().add(
117              reader.readElementValue(XmlNamespace.Messages,
118                  XmlElementNames.SubscriptionId));
119        }
120      }
121      while (!reader.isEndElement(XmlNamespace.Messages,
122          XmlElementNames.ErrorSubscriptionIds));
123
124      return true;
125    } else {
126      return baseReturnVal;
127    }
128  }
129
130  /**
131   * Gets event results from subscription.
132   */
133  public GetStreamingEventsResults getResults() {
134    return this.results;
135  }
136
137  private List<String> errorSubscriptionIds;
138
139  /**
140   * Gets the error subscription ids.
141   */
142  public List<String> getErrorSubscriptionIds() {
143    return this.errorSubscriptionIds;
144  }
145
146  /**
147   * Sets the error subscription ids.
148   */
149  private void setErrorSubscriptionIds(List<String> value) {
150    this.errorSubscriptionIds = value;
151  }
152
153
154}