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