Search in sources :

Example 1 with AbstractExchange

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

the class AbstractExchangeStore method notifyListenersOnExchangeRemoval.

public void notifyListenersOnExchangeRemoval(AbstractExchange exchange) {
    for (IExchangesStoreListener listener : exchangesStoreListeners) {
        exchange.removeExchangeStoreListener(listener);
        listener.removeExchange(exchange);
    }
}
Also used : IExchangesStoreListener(com.predic8.membrane.core.model.IExchangesStoreListener)

Example 2 with AbstractExchange

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

the class AbstractExchangeStore method notifyListenersOnExchangeAdd.

public void notifyListenersOnExchangeAdd(Rule rule, AbstractExchange exchange) {
    for (IExchangesStoreListener listener : exchangesStoreListeners) {
        exchange.addExchangeStoreListener(listener);
        listener.addExchange(rule, exchange);
    }
}
Also used : IExchangesStoreListener(com.predic8.membrane.core.model.IExchangesStoreListener)

Example 3 with AbstractExchange

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

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

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

Aggregations

AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)17 IOException (java.io.IOException)7 Exchange (com.predic8.membrane.core.exchange.Exchange)5 AbstractExchangeSnapshot (com.predic8.membrane.core.exchange.snapshots.AbstractExchangeSnapshot)4 DynamicAbstractExchangeSnapshot (com.predic8.membrane.core.exchange.snapshots.DynamicAbstractExchangeSnapshot)4 Message (com.predic8.membrane.core.http.Message)4 IExchangesStoreListener (com.predic8.membrane.core.model.IExchangesStoreListener)4 StatisticCollector (com.predic8.membrane.core.rules.StatisticCollector)4 UnknownHostException (java.net.UnknownHostException)4 CacheBuilder (com.google.common.cache.CacheBuilder)3 AbstractExchangeViewerListener (com.predic8.membrane.core.model.AbstractExchangeViewerListener)3 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 MCElement (com.predic8.membrane.annot.MCElement)2 AbstractBody (com.predic8.membrane.core.http.AbstractBody)2 Header (com.predic8.membrane.core.http.Header)2 JSONContent (com.predic8.membrane.core.interceptor.rest.JSONContent)2 SQLException (java.sql.SQLException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Cache (com.google.common.cache.Cache)1