Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Gives HttpQuery subclasses ability to customize HTTP client #182

Merged
merged 3 commits into from
Sep 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ public class HttpQuery extends Log implements AbortParticipant, Configurable, De
private String statusName;
private String contentTypeName;
private String basicAuthenticationName;
private RedirectStrategy redirectStrategy;

// A shared client for the instance.
// Created at configuration time; destroyed when this participant is destroyed.
private CloseableHttpAsyncClient client = null;
private CloseableHttpAsyncClient httpClient = null;

public HttpQuery () {
super();
Expand Down Expand Up @@ -116,7 +117,7 @@ public int prepare (long id, Serializable o) {
// Ref: https://hc.apache.org/httpcomponents-client-4.5.x/tutorial/html/authentication.html
}

client.execute(httpRequest, httpCtx, new FutureCallback<HttpResponse>() {
getHttpClient().execute(httpRequest, httpCtx, new FutureCallback<HttpResponse>() {
@Override
public void completed(HttpResponse result) {
ctx.log (result.getStatusLine());
Expand Down Expand Up @@ -199,25 +200,29 @@ public void setConfiguration (Configuration cfg) throws ConfigurationException {
headers[i].substring(colonPos+1)); // header value
}

buildClient(cfg);
}

protected void buildClient(Configuration cfg) throws ConfigurationException {
String redirProp = cfg.get("redirect-strategy", "default");
RedirectStrategy redirectStrategy;
if ("default".equals(redirProp))
redirectStrategy= DefaultRedirectStrategy.INSTANCE;
else if ("lax".equals(redirProp))
redirectStrategy= LaxRedirectStrategy.INSTANCE;
else
throw new ConfigurationException("'redirect-strategy' must be 'lax' or 'default'");
}

HttpAsyncClientBuilder builder = HttpAsyncClients.custom()
.useSystemProperties()
.setRedirectStrategy(redirectStrategy);
public CloseableHttpAsyncClient getHttpClient() {
if (httpClient == null) {
setHttpClient(getClientBuilder().build());
httpClient.start();
}
return httpClient;
}

public void setHttpClient(CloseableHttpAsyncClient httpClient) {
this.httpClient = httpClient;
}

client = builder.build();
client.start();
protected HttpAsyncClientBuilder getClientBuilder() {
return HttpAsyncClients.custom().useSystemProperties().setRedirectStrategy(redirectStrategy);
}

private String getURL (Context ctx) {
Expand Down Expand Up @@ -306,9 +311,9 @@ else if (hObj instanceof List)

@Override
public void destroy() {
if (client != null && client.isRunning()) {
if (httpClient != null && httpClient.isRunning()) {
try {
client.close();
httpClient.close();
} catch (IOException e) {
warn(e);
}
Expand Down