Search in sources :

Example 6 with Exchange

use of com.predic8.membrane.core.exchange.Exchange in project service-proxy by membrane.

the class ElasticSearchExchangeStore method removeAllExchanges.

@Override
public void removeAllExchanges(Rule rule) {
    String name = rule.toString();
    try {
        Exchange exc = new Request.Builder().post(getElasticSearchExchangesPath() + "_delete_by_query").body("{\n" + "  \"query\": {\n" + "    \"bool\": {\n" + "      \"must\": [\n" + "        {\n" + "          \"wildcard\": {\n" + "            \"issuer\": \"" + documentPrefix + "\"\n" + "          }\n" + "        },\n" + "        {\n" + "          \"match\": {\n" + "            \"rule.name\": \"" + name + "\"\n" + "          }\n" + "        }\n" + "      ]\n" + "    }\n" + "  }\n" + "}").header("Content-Type", "application/json").buildExchange();
        client.call(exc);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange) Exchange(com.predic8.membrane.core.exchange.Exchange) CacheBuilder(com.google.common.cache.CacheBuilder) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 7 with Exchange

use of com.predic8.membrane.core.exchange.Exchange in project service-proxy by membrane.

the class AuthHead2BodyInterceptor method handleRequest.

public Outcome handleRequest(AbstractExchange exchange) throws Exception {
    Document doc = getDocument(exchange.getRequest().getBodyAsStreamDecoded(), exchange.getRequest().getCharset());
    Element header = getAuthorisationHeader(doc);
    if (header == null)
        return Outcome.CONTINUE;
    // System.out.println(DOM2String(doc));
    Element nor = getNorElement(doc);
    nor.appendChild(getUsername(doc, header));
    nor.appendChild(getPassword(doc, header));
    header.getParentNode().removeChild(header);
    exchange.getRequest().setBody(new Body(DOM2String(doc).getBytes(exchange.getRequest().getCharset())));
    return Outcome.CONTINUE;
}
Also used : Element(org.w3c.dom.Element) MCElement(com.predic8.membrane.annot.MCElement) Document(org.w3c.dom.Document) Body(com.predic8.membrane.core.http.Body)

Example 8 with Exchange

use of com.predic8.membrane.core.exchange.Exchange in project service-proxy by membrane.

the class DispatchingInterceptor method getForwardingDestination.

public static String getForwardingDestination(Exchange exc) throws Exception {
    AbstractServiceProxy p = (AbstractServiceProxy) exc.getRule();
    if (p.getTargetURL() != null) {
        log.debug("destination: " + p.getTargetURL());
        return p.getTargetURL();
    }
    if (p.getTargetHost() != null) {
        String url = new URL(p.getTargetScheme(), p.getTargetHost(), p.getTargetPort(), exc.getRequest().getUri()).toString();
        log.debug("destination: " + url);
        return url;
    }
    return exc.getRequest().getUri();
}
Also used : AbstractServiceProxy(com.predic8.membrane.core.rules.AbstractServiceProxy) URL(java.net.URL)

Example 9 with Exchange

use of com.predic8.membrane.core.exchange.Exchange in project service-proxy by membrane.

the class UserFeatureInterceptor method handleRequest.

@Override
public Outcome handleRequest(Exchange exc) throws Exception {
    Rule predecessorRule = exc.getRule();
    Outcome outcome = flowController.invokeRequestHandlers(exc, predecessorRule.getInterceptors());
    while (isTargetInternalAndContinue(exc, outcome)) {
        log.debug("routing to serviceProxy with name: " + getServiceProxyName(exc));
        // rule matching
        String destination = exc.getDestinations().get(0);
        Rule newRule = getRuleByDest(destination);
        if (newRule == null)
            throw new Exception("No proxy found for destination " + destination);
        exc.setRule(newRule);
        // dispatching
        exc.getDestinations().clear();
        exc.getDestinations().add(DispatchingInterceptor.getForwardingDestination(exc));
        // user feature
        outcome = flowController.invokeRequestHandlers(exc, newRule.getInterceptors());
    }
    exc.setRule(predecessorRule);
    return outcome;
}
Also used : Rule(com.predic8.membrane.core.rules.Rule)

Example 10 with Exchange

use of com.predic8.membrane.core.exchange.Exchange in project service-proxy by membrane.

the class WADLInterceptor method rewrite.

@Override
protected void rewrite(Exchange exc) throws Exception, IOException {
    log.debug("Changing endpoint address in WADL");
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    Relocator relocator = new Relocator(new OutputStreamWriter(stream, exc.getResponse().getCharset()), getLocationProtocol(), getLocationHost(exc), getLocationPort(exc), pathRewriter);
    relocator.getRelocatingAttributes().put(new QName(WADL_NS, "resources"), "base");
    relocator.getRelocatingAttributes().put(new QName(WADL_NS, "include"), "href");
    relocator.relocate(new InputStreamReader(exc.getResponse().getBodyAsStreamDecoded(), exc.getResponse().getCharset()));
    exc.getResponse().setBodyContent(stream.toByteArray());
}
Also used : InputStreamReader(java.io.InputStreamReader) QName(javax.xml.namespace.QName) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Relocator(com.predic8.membrane.core.ws.relocator.Relocator)

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