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.request;
025
026 import microsoft.exchange.webservices.data.autodiscover.AutodiscoverService;
027 import microsoft.exchange.webservices.data.autodiscover.enumeration.AutodiscoverErrorCode;
028 import microsoft.exchange.webservices.data.autodiscover.response.AutodiscoverResponse;
029 import microsoft.exchange.webservices.data.autodiscover.response.GetUserSettingsResponseCollection;
030 import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
031 import microsoft.exchange.webservices.data.core.EwsUtilities;
032 import microsoft.exchange.webservices.data.core.EwsXmlReader;
033 import microsoft.exchange.webservices.data.core.ExchangeServiceBase;
034 import microsoft.exchange.webservices.data.core.XmlElementNames;
035 import microsoft.exchange.webservices.data.autodiscover.enumeration.UserSettingName;
036 import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
037 import microsoft.exchange.webservices.data.core.exception.service.local.ServiceValidationException;
038 import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
039
040 import javax.xml.stream.XMLStreamException;
041
042 import java.net.URI;
043 import java.util.List;
044
045 /**
046 * Represents a GetUserSettings request.
047 */
048 public class GetUserSettingsRequest extends AutodiscoverRequest {
049
050 /**
051 * Action Uri of Autodiscover.GetUserSettings method.
052 */
053 private static final String GetUserSettingsActionUri = EwsUtilities.
054 AutodiscoverSoapNamespace +
055 "/Autodiscover/GetUserSettings";
056
057 private List<String> smtpAddresses;
058 private List<UserSettingName> settings;
059
060
061 // Expect this request to return the partner token.
062
063 private boolean expectPartnerToken = false;
064 private String partnerTokenReference;
065 private String partnerToken;
066
067 /**
068 * Initializes a new instance of the {@link GetUserSettingsRequest} class.
069 *
070 * @param service the service
071 * @param url the url
072 * @throws ServiceValidationException on validation error
073 */
074 public GetUserSettingsRequest(AutodiscoverService service, URI url) throws ServiceValidationException {
075 this(service, url, false);
076 }
077
078 /**
079 * Initializes a new instance of the {@link GetUserSettingsRequest} class.
080 *
081 * @param service autodiscover service associated with this request
082 * @param url URL of Autodiscover service
083 * @param expectPartnerToken expect partner token or not
084 * @throws ServiceValidationException on validation error
085 */
086 public GetUserSettingsRequest(AutodiscoverService service, URI url, boolean expectPartnerToken)
087 throws ServiceValidationException {
088 super(service, url);
089 this.expectPartnerToken = expectPartnerToken;
090
091 // make an explicit https check.
092 if (expectPartnerToken && !url.getScheme().equalsIgnoreCase("https")) {
093 throw new ServiceValidationException("Https is required.");
094 }
095 }
096
097 /**
098 * Validates the request.
099 *
100 * @throws Exception the exception
101 */
102 @Override
103 protected void validate() throws Exception {
104 super.validate();
105
106 EwsUtilities.validateParam(this.getSmtpAddresses(), "smtpAddresses");
107 EwsUtilities.validateParam(this.getSettings(), "settings");
108
109 if (this.getSettings().size() == 0) {
110 throw new ServiceValidationException("At least one setting must be requested.");
111 }
112
113 if (this.getSmtpAddresses().size() == 0) {
114 throw new ServiceValidationException("At least one SMTP address must be requested.");
115 }
116
117 for (String smtpAddress : this.getSmtpAddresses()) {
118 if (smtpAddress == null || smtpAddress.isEmpty()) {
119 throw new ServiceValidationException("A valid SMTP address must be specified.");
120 }
121 }
122 }
123
124 /**
125 * Executes this instance.
126 *
127 * @return the gets the user settings response collection
128 * @throws Exception the exception
129 */
130 public GetUserSettingsResponseCollection execute() throws Exception {
131 GetUserSettingsResponseCollection responses =
132 (GetUserSettingsResponseCollection) this
133 .internalExecute();
134 if (responses.getErrorCode() == AutodiscoverErrorCode.NoError) {
135 this.postProcessResponses(responses);
136 }
137 return responses;
138 }
139
140 /**
141 * Post-process response to GetUserSettings.
142 *
143 * @param responses The GetUserSettings response.
144 */
145 private void postProcessResponses(
146 GetUserSettingsResponseCollection responses) {
147 // Note:The response collection may not include all of the requested
148 // users if the request has been throttled.
149 for (int index = 0; index < responses.getCount(); index++) {
150 responses.getResponses().get(index).setSmtpAddress(
151 this.getSmtpAddresses().get(index));
152 }
153 }
154
155 /**
156 * Gets the name of the request XML element.
157 *
158 * @return Request XML element name.
159 */
160 @Override
161 protected String getRequestXmlElementName() {
162 return XmlElementNames.GetUserSettingsRequestMessage;
163 }
164
165 /**
166 * Gets the name of the response XML element.
167 *
168 * @return Response XML element name.
169 */
170 @Override
171 protected String getResponseXmlElementName() {
172 return XmlElementNames.GetUserSettingsResponseMessage;
173 }
174
175 /**
176 * Gets the WS-Addressing action name.
177 *
178 * @return WS-Addressing action name.
179 */
180 @Override
181 protected String getWsAddressingActionName() {
182 return GetUserSettingsActionUri;
183 }
184
185 /**
186 * Creates the service response.
187 *
188 * @return AutodiscoverResponse
189 */
190 @Override
191 protected AutodiscoverResponse createServiceResponse() {
192 return new GetUserSettingsResponseCollection();
193 }
194
195 /**
196 * Writes the attribute to XML.
197 *
198 * @param writer The writer.
199 * @throws ServiceXmlSerializationException the service xml serialization exception
200 */
201 @Override
202 protected void writeAttributesToXml(EwsServiceXmlWriter writer)
203 throws ServiceXmlSerializationException {
204 writer.writeAttributeValue("xmlns",
205 EwsUtilities.AutodiscoverSoapNamespacePrefix,
206 EwsUtilities.AutodiscoverSoapNamespace);
207 }
208
209 /**
210 * @param writer XML writer
211 * @throws XMLStreamException the XML stream exception
212 * @throws ServiceXmlSerializationException the service xml serialization exception
213 */
214 @Override public void writeExtraCustomSoapHeadersToXml(EwsServiceXmlWriter writer) throws XMLStreamException,
215 ServiceXmlSerializationException {
216 if (this.expectPartnerToken) {
217 writer
218 .writeElementValue(XmlNamespace.Autodiscover,
219 XmlElementNames.BinarySecret,
220 new String(org.apache.commons.codec.binary.Base64.
221 encodeBase64(ExchangeServiceBase.getSessionKey())));
222 }
223 }
224
225 /**
226 * Writes request to XML.
227 *
228 * @param writer the writer
229 * @throws XMLStreamException the XML stream exception
230 * @throws ServiceXmlSerializationException the service xml serialization exception
231 */
232 @Override
233 protected void writeElementsToXml(EwsServiceXmlWriter writer)
234 throws XMLStreamException, ServiceXmlSerializationException {
235 writer.writeStartElement(XmlNamespace.Autodiscover,
236 XmlElementNames.Request);
237
238 writer.writeStartElement(XmlNamespace.Autodiscover,
239 XmlElementNames.Users);
240
241 for (String smtpAddress : this.getSmtpAddresses()) {
242 writer.writeStartElement(XmlNamespace.Autodiscover,
243 XmlElementNames.User);
244
245 if (!(smtpAddress == null || smtpAddress.isEmpty())) {
246 writer.writeElementValue(XmlNamespace.Autodiscover,
247 XmlElementNames.Mailbox, smtpAddress);
248 }
249 writer.writeEndElement(); // User
250 }
251 writer.writeEndElement(); // Users
252
253 writer.writeStartElement(XmlNamespace.Autodiscover,
254 XmlElementNames.RequestedSettings);
255 for (UserSettingName setting : this.getSettings()) {
256 writer.writeElementValue(XmlNamespace.Autodiscover,
257 XmlElementNames.Setting, setting);
258 }
259
260 writer.writeEndElement(); // RequestedSettings
261
262 writer.writeEndElement(); // Request
263 }
264
265 /**
266 * Read the partner token soap header.
267 *
268 * @param reader EWS XML reader
269 * @throws Exception on error
270 */
271 @Override
272 protected void readSoapHeader(EwsXmlReader reader) throws Exception {
273 super.readSoapHeader(reader);
274
275 if (this.expectPartnerToken) {
276 if (reader.isStartElement(XmlNamespace.Autodiscover,
277 XmlElementNames.PartnerToken)) {
278 this.partnerToken = reader.readInnerXml();
279 }
280
281 if (reader.isStartElement(XmlNamespace.Autodiscover,
282 XmlElementNames.PartnerTokenReference)) {
283 partnerTokenReference = reader.readInnerXml();
284 }
285 }
286 }
287
288 /**
289 * Gets the SMTP addresses.
290 * @return the SMTP addresses
291 */
292 protected List<String> getSmtpAddresses() {
293 return smtpAddresses;
294 }
295
296 /**
297 * Sets the smtp addresses.
298 * @param value the new smtp addresses
299 */
300 public void setSmtpAddresses(List<String> value) {
301 this.smtpAddresses = value;
302 }
303
304 /**
305 * Gets the settings.
306 * @return the settings
307 */
308 protected List<UserSettingName> getSettings() {
309 return settings;
310 }
311
312 /**
313 * Sets the settings.
314 *
315 * @param value the new settings
316 */
317 public void setSettings(List<UserSettingName> value) {
318 this.settings = value;
319
320 }
321
322 /**
323 * Gets the partner token.
324 * @return partner token
325 */
326 protected String getPartnerToken() {
327 return partnerToken;
328 }
329
330 private void setPartnerToken(String value) {
331 partnerToken = value;
332 }
333
334 /**
335 * Gets the partner token reference.
336 * @return partner token reference
337 */
338 protected String getPartnerTokenReference() {
339 return partnerTokenReference;
340
341 }
342
343 private void setPartnerTokenReference(String tokenReference) {
344 partnerTokenReference = tokenReference;
345 }
346 }