use of com.predic8.membrane.core.transport.http.HttpServerHandler in project service-proxy by membrane.
the class HttpClient method setupConnectionForwarding.
public static void setupConnectionForwarding(Exchange exc, final Connection con, final String protocol, StreamPump.StreamPumpStats streamPumpStats) throws SocketException {
final HttpServerHandler hsr = (HttpServerHandler) exc.getHandler();
String source = hsr.getSourceSocket().getRemoteSocketAddress().toString();
String dest = con.toString();
final StreamPump a;
final StreamPump b;
if ("WebSocket".equals(protocol)) {
WebSocketStreamPump aTemp = new WebSocketStreamPump(hsr.getSrcIn(), con.out, streamPumpStats, protocol + " " + source + " -> " + dest, exc.getRule(), true, exc);
WebSocketStreamPump bTemp = new WebSocketStreamPump(con.in, hsr.getSrcOut(), streamPumpStats, protocol + " " + source + " <- " + dest, exc.getRule(), false, null);
aTemp.init(bTemp);
bTemp.init(aTemp);
a = aTemp;
b = bTemp;
} else {
a = new StreamPump(hsr.getSrcIn(), con.out, streamPumpStats, protocol + " " + source + " -> " + dest, exc.getRule());
b = new StreamPump(con.in, hsr.getSrcOut(), streamPumpStats, protocol + " " + source + " <- " + dest, exc.getRule());
}
hsr.getSourceSocket().setSoTimeout(0);
exc.addExchangeViewerListener(new AbstractExchangeViewerListener() {
@Override
public void setExchangeFinished() {
String threadName = Thread.currentThread().getName();
new Thread(b, threadName + " " + protocol + " Backward Thread").start();
try {
Thread.currentThread().setName(threadName + " " + protocol + " Onward Thread");
a.run();
} finally {
try {
con.close();
} catch (IOException e) {
log.debug("", e);
}
}
}
});
}
use of com.predic8.membrane.core.transport.http.HttpServerHandler in project service-proxy by membrane.
the class HttpKeepAliveTest method setUp.
@Before
public void setUp() throws Exception {
set = new HashSet<Integer>();
service1 = new HttpRouter();
sp1 = new ServiceProxy(new ServiceProxyKey("localhost", "POST", ".*", 2003), "thomas-bayer.com", 80);
sp1.getInterceptors().add(new AbstractInterceptor() {
@Override
public Outcome handleRequest(Exchange exc) throws Exception {
exc.getRequest().readBody();
exc.setResponse(Response.ok("OK.").build());
set.add(((HttpServerHandler) exc.getHandler()).getSrcOut().hashCode());
return Outcome.RETURN;
}
});
service1.getRuleManager().addProxyAndOpenPortIfNew(sp1);
service1.init();
}
use of com.predic8.membrane.core.transport.http.HttpServerHandler in project service-proxy by membrane.
the class BoundConnectionTest method setUp.
@Before
public void setUp() throws Exception {
router = new HttpRouter();
ServiceProxy sp1 = new ServiceProxy(new ServiceProxyKey("*", "*", ".*", 3021), "localhost", 3022);
router.getRuleManager().addProxyAndOpenPortIfNew(sp1);
ServiceProxy sp2 = new ServiceProxy(new ServiceProxyKey("*", "*", ".*", 3022), "", -1);
sp2.getInterceptors().add(new AbstractInterceptor() {
@Override
public Outcome handleRequest(Exchange exc) throws Exception {
exc.getRequest().readBody();
exc.setResponse(Response.ok("OK.").build());
connectionHash = ((HttpServerHandler) exc.getHandler()).getSrcOut().hashCode();
return Outcome.RETURN;
}
});
router.getRuleManager().addProxyAndOpenPortIfNew(sp2);
router.init();
}
use of com.predic8.membrane.core.transport.http.HttpServerHandler in project service-proxy by membrane.
the class IndexInterceptor method handleRequest.
@Override
public Outcome handleRequest(final Exchange exc) throws Exception {
StringWriter sw = new StringWriter();
new Html(sw) {
{
html();
head();
title().text(Constants.PRODUCT_NAME + ": Service Proxies").end();
style();
raw("<!--\r\n" + "body { font-family: sans-serif; }\r\n" + "h1 { font-size: 24pt; }\r\n" + "td, th { border: 1px solid black; padding: 0pt 10pt; }\r\n" + "table { border-collapse: collapse; }\r\n" + ".help { margin-top:20pt; color:#AAAAAA; padding:1em 0em 0em 0em; font-size:10pt; }\r\n" + ".footer { color:#AAAAAA; padding:0em 0em; font-size:10pt; }\r\n" + ".footer a { color:#AAAAAA; }\r\n" + ".footer a:hover { color:#000000; }\r\n" + "-->");
end();
end();
body();
h1().text("Service Proxies").end();
List<ServiceInfo> services = getServices(exc);
if (services.isEmpty())
p().text("There are no services defined.").end();
else
createIndexTable(services, exc.getHandler() instanceof HttpServerHandler);
p().classAttr("help").text("The hyperlinks might not work due to semantics which is not known to " + Constants.PRODUCT_NAME + ".").end();
p().classAttr("footer").raw(Constants.HTML_FOOTER).end();
end();
end();
}
private void createIndexTable(List<ServiceInfo> services, boolean showSSL) {
table().cellspacing("0").cellpadding("0").border("" + 1);
tr();
th().text("Name").end();
th().text("Virtual Host").end();
th().text("Port").end();
th().text("Path").end();
if (showSSL)
th().text("SSL").end();
end();
for (ServiceInfo ri : services) {
tr();
td();
if (ri.url != null && !"POST".equals(ri.method)) {
a().href(ri.url);
text(ri.name);
end();
} else {
text(ri.name);
}
end();
td().raw(ri.host).end();
td().raw(ri.port).end();
td().raw(ri.path).end();
if (showSSL)
td().raw(ri.ssl ? "yes" : "").end();
end();
}
end();
}
};
exc.setResponse(Response.ok(sw.toString()).build());
return Outcome.RETURN;
}
use of com.predic8.membrane.core.transport.http.HttpServerHandler in project service-proxy by membrane.
the class HttpEndpointListener method run.
@Override
public void run() {
while (!closed) {
try {
Socket socket = serverSocket.accept();
openSockets.put(socket, Boolean.TRUE);
try {
if (log.isDebugEnabled())
log.debug("Accepted connection from " + socket.getRemoteSocketAddress());
transport.getExecutorService().execute(new HttpServerHandler(socket, this));
} catch (RejectedExecutionException e) {
openSockets.remove(socket);
log.error("HttpServerHandler execution rejected. Might be due to a proxies.xml hot deployment in progress or a low" + " value for <transport maxThreadPoolSize=\"...\">.");
socket.close();
}
} catch (SocketException e) {
String message = e.getMessage();
if (message != null && (message.endsWith("socket closed") || message.endsWith("Socket closed"))) {
log.debug("socket closed.");
break;
} else
log.error("", e);
} catch (NullPointerException e) {
// Ignore this. serverSocket variable is set null during a loop in the process of closing server socket.
} catch (Exception e) {
log.error("", e);
}
}
}
Aggregations