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;
}
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;
}
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;
}
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;
}
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));
}
Aggregations