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.EwsUtilities;
028    import microsoft.exchange.webservices.data.core.ExchangeService;
029    import microsoft.exchange.webservices.data.core.XmlElementNames;
030    import microsoft.exchange.webservices.data.core.response.SubscribeResponse;
031    import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
032    import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
033    import microsoft.exchange.webservices.data.core.exception.misc.ArgumentException;
034    import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
035    import microsoft.exchange.webservices.data.notification.PushSubscription;
036    
037    import javax.xml.stream.XMLStreamException;
038    
039    import java.net.URI;
040    
041    /**
042     * The Class SubscribeToPushNotificationsRequest.
043     */
044    public class SubscribeToPushNotificationsRequest extends
045        SubscribeRequest<PushSubscription> {
046    
047      /**
048       * The frequency.
049       */
050      private int frequency = 30;
051    
052      /**
053       * The url.
054       */
055      private URI url;
056    
057      /**
058       * Instantiates a new subscribe to push notification request.
059       *
060       * @param service the service
061       * @throws Exception
062       */
063      public SubscribeToPushNotificationsRequest(ExchangeService service)
064          throws Exception {
065        super(service);
066      }
067    
068      /*
069       * (non-Javadoc)
070       *
071       * @see microsoft.exchange.webservices.SubscribeRequest#validate()
072       */
073      @Override
074      protected void validate() throws Exception {
075        super.validate();
076        EwsUtilities.validateParam(this.getUrl(), "Url");
077        if ((this.getFrequency() < 1) || (this.getFrequency() > 1440)) {
078          throw new ArgumentException(String.format(
079              "%d is not a valid frequency value. Valid values range from 1 to 1440.", this.getFrequency()));
080        }
081      }
082    
083      /*
084       * (non-Javadoc)
085       *
086       * @see
087       * microsoft.exchange.webservices.SubscribeRequest
088       * #getSubscriptionXmlElementName
089       * ()
090       */
091      @Override
092      protected String getSubscriptionXmlElementName() {
093        return XmlElementNames.PushSubscriptionRequest;
094      }
095    
096      /*
097       * (non-Javadoc)
098       *
099       * @see
100       * microsoft.exchange.webservices.SubscribeRequest
101       * #internalWriteElementsToXml
102       * (microsoft.exchange.webservices.EwsServiceXmlWriter)
103       */
104      @Override
105      protected void internalWriteElementsToXml(EwsServiceXmlWriter writer)
106          throws XMLStreamException, ServiceXmlSerializationException {
107        writer.writeElementValue(XmlNamespace.Types,
108            XmlElementNames.StatusFrequency, this.getFrequency());
109        writer.writeElementValue(XmlNamespace.Types, XmlElementNames.URL, this
110            .getUrl().toString());
111      }
112    
113      /*
114       * (non-Javadoc)
115       *
116       * @seemicrosoft.exchange.webservices.MultiResponseServiceRequest#
117       * createServiceResponse(microsoft.exchange.webservices.ExchangeService,
118       * int)
119       */
120      @Override
121      protected SubscribeResponse<PushSubscription> createServiceResponse(
122          ExchangeService service, int responseIndex) throws Exception {
123        return new SubscribeResponse<PushSubscription>(new PushSubscription(
124            service));
125      }
126    
127      /*
128       * (non-Javadoc)
129       *
130       * @seemicrosoft.exchange.webservices.ServiceRequestBase#
131       * getMinimumRequiredServerVersion()
132       */
133      @Override
134      protected ExchangeVersion getMinimumRequiredServerVersion() {
135        return ExchangeVersion.Exchange2007_SP1;
136      }
137    
138      /**
139       * Gets the frequency.
140       *
141       * @return the frequency
142       */
143      public int getFrequency() {
144        return this.frequency;
145      }
146    
147      /**
148       * Sets the frequency.
149       *
150       * @param frequency the new frequency
151       */
152      public void setFrequency(int frequency) {
153        this.frequency = frequency;
154      }
155    
156      /**
157       * Gets the url.
158       *
159       * @return the url
160       */
161      public URI getUrl() {
162        return this.url;
163      }
164    
165      /**
166       * Sets the url.
167       *
168       * @param url the new url
169       */
170      public void setUrl(URI url) {
171        this.url = url;
172      }
173    
174    }