Search in sources :

Example 1 with Body

use of com.predic8.membrane.core.http.Body in project service-proxy by membrane.

the class ElasticSearchExchangeStore method getExchanges.

@Override
public AbstractExchange[] getExchanges(RuleKey ruleKey) {
    int port = ruleKey.getPort();
    try {
        Exchange exc = new Request.Builder().post(getElasticSearchExchangesPath() + "_search").body("{\n" + "  \"query\": {\n" + "    \"bool\": {\n" + "      \"must\": [\n" + "        {\n" + "          \"wildcard\": {\n" + "            \"issuer\": \"" + documentPrefix + "\"\n" + "          }\n" + "        },\n" + "        {\n" + "          \"match\": {\n" + "            \"rule.port\": \"" + port + "\"\n" + "          }\n" + "        }\n" + "      ]\n" + "    }\n" + "  }\n" + "}").header("Content-Type", "application/json").buildExchange();
        exc = client.call(exc);
        List source = getSourceElementFromElasticSearchResponse(responseToMap(exc));
        AbstractExchangeSnapshot[] snapshots = mapper.readValue(mapper.writeValueAsString(source), AbstractExchangeSnapshot[].class);
        return Stream.of(snapshots).map(snapshot -> snapshot.toAbstractExchange()).collect(Collectors.toList()).toArray(new AbstractExchange[0]);
    } catch (Exception e) {
        e.printStackTrace();
        return new AbstractExchange[0];
    }
}
Also used : AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange) Exchange(com.predic8.membrane.core.exchange.Exchange) CacheBuilder(com.google.common.cache.CacheBuilder) DynamicAbstractExchangeSnapshot(com.predic8.membrane.core.exchange.snapshots.DynamicAbstractExchangeSnapshot) AbstractExchangeSnapshot(com.predic8.membrane.core.exchange.snapshots.AbstractExchangeSnapshot) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 2 with Body

use of com.predic8.membrane.core.http.Body in project service-proxy by membrane.

the class ElasticSearchExchangeStore method getAllExchangesAsList.

@Override
public List<AbstractExchange> getAllExchangesAsList() {
    try {
        Exchange exc = new Request.Builder().post(getElasticSearchExchangesPath() + "_search").header("Content-Type", "application/json").body("{\n" + "  \"query\": {\n" + "    \"wildcard\": {\n" + "      \"issuer\": \"" + documentPrefix + "\"\n" + "    }\n" + "  }\n" + "}").buildExchange();
        exc = client.call(exc);
        if (!exc.getResponse().isOk())
            return new ArrayList<>();
        List sources = getSourceElementFromElasticSearchResponse(responseToMap(exc));
        return (List) sources.stream().map(source -> {
            try {
                return mapper.readValue(mapper.writeValueAsString(source), AbstractExchangeSnapshot.class).toAbstractExchange();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }).collect(Collectors.toList());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
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) DynamicAbstractExchangeSnapshot(com.predic8.membrane.core.exchange.snapshots.DynamicAbstractExchangeSnapshot) AbstractExchangeSnapshot(com.predic8.membrane.core.exchange.snapshots.AbstractExchangeSnapshot) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 3 with Body

use of com.predic8.membrane.core.http.Body 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 4 with Body

use of com.predic8.membrane.core.http.Body 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 5 with Body

use of com.predic8.membrane.core.http.Body in project service-proxy by membrane.

the class LimitedMemoryExchangeStore method oldSnap.

private void oldSnap(AbstractExchange exc, Flow flow) {
    // TODO: [fix me] support multi-snap
    // TODO: [fix me] snap message headers and request *here*, not in observer/response
    exc.addExchangeViewerListener(new AbstractExchangeViewerListener() {

        @Override
        public void setExchangeFinished() {
            inflight.remove(exc);
        }
    });
    if (flow == Flow.REQUEST) {
        exc.getRequest().addObserver(new MessageObserver() {

            @Override
            public void bodyRequested(AbstractBody body) {
            }

            @Override
            public void bodyComplete(AbstractBody body) {
                Response r = exc.getResponse();
                if (r != null) {
                    AbstractBody b = r.getBody();
                    if (b != null && b.isRead())
                        // request-bodyComplete might occur after response-bodyComplete
                        return;
                }
                // System.out.println("Exchange put inflight " + exc.hashCode() + " " + exc.getRequest().getStartLine());
                inflight.put(exc, exc.getRequest());
                modify();
            }
        });
        return;
    }
    try {
        Message m = exc.getResponse();
        if (m != null)
            m.addObserver(new MessageObserver() {

                public void bodyRequested(AbstractBody body) {
                }

                public void bodyComplete(AbstractBody body) {
                    snapInternal(exc, flow);
                    inflight.remove(exc);
                    modify();
                // System.out.println("Exchange remove inflight " + exc.hashCode());
                }
            });
        else {
            inflight.remove(exc);
            modify();
        // System.out.println("Exchange remove inflight " + exc.hashCode() + " (2)");
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : AbstractExchangeViewerListener(com.predic8.membrane.core.model.AbstractExchangeViewerListener)

Aggregations

Exchange (com.predic8.membrane.core.exchange.Exchange)30 IOException (java.io.IOException)17 Response (com.predic8.membrane.core.http.Response)15 Request (com.predic8.membrane.core.http.Request)12 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)10 Test (org.junit.Test)10 CacheBuilder (com.google.common.cache.CacheBuilder)8 MCElement (com.predic8.membrane.annot.MCElement)6 Body (com.predic8.membrane.core.http.Body)6 HttpClient (com.predic8.membrane.core.transport.http.HttpClient)6 UnknownHostException (java.net.UnknownHostException)6 Message (com.predic8.membrane.core.http.Message)5 ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)5 InputStream (java.io.InputStream)5 JsonFactory (com.fasterxml.jackson.core.JsonFactory)4 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)4 HttpRouter (com.predic8.membrane.core.HttpRouter)4 AbstractExchangeSnapshot (com.predic8.membrane.core.exchange.snapshots.AbstractExchangeSnapshot)4 DynamicAbstractExchangeSnapshot (com.predic8.membrane.core.exchange.snapshots.DynamicAbstractExchangeSnapshot)4 Header (com.predic8.membrane.core.http.Header)4