Skip to content

Commit

Permalink
add basic BasicAuthentication support
Browse files Browse the repository at this point in the history
  • Loading branch information
ar committed Oct 20, 2019
1 parent dca2bd0 commit 3a14823
Showing 1 changed file with 31 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,30 @@
import java.util.*;

import org.apache.http.*;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.*;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.jpos.core.ConfigurationException;
import org.jpos.transaction.AbortParticipant;
import org.jpos.util.Destroyable;
import org.jpos.util.Log;
import org.jpos.core.Configurable;
import org.jpos.core.Configuration;
import org.jpos.transaction.Context;


@SuppressWarnings("unused")
public class HttpQuery extends Log implements AbortParticipant, Configurable, Destroyable {
public class HttpQuery extends Log implements AbortParticipant, Configurable {
private static final int DEFAULT_CONNECT_TIMEOUT = 10000;
private static final int DEFAULT_TIMEOUT = 15000;

Expand All @@ -65,12 +68,10 @@ public class HttpQuery extends Log implements AbortParticipant, Configurable, De
private String statusName;
private String contentTypeName;


private CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
private String basicAuthenticationName;

public HttpQuery () {
super();
client.start();

}
public int prepare (long id, Serializable o) {
Expand All @@ -80,13 +81,28 @@ public int prepare (long id, Serializable o) {
if (httpRequest == null)
return FAIL; // probably wrong http method; abort early and avoid NPEs later


addHeaders(ctx, httpRequest);

httpRequest.setConfig(RequestConfig.custom().
setConnectTimeout(connectTimeout).
setSocketTimeout(timeout).
build());


CloseableHttpAsyncClient client;
String basicAuth = ctx.get(basicAuthenticationName);
if (basicAuth != null && basicAuth.contains(":")) {
CredentialsProvider credsProvider = null;
String[] credentials = basicAuth.split(":");
credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(credentials[0], credentials[1]));
client = HttpAsyncClients.custom().setDefaultCredentialsProvider(credsProvider).build();
} else {
client = HttpAsyncClients.createDefault();
}
client.start();
client.execute(httpRequest, new FutureCallback<HttpResponse>() {
@Override
public void completed(HttpResponse result) {
Expand All @@ -105,6 +121,7 @@ public void completed(HttpResponse result) {
}

ctx.put (statusName, sc); // status has to be the last entry because client might be waiting on it
close (ctx, client);
ctx.resume();
}

Expand All @@ -116,11 +133,13 @@ public void failed(Exception ex) {
// java.net.ConnectException: Timeout connecting to [mydomain.com/xxx.xxx.xxx.xxx:ppp]
// java.net.SocketTimeoutException: ... (when no HTTP response)
ctx.log(ex);
close (ctx, client);
ctx.resume();
}

@Override
public void cancelled() {
close (ctx, client);
ctx.resume();
}
});
Expand Down Expand Up @@ -149,6 +168,8 @@ public void setConfiguration (Configuration cfg) throws ConfigurationException {
responseName = cfg.get("responseName", "HTTP_RESPONSE");
statusName = cfg.get("responseStatusName", "HTTP_STATUS");

basicAuthenticationName = cfg.get("basicAuthenticationdName", ".HTTP_BASIC_AUTHENTICATION");

// ctx name under which extra http headers could exist at runtime
// the object could be a List<String> or String[] (in the "name:value" format)
// or a Map<String,String>
Expand Down Expand Up @@ -245,15 +266,11 @@ else if (hObj instanceof List)
}
} // addHeaders


@Override
public void destroy() {
if (client.isRunning()) {
try {
client.close();
} catch (IOException e) {
warn(e);
}
private void close (Context ctx, CloseableHttpAsyncClient client) {
try {
client.close();
} catch (IOException e) {
ctx.log(e);
}
}
}

0 comments on commit 3a14823

Please sign in to comment.