View Javadoc
1   /*
2    * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3    * Copyright (C) 2010  Mickael Guessant
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18   */
19  
20  package davmail.http.request;
21  
22  import davmail.http.HttpClientAdapter;
23  import org.apache.http.Header;
24  import org.apache.http.HttpResponse;
25  import org.apache.http.client.ResponseHandler;
26  import org.apache.http.client.methods.HttpGet;
27  import org.apache.http.impl.client.BasicResponseHandler;
28  
29  import java.io.IOException;
30  import java.net.URI;
31  
32  /**
33   * Http get request with a string response handler.
34   */
35  public class GetRequest extends HttpGet implements ResponseHandler<String>, ResponseWrapper {
36      private HttpResponse response;
37      private String responseBodyAsString;
38  
39      public GetRequest(final URI uri) {
40          super(uri);
41      }
42  
43      /**
44       * @throws IllegalArgumentException if the uri is invalid.
45       */
46      public GetRequest(final String uri) {
47          super(uri);
48      }
49  
50  
51      /**
52       * Handle request response and return response as string.
53       * response body is null on redirect
54       *
55       * @param response response object
56       * @return response body as string
57       * @throws IOException on error
58       */
59      @Override
60      public String handleResponse(HttpResponse response) throws IOException {
61          this.response = response;
62          if (HttpClientAdapter.isRedirect(response)) {
63              return null;
64          } else {
65              responseBodyAsString = new BasicResponseHandler().handleResponse(response);
66              return responseBodyAsString;
67          }
68      }
69  
70      public String getResponseBodyAsString() throws IOException {
71          checkResponse();
72          if (responseBodyAsString == null) {
73              throw new IOException("No response body available");
74          }
75          return responseBodyAsString;
76      }
77  
78  
79      public Header getResponseHeader(String name) {
80          checkResponse();
81          return response.getFirstHeader(name);
82      }
83  
84      /**
85       * Get status code from response.
86       * @return Http status code
87       */
88      public int getStatusCode() {
89          checkResponse();
90          return response.getStatusLine().getStatusCode();
91      }
92  
93      /**
94       * Get reason phrase from response.
95       * @return reason phrase
96       */
97      public String getReasonPhrase() {
98          checkResponse();
99          return response.getStatusLine().getReasonPhrase();
100     }
101 
102     public URI getRedirectLocation() {
103         checkResponse();
104         return HttpClientAdapter.getRedirectLocation(response);
105     }
106 
107     public HttpResponse getHttpResponse() {
108         return response;
109     }
110 
111     /**
112      * Check if response is available.
113      */
114     private void checkResponse() {
115         if (response == null) {
116             throw new IllegalStateException("Should execute request first");
117         }
118     }
119 
120 }