Search in sources :

Example 41 with Outcome

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

the class RevocationEndpointProcessor method process.

@Override
public Outcome process(Exchange exc) throws Exception {
    Map<String, String> params = URLParamUtil.getParams(uriFactory, exc);
    if (!params.containsKey("token")) {
        exc.setResponse(OAuth2Util.createParameterizedJsonErrorResponse(exc, jsonGen, "error", "invalid_request"));
        return Outcome.RETURN;
    }
    SessionManager.Session session = authServer.getSessionFinder().getSessionForToken(params.get("token"));
    if (session == null) {
        // token doesnt exist -> token is already invalid
        exc.setResponse(Response.ok().bodyEmpty().build());
        return Outcome.RETURN;
    }
    Client client;
    Map<String, String> userAttributes = session.getUserAttributes();
    synchronized (userAttributes) {
        try {
            client = authServer.getClientList().getClient(userAttributes.get(ParamNames.CLIENT_ID));
        } catch (Exception e) {
            // This should never happen
            exc.setResponse(Response.ok().bodyEmpty().build());
            return Outcome.RETURN;
        }
    }
    String paramClientId = params.get(ParamNames.CLIENT_ID);
    String paramClientSecret = params.get(ParamNames.CLIENT_SECRET);
    if ((paramClientId != null && !client.getClientId().equals(paramClientId)) || (paramClientSecret != null && !client.getClientSecret().equals(paramClientSecret))) {
        exc.setResponse(OAuth2Util.createParameterizedJsonErrorResponse(exc, jsonGen, "error", "invalid_grant"));
        return Outcome.RETURN;
    }
    try {
        authServer.getTokenGenerator().invalidateToken(params.get("token"), client.getClientId(), client.getClientSecret());
    } catch (Exception e) {
        exc.setResponse(OAuth2Util.createParameterizedJsonErrorResponse(exc, jsonGen, "error", "invalid_grant"));
        return Outcome.RETURN;
    }
    synchronized (session) {
        session.clear();
    }
    synchronized (authServer.getSessionManager()) {
        authServer.getSessionManager().removeSession(session);
    }
    synchronized (authServer.getSessionFinder()) {
        authServer.getSessionFinder().removeSessionForToken(params.get("token"));
    }
    exc.setResponse(Response.ok().bodyEmpty().build());
    return Outcome.RETURN;
}
Also used : SessionManager(com.predic8.membrane.core.interceptor.authentication.session.SessionManager) Client(com.predic8.membrane.core.interceptor.oauth2.Client)

Example 42 with Outcome

use of com.predic8.membrane.core.interceptor.Outcome 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 43 with Outcome

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

the class LimitInterceptor method handleMessage.

private Outcome handleMessage(Exchange exc, Message msg) throws IOException {
    if (maxBodyLength == -1)
        return Outcome.CONTINUE;
    long len = msg.getHeader().getContentLength();
    if (len != -1 && len > maxBodyLength) {
        log.info("Message length (" + len + ") exceeded limit (" + maxBodyLength + ")");
        exc.setResponse(createFailureResponse());
        return Outcome.ABORT;
    }
    msg.setBody(new Body(new LengthLimitingStream(msg.getBodyAsStream())));
    return Outcome.CONTINUE;
}
Also used : Body(com.predic8.membrane.core.http.Body)

Example 44 with Outcome

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

the class RuleMatchingInterceptor method handleRequest.

@Override
public Outcome handleRequest(Exchange exc) throws Exception {
    if (exc.getRule() != null)
        return Outcome.CONTINUE;
    Rule rule = getRule(exc);
    exc.setRule(rule);
    if (exc.getRule().getSslOutboundContext() != null) {
        exc.setProperty(Exchange.SSL_CONTEXT, exc.getRule().getSslOutboundContext());
    }
    if (rule instanceof NullRule) {
        handleNoRuleFound(exc);
        return Outcome.ABORT;
    }
    if (xForwardedForEnabled && (rule instanceof AbstractServiceProxy))
        insertXForwardedFor(exc);
    return Outcome.CONTINUE;
}
Also used : AbstractServiceProxy(com.predic8.membrane.core.rules.AbstractServiceProxy) NullRule(com.predic8.membrane.core.rules.NullRule) Rule(com.predic8.membrane.core.rules.Rule) NullRule(com.predic8.membrane.core.rules.NullRule) ProxyRule(com.predic8.membrane.core.rules.ProxyRule)

Example 45 with Outcome

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

the class AMRateLimitInterceptorTest method testHandleRequestRateLimit5SecondConcurrency.

@Test
public void testHandleRequestRateLimit5SecondConcurrency() throws Exception {
    final Exchange exc = new Exchange(null);
    exc.setResponse(Response.ResponseBuilder.newInstance().build());
    exc.setProperty(Exchange.API_KEY, "junit");
    exc.setRule(new ServiceProxy());
    exc.getRule().setName("junit API");
    final AMRateLimiter rli = new AMRateLimiter();
    ApiManagementConfiguration amc = new ApiManagementConfiguration(System.getProperty("user.dir"), "src\\test\\resources\\apimanagement\\api.yaml");
    rli.setAmc(amc);
    ArrayList<Thread> threads = new ArrayList<Thread>();
    final AtomicInteger continues = new AtomicInteger();
    final AtomicInteger returns = new AtomicInteger();
    for (int i = 0; i < 1000; i++) {
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Outcome out = rli.handleRequest(exc);
                    if (out == Outcome.CONTINUE) {
                        continues.incrementAndGet();
                    } else if (out == Outcome.RETURN) {
                        returns.incrementAndGet();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        threads.add(t);
        t.start();
    }
    for (Thread t : threads) {
        t.join();
    }
    assertEquals(5, continues.get());
    assertEquals(995, returns.get());
    Thread.sleep(2000);
    assertEquals(Outcome.CONTINUE, rli.handleRequest(exc));
}
Also used : AMRateLimiter(com.predic8.membrane.core.interceptor.apimanagement.rateLimiter.AMRateLimiter) ArrayList(java.util.ArrayList) Exchange(com.predic8.membrane.core.exchange.Exchange) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Outcome(com.predic8.membrane.core.interceptor.Outcome) Test(org.junit.Test)

Aggregations

Outcome (com.predic8.membrane.core.interceptor.Outcome)25 Exchange (com.predic8.membrane.core.exchange.Exchange)19 AbstractInterceptor (com.predic8.membrane.core.interceptor.AbstractInterceptor)16 ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)14 IOException (java.io.IOException)14 ServiceProxyKey (com.predic8.membrane.core.rules.ServiceProxyKey)10 Test (org.junit.Test)9 HttpRouter (com.predic8.membrane.core.HttpRouter)8 URISyntaxException (java.net.URISyntaxException)7 Before (org.junit.Before)6 ArrayList (java.util.ArrayList)4 Header (com.predic8.membrane.core.http.Header)3 Session (com.predic8.membrane.core.interceptor.authentication.session.SessionManager.Session)3 Rule (com.predic8.membrane.core.rules.Rule)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Body (com.predic8.membrane.core.http.Body)2 HttpClientConfiguration (com.predic8.membrane.core.transport.http.client.HttpClientConfiguration)2 MalformedURLException (java.net.MalformedURLException)2 InvalidParameterException (java.security.InvalidParameterException)2