Search in sources :

Example 21 with Exchange

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!");
}
Also used : Outcome(com.predic8.membrane.core.interceptor.Outcome)

Example 22 with Exchange

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);
}
Also used : Response(com.predic8.membrane.core.http.Response) Header(com.predic8.membrane.core.http.Header) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateTime(org.joda.time.DateTime)

Example 23 with Exchange

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;
}
Also used : Request(com.predic8.membrane.core.http.xml.Request)

Example 24 with Exchange

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;
}
Also used : InvalidParameterException(java.security.InvalidParameterException) Matcher(java.util.regex.Matcher) Mapping(com.predic8.membrane.core.interceptor.administration.Mapping) Method(java.lang.reflect.Method)

Example 25 with Exchange

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;
}
Also used : Outcome(com.predic8.membrane.core.interceptor.Outcome)

Aggregations

Exchange (com.predic8.membrane.core.exchange.Exchange)107 Test (org.junit.Test)39 IOException (java.io.IOException)32 Request (com.predic8.membrane.core.http.Request)25 Outcome (com.predic8.membrane.core.interceptor.Outcome)24 Response (com.predic8.membrane.core.http.Response)16 AbstractInterceptor (com.predic8.membrane.core.interceptor.AbstractInterceptor)16 ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)16 HttpRouter (com.predic8.membrane.core.HttpRouter)14 Before (org.junit.Before)13 ServiceProxyKey (com.predic8.membrane.core.rules.ServiceProxyKey)12 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)11 Header (com.predic8.membrane.core.http.Header)10 HttpClient (com.predic8.membrane.core.transport.http.HttpClient)10 CacheBuilder (com.google.common.cache.CacheBuilder)9 Rule (com.predic8.membrane.core.rules.Rule)6 URISyntaxException (java.net.URISyntaxException)6 UnknownHostException (java.net.UnknownHostException)6 ArrayList (java.util.ArrayList)6 Session (com.predic8.membrane.core.interceptor.authentication.session.SessionManager.Session)5