1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springbyexample.httpclient;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.Map;
23
24 import org.apache.commons.httpclient.HttpException;
25 import org.apache.commons.httpclient.HttpMethod;
26 import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
27 import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
28 import org.apache.commons.httpclient.methods.PostMethod;
29 import org.apache.commons.httpclient.methods.RequestEntity;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.stereotype.Component;
33
34
35
36
37
38
39 @Component
40 public class HttpClientTemplate extends AbstractHttpClientTemplate<RequestEntity> {
41
42 final Logger logger = LoggerFactory.getLogger(HttpClientTemplate.class);
43
44
45
46
47 public HttpClientTemplate() {}
48
49
50
51
52
53
54 public HttpClientTemplate(String defaultUri) {
55 super(defaultUri, false);
56 }
57
58
59
60
61
62
63
64 public HttpClientTemplate(String defaultUri, boolean init) {
65 super(defaultUri, init);
66 }
67
68
69
70
71
72
73
74 public void executePostMethod(byte[] input) {
75 executePostMethod(input, null);
76 }
77
78
79
80
81
82
83
84
85 public void executePostMethod(byte[] input, ResponseCallback<?> callback) {
86 executePostMethod(defaultUri,
87 (input != null ? new ByteArrayRequestEntity(input) : null),
88 null, callback);
89 }
90
91
92
93
94
95
96
97 public void executePostMethod(String input) {
98 executePostMethod(input, null);
99 }
100
101
102
103
104
105
106
107
108 public void executePostMethod(String input, ResponseCallback<?> callback) {
109 executePostMethod((input != null ? new ByteArrayInputStream(input.getBytes()) : null),
110 callback);
111 }
112
113
114
115
116
117
118
119 public void executePostMethod(InputStream input) {
120 executePostMethod(input, null);
121 }
122
123
124
125
126
127
128
129
130 public void executePostMethod(InputStream input, ResponseCallback<?> callback) {
131 executePostMethod(defaultUri, new InputStreamRequestEntity(input), null, callback);
132 }
133
134
135
136
137
138
139
140
141
142
143 public void executePostMethod(String uri,
144 RequestEntity requestPayload, Map<String, String> hParams,
145 ResponseCallback<?> callback) {
146 PostMethod post = new PostMethod(uri);
147
148 if (requestPayload != null) {
149 post.setRequestEntity(requestPayload);
150 }
151
152 processHttpMethodParams(post, hParams);
153
154 processHttpMethod(post, callback);
155 }
156
157
158
159
160
161
162
163
164 protected void processHttpMethod(HttpMethod httpMethod, ResponseCallback<?> callback) {
165 try {
166 client.executeMethod(httpMethod);
167
168 validateResponse(httpMethod);
169
170 if (callback instanceof ResponseByteCallback) {
171 ((ResponseByteCallback)callback).doWithResponse(httpMethod.getResponseBody());
172 } else if (callback instanceof ResponseStreamCallback) {
173 ((ResponseStreamCallback)callback).doWithResponse(httpMethod.getResponseBodyAsStream());
174 } else if (callback instanceof ResponseStringCallback) {
175 ((ResponseStringCallback)callback).doWithResponse(httpMethod.getResponseBodyAsString());
176 }
177 } catch (HttpException e) {
178 throw new HttpAccessException(e.getMessage(), e, httpMethod.getStatusCode());
179 } catch (IOException e) {
180 throw new HttpAccessException(e.getMessage(), e);
181 } finally {
182 httpMethod.releaseConnection();
183 }
184 }
185
186 }