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.request;
025
026import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
027import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
028import microsoft.exchange.webservices.data.core.ExchangeService;
029import microsoft.exchange.webservices.data.core.XmlElementNames;
030import microsoft.exchange.webservices.data.core.response.GetStreamingEventsResponse;
031import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
032import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
033import microsoft.exchange.webservices.data.core.exception.service.local.ServiceVersionException;
034import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
035
036import javax.xml.stream.XMLStreamException;
037
038/**
039 * Defines the GetStreamingEventsRequest class.
040 */
041public class GetStreamingEventsRequest extends HangingServiceRequestBase<GetStreamingEventsResponse> {
042
043  protected final static int HeartbeatFrequencyDefault = 45000; ////45s in ms
044  private static int heartbeatFrequency = HeartbeatFrequencyDefault;
045
046  private Iterable<String> subscriptionIds;
047  private int connectionTimeout;
048
049  /**
050   * Initializes a new instance of the GetStreamingEventsRequest class.
051   *
052   * @param service              The service
053   * @param serviceObjectHandler The serviceObjectHandler
054   * @param subscriptionIds      The subscriptionIds
055   * @param connectionTimeout    The connectionTimeout
056   * @throws ServiceVersionException
057   */
058  public GetStreamingEventsRequest(ExchangeService service, IHandleResponseObject serviceObjectHandler,
059      Iterable<String> subscriptionIds, int connectionTimeout)
060      throws ServiceVersionException {
061    super(service, serviceObjectHandler,
062        GetStreamingEventsRequest.heartbeatFrequency);
063    this.subscriptionIds = subscriptionIds;
064    this.connectionTimeout = connectionTimeout;
065  }
066
067  /**
068   * Gets the name of the XML element.
069   *
070   * @return XmlElementNames
071   */
072  @Override public String getXmlElementName() {
073    return XmlElementNames.GetStreamingEvents;
074  }
075
076  /**
077   * Gets the name of the response XML element.
078   *
079   * @return XmlElementNames
080   */
081  @Override
082  protected String getResponseXmlElementName() {
083    return XmlElementNames.GetStreamingEventsResponse;
084  }
085
086  /**
087   * Writes the elements to XML writer.
088   *
089   * @param writer the writer
090   * @throws XMLStreamException the XML stream exception
091   * @throws ServiceXmlSerializationException
092   */
093  @Override
094  protected void writeElementsToXml(EwsServiceXmlWriter writer)
095      throws ServiceXmlSerializationException, XMLStreamException {
096    writer.writeStartElement(XmlNamespace.Messages,
097        XmlElementNames.SubscriptionIds);
098
099    for (String id : this.subscriptionIds) {
100      writer.writeElementValue(
101          XmlNamespace.Types,
102          XmlElementNames.SubscriptionId,
103          id);
104    }
105
106    writer.writeEndElement();
107
108    writer.writeElementValue(
109        XmlNamespace.Messages,
110        XmlElementNames.ConnectionTimeout,
111        this.connectionTimeout);
112  }
113
114  /**
115   * Gets the request version.
116   *
117   * @return ExchangeVersion
118   */
119  @Override
120  protected ExchangeVersion getMinimumRequiredServerVersion() {
121    return ExchangeVersion.Exchange2010_SP1;
122  }
123
124  /**
125   * {@inheritDoc}
126   */
127  @Override
128  protected GetStreamingEventsResponse parseResponse(EwsServiceXmlReader reader)
129      throws Exception {
130    reader.readStartElement(XmlNamespace.Messages,
131        XmlElementNames.ResponseMessages);
132
133    GetStreamingEventsResponse response =
134        new GetStreamingEventsResponse(this);
135    response.loadFromXml(reader, XmlElementNames.
136        GetStreamingEventsResponseMessage);
137
138    reader.readEndElementIfNecessary(XmlNamespace.Messages,
139        XmlElementNames.ResponseMessages);
140
141    return response;
142  }
143
144  /**
145   * region Test hooks
146   * Allow test code to change heartbeat value
147   */
148  protected static void setHeartbeatFrequency(int heartbeatFrequency) {
149    GetStreamingEventsRequest.heartbeatFrequency = heartbeatFrequency;
150  }
151
152  @Override
153        protected HttpWebRequest buildEwsHttpWebRequest() throws Exception
154        {
155                return super.buildEwsHttpPoolingWebRequest();
156        }
157}