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.Consts;
24 import org.apache.http.Header;
25 import org.apache.http.HttpEntity;
26 import org.apache.http.HttpResponse;
27 import org.apache.http.NameValuePair;
28 import org.apache.http.client.ResponseHandler;
29 import org.apache.http.client.entity.UrlEncodedFormEntity;
30 import org.apache.http.client.methods.HttpPost;
31 import org.apache.http.impl.client.BasicResponseHandler;
32 import org.apache.http.message.BasicNameValuePair;
33
34 import java.io.IOException;
35 import java.net.URI;
36 import java.util.ArrayList;
37
38
39
40
41 public class PostRequest extends HttpPost implements ResponseHandler<String>, ResponseWrapper {
42 private ArrayList<NameValuePair> parameters = new ArrayList<>();
43 private String responseBodyAsString = null;
44 private HttpResponse response;
45
46 public PostRequest(final URI uri) {
47 super(uri);
48 }
49
50 public PostRequest(final String url) {
51 super(URI.create(url));
52 }
53
54 public void setRequestHeader(String name, String value) {
55 setHeader(name, value);
56 }
57
58 @Override
59 public HttpEntity getEntity() {
60 return new UrlEncodedFormEntity(parameters, Consts.UTF_8);
61 }
62
63 @Override
64 public String handleResponse(HttpResponse response) throws IOException {
65 this.response = response;
66 if (HttpClientAdapter.isRedirect(response)) {
67 return null;
68 } else {
69 responseBodyAsString = new BasicResponseHandler().handleResponse(response);
70 return responseBodyAsString;
71 }
72
73 }
74
75 public void setParameter(final String name, final String value) {
76 parameters.add(new BasicNameValuePair(name, value));
77 }
78
79 public void removeParameter(final String name) {
80 ArrayList<NameValuePair> toDelete = new ArrayList<>();
81 for (NameValuePair param: parameters) {
82 if (param.getName().equals(name)) {
83 toDelete.add(param);
84 }
85 }
86 parameters.removeAll(toDelete);
87 }
88
89 public ArrayList<NameValuePair> getParameters() {
90 return parameters;
91 }
92
93 public String getResponseBodyAsString() throws IOException {
94 if (responseBodyAsString == null) {
95 throw new IOException("No response body available");
96 }
97 return responseBodyAsString;
98 }
99
100 public Header getResponseHeader(String name) {
101 checkResponse();
102 return response.getFirstHeader(name);
103 }
104
105
106
107
108
109 public int getStatusCode() {
110 checkResponse();
111 return response.getStatusLine().getStatusCode();
112 }
113
114
115
116
117
118 public String getReasonPhrase() {
119 checkResponse();
120 return response.getStatusLine().getReasonPhrase();
121 }
122
123 public URI getRedirectLocation() {
124 checkResponse();
125 return HttpClientAdapter.getRedirectLocation(response);
126 }
127
128 public HttpResponse getHttpResponse() {
129 return response;
130 }
131
132
133
134
135 private void checkResponse() {
136 if (response == null) {
137 throw new IllegalStateException("Should execute request first");
138 }
139 }
140
141 }