Skip to content

Commit

Permalink
In which our hero fixed the issue of blocking requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximilian Voß committed Mar 14, 2019
1 parent a392f89 commit 4136e25
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/main/java/rocks/voss/toniebox/RequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,17 @@
public class RequestHandler {
private Logger log = Logger.getLogger(getClass().getName());

final private CloseableHttpClient httpClient;
final private HttpHost proxy = null;
private JWTToken jwtToken;
private final Header[] headerContentTypeJson = new Header[]{new BasicHeader("Content-Type", "application/json")};

public RequestHandler() {
httpClient = HttpClients.createDefault();
}

public RequestHandler(String proxySchema, String proxyHost, int proxyPort) {
HttpHost proxy = new HttpHost(proxyHost, proxyPort, proxySchema);
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
httpClient = HttpClients.custom()
.setRoutePlanner(routePlanner)
.build();
}


public void Login(Login loginBean) throws IOException {
jwtToken = executePostRequest(Constants.SESSION, headerContentTypeJson, new StringEntity(Transformer.getJsonString(loginBean), "UTF-8"), null, JWTToken.class);
}
Expand Down Expand Up @@ -145,11 +139,24 @@ private <T> T executeRequest(HttpRequestBase method, JWTToken jwtToken, Class<T>
method.setConfig(requestConfig);

log.debug(method.toString());
CloseableHttpResponse response = httpClient.execute(method);

CloseableHttpResponse response = null;
response = getHttpClient().execute(method);
log.debug("Status Code: " + response.getStatusLine());
if (clazz == null) {
return null;
}
return Transformer.createBean(clazz, response.getEntity().getContent());
}

private CloseableHttpClient getHttpClient() {
if (proxy == null) {
return HttpClients.createDefault();
} else {
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
return HttpClients.custom()
.setRoutePlanner(routePlanner)
.build();
}
}
}

0 comments on commit 4136e25

Please sign in to comment.