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    
024    package microsoft.exchange.webservices.data.core.request;
025    
026    import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
027    import microsoft.exchange.webservices.data.core.ExchangeService;
028    import microsoft.exchange.webservices.data.core.XmlElementNames;
029    import microsoft.exchange.webservices.data.core.response.SubscribeResponse;
030    import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
031    import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
032    import microsoft.exchange.webservices.data.core.exception.misc.ArgumentException;
033    import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
034    import microsoft.exchange.webservices.data.notification.PullSubscription;
035    
036    import javax.xml.stream.XMLStreamException;
037    
038    /**
039     * Represents a "pull" Subscribe request.
040     */
041    public class SubscribeToPullNotificationsRequest extends
042        SubscribeRequest<PullSubscription> {
043    
044      /**
045       * The timeout.
046       */
047      private int timeout = 30;
048    
049      /**
050       * Instantiates a new subscribe to pull notification request.
051       *
052       * @param service the service
053       * @throws Exception the exception
054       */
055      public SubscribeToPullNotificationsRequest(ExchangeService service)
056          throws Exception {
057    
058        super(service);
059    
060      }
061    
062      /**
063       * Gets the timeout.
064       *
065       * @return the timeout
066       */
067      public int getTimeout() {
068        return this.timeout;
069      }
070    
071      /**
072       * Sets the time out.
073       *
074       * @param timeout the new time out
075       */
076      public void setTimeOut(int timeout) {
077        this.timeout = timeout;
078      }
079    
080      /**
081       * Validate request.
082       *
083       * @throws Exception the exception
084       */
085      protected void validate() throws Exception {
086        super.validate();
087        if ((this.getTimeout() < 1) || (this.getTimeout() > 1440)) {
088          throw new ArgumentException(String.format(
089              "%d is not a valid timeout value. Valid values range from 1 to 1440.", this.getTimeout()));
090        }
091      }
092    
093      /**
094       * Creates the service response.
095       *
096       * @param service       The service.
097       * @param responseIndex Index of the response.
098       * @return Service response.
099       * @throws Exception the exception
100       */
101      @Override
102      protected SubscribeResponse<PullSubscription> createServiceResponse(
103          ExchangeService service, int responseIndex) throws Exception {
104        return new SubscribeResponse<PullSubscription>(new PullSubscription(
105            service));
106      }
107    
108      /**
109       * Gets the minimum server version required to process this request.
110       *
111       * @return Exchange server version.
112       */
113      @Override
114      protected ExchangeVersion getMinimumRequiredServerVersion() {
115        return ExchangeVersion.Exchange2007_SP1;
116      }
117    
118      /**
119       * Gets the name of the subscription XML element.
120       *
121       * @return XML element name
122       */
123      @Override
124      protected String getSubscriptionXmlElementName() {
125        return XmlElementNames.PullSubscriptionRequest;
126      }
127    
128      /**
129       * Reads response elements from XML.
130       *
131       * @param writer the writer
132       * @throws XMLStreamException the XML stream exception
133       * @throws ServiceXmlSerializationException the service xml serialization exception
134       */
135      @Override
136      protected void internalWriteElementsToXml(EwsServiceXmlWriter writer)
137          throws XMLStreamException, ServiceXmlSerializationException {
138        writer.writeElementValue(XmlNamespace.Types, XmlElementNames.Timeout,
139            this.getTimeout());
140    
141      }
142    }