package br.ufms.dct.simplerep.proxies.http;
import java.io.IOException;
import java.util.concurrent.SynchronousQueue;
import org.apache.http.HttpClientConnection;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestExecutor;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
public class LocalInvocationRunner implements Runnable {
private final HttpRequestExecutor httpexecutor;
private HttpRequest request;
private HttpContext context;
private HttpClientConnection conn;
static Logger logger = Logger.getLogger(LocalInvocationRunner.class.getName());
// the queue from which the proxy is waiting the local response
SynchronousQueue<String> outQueue;
public LocalInvocationRunner(SynchronousQueue<String> out
, HttpRequestExecutor exec
, HttpRequest request
, HttpContext ctxt
, HttpClientConnection conn
) {
this.outQueue = out;
this.httpexecutor = exec;
this.context = ctxt;
this.request = request;
this.conn = conn;
}
public void run() {
try {
HttpResponse targetResponse = this.httpexecutor.execute(request, conn, context);
String response = EntityUtils.toString(targetResponse.getEntity());
logger.info("Local response received. Notifying the proxy.");
outQueue.put(response);
}
catch (IOException e) {
if (conn != null && !conn.isOpen()) {
logger.debug("IOException. The connection to the AppServer is closed. It's very likely that the response has already been generated by another replica.");
}
else {
logger.error("IOException when contacting the local AppServer.");
}
}
catch (IllegalStateException e) {
logger.debug("The connection to the AppServer is closed. It's very likely that the response has already been generated by another replica.");
}
catch (HttpException e) {
logger.error("HttpException Error when contacting the local AppServer.");
}
catch (InterruptedException e) {
logger.error("InterruptedException when trying to notify the HTTP Proxy's response queue.");
}
}
}