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.response;
025
026import microsoft.exchange.webservices.data.autodiscover.exception.error.DomainSettingError;
027import microsoft.exchange.webservices.data.core.EwsUtilities;
028import microsoft.exchange.webservices.data.core.EwsXmlReader;
029import microsoft.exchange.webservices.data.core.XmlAttributeNames;
030import microsoft.exchange.webservices.data.core.XmlElementNames;
031import microsoft.exchange.webservices.data.autodiscover.enumeration.DomainSettingName;
032import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
033import microsoft.exchange.webservices.data.security.XmlNodeType;
034import org.apache.commons.logging.Log;
035import org.apache.commons.logging.LogFactory;
036
037import java.util.ArrayList;
038import java.util.Collection;
039import java.util.HashMap;
040import java.util.Map;
041
042/**
043 * Represents the response to a GetDomainSettings call for an individual domain.
044 */
045public final class GetDomainSettingsResponse extends AutodiscoverResponse {
046
047  private static final Log LOG = LogFactory.getLog(GetDomainSettingsResponse.class);
048
049  /**
050   * The domain.
051   */
052  private String domain;
053
054  /**
055   * The redirect target.
056   */
057  private String redirectTarget;
058
059  /**
060   * The settings.
061   */
062  private Map<DomainSettingName, Object> settings;
063
064  /**
065   * The domain setting errors.
066   */
067  private Collection<DomainSettingError> domainSettingErrors;
068
069  /**
070   * Initializes a new instance of the {@link GetDomainSettingsResponse} class.
071   */
072  public GetDomainSettingsResponse() {
073    super();
074    this.domain = "";
075    this.settings = new HashMap<DomainSettingName, Object>();
076    this.domainSettingErrors = new ArrayList<DomainSettingError>();
077  }
078
079  /**
080   * Gets the domain this response applies to.
081   *
082   * @return the domain
083   */
084  public String getDomain() {
085    return this.domain;
086  }
087
088  /**
089   * Sets the domain.
090   *
091   * @param value the new domain
092   */
093  public void setDomain(String value) {
094    this.domain = value;
095  }
096
097  /**
098   * Gets the redirectionTarget (URL or email address).
099   *
100   * @return the redirect target
101   */
102  public String getRedirectTarget() {
103    return this.redirectTarget;
104  }
105
106  /**
107   * Gets the requested settings for the domain.
108   *
109   * @return the settings
110   */
111  public Map<DomainSettingName, Object> getSettings() {
112    return this.settings;
113  }
114
115  /**
116   * Gets error information for settings that could not be returned.
117   *
118   * @return the domain setting errors
119   */
120  public Collection<DomainSettingError> getDomainSettingErrors() {
121    return this.domainSettingErrors;
122  }
123
124  /**
125   * Loads response from XML.
126   *
127   * @param reader         The reader.
128   * @param endElementName End element name.
129   * @throws Exception the exception
130   */
131  @Override public void loadFromXml(EwsXmlReader reader, String endElementName)
132      throws Exception {
133    do {
134      reader.read();
135
136      if (reader.getNodeType().nodeType == XmlNodeType.START_ELEMENT) {
137        if (reader.getLocalName()
138            .equals(XmlElementNames.RedirectTarget)) {
139          this.redirectTarget = reader.readElementValue();
140        } else if (reader.getLocalName().equals(
141            XmlElementNames.DomainSettingErrors)) {
142          this.loadDomainSettingErrorsFromXml(reader);
143        } else if (reader.getLocalName().equals(
144            XmlElementNames.DomainSettings)) {
145          try {
146            this.loadDomainSettingsFromXml(reader);
147          } catch (Exception e) {
148            LOG.error(e);
149          }
150        } else {
151          super.loadFromXml(reader, endElementName);
152          break;
153        }
154      }
155    } while (!reader
156        .isEndElement(XmlNamespace.Autodiscover, endElementName));
157  }
158
159  /**
160   * Loads from XML.
161   *
162   * @param reader The reader.
163   * @throws Exception the exception
164   */
165  protected void loadDomainSettingsFromXml(EwsXmlReader reader)
166      throws Exception {
167    if (!reader.isEmptyElement()) {
168      do {
169        reader.read();
170
171        if ((reader.getNodeType().nodeType == XmlNodeType.START_ELEMENT) &&
172            (reader.getLocalName()
173                .equals(XmlElementNames.DomainSetting))) {
174          String settingClass = reader.readAttributeValue(
175              XmlNamespace.XmlSchemaInstance,
176              XmlAttributeNames.Type);
177
178          if (settingClass
179              .equals(XmlElementNames.DomainStringSetting)) {
180
181            this.readSettingFromXml(reader);
182          } else {
183            EwsUtilities
184                .ewsAssert(false, "GetDomainSettingsResponse." + "LoadDomainSettingsFromXml",
185                           String.format("%s,%s", "Invalid setting " + "class '%s' returned", settingClass));
186            break;
187          }
188        }
189      } while (!reader.isEndElement(XmlNamespace.Autodiscover,
190          XmlElementNames.DomainSettings));
191    } else {
192      reader.read();
193    }
194  }
195
196  /**
197   * Reads domain setting from XML.
198   *
199   * @param reader The reader.
200   * @throws Exception the exception
201   */
202  private void readSettingFromXml(EwsXmlReader reader) throws Exception {
203    DomainSettingName name = null;
204    Object value = null;
205
206    do {
207      reader.read();
208
209      if (reader.getNodeType().nodeType == XmlNodeType.START_ELEMENT) {
210        if (reader.getLocalName().equals(
211            XmlElementNames.DomainStringSetting)) {
212          name = reader.readElementValue(DomainSettingName.class);
213        } else if (reader.getLocalName().equals(XmlElementNames.Value)) {
214          value = reader.readElementValue();
215        }
216      }
217    } while (!reader.isEndElement(XmlNamespace.Autodiscover,
218        XmlElementNames.DomainSetting));
219
220    EwsUtilities.ewsAssert(name != null, "GetDomainSettingsResponse.ReadSettingFromXml",
221                           "Missing name element in domain setting");
222
223    this.settings.put(name, value);
224  }
225
226  /**
227   * Loads the domain setting errors.
228   *
229   * @param reader The reader.
230   * @throws Exception the exception
231   */
232  private void loadDomainSettingErrorsFromXml(EwsXmlReader reader)
233      throws Exception {
234    if (!reader.isEmptyElement()) {
235      do {
236        reader.read();
237
238        if ((reader.getNodeType().nodeType == XmlNodeType.START_ELEMENT) &&
239            (reader.getLocalName()
240                .equals(XmlElementNames.DomainSettingError))) {
241          DomainSettingError error = new DomainSettingError();
242          error.loadFromXml(reader);
243          domainSettingErrors.add(error);
244        }
245      } while (!reader.isEndElement(XmlNamespace.Autodiscover,
246          XmlElementNames.DomainSettingErrors));
247    } else {
248      reader.read();
249    }
250  }
251}