use of com.predic8.membrane.core.exchange.Exchange in project service-proxy by membrane.
the class OAuth2Processors method runProcessors.
public Outcome runProcessors(Exchange exc) throws Exception {
for (EndpointProcessor excProc : processors) {
if (excProc.isResponsible(exc)) {
Outcome result = excProc.process(exc);
postProcessing(exc);
return result;
}
}
throw new RuntimeException("No OAuthEndpointProcessor found. This should never happen!");
}
use of com.predic8.membrane.core.exchange.Exchange in project service-proxy by membrane.
the class RateLimitInterceptor method setResponseToServiceUnavailable.
public void setResponseToServiceUnavailable(Exchange exc) throws UnsupportedEncodingException {
Header hd = new Header();
DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("EEE, dd MMM yyyy HH:mm:ss 'GMT'").withZoneUTC().withLocale(Locale.US);
hd.add("Date", dateFormatter.print(DateTime.now()));
hd.add("X-LimitDuration", PeriodFormat.getDefault().print(rateLimitStrategy.requestLimitDuration.toPeriod()));
hd.add("X-LimitRequests", Integer.toString(rateLimitStrategy.requestLimit));
String ip = exc.getRemoteAddrIp();
DateTime availableAgainDateTime = rateLimitStrategy.getServiceAvailableAgainTime(ip);
hd.add("X-LimitReset", Long.toString(availableAgainDateTime.getMillis()));
StringBuilder bodyString = new StringBuilder();
DateTimeFormatter dtFormatter = DateTimeFormat.forPattern("HH:mm:ss aa");
bodyString.append(ip).append(" exceeded the rate limit of ").append(rateLimitStrategy.requestLimit).append(" requests per ").append(PeriodFormat.getDefault().print(rateLimitStrategy.requestLimitDuration.toPeriod())).append(". The next request can be made at ").append(dtFormatter.print(availableAgainDateTime));
Response resp = ResponseBuilder.newInstance().status(429, "Too Many Requests.").contentType(MimeType.TEXT_PLAIN_UTF8).header(hd).body(bodyString.toString()).build();
exc.setResponse(resp);
}
use of com.predic8.membrane.core.exchange.Exchange in project service-proxy by membrane.
the class HTTP2XMLInterceptor method handleRequest.
@Override
public Outcome handleRequest(Exchange exc) throws Exception {
log.debug("uri: " + exc.getRequest().getUri());
String res = new Request(exc.getRequest()).toXml();
log.debug("http-xml: " + res);
exc.getRequest().setBodyContent(res.getBytes("UTF-8"));
// TODO
exc.getRequest().setMethod("POST");
exc.getRequest().getHeader().setSOAPAction("");
return Outcome.CONTINUE;
}
use of com.predic8.membrane.core.exchange.Exchange in project service-proxy by membrane.
the class RESTInterceptor method dispatchRequest.
private Outcome dispatchRequest(Exchange exc) throws Exception {
String path = router.getUriFactory().create(exc.getDestinations().get(0)).getPath();
for (Method m : getClass().getMethods()) {
Mapping a = m.getAnnotation(Mapping.class);
if (a == null)
continue;
Matcher matcher = Pattern.compile(a.value()).matcher(path);
if (matcher.matches()) {
Object[] parameters;
switch(m.getParameterTypes().length) {
case 2:
parameters = new Object[] { new QueryParameter(URLParamUtil.getParams(router.getUriFactory(), exc), matcher), getRelativeRootPath(path) };
break;
case 3:
parameters = new Object[] { new QueryParameter(URLParamUtil.getParams(router.getUriFactory(), exc), matcher), getRelativeRootPath(path), exc };
break;
default:
throw new InvalidParameterException("@Mapping is supposed to annotate a 2-parameter method.");
}
exc.setResponse((Response) m.invoke(this, parameters));
return Outcome.RETURN;
}
}
return Outcome.CONTINUE;
}
use of com.predic8.membrane.core.exchange.Exchange in project service-proxy by membrane.
the class RESTInterceptor method handleRequest.
@Override
public Outcome handleRequest(Exchange exc) throws Exception {
log.debug("request: " + exc.getOriginalRequestUri());
exc.setTimeReqSent(System.currentTimeMillis());
Outcome o = dispatchRequest(exc);
exc.setReceived();
exc.setTimeResReceived(System.currentTimeMillis());
return o;
}
Aggregations