Search in sources :

Example 1 with HttpServerHandler

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);
                }
            }
        }
    });
}
Also used : IOException(java.io.IOException) AbstractExchangeViewerListener(com.predic8.membrane.core.model.AbstractExchangeViewerListener)

Example 2 with HttpServerHandler

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();
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) Outcome(com.predic8.membrane.core.interceptor.Outcome) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) HttpRouter(com.predic8.membrane.core.HttpRouter) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Before(org.junit.Before)

Example 3 with HttpServerHandler

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();
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) Outcome(com.predic8.membrane.core.interceptor.Outcome) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) HttpRouter(com.predic8.membrane.core.HttpRouter) IOException(java.io.IOException) EndOfStreamException(com.predic8.membrane.core.util.EndOfStreamException) Before(org.junit.Before)

Example 4 with HttpServerHandler

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;
}
Also used : StringWriter(java.io.StringWriter) Html(com.googlecode.jatl.Html) ArrayList(java.util.ArrayList) List(java.util.List) HttpServerHandler(com.predic8.membrane.core.transport.http.HttpServerHandler)

Example 5 with HttpServerHandler

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);
        }
    }
}
Also used : SocketException(java.net.SocketException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IOException(java.io.IOException) BindException(java.net.BindException) PortOccupiedException(com.predic8.membrane.core.transport.PortOccupiedException) SocketException(java.net.SocketException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) InvalidParameterException(java.security.InvalidParameterException)

Aggregations

IOException (java.io.IOException)4 HttpRouter (com.predic8.membrane.core.HttpRouter)2 Exchange (com.predic8.membrane.core.exchange.Exchange)2 AbstractInterceptor (com.predic8.membrane.core.interceptor.AbstractInterceptor)2 Outcome (com.predic8.membrane.core.interceptor.Outcome)2 ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)2 ServiceProxyKey (com.predic8.membrane.core.rules.ServiceProxyKey)2 Before (org.junit.Before)2 Html (com.googlecode.jatl.Html)1 AbstractExchangeViewerListener (com.predic8.membrane.core.model.AbstractExchangeViewerListener)1 PortOccupiedException (com.predic8.membrane.core.transport.PortOccupiedException)1 HttpServerHandler (com.predic8.membrane.core.transport.http.HttpServerHandler)1 EndOfStreamException (com.predic8.membrane.core.util.EndOfStreamException)1 StringWriter (java.io.StringWriter)1 BindException (java.net.BindException)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 SocketException (java.net.SocketException)1 URISyntaxException (java.net.URISyntaxException)1 InvalidParameterException (java.security.InvalidParameterException)1