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.autodiscover.response;
025
026 import microsoft.exchange.webservices.data.autodiscover.AlternateMailboxCollection;
027 import microsoft.exchange.webservices.data.autodiscover.ProtocolConnectionCollection;
028 import microsoft.exchange.webservices.data.autodiscover.WebClientUrlCollection;
029 import microsoft.exchange.webservices.data.autodiscover.exception.error.UserSettingError;
030 import microsoft.exchange.webservices.data.core.EwsUtilities;
031 import microsoft.exchange.webservices.data.core.EwsXmlReader;
032 import microsoft.exchange.webservices.data.core.XmlAttributeNames;
033 import microsoft.exchange.webservices.data.core.XmlElementNames;
034 import microsoft.exchange.webservices.data.autodiscover.enumeration.UserSettingName;
035 import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
036 import microsoft.exchange.webservices.data.misc.OutParam;
037 import microsoft.exchange.webservices.data.security.XmlNodeType;
038
039 import java.util.ArrayList;
040 import java.util.Collection;
041 import java.util.HashMap;
042 import java.util.Map;
043
044 /**
045 * Represents the response to a GetUsersSettings call for an individual user.
046 */
047 public final class GetUserSettingsResponse extends AutodiscoverResponse {
048
049 /**
050 * The smtp address.
051 */
052 private String smtpAddress;
053
054 /**
055 * The redirect target.
056 */
057 private String redirectTarget;
058
059 /**
060 * The settings.
061 */
062 private Map<UserSettingName, Object> settings;
063
064 /**
065 * The user setting errors.
066 */
067 private Collection<UserSettingError> userSettingErrors;
068
069 /**
070 * Initializes a new instance of the {@link GetUserSettingsResponse} class.
071 */
072 public GetUserSettingsResponse() {
073 super();
074 this.setSmtpAddress(null);
075 this.setSettings(new HashMap<UserSettingName, Object>());
076 this.setUserSettingErrors(new ArrayList<UserSettingError>());
077 }
078
079 /**
080 * Tries the get the user setting value.
081 *
082 * @param cls Type of user setting.
083 * @param setting The setting.
084 * @param value The setting value.
085 * @return True if setting was available.
086 */
087 public <T> boolean tryGetSettingValue(Class<T> cls,
088 UserSettingName setting, OutParam<T> value) {
089 Object objValue;
090 if (this.getSettings().containsKey(setting)) {
091 objValue = this.getSettings().get(setting);
092 value.setParam((T) objValue);
093 return true;
094 } else {
095 value.setParam(null);
096 return false;
097 }
098 }
099
100 /**
101 * Gets the SMTP address this response applies to.
102 *
103 * @return the smtp address
104 */
105 public String getSmtpAddress() {
106 return this.smtpAddress;
107 }
108
109 /**
110 * Sets the smtp address.
111 *
112 * @param value the new smtp address
113 */
114 public void setSmtpAddress(String value) {
115 this.smtpAddress = value;
116 }
117
118 /**
119 * Gets the redirectionTarget (URL or email address).
120 *
121 * @return the redirect target
122 */
123 public String getRedirectTarget() {
124 return this.redirectTarget;
125 }
126
127 /**
128 * Sets the redirectionTarget (URL or email address).
129 * @param value redirect target value
130 */
131 public void setRedirectTarget(String value) {
132 this.redirectTarget = value;
133 }
134
135 /**
136 * Gets the requested settings for the user.
137 *
138 * @return the settings
139 */
140 public Map<UserSettingName, Object> getSettings() {
141 return this.settings;
142 }
143
144 /**
145 * Sets the requested settings for the user.
146 * @param settings settings map
147 */
148 public void setSettings(Map<UserSettingName, Object> settings) {
149 this.settings = settings;
150 }
151
152 /**
153 * Gets error information for settings that could not be returned.
154 *
155 * @return the user setting errors
156 */
157 public Collection<UserSettingError> getUserSettingErrors() {
158 return this.userSettingErrors;
159 }
160
161 /**
162 * Sets the requested settings for the user.
163 * @param value user setting errors
164 */
165 protected void setUserSettingErrors(Collection<UserSettingError> value) {
166 this.userSettingErrors = value;
167 }
168
169 /**
170 * Loads response from XML.
171 *
172 * @param reader The reader.
173 * @param endElementName End element name.
174 * @throws Exception the exception
175 */
176 @Override public void loadFromXml(EwsXmlReader reader, String endElementName)
177 throws Exception {
178 do {
179 reader.read();
180
181 if (reader.getNodeType().getNodeType() == XmlNodeType.START_ELEMENT) {
182 if (reader.getLocalName()
183 .equals(XmlElementNames.RedirectTarget)) {
184
185 this.setRedirectTarget(reader.readElementValue());
186 } else if (reader.getLocalName().equals(
187 XmlElementNames.UserSettingErrors)) {
188 this.loadUserSettingErrorsFromXml(reader);
189 } else if (reader.getLocalName().equals(
190 XmlElementNames.UserSettings)) {
191 this.loadUserSettingsFromXml(reader);
192 } else {
193 super.loadFromXml(reader, endElementName);
194 }
195 }
196 } while (!reader
197 .isEndElement(XmlNamespace.Autodiscover, endElementName));
198 }
199
200 /**
201 * Loads from XML.
202 *
203 * @param reader The reader.
204 * @throws Exception the exception
205 */
206 protected void loadUserSettingsFromXml(EwsXmlReader reader)
207 throws Exception {
208 if (!reader.isEmptyElement()) {
209 do {
210 reader.read();
211
212 if ((reader.getNodeType().getNodeType() == XmlNodeType.START_ELEMENT) &&
213 (reader.getLocalName()
214 .equals(XmlElementNames.UserSetting))) {
215 String settingClass = reader.readAttributeValue(
216 XmlNamespace.XmlSchemaInstance,
217 XmlAttributeNames.Type);
218
219 if (settingClass.equals(XmlElementNames.StringSetting)) {
220 this.readSettingFromXml(reader);
221 } else if (settingClass.equals(XmlElementNames.WebClientUrlCollectionSetting)) {
222 this.readSettingFromXml(reader);
223 } else if (settingClass.equals(XmlElementNames.AlternateMailboxCollectionSetting)) {
224 this.readSettingFromXml(reader);
225 } else if (settingClass.equals(XmlElementNames.ProtocolConnectionCollectionSetting)) {
226 this.readSettingFromXml(reader);
227 } else {
228 EwsUtilities.ewsAssert(false, "GetUserSettingsResponse." + "LoadUserSettingsFromXml", String
229 .format("%s,%s", "Invalid setting class '%s' returned", settingClass));
230 break;
231 }
232 }
233 } while (!reader.isEndElement(XmlNamespace.Autodiscover,
234 XmlElementNames.UserSettings));
235 } else {
236 reader.read();
237 }
238 }
239
240 /**
241 * Reads user setting from XML.
242 *
243 * @param reader The reader.
244 * @throws Exception the exception
245 */
246 private void readSettingFromXml(EwsXmlReader reader) throws Exception {
247 UserSettingName name = null;
248 Object value = null;
249
250 do {
251 reader.read();
252
253 if (reader.getNodeType().getNodeType() == XmlNodeType.START_ELEMENT) {
254 if (reader.getLocalName().equals(XmlElementNames.Name)) {
255 name = reader.readElementValue(UserSettingName.class);
256 } else if (reader.getLocalName().equals(XmlElementNames.Value)) {
257 value = reader.readElementValue();
258 } else if (reader.getLocalName().equals(
259 XmlElementNames.WebClientUrls)) {
260
261 value = WebClientUrlCollection.loadFromXml(reader);
262 } else if (reader.getLocalName().equals(
263 XmlElementNames.ProtocolConnections)) {
264 value = ProtocolConnectionCollection.loadFromXml(reader);
265 } else if (reader.getLocalName().equals(
266 XmlElementNames.AlternateMailboxes)) {
267 value = AlternateMailboxCollection.loadFromXml(reader);
268 }
269 }
270 } while (!reader.isEndElement(XmlNamespace.Autodiscover,
271 XmlElementNames.UserSetting));
272
273 EwsUtilities.ewsAssert(name != null, "GetUserSettingsResponse.ReadSettingFromXml",
274 "Missing name element in user setting");
275
276 this.getSettings().put(name, value);
277 }
278
279 /**
280 * Loads the user setting errors.
281 *
282 * @param reader The reader.
283 * @throws Exception the exception
284 */
285 private void loadUserSettingErrorsFromXml(EwsXmlReader reader)
286 throws Exception {
287 if (!reader.isEmptyElement()) {
288 do {
289 reader.read();
290
291 if ((reader.getNodeType().getNodeType() == XmlNodeType.START_ELEMENT) &&
292 (reader.getLocalName()
293 .equals(XmlElementNames.UserSettingError))) {
294 UserSettingError error = new UserSettingError();
295 error.loadFromXml(reader);
296 this.getUserSettingErrors().add(error);
297 }
298 } while (!reader.isEndElement(XmlNamespace.Autodiscover,
299 XmlElementNames.UserSettingErrors));
300 } else {
301 reader.read();
302 }
303 }
304 }