Search in sources :

Example 41 with Client

use of com.predic8.membrane.core.interceptor.oauth2.Client in project service-proxy by membrane.

the class LoadBalancerClient2Test method test.

@Test
public void test() throws IOException, InterruptedException {
    File base = getExampleDir("loadbalancer-client-2");
    AssertUtils.replaceInFile(new File(base, "proxies.xml"), "8080", "3023");
    AssertUtils.replaceInFile(new File(base, "lb-client-secured.proxies.xml"), "8080", "3023");
    Process2 sl = new Process2.Builder().in(base).script("service-proxy").waitForMembrane().start();
    try {
        assertEquals(1, LoadBalancerUtil.getRespondingNode("http://localhost:4000/"));
        assertEquals(2, LoadBalancerUtil.getRespondingNode("http://localhost:4001/"));
        assertEquals(3, LoadBalancerUtil.getRespondingNode("http://localhost:4002/"));
        addNodeViaScript(base, "localhost", 4000);
        Thread.sleep(1000);
        assertNodeStatus(getAndAssert200("http://localhost:9000/admin/clusters/show?cluster=Default"), "localhost", 4000, "UP");
        addNodeViaScript(base, "localhost", 4001);
        addNodeViaScript(base, "localhost", 4002);
        Thread.sleep(100);
        assertEquals(1, LoadBalancerUtil.getRespondingNode("http://localhost:3023/service"));
        assertEquals(2, LoadBalancerUtil.getRespondingNode("http://localhost:3023/service"));
        assertEquals(3, LoadBalancerUtil.getRespondingNode("http://localhost:3023/service"));
        removeNodeViaScript(base, "localhost", 4000);
        Thread.sleep(100);
        assertEquals(2, LoadBalancerUtil.getRespondingNode("http://localhost:3023/service"));
        assertEquals(3, LoadBalancerUtil.getRespondingNode("http://localhost:3023/service"));
    } finally {
        sl.killScript();
    }
    sl = new Process2.Builder().in(base).script("service-proxy-secured").waitForMembrane().start();
    try {
        // 1 indicates failure
        controlNodeViaScript(1, base, "up", "localhost", 4000);
        File propFile = new File(base, "client.properties");
        writeStringToFile(propFile, readFileToString(propFile).replace("#", ""));
        Thread.sleep(1000);
        addNodeViaScript(base, "localhost", 4000);
        Thread.sleep(1000);
        AssertUtils.setupHTTPAuthentication("localhost", 9000, "admin", "admin");
        AssertUtils.assertContains("localhost:4000", getAndAssert200("http://localhost:9000/admin/clusters/show?cluster=Default"));
    } finally {
        sl.killScript();
    }
}
Also used : Process2(com.predic8.membrane.examples.Process2) FileUtils.writeStringToFile(org.apache.commons.io.FileUtils.writeStringToFile) File(java.io.File) Test(org.junit.Test)

Example 42 with Client

use of com.predic8.membrane.core.interceptor.oauth2.Client in project service-proxy by membrane.

the class HttpServletHandler method run.

public void run() {
    try {
        srcReq = createRequest();
        exchange.received();
        try {
            DNSCache dnsCache = getTransport().getRouter().getDnsCache();
            String ip = dnsCache.getHostAddress(remoteAddr);
            exchange.setRemoteAddrIp(ip);
            exchange.setRemoteAddr(getTransport().isReverseDNS() ? dnsCache.getHostName(remoteAddr) : ip);
            exchange.setRequest(srcReq);
            exchange.setOriginalRequestUri(srcReq.getUri());
            invokeHandlers();
        } catch (AbortException e) {
            exchange.finishExchange(true, exchange.getErrorMessage());
            writeResponse(exchange.getResponse());
            return;
        }
        // read if not alread read
        exchange.getRequest().readBody();
        writeResponse(exchange.getResponse());
        exchange.setCompleted();
    } catch (EndOfStreamException e) {
        log.debug("stream closed");
    } catch (EOFWhileReadingFirstLineException e) {
        log.debug("Client connection terminated before line was read. Line so far: (" + e.getLineSoFar() + ")");
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    } finally {
        exchange.detach();
    }
}
Also used : EndOfStreamException(com.predic8.membrane.core.util.EndOfStreamException) EOFWhileReadingFirstLineException(com.predic8.membrane.core.transport.http.EOFWhileReadingFirstLineException) DNSCache(com.predic8.membrane.core.util.DNSCache) IOException(java.io.IOException) AbortException(com.predic8.membrane.core.transport.http.AbortException) EOFWhileReadingFirstLineException(com.predic8.membrane.core.transport.http.EOFWhileReadingFirstLineException) EndOfStreamException(com.predic8.membrane.core.util.EndOfStreamException) AbortException(com.predic8.membrane.core.transport.http.AbortException)

Example 43 with Client

use of com.predic8.membrane.core.interceptor.oauth2.Client in project service-proxy by membrane.

the class OAuth2MembraneTest method test.

@Test
public void test() throws Exception {
    Process2 sl = new Process2.Builder().in(getExampleDir("oauth2/membrane/authorization_server")).script("service-proxy").waitForMembrane().start();
    Process2 sl2 = new Process2.Builder().in(getExampleDir("oauth2/membrane/client")).script("service-proxy").waitForMembrane().start();
    try {
        AssertUtils.getAndAssert200("http://localhost:2001");
        String[] headers = new String[2];
        headers[0] = "Content-Type";
        headers[1] = "application/x-www-form-urlencoded";
        AssertUtils.postAndAssert(200, "http://localhost:2000/login/", headers, "target=&username=john&password=password");
        AssertUtils.postAndAssert(200, "http://localhost:2000/login/consent", headers, "target=&consent=Accept");
        Assert.assertEquals(AssertUtils.getAndAssert200("http://thomas-bayer.com"), AssertUtils.getAndAssert200("http://localhost:2000/"));
    } finally {
        sl.killScript();
        sl2.killScript();
    }
}
Also used : Process2(com.predic8.membrane.examples.Process2) Test(org.junit.Test)

Example 44 with Client

use of com.predic8.membrane.core.interceptor.oauth2.Client in project service-proxy by membrane.

the class HttpServerHandler method process.

private void process() throws Exception {
    try {
        DNSCache dnsCache = getTransport().getRouter().getDnsCache();
        InetAddress remoteAddr = sourceSocket.getInetAddress();
        String ip = dnsCache.getHostAddress(remoteAddr);
        exchange.setRemoteAddrIp(ip);
        exchange.setRemoteAddr(getTransport().isReverseDNS() ? dnsCache.getHostName(remoteAddr) : ip);
        exchange.setRequest(srcReq);
        exchange.setOriginalRequestUri(srcReq.getUri());
        if (exchange.getRequest().getHeader().is100ContinueExpected()) {
            final Request request = exchange.getRequest();
            request.addObserver(new MessageObserver() {

                public void bodyRequested(AbstractBody body) {
                    try {
                        if (request.getHeader().is100ContinueExpected()) {
                            // request body from client so that interceptors can handle it
                            Response.continue100().build().write(srcOut);
                            // remove "Expect: 100-continue" since we already sent "100 Continue"
                            request.getHeader().removeFields(Header.EXPECT);
                        }
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }

                public void bodyComplete(AbstractBody body) {
                }
            });
        }
        invokeHandlers();
        exchange.blockResponseIfNeeded();
    } catch (AbortException e) {
        log.debug("Aborted");
        exchange.finishExchange(true, e.getMessage());
        removeBodyFromBuffer();
        writeResponse(exchange.getResponse());
        log.debug("exchange set aborted");
        return;
    }
    try {
        removeBodyFromBuffer();
        writeResponse(exchange.getResponse());
        exchange.setCompleted();
        log.debug("exchange set completed");
    } catch (Exception e) {
        exchange.finishExchange(true, e.getMessage());
        throw e;
    }
}
Also used : MessageObserver(com.predic8.membrane.core.http.MessageObserver) Request(com.predic8.membrane.core.http.Request) AbstractBody(com.predic8.membrane.core.http.AbstractBody) DNSCache(com.predic8.membrane.core.util.DNSCache) InetAddress(java.net.InetAddress) IOException(java.io.IOException) EndOfStreamException(com.predic8.membrane.core.util.EndOfStreamException) SocketException(java.net.SocketException) SSLException(javax.net.ssl.SSLException) SocketTimeoutException(java.net.SocketTimeoutException)

Example 45 with Client

use of com.predic8.membrane.core.interceptor.oauth2.Client in project service-proxy by membrane.

the class HttpServerHandler method run.

public void run() {
    // see Request.isBindTargetConnectionToIncoming()
    Connection boundConnection = null;
    try {
        updateThreadName(true);
        setup();
        while (true) {
            srcReq = new Request();
            endpointListener.setIdleStatus(sourceSocket, true);
            try {
                srcIn.mark(2);
                if (srcIn.read() == -1)
                    break;
                srcIn.reset();
            } finally {
                endpointListener.setIdleStatus(sourceSocket, false);
            }
            if (boundConnection != null) {
                exchange.setTargetConnection(boundConnection);
                boundConnection = null;
            }
            srcReq.read(srcIn, true);
            exchange.received();
            if (srcReq.getHeader().getProxyConnection() != null) {
                srcReq.getHeader().add(Header.CONNECTION, srcReq.getHeader().getProxyConnection());
                srcReq.getHeader().removeFields(Header.PROXY_CONNECTION);
            }
            process();
            if (srcReq.isCONNECTRequest()) {
                log.debug("stopping HTTP Server Thread after establishing an HTTP connect");
                return;
            }
            boundConnection = exchange.getTargetConnection();
            exchange.setTargetConnection(null);
            if (!exchange.canKeepConnectionAlive())
                break;
            if (exchange.getResponse().isRedirect()) {
                break;
            }
            exchange.detach();
            exchange = new Exchange(this);
        }
    } catch (SocketTimeoutException e) {
        log.debug("Socket of thread " + counter + " timed out");
    } catch (SocketException se) {
        log.debug("client socket closed");
    } catch (SSLException s) {
        if (showSSLExceptions) {
            if (s.getCause() instanceof SSLException)
                s = (SSLException) s.getCause();
            if (s.getCause() instanceof SocketException)
                log.debug("ssl socket closed");
            else
                log.error("", s);
        }
    } catch (IOException e) {
        log.error("", e);
    } catch (EndOfStreamException e) {
        log.debug("stream closed");
    } catch (AbortException e) {
        log.debug("exchange aborted.");
    } catch (NoMoreRequestsException e) {
    // happens at the end of a keep-alive connection
    } catch (NoResponseException e) {
        log.debug("No response received. Maybe increase the keep-alive timeout on the server.");
    } catch (EOFWhileReadingFirstLineException e) {
        log.debug("Client connection terminated before line was read. Line so far: (" + e.getLineSoFar() + ")");
    } catch (Exception e) {
        log.error("", e);
    } finally {
        endpointListener.setOpenStatus(sourceSocket, false);
        if (boundConnection != null)
            try {
                boundConnection.close();
            } catch (IOException e) {
                log.debug("Closing bound connection.", e);
            }
        closeConnections();
        exchange.detach();
        updateThreadName(false);
    }
}
Also used : SocketException(java.net.SocketException) EndOfStreamException(com.predic8.membrane.core.util.EndOfStreamException) Request(com.predic8.membrane.core.http.Request) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException) IOException(java.io.IOException) EndOfStreamException(com.predic8.membrane.core.util.EndOfStreamException) SocketException(java.net.SocketException) SSLException(javax.net.ssl.SSLException) SocketTimeoutException(java.net.SocketTimeoutException) Exchange(com.predic8.membrane.core.exchange.Exchange) SocketTimeoutException(java.net.SocketTimeoutException)

Aggregations

Test (org.junit.Test)27 Client (org.orcid.jaxb.model.client_v2.Client)18 Exchange (com.predic8.membrane.core.exchange.Exchange)12 ClientDetailsEntity (org.orcid.persistence.jpa.entities.ClientDetailsEntity)10 IOException (java.io.IOException)8 AbstractInterceptor (com.predic8.membrane.core.interceptor.AbstractInterceptor)6 Outcome (com.predic8.membrane.core.interceptor.Outcome)6 HashSet (java.util.HashSet)6 BaseTest (org.orcid.core.BaseTest)6 ClientRedirectUri (org.orcid.jaxb.model.client_v2.ClientRedirectUri)6 URISyntaxException (java.net.URISyntaxException)5 HttpClient (org.apache.commons.httpclient.HttpClient)5 ScopePathType (org.orcid.jaxb.model.message.ScopePathType)5 Request (com.predic8.membrane.core.http.Request)4 Response (com.predic8.membrane.core.http.Response)4 Process2 (com.predic8.membrane.examples.Process2)4 Http11Test (com.predic8.membrane.integration.Http11Test)4 SessionManager (com.predic8.membrane.core.interceptor.authentication.session.SessionManager)3 Client (com.predic8.membrane.core.interceptor.oauth2.Client)3 HttpClient (com.predic8.membrane.core.transport.http.HttpClient)3