1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
45
46 public GetRequest(final String uri) {
47 super(uri);
48 }
49
50
51
52
53
54
55
56
57
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
86
87
88 public int getStatusCode() {
89 checkResponse();
90 return response.getStatusLine().getStatusCode();
91 }
92
93
94
95
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
113
114 private void checkResponse() {
115 if (response == null) {
116 throw new IllegalStateException("Should execute request first");
117 }
118 }
119
120 }