Search in sources :

Example 6 with Outcome

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

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

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

Example 9 with Outcome

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

the class WSDLPublisherInterceptor method handleRequest.

@Override
public Outcome handleRequest(final Exchange exc) throws Exception {
    if (!"GET".equals(exc.getRequest().getMethod()))
        return Outcome.CONTINUE;
    try {
        String resource = null;
        if (exc.getRequestURI().endsWith("?wsdl") || exc.getRequestURI().endsWith("?WSDL")) {
            processDocuments(exc);
            exc.setResponse(WebServerInterceptor.createResponse(router.getResolverMap(), resource = wsdl));
            exc.getResponse().getHeader().setContentType(MimeType.TEXT_XML);
        }
        if (exc.getRequestURI().contains("?xsd=")) {
            Map<String, String> params = URLParamUtil.getParams(router.getUriFactory(), exc);
            if (params.containsKey("xsd")) {
                int n = Integer.parseInt(params.get("xsd"));
                String path;
                processDocuments(exc);
                synchronized (paths) {
                    if (!paths.containsKey(n)) {
                        exc.setResponse(Response.forbidden("Unknown parameter. You may only retrieve documents referenced by the WSDL.").build());
                        return Outcome.ABORT;
                    }
                    path = paths.get(n);
                }
                exc.setResponse(WebServerInterceptor.createResponse(router.getResolverMap(), resource = path));
                exc.getResponse().getHeader().setContentType(MimeType.TEXT_XML);
            }
        }
        if (resource != null) {
            WSDLInterceptor wi = new WSDLInterceptor();
            wi.setRewriteEndpoint(false);
            wi.setPathRewriter(new RelativePathRewriter(exc, resource));
            wi.handleResponse(exc);
            return Outcome.RETURN;
        }
    } catch (NumberFormatException e) {
        exc.setResponse(HttpUtil.setHTMLErrorResponse(Response.internalServerError(), "Bad parameter format.", ""));
        return Outcome.ABORT;
    } catch (ResourceRetrievalException e) {
        exc.setResponse(Response.notFound().build());
        return Outcome.ABORT;
    }
    return Outcome.CONTINUE;
}
Also used : WSDLInterceptor(com.predic8.membrane.core.interceptor.WSDLInterceptor) ResourceRetrievalException(com.predic8.membrane.core.resolver.ResourceRetrievalException)

Example 10 with Outcome

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

the class SwaggerRewriterInterceptor method handleRequest.

@Override
public Outcome handleRequest(Exchange exc) throws Exception {
    ((ServiceProxy) exc.getRule()).setTargetHost(swagger.getHost());
    URL url = new URL(swaggerUrl);
    exc.getDestinations().set(0, url.getProtocol() + "://" + url.getHost() + (url.getPort() < 0 ? "" : ":" + url.getPort()) + exc.getOriginalRequestUri());
    return super.handleRequest(exc);
}
Also used : ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) URL(java.net.URL)

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