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.core.request;
025
026import microsoft.exchange.webservices.data.EWSConstants;
027import microsoft.exchange.webservices.data.core.WebProxy;
028import microsoft.exchange.webservices.data.core.exception.http.EWSHttpException;
029
030import java.io.Closeable;
031import java.io.IOException;
032import java.io.InputStream;
033import java.io.OutputStream;
034import java.net.URL;
035import java.util.Map;
036
037/**
038 * The Class HttpWebRequest.
039 */
040public abstract class HttpWebRequest implements Closeable {
041
042  /**
043   * The url.
044   */
045  private URL url;
046
047  /**
048   * The pre authenticate.
049   */
050  private boolean preAuthenticate;
051
052  /**
053   * The timeout.
054   */
055  private int timeout;
056
057  /**
058   * The content type.
059   */
060  private String contentType = "text/xml; charset=utf-8";
061
062  /**
063   * The accept.
064   */
065  private String accept = "text/xml";
066
067  /**
068   * The user agent.
069   */
070  private String userAgent = "EWS SDK";
071
072  /**
073   * The allow auto redirect.
074   */
075  private boolean allowAutoRedirect;
076
077  /**
078   * The keep alive.
079   */
080  private boolean keepAlive = true;
081
082  /**
083   * The accept gzip encoding.
084   */
085  private boolean acceptGzipEncoding;
086
087  /**
088   * The use default credential.
089   */
090  private boolean useDefaultCredentials;
091
092  private boolean allowAuthentication = true;
093
094  /**
095   * The user name.
096   */
097  private String username;
098
099  /**
100   * The password.
101   */
102  private String password;
103
104  /**
105   * The domain.
106   */
107  private String domain;
108
109  /**
110   * The request Method.
111   */
112  private String requestMethod = "POST";
113
114  /**
115   * The request headers.
116   */
117  private Map<String, String> headers;
118
119  /**
120   * The Web Proxy.
121   */
122  private WebProxy proxy;
123
124  /**
125   * Gets the Web Proxy.
126   *
127   * @return the proxy
128   */
129  public WebProxy getProxy() {
130    return proxy;
131  }
132
133  /**
134   * Sets the Web Proxy.
135   *
136   * @param proxy The Web Proxy
137   */
138  public void setProxy(WebProxy proxy) {
139    this.proxy = proxy;
140  }
141
142  /**
143   * Checks if is http scheme.
144   *
145   * @return true, if is http scheme
146   */
147  public boolean isHttpScheme() {
148    return getUrl().getProtocol().equalsIgnoreCase(EWSConstants.HTTP_SCHEME);
149  }
150
151  /**
152   * Checks if is https scheme.
153   *
154   * @return true, if is https scheme
155   */
156  public boolean isHttpsScheme() {
157    return getUrl().getProtocol().equalsIgnoreCase(EWSConstants.HTTPS_SCHEME);
158  }
159
160  /**
161   * Gets the user name.
162   *
163   * @return the user name
164   */
165  public String getUsername() {
166    return username;
167  }
168
169  /**
170   * Sets the user name.
171   *
172   * @param username the new user name
173   */
174  public void setUsername(String username) {
175    this.username = username;
176  }
177
178  /**
179   * Gets the password.
180   *
181   * @return the password
182   */
183  public String getPassword() {
184    return password;
185  }
186
187  /**
188   * Sets the password.
189   *
190   * @param password the new password
191   */
192  public void setPassword(String password) {
193    this.password = password;
194  }
195
196  /**
197   * Gets the domain.
198   *
199   * @return the domain
200   */
201  public String getDomain() {
202    return domain;
203  }
204
205  /**
206   * Sets the domain.
207   *
208   * @param domain the new domain
209   */
210  public void setDomain(String domain) {
211    this.domain = domain;
212  }
213
214  /**
215   * Gets the url.
216   *
217   * @return the url
218   */
219  public URL getUrl() {
220
221    return url;
222  }
223
224  /**
225   * Sets the url.
226   *
227   * @param url the new url
228   */
229  public void setUrl(URL url) {
230    this.url = url;
231  }
232
233  /**
234   * Whether to use preemptive authentication. Currently not implemented, though.
235   */
236  public boolean isPreAuthenticate() {
237    return preAuthenticate;
238  }
239
240  /**
241   * Whether to use preemptive authentication. Currently not implemented, though.
242   */
243  public void setPreAuthenticate(boolean preAuthenticate) {
244    this.preAuthenticate = preAuthenticate;
245  }
246
247  /**
248   * Gets the timeout.
249   *
250   * @return the timeout
251   */
252  public int getTimeout() {
253    return timeout;
254  }
255
256  /**
257   * Sets the timeout.
258   *
259   * @param timeout the new timeout
260   */
261  public void setTimeout(int timeout) {
262    this.timeout = timeout;
263  }
264
265  /**
266   * Gets the content type.
267   *
268   * @return the content type
269   */
270  public String getContentType() {
271    return contentType;
272  }
273
274  /**
275   * Sets the content type.
276   *
277   * @param contentType the new content type
278   */
279  public void setContentType(String contentType) {
280    this.contentType = contentType;
281  }
282
283  /**
284   * Gets the accept.
285   *
286   * @return the accept
287   */
288  public String getAccept() {
289    return accept;
290  }
291
292  /**
293   * Sets the accept.
294   *
295   * @param accept the new accept
296   */
297  public void setAccept(String accept) {
298    this.accept = accept;
299  }
300
301  /**
302   * Gets the user agent.
303   *
304   * @return the user agent
305   */
306  public String getUserAgent() {
307    return userAgent;
308  }
309
310  /**
311   * Sets the user agent.
312   *
313   * @param userAgent the new user agent
314   */
315  public void setUserAgent(String userAgent) {
316    this.userAgent = userAgent;
317  }
318
319  /**
320   * Checks if is allow auto redirect.
321   *
322   * @return true, if is allow auto redirect
323   */
324  public boolean isAllowAutoRedirect() {
325    return allowAutoRedirect;
326  }
327
328  /**
329   * Sets the allow auto redirect.
330   *
331   * @param allowAutoRedirect the new allow auto redirect
332   */
333  public void setAllowAutoRedirect(boolean allowAutoRedirect) {
334    this.allowAutoRedirect = allowAutoRedirect;
335  }
336
337  /**
338   * Checks if is keep alive.
339   *
340   * @return true, if is keep alive
341   */
342  public boolean isKeepAlive() {
343    return keepAlive;
344  }
345
346  /**
347   * Sets the keep alive.
348   *
349   * @param keepAlive the new keep alive
350   */
351  public void setKeepAlive(boolean keepAlive) {
352    this.keepAlive = keepAlive;
353  }
354
355  /**
356   * Checks if is accept gzip encoding.
357   *
358   * @return true, if is accept gzip encoding
359   */
360  public boolean isAcceptGzipEncoding() {
361    return acceptGzipEncoding;
362  }
363
364  /**
365   * Sets the accept gzip encoding.
366   *
367   * @param acceptGzipEncoding the new accept gzip encoding
368   */
369  public void setAcceptGzipEncoding(boolean acceptGzipEncoding) {
370    this.acceptGzipEncoding = acceptGzipEncoding;
371  }
372
373  /**
374   * Checks if is use default credential.
375   *
376   * @return true, if is use default credential
377   */
378  public boolean isUseDefaultCredentials() {
379    return useDefaultCredentials;
380  }
381
382  /**
383   * Sets the use default credential.
384   *
385   * @param useDefaultCredentials the new use default credential
386   */
387  public void setUseDefaultCredentials(boolean useDefaultCredentials) {
388    this.useDefaultCredentials = useDefaultCredentials;
389  }
390
391  /**
392   * Whether web service authentication is allowed.
393   * This can be set to {@code false} to disallow sending credential with this request.
394   *
395   * This is useful for the autodiscover request to the legacy HTTP url, because this single request doesn't
396   * require authentication and we don't want to send credential over HTTP.
397   *
398   * @return {@code true} if authentication is allowed.
399   */
400  public boolean isAllowAuthentication() {
401    return allowAuthentication;
402  }
403
404  /**
405   * Whether web service authentication is allowed.
406   * This can be set to {@code false} to disallow sending credential with this request.
407   *
408   * This is useful for the autodiscover request to the legacy HTTP url, because this single request doesn't
409   * require authentication and we don't want to send credential over HTTP.
410   *
411   * Default is {@code true}.
412   *
413   * @param allowAuthentication {@code true} if authentication is allowed.
414   */
415  public void setAllowAuthentication(boolean allowAuthentication) {
416    this.allowAuthentication = allowAuthentication;
417  }
418
419  /**
420   * Gets the request method type.
421   *
422   * @return the request method type.
423   */
424  public String getRequestMethod() {
425    return requestMethod;
426  }
427
428  /**
429   * Sets the request method type.
430   *
431   * @param requestMethod the request method type.
432   */
433  public void setRequestMethod(String requestMethod) {
434    this.requestMethod = requestMethod;
435  }
436
437  /**
438   * Gets the Headers.
439   *
440   * @return the content type
441   */
442  public Map<String, String> getHeaders() {
443    return headers;
444  }
445
446  /**
447   * Sets the Headers.
448   *
449   * @param headers The headers
450   */
451  public void setHeaders(Map<String, String> headers) {
452    this.headers = headers;
453  }
454
455  /**
456   * Sets the credential.
457   *
458   * @param domain user domain
459   * @param user   user name
460   * @param pwd    password
461   */
462  public void setCredentials(String domain, String user, String pwd) {
463    this.domain = domain;
464    this.username = user;
465    this.password = pwd;
466  }
467
468  /**
469   * Gets the input stream.
470   *
471   * @return the input stream
472   * @throws EWSHttpException the eWS http exception
473   * @throws IOException the IO exception
474   */
475  public abstract InputStream getInputStream() throws EWSHttpException, IOException;
476
477  /**
478   * Gets the error stream.
479   *
480   * @return the error stream
481   * @throws EWSHttpException the eWS http exception
482   */
483  public abstract InputStream getErrorStream() throws EWSHttpException;
484
485  /**
486   * Gets the output stream.
487   *
488   * @return the output stream
489   * @throws EWSHttpException the eWS http exception
490   */
491  public abstract OutputStream getOutputStream() throws EWSHttpException;
492
493  /**
494   * Close.
495   */
496  public abstract void close() throws IOException;
497
498  /**
499   * Prepare connection.
500   */
501  public abstract void prepareConnection();
502
503  /**
504   * Gets the response headers.
505   *
506   * @return the response headers
507   * @throws EWSHttpException the eWS http exception
508   */
509  public abstract Map<String, String> getResponseHeaders()
510      throws EWSHttpException;
511
512  /**
513   * Gets the content encoding.
514   *
515   * @return the content encoding
516   * @throws EWSHttpException the EWS http exception
517   */
518  public abstract String getContentEncoding() throws EWSHttpException;
519
520  /**
521   * Gets the response content type.
522   *
523   * @return the response content type
524   * @throws EWSHttpException the EWS http exception
525   */
526  public abstract String getResponseContentType() throws EWSHttpException;
527
528  /**
529   * Gets the response code.
530   *
531   * @return the response code
532   * @throws EWSHttpException the EWS http exception
533   */
534  public abstract int getResponseCode() throws EWSHttpException;
535
536  /**
537   * Gets the response message.
538   *
539   * @return the response message
540   * @throws EWSHttpException the EWS http exception
541   */
542  public abstract String getResponseText() throws EWSHttpException;
543
544  /**
545   * Gets the response header field.
546   *
547   * @param headerName the header name
548   * @return the response header field
549   * @throws EWSHttpException the EWS http exception
550   */
551  public abstract String getResponseHeaderField(String headerName)
552      throws EWSHttpException;
553
554  /**
555   * Gets the request property.
556   *
557   * @return the request property
558   * @throws EWSHttpException the EWS http exception
559   */
560  public abstract Map<String, String> getRequestProperty()
561      throws EWSHttpException;
562
563  /**
564   * Executes Request by sending request xml data to server.
565   *
566   * @throws EWSHttpException    the EWS http exception
567   * @throws java.io.IOException the IO Exception
568   */
569  public abstract int executeRequest() throws EWSHttpException, IOException;
570
571}