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.autodiscover.request;
025
026import microsoft.exchange.webservices.data.autodiscover.AutodiscoverService;
027import microsoft.exchange.webservices.data.autodiscover.enumeration.AutodiscoverErrorCode;
028import microsoft.exchange.webservices.data.autodiscover.response.AutodiscoverResponse;
029import microsoft.exchange.webservices.data.autodiscover.response.GetDomainSettingsResponseCollection;
030import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
031import microsoft.exchange.webservices.data.core.EwsUtilities;
032import microsoft.exchange.webservices.data.core.XmlElementNames;
033import microsoft.exchange.webservices.data.autodiscover.enumeration.DomainSettingName;
034import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
035import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
036import microsoft.exchange.webservices.data.core.exception.service.local.ServiceValidationException;
037import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
038
039import javax.xml.stream.XMLStreamException;
040
041import java.net.URI;
042import java.util.List;
043
044/**
045 * Represents a GetDomainSettings request.
046 */
047public class GetDomainSettingsRequest extends AutodiscoverRequest {
048
049  /**
050   * Action Uri of Autodiscover.GetDomainSettings method.
051   */
052  private static final String GetDomainSettingsActionUri =
053      EwsUtilities.AutodiscoverSoapNamespace +
054          "/Autodiscover/GetDomainSettings";
055
056  /**
057   * The domains.
058   */
059  private List<String> domains;
060
061  /**
062   * The settings.
063   */
064  private List<DomainSettingName> settings;
065
066  private ExchangeVersion requestedVersion;
067
068  /**
069   * Initializes a new instance of the {@link GetDomainSettingsRequest} class.
070   *
071   * @param service the service
072   * @param url     the url
073   */
074  public GetDomainSettingsRequest(AutodiscoverService service, URI url) {
075    super(service, url);
076  }
077
078  /**
079   * Validates the request.
080   *
081   * @throws Exception the exception
082   */
083  @Override
084  protected void validate() throws Exception {
085    super.validate();
086
087    EwsUtilities.validateParam(this.getDomains(), "domains");
088    EwsUtilities.validateParam(this.getSettings(), "settings");
089
090    if (this.getSettings().size() == 0) {
091      throw new ServiceValidationException("At least one setting must be requested.");
092    }
093
094    if (domains.size() == 0) {
095      throw new ServiceValidationException("At least one domain name must be requested.");
096    }
097
098    for (String domain : this.getDomains()) {
099      if (domain == null || domain.isEmpty()) {
100        throw new ServiceValidationException("The domain name must be specified.");
101      }
102    }
103  }
104
105  /**
106   * Executes this instance.
107   *
108   * @return the gets the domain settings response collection
109   * @throws Exception the exception
110   */
111  public GetDomainSettingsResponseCollection execute() throws Exception {
112    GetDomainSettingsResponseCollection responses =
113        (GetDomainSettingsResponseCollection) this
114            .internalExecute();
115    if (responses.getErrorCode() == AutodiscoverErrorCode.NoError) {
116      this.PostProcessResponses(responses);
117    }
118    return responses;
119  }
120
121  /**
122   * Post-process response to GetDomainSettings.
123   *
124   * @param responses The GetDomainSettings response.
125   */
126  private void PostProcessResponses(
127      GetDomainSettingsResponseCollection responses) {
128    // Note:The response collection may not include all of the requested
129    // domains if the request has been throttled.
130    for (int index = 0; index < responses.getCount(); index++) {
131      responses.getResponses().get(index).setDomain(
132          this.getDomains().get(index));
133    }
134  }
135
136  /**
137   * Gets the name of the request XML element.
138   *
139   * @return Request XML element name.
140   */
141  @Override
142  protected String getRequestXmlElementName() {
143    return XmlElementNames.GetDomainSettingsRequestMessage;
144  }
145
146  /**
147   * Gets the name of the response XML element.
148   *
149   * @return Response XML element name.
150   */
151  @Override
152  protected String getResponseXmlElementName() {
153    return XmlElementNames.GetDomainSettingsResponseMessage;
154  }
155
156  /**
157   * Gets the WS-Addressing action name.
158   *
159   * @return WS-Addressing action name.
160   */
161  @Override
162  protected String getWsAddressingActionName() {
163    return GetDomainSettingsActionUri;
164  }
165
166  /**
167   * Creates the service response.
168   *
169   * @return AutodiscoverResponse
170   */
171  @Override
172  protected AutodiscoverResponse createServiceResponse() {
173    return new GetDomainSettingsResponseCollection();
174  }
175
176  /**
177   * Writes the attribute to XML.
178   *
179   * @param writer The writer.
180   * @throws ServiceXmlSerializationException the service xml serialization exception
181   */
182  @Override
183  protected void writeAttributesToXml(EwsServiceXmlWriter writer)
184      throws ServiceXmlSerializationException {
185    writer.writeAttributeValue("xmlns",
186        EwsUtilities.AutodiscoverSoapNamespacePrefix,
187        EwsUtilities.AutodiscoverSoapNamespace);
188  }
189
190  /**
191   * Writes request to XML.
192   *
193   * @param writer the writer
194   * @throws XMLStreamException the XML stream exception
195   * @throws ServiceXmlSerializationException the service xml serialization exception
196   */
197  @Override
198  protected void writeElementsToXml(EwsServiceXmlWriter writer)
199      throws XMLStreamException, ServiceXmlSerializationException {
200    writer.writeStartElement(XmlNamespace.Autodiscover,
201        XmlElementNames.Request);
202
203    writer.writeStartElement(XmlNamespace.Autodiscover,
204        XmlElementNames.Domains);
205
206    for (String domain : this.getDomains()) {
207      if (!(domain == null || domain.isEmpty())) {
208        writer.writeElementValue(XmlNamespace.Autodiscover,
209            XmlElementNames.Domain, domain);
210      }
211    }
212    writer.writeEndElement(); // Domains
213
214    writer.writeStartElement(XmlNamespace.Autodiscover,
215        XmlElementNames.RequestedSettings);
216    for (DomainSettingName setting : settings) {
217      writer.writeElementValue(XmlNamespace.Autodiscover,
218          XmlElementNames.Setting, setting);
219    }
220
221    writer.writeEndElement(); // RequestedSettings
222
223    if (this.requestedVersion != null) {
224      writer.writeElementValue(XmlNamespace.Autodiscover,
225          XmlElementNames.RequestedVersion, this.requestedVersion);
226    }
227
228    writer.writeEndElement(); // Request
229  }
230
231  /**
232   * Gets  the domains.
233   *
234   * @return the domains
235   */
236  protected List<String> getDomains() {
237    return domains;
238  }
239
240  /**
241   * Sets the domains.
242   *
243   * @param value the new domains
244   */
245  public void setDomains(List<String> value) {
246    domains = value;
247  }
248
249  /**
250   * Gets or sets the settings.
251   *
252   * @return the settings
253   */
254  protected List<DomainSettingName> getSettings() {
255    return settings;
256  }
257
258  /**
259   * Sets the settings.
260   *
261   * @param value the new settings
262   */
263  public void setSettings(List<DomainSettingName> value) {
264    settings = value;
265  }
266
267  /**
268   * Gets or sets the requestedVersion.
269   *
270   * @return the requestedVersion
271   */
272  protected ExchangeVersion getRequestedVersion() {
273    return requestedVersion;
274  }
275
276  /**
277   * Sets the requestedVersion.
278   *
279   * @param value the new requestedVersion
280   */
281  public void setRequestedVersion(ExchangeVersion value) {
282    requestedVersion = value;
283  }
284
285}