13
13
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
14
See the License for the specific language governing permissions and
15
15
limitations under the License.
16
- */
16
+ */
17
17
package com .ericsson .eiffelcommons .utils ;
18
18
19
19
import java .io .File ;
20
20
import java .io .IOException ;
21
+ import java .net .URI ;
21
22
import java .net .URISyntaxException ;
22
23
import java .util .HashMap ;
23
24
import java .util .Map ;
24
25
25
26
import org .apache .commons .io .FileUtils ;
27
+ import org .apache .http .Header ;
26
28
import org .apache .http .client .ClientProtocolException ;
27
29
import org .apache .http .client .methods .HttpDelete ;
28
30
import org .apache .http .client .methods .HttpEntityEnclosingRequestBase ;
31
33
import org .apache .http .client .methods .HttpPut ;
32
34
import org .apache .http .client .methods .HttpRequestBase ;
33
35
import org .apache .http .client .utils .URIBuilder ;
36
+ import org .apache .http .entity .ContentType ;
34
37
import org .apache .http .entity .StringEntity ;
38
+ import org .apache .http .message .BasicHeader ;
35
39
36
40
import lombok .Getter ;
37
41
import lombok .Setter ;
@@ -56,28 +60,23 @@ public enum HttpMethod {
56
60
@ Getter
57
61
protected Map <String , String > params ;
58
62
63
+ public HttpRequest () {
64
+ params = new HashMap <>();
65
+ initExecutor (false );
66
+ }
67
+
59
68
public HttpRequest (HttpMethod method ) {
60
69
this (method , false );
61
70
}
62
71
72
+ public HttpRequest (HttpMethod method , HttpExecutor executor ) {
73
+ this (method , false );
74
+ this .executor = executor ;
75
+ }
76
+
63
77
public HttpRequest (HttpMethod method , boolean persistentClient ) {
64
78
params = new HashMap <>();
65
-
66
- switch (method ) {
67
- case POST :
68
- request = new HttpPost ();
69
- break ;
70
- case GET :
71
- request = new HttpGet ();
72
- break ;
73
- case DELETE :
74
- request = new HttpDelete ();
75
- break ;
76
- case PUT :
77
- request = new HttpPut ();
78
- break ;
79
- }
80
-
79
+ setHttpMethod (method );
81
80
initExecutor (persistentClient );
82
81
}
83
82
@@ -89,6 +88,29 @@ private void initExecutor(boolean persistentClient) {
89
88
}
90
89
}
91
90
91
+ /**
92
+ * Sets the http method for this request object
93
+ * @param method
94
+ */
95
+ public HttpRequest setHttpMethod (HttpMethod method ) {
96
+ switch (method ) {
97
+ case POST :
98
+ request = new HttpPost ();
99
+ break ;
100
+ case GET :
101
+ request = new HttpGet ();
102
+ break ;
103
+ case DELETE :
104
+ request = new HttpDelete ();
105
+ break ;
106
+ case PUT :
107
+ request = new HttpPut ();
108
+ break ;
109
+ }
110
+
111
+ return this ;
112
+ }
113
+
92
114
/**
93
115
* Gets the base url(not including endpoint) for example: http://localhost:8080
94
116
* @return String
@@ -129,12 +151,24 @@ public void resetHttpRequestObject() {
129
151
* :: the key of the header
130
152
* @param value
131
153
* :: the value of the header
132
- * @return
154
+ * @return HttpRequest
133
155
*/
134
156
public HttpRequest addHeader (String key , String value ) {
135
157
request .addHeader (key , value );
136
158
return this ;
137
159
}
160
+ /**
161
+ * Function that overwrites the first header with the same name. The new header will be appended to the end of the list, if no header with the given name can be found.
162
+ *
163
+ * @param key
164
+ * @param value
165
+ * @return HttpRequest
166
+ */
167
+ public HttpRequest setHeader (String key , String value ) {
168
+ Header header = new BasicHeader (key , value );
169
+ request .setHeader (header );
170
+ return this ;
171
+ }
138
172
139
173
/**
140
174
* Takes a header key as input and removes that key and value from the list of headers.
@@ -165,34 +199,60 @@ public void addParameters(Map<String, String> parameters) {
165
199
* :: the key of the parameter
166
200
* @param value
167
201
* :: the value of the parameter
168
- * @return
202
+ * @return HttpRequest
169
203
*/
170
204
public HttpRequest addParam (String key , String value ) {
171
205
params .put (key , value );
172
206
return this ;
173
207
}
174
208
175
209
/**
176
- * Function that sets the body of the http request.
210
+ * Function that sets the body of the http request with a chosen content type.
211
+ *
212
+ * @param body
213
+ * :: String input
214
+ * @return HTTPRequest
215
+ */
216
+ public HttpRequest setBody (String body , ContentType contentType ) {
217
+ ((HttpEntityEnclosingRequestBase ) request ).setEntity (new StringEntity (body , contentType ));
218
+ return this ;
219
+ }
220
+
221
+ /**
222
+ * Function that sets the body of the http request with default content type(text/plain).
177
223
*
178
224
* @param body
179
225
* :: String input
180
226
* @return HTTPRequest
181
227
*/
182
228
public HttpRequest setBody (String body ) {
183
- (( HttpEntityEnclosingRequestBase ) request ). setEntity ( new StringEntity ( body , "UTF-8" ) );
229
+ setBody ( body , ContentType . TEXT_PLAIN );
184
230
return this ;
185
231
}
186
232
187
233
/**
188
- * Function that sets the body of the http request.
234
+ * Function that sets the body of the http request with default value of content type (text/plain) .
189
235
*
190
236
* @param file
191
237
* :: File input
192
238
* @return HTTPRequest
193
239
* @throws IOException
194
240
*/
195
241
public HttpRequest setBody (File file ) throws IOException {
242
+ setBody (file , ContentType .TEXT_PLAIN );
243
+ return this ;
244
+ }
245
+
246
+ /**
247
+ * Function that sets the body of the http request with a chosen content type.
248
+ *
249
+ * @param file
250
+ * :: File input
251
+ * @param type
252
+ * @return HTTPRequest
253
+ * @throws IOException
254
+ */
255
+ public HttpRequest setBody (File file , ContentType contentType ) throws IOException {
196
256
String fileContent = "" ;
197
257
try {
198
258
fileContent = FileUtils .readFileToString (file , "UTF-8" );
@@ -201,7 +261,7 @@ public HttpRequest setBody(File file) throws IOException {
201
261
+ e .getMessage ();
202
262
throw new IOException (message );
203
263
}
204
- return setBody (fileContent );
264
+ return setBody (fileContent , contentType );
205
265
}
206
266
207
267
/**
@@ -215,11 +275,21 @@ public HttpRequest setBody(File file) throws IOException {
215
275
public ResponseEntity performRequest () throws URISyntaxException , ClientProtocolException , IOException {
216
276
URIBuilder builder = createURIBuilder ();
217
277
builder = addParametersToURIBuilder (builder );
218
-
219
278
request .setURI (builder .build ());
220
279
return executor .executeRequest (request );
221
280
}
222
281
282
+ /**
283
+ * Function that returns the URI of the request.
284
+ *
285
+ * @return URI
286
+ * @throws URISyntaxException
287
+ */
288
+ public URI getURI () throws URISyntaxException {
289
+ URIBuilder builder = createURIBuilder ();
290
+ return builder .build ();
291
+ }
292
+
223
293
/**
224
294
* Function that adds parameters to the URIBuilder
225
295
*
0 commit comments