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.configuration;
025
026import microsoft.exchange.webservices.data.attribute.EditorBrowsable;
027import microsoft.exchange.webservices.data.autodiscover.enumeration.AutodiscoverResponseType;
028import microsoft.exchange.webservices.data.autodiscover.exception.error.AutodiscoverError;
029import microsoft.exchange.webservices.data.autodiscover.response.GetUserSettingsResponse;
030import microsoft.exchange.webservices.data.core.EwsXmlReader;
031import microsoft.exchange.webservices.data.core.XmlElementNames;
032import microsoft.exchange.webservices.data.core.enumeration.attribute.EditorBrowsableState;
033import microsoft.exchange.webservices.data.autodiscover.enumeration.UserSettingName;
034import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
035
036import java.net.URI;
037import java.util.List;
038
039/**
040 * Represents the base class for configuration settings.
041 */
042@EditorBrowsable(state = EditorBrowsableState.Never) public abstract class ConfigurationSettingsBase {
043
044  /**
045   * The error.
046   */
047  private AutodiscoverError error;
048
049  /**
050   * Initializes a new instance of the ConfigurationSettingsBase class.
051   */
052  public ConfigurationSettingsBase() {
053  }
054
055  /**
056   * Tries to read the current XML element.
057   *
058   * @param reader the reader
059   * @return True is the current element was read, false otherwise.
060   * @throws Exception the exception
061   */
062  public boolean tryReadCurrentXmlElement(EwsXmlReader reader)
063      throws Exception {
064    if (reader.getLocalName().equalsIgnoreCase(XmlElementNames.Error)) {
065      this.error = AutodiscoverError.parse(reader);
066
067      return true;
068    } else {
069      return false;
070    }
071  }
072
073  /**
074   * Loads the settings from XML.
075   *
076   * @param reader the reader
077   * @throws Exception the exception
078   */
079  public void loadFromXml(EwsXmlReader reader) throws Exception {
080    reader.readStartElement(XmlNamespace.NotSpecified,
081        XmlElementNames.Autodiscover);
082    reader.readStartElement(XmlNamespace.NotSpecified,
083        XmlElementNames.Response);
084
085    do {
086      reader.read();
087
088      if (reader.isStartElement()) {
089        if (!this.tryReadCurrentXmlElement(reader)) {
090          reader.skipCurrentElement();
091        }
092      }
093    } while (!reader.isEndElement(XmlNamespace.NotSpecified,
094        XmlElementNames.Response));
095
096    reader.readEndElement(XmlNamespace.NotSpecified,
097        XmlElementNames.Autodiscover);
098  }
099
100  /**
101   * Gets the namespace that defines the settings.
102   *
103   * @return The namespace that defines the settings
104   */
105  public abstract String getNamespace();
106
107  /**
108   * Makes this instance a redirection response.
109   *
110   * @param redirectUrl the redirect url
111   */
112  public abstract void makeRedirectionResponse(URI redirectUrl);
113
114  /**
115   * Gets the type of the response.
116   *
117   * @return The type of the response.
118   */
119  public abstract AutodiscoverResponseType getResponseType();
120
121  /**
122   * Gets the redirect target.
123   *
124   * @return The redirect target.
125   */
126  public abstract String getRedirectTarget();
127
128  /**
129   * Convert ConfigurationSettings to GetUserSettings response.
130   *
131   * @param smtpAddress       SMTP address.
132   * @param requestedSettings The requested settings.
133   * @return GetUserSettingsResponse.
134   */
135  public abstract GetUserSettingsResponse convertSettings(
136      String smtpAddress,
137      List<UserSettingName> requestedSettings);
138
139
140  /**
141   * Gets the error.
142   *
143   * @return The error.
144   */
145  public AutodiscoverError getError() {
146    return this.error;
147  }
148}