Skip to content

Instantly share code, notes, and snippets.

@ar
Created July 31, 2016 21:41
Show Gist options
  • Save ar/5fbe4191242696bbfe595d8035065525 to your computer and use it in GitHub Desktop.
Save ar/5fbe4191242696bbfe595d8035065525 to your computer and use it in GitHub Desktop.
Sample HTTP client participant
package org.jpos.rest.client;
import java.io.IOException;
import java.io.Serializable;
import java.io.ByteArrayInputStream;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.jdom2.Element;
import org.jdom2.Document;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jpos.util.Log;
import org.jpos.core.Configurable;
import org.jpos.core.Configuration;
import org.jpos.transaction.Context;
import org.jpos.transaction.TransactionParticipant;
@SuppressWarnings("unused")
public class HttpQuery extends Log implements TransactionParticipant, Configurable {
Configuration cfg;
public static final String REST_URL = "REST_URL";
public static final String REST_PARAMS = "REST_PARAMS";
public static final String REST_METHOD = "REST_METHOD";
public static final String REST_REQUEST = "REST_REQUEST";
public static final String REST_RESPONSE = "REST_RESPONSE";
public HttpQuery () {
super();
}
public int prepare (long id, Serializable o) {
Context ctx = (Context) o;
HttpClient client = new HttpClient();
client.setConnectionTimeout (cfg.getInt ("timeout", 60000));
HttpMethod method;
String methodName = ctx.getString (REST_METHOD, cfg.get ("method", "GET"));
String url = getURL (ctx);
byte[] payLoad = (byte[]) ctx.get (REST_REQUEST);
// payLoad = "<isomsg><field id='0' value='0800' /></isomsg>".getBytes();
if ("PUT".equalsIgnoreCase (methodName)) {
method = new PutMethod(url);
if (payLoad != null)
((PutMethod)method).setRequestBody(new ByteArrayInputStream(payLoad));
} else if ("POST".equalsIgnoreCase (methodName)) {
method = new PostMethod(url);
if (payLoad != null)
((PostMethod)method).setRequestBody(new ByteArrayInputStream(payLoad));
} else if ("DELETE".equalsIgnoreCase (methodName)) {
method = new DeleteMethod(url);
} else {
method = new GetMethod(url);
}
method.getParams().setParameter(
HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(cfg.getInt ("retry", 3), false));
try {
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
ctx.log ("HttpQuery error -- status code=" + statusCode);
return ABORTED;
}
byte[] responseBody = method.getResponseBody();
ctx.put (cfg.get ("response", REST_RESPONSE), parseMsg (responseBody));
return PREPARED;
} catch (Exception e) {
ctx.log (e);
} finally {
method.releaseConnection();
}
return ABORTED;
}
public int prepareForAbort (long id, Serializable o) {
if (cfg.getBoolean ("on-abort", false))
return prepare (id, o);
return PREPARED;
}
public void commit (long id, Serializable o) { }
public void abort (long id, Serializable o) { }
public void setConfiguration (Configuration cfg) {
this.cfg = cfg;
}
private String getURL (Context ctx) {
StringBuilder sb = new StringBuilder (
ctx.getString (REST_URL, cfg.get ("url", ""))
);
String params = ctx.getString (REST_PARAMS, "");
if (params.length() > 0) {
sb.append ('?');
sb.append (params);
}
return sb.toString();
}
private Element parseMsg (byte[] b) throws JDOMException, IOException {
if (b == null)
return null;
SAXBuilder builder = new SAXBuilder ();
builder.setValidation (false);
Document doc = builder.build (
new ByteArrayInputStream (b)
);
return doc.getRootElement ();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment