18
18
19
19
import java .io .File ;
20
20
import java .io .IOException ;
21
+ import java .io .UnsupportedEncodingException ;
21
22
import java .net .URI ;
22
23
import java .net .URISyntaxException ;
23
24
import java .util .HashMap ;
24
25
import java .util .Map ;
25
26
27
+ import org .apache .commons .codec .binary .Base64 ;
26
28
import org .apache .commons .io .FileUtils ;
27
29
import org .apache .http .Header ;
30
+ import org .apache .http .HttpHeaders ;
28
31
import org .apache .http .client .ClientProtocolException ;
29
32
import org .apache .http .client .methods .HttpDelete ;
30
33
import org .apache .http .client .methods .HttpEntityEnclosingRequestBase ;
@@ -81,7 +84,7 @@ public HttpRequest(HttpMethod method, boolean persistentClient) {
81
84
}
82
85
83
86
private void initExecutor (boolean persistentClient ) {
84
- if (persistentClient ) {
87
+ if (persistentClient ) {
85
88
executor = HttpExecutor .getInstance ();
86
89
} else {
87
90
executor = new HttpExecutor ();
@@ -90,6 +93,7 @@ private void initExecutor(boolean persistentClient) {
90
93
91
94
/**
92
95
* Sets the http method for this request object
96
+ *
93
97
* @param method
94
98
*/
95
99
public HttpRequest setHttpMethod (HttpMethod method ) {
@@ -113,6 +117,7 @@ public HttpRequest setHttpMethod(HttpMethod method) {
113
117
114
118
/**
115
119
* Gets the base url(not including endpoint) for example: http://localhost:8080
120
+ *
116
121
* @return String
117
122
*/
118
123
public String getBaseUrl () {
@@ -121,6 +126,7 @@ public String getBaseUrl() {
121
126
122
127
/**
123
128
* Sets the base url(not including endpoint) for example: http://localhost:8080
129
+ *
124
130
* @param baseUrl
125
131
*/
126
132
public HttpRequest setBaseUrl (String baseUrl ) {
@@ -147,18 +153,18 @@ public void resetHttpRequestObject() {
147
153
/**
148
154
* Function the adds a header to the http request.
149
155
*
150
- * @param key
151
- * :: the key of the header
152
- * @param value
153
- * :: the value of the header
156
+ * @param key :: the key of the header
157
+ * @param value :: the value of the header
154
158
* @return HttpRequest
155
159
*/
156
160
public HttpRequest addHeader (String key , String value ) {
157
161
request .addHeader (key , value );
158
162
return this ;
159
163
}
164
+
160
165
/**
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.
166
+ * Function that overwrites the first header with the same name. The new header will be appended
167
+ * to the end of the list, if no header with the given name can be found.
162
168
*
163
169
* @param key
164
170
* @param value
@@ -173,8 +179,7 @@ public HttpRequest setHeader(String key, String value) {
173
179
/**
174
180
* Takes a header key as input and removes that key and value from the list of headers.
175
181
*
176
- * @param headerKey
177
- * :: the header to remove
182
+ * @param headerKey :: the header to remove
178
183
*/
179
184
public void removeHeader (String headerKey ) {
180
185
request .removeHeaders (headerKey );
@@ -183,34 +188,30 @@ public void removeHeader(String headerKey) {
183
188
/**
184
189
* Function that adds multiple parameters to the http request.
185
190
*
186
- * @param parameters
187
- * :: List<NameValuePair>
191
+ * @param parameters :: List<NameValuePair>
188
192
*/
189
193
public void addParameters (Map <String , String > parameters ) {
190
194
for (Map .Entry <String , String > entry : parameters .entrySet ()) {
191
- addParam (entry .getKey (), entry .getValue ());
195
+ addParameter (entry .getKey (), entry .getValue ());
192
196
}
193
197
}
194
198
195
199
/**
196
200
* Function that adds a parameter to the http request.
197
201
*
198
- * @param key
199
- * :: the key of the parameter
200
- * @param value
201
- * :: the value of the parameter
202
+ * @param key :: the key of the parameter
203
+ * @param value :: the value of the parameter
202
204
* @return HttpRequest
203
205
*/
204
- public HttpRequest addParam (String key , String value ) {
206
+ public HttpRequest addParameter (String key , String value ) {
205
207
params .put (key , value );
206
208
return this ;
207
209
}
208
210
209
211
/**
210
212
* Function that sets the body of the http request with a chosen content type.
211
213
*
212
- * @param body
213
- * :: String input
214
+ * @param body :: String input
214
215
* @return HTTPRequest
215
216
*/
216
217
public HttpRequest setBody (String body , ContentType contentType ) {
@@ -221,8 +222,7 @@ public HttpRequest setBody(String body, ContentType contentType) {
221
222
/**
222
223
* Function that sets the body of the http request with default content type(text/plain).
223
224
*
224
- * @param body
225
- * :: String input
225
+ * @param body :: String input
226
226
* @return HTTPRequest
227
227
*/
228
228
public HttpRequest setBody (String body ) {
@@ -231,10 +231,10 @@ public HttpRequest setBody(String body) {
231
231
}
232
232
233
233
/**
234
- * Function that sets the body of the http request with default value of content type (text/plain).
234
+ * Function that sets the body of the http request with default value of content type
235
+ * (text/plain).
235
236
*
236
- * @param file
237
- * :: File input
237
+ * @param file :: File input
238
238
* @return HTTPRequest
239
239
* @throws IOException
240
240
*/
@@ -246,8 +246,7 @@ public HttpRequest setBody(File file) throws IOException {
246
246
/**
247
247
* Function that sets the body of the http request with a chosen content type.
248
248
*
249
- * @param file
250
- * :: File input
249
+ * @param file :: File input
251
250
* @param type
252
251
* @return HTTPRequest
253
252
* @throws IOException
@@ -257,13 +256,30 @@ public HttpRequest setBody(File file, ContentType contentType) throws IOExceptio
257
256
try {
258
257
fileContent = FileUtils .readFileToString (file , "UTF-8" );
259
258
} catch (IOException e ) {
260
- final String message = "Failed to read the Request body file:" + file .getPath () + ". Message: "
259
+ final String message = "Failed to read the Request body file:" + file .getPath ()
260
+ + ". Message: "
261
261
+ e .getMessage ();
262
262
throw new IOException (message );
263
263
}
264
264
return setBody (fileContent , contentType );
265
265
}
266
266
267
+ /**
268
+ * Function that sets the Authorization header of the http request.
269
+ *
270
+ * @param username
271
+ * @param password
272
+ * @return
273
+ * @throws UnsupportedEncodingException
274
+ */
275
+ public HttpRequest setBasicAuth (String username , String password )
276
+ throws UnsupportedEncodingException {
277
+ String auth = String .format ("%s:%s" , username , password );
278
+ String encodedAuth = new String (Base64 .encodeBase64 (auth .getBytes ()), "UTF-8" );
279
+ params .put (HttpHeaders .AUTHORIZATION , "Basic " + encodedAuth );
280
+ return this ;
281
+ }
282
+
267
283
/**
268
284
* Function that executes the http request.
269
285
*
@@ -272,7 +288,8 @@ public HttpRequest setBody(File file, ContentType contentType) throws IOExceptio
272
288
* @throws IOException
273
289
* @throws ClientProtocolException
274
290
*/
275
- public ResponseEntity performRequest () throws URISyntaxException , ClientProtocolException , IOException {
291
+ public ResponseEntity performRequest ()
292
+ throws URISyntaxException , ClientProtocolException , IOException {
276
293
URIBuilder builder = createURIBuilder ();
277
294
builder = addParametersToURIBuilder (builder );
278
295
request .setURI (builder .build ());
@@ -314,7 +331,7 @@ private URIBuilder addParametersToURIBuilder(URIBuilder builder) {
314
331
* @throws URISyntaxException
315
332
*/
316
333
private URIBuilder createURIBuilder () throws URISyntaxException {
317
- if (endpoint .startsWith ("/" )) {
334
+ if (endpoint .startsWith ("/" )) {
318
335
return new URIBuilder (baseUrl + endpoint );
319
336
} else {
320
337
return new URIBuilder (baseUrl + "/" + endpoint );
@@ -327,7 +344,7 @@ private URIBuilder createURIBuilder() throws URISyntaxException {
327
344
* @return HttpRequest
328
345
*/
329
346
private String trimBaseUrl (String baseUrl ) {
330
- if (baseUrl .endsWith ("/" )) {
347
+ if (baseUrl .endsWith ("/" )) {
331
348
baseUrl = baseUrl .substring (0 , baseUrl .length () - 1 );
332
349
}
333
350
0 commit comments