Skip to content

Commit 10db72a

Browse files
authored
Change functionality in HttpRequest and HttpExecutor (#24)
- HttpRequest is now mockable by defining new ways of initializing the class. - HttpMethod can now be set after initializing as well. This makes it possible for spring to autowire in a class while different member functions still can execute different types of requests. - Headers can now be set in request body. - URI can now be retrieved from HttpRequest.
1 parent b60ea57 commit 10db72a

File tree

3 files changed

+100
-26
lines changed

3 files changed

+100
-26
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.github.ericsson</groupId>
66
<artifactId>eiffel-commons-java</artifactId>
7-
<version>0.0.13</version>
7+
<version>0.0.15</version>
88

99
<name>eiffel commons java</name>
1010
<description>A shared library for eiffel components</description>

src/main/java/com/ericsson/eiffelcommons/utils/HttpExecutor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import org.apache.http.impl.client.CloseableHttpClient;
2525
import org.apache.http.impl.client.HttpClientBuilder;
2626

27-
public final class HttpExecutor {
27+
public class HttpExecutor {
2828

2929
private static HttpExecutor instance;
3030
private CloseableHttpClient client = HttpClientBuilder.create()
@@ -34,6 +34,10 @@ public HttpExecutor() {
3434

3535
}
3636

37+
public HttpExecutor(CloseableHttpClient client) {
38+
this.client = client;
39+
}
40+
3741
public static HttpExecutor getInstance() {
3842
if (instance == null) {
3943
instance = new HttpExecutor();

src/main/java/com/ericsson/eiffelcommons/utils/HttpRequest.java

Lines changed: 94 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@
1313
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
16-
*/
16+
*/
1717
package com.ericsson.eiffelcommons.utils;
1818

1919
import java.io.File;
2020
import java.io.IOException;
21+
import java.net.URI;
2122
import java.net.URISyntaxException;
2223
import java.util.HashMap;
2324
import java.util.Map;
2425

2526
import org.apache.commons.io.FileUtils;
27+
import org.apache.http.Header;
2628
import org.apache.http.client.ClientProtocolException;
2729
import org.apache.http.client.methods.HttpDelete;
2830
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
@@ -31,7 +33,9 @@
3133
import org.apache.http.client.methods.HttpPut;
3234
import org.apache.http.client.methods.HttpRequestBase;
3335
import org.apache.http.client.utils.URIBuilder;
36+
import org.apache.http.entity.ContentType;
3437
import org.apache.http.entity.StringEntity;
38+
import org.apache.http.message.BasicHeader;
3539

3640
import lombok.Getter;
3741
import lombok.Setter;
@@ -56,28 +60,23 @@ public enum HttpMethod {
5660
@Getter
5761
protected Map<String, String> params;
5862

63+
public HttpRequest() {
64+
params = new HashMap<>();
65+
initExecutor(false);
66+
}
67+
5968
public HttpRequest(HttpMethod method) {
6069
this(method, false);
6170
}
6271

72+
public HttpRequest(HttpMethod method, HttpExecutor executor) {
73+
this(method, false);
74+
this.executor = executor;
75+
}
76+
6377
public HttpRequest(HttpMethod method, boolean persistentClient) {
6478
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);
8180
initExecutor(persistentClient);
8281
}
8382

@@ -89,6 +88,29 @@ private void initExecutor(boolean persistentClient) {
8988
}
9089
}
9190

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+
92114
/**
93115
* Gets the base url(not including endpoint) for example: http://localhost:8080
94116
* @return String
@@ -129,12 +151,24 @@ public void resetHttpRequestObject() {
129151
* :: the key of the header
130152
* @param value
131153
* :: the value of the header
132-
* @return
154+
* @return HttpRequest
133155
*/
134156
public HttpRequest addHeader(String key, String value) {
135157
request.addHeader(key, value);
136158
return this;
137159
}
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+
}
138172

139173
/**
140174
* 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) {
165199
* :: the key of the parameter
166200
* @param value
167201
* :: the value of the parameter
168-
* @return
202+
* @return HttpRequest
169203
*/
170204
public HttpRequest addParam(String key, String value) {
171205
params.put(key, value);
172206
return this;
173207
}
174208

175209
/**
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).
177223
*
178224
* @param body
179225
* :: String input
180226
* @return HTTPRequest
181227
*/
182228
public HttpRequest setBody(String body) {
183-
((HttpEntityEnclosingRequestBase) request).setEntity(new StringEntity(body, "UTF-8"));
229+
setBody(body, ContentType.TEXT_PLAIN);
184230
return this;
185231
}
186232

187233
/**
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).
189235
*
190236
* @param file
191237
* :: File input
192238
* @return HTTPRequest
193239
* @throws IOException
194240
*/
195241
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 {
196256
String fileContent = "";
197257
try {
198258
fileContent = FileUtils.readFileToString(file, "UTF-8");
@@ -201,7 +261,7 @@ public HttpRequest setBody(File file) throws IOException {
201261
+ e.getMessage();
202262
throw new IOException(message);
203263
}
204-
return setBody(fileContent);
264+
return setBody(fileContent, contentType);
205265
}
206266

207267
/**
@@ -215,11 +275,21 @@ public HttpRequest setBody(File file) throws IOException {
215275
public ResponseEntity performRequest() throws URISyntaxException, ClientProtocolException, IOException {
216276
URIBuilder builder = createURIBuilder();
217277
builder = addParametersToURIBuilder(builder);
218-
219278
request.setURI(builder.build());
220279
return executor.executeRequest(request);
221280
}
222281

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+
223293
/**
224294
* Function that adds parameters to the URIBuilder
225295
*

0 commit comments

Comments
 (0)