Search in sources :

Example 36 with Header

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

the class ElasticSearchExchangeStore method removeAllExchanges.

@Override
public void removeAllExchanges(AbstractExchange[] exchanges) {
    StringBuilder sb = Stream.of(exchanges).map(exc -> exc.getId()).collect(() -> {
        StringBuilder acc = new StringBuilder();
        acc.append("[");
        return acc;
    }, (acc, id) -> acc.append(id).append(","), (acc1, acc2) -> acc1.append(",").append(acc2));
    sb.deleteCharAt(sb.length() - 1);
    sb.append("]");
    String exchangeIdsAsJsonArray = sb.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" + "          \"terms\": {\n" + "            \"id\": \"" + exchangeIdsAsJsonArray + "\"\n" + "          }\n" + "        }\n" + "      ]\n" + "    }\n" + "  }\n" + "}").header("Content-Type", "application/json").buildExchange();
        client.call(exc);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : java.util(java.util) AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange) LoggerFactory(org.slf4j.LoggerFactory) DynamicAbstractExchangeSnapshot(com.predic8.membrane.core.exchange.snapshots.DynamicAbstractExchangeSnapshot) InetAddress(java.net.InetAddress) Exchange(com.predic8.membrane.core.exchange.Exchange) MCElement(com.predic8.membrane.annot.MCElement) Interceptor(com.predic8.membrane.core.interceptor.Interceptor) RuleKey(com.predic8.membrane.core.rules.RuleKey) MCAttribute(com.predic8.membrane.annot.MCAttribute) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) AbstractExchangeSnapshot(com.predic8.membrane.core.exchange.snapshots.AbstractExchangeSnapshot) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) Rule(com.predic8.membrane.core.rules.Rule) TimeUnit(java.util.concurrent.TimeUnit) IOUtils(org.apache.commons.io.IOUtils) Stream(java.util.stream.Stream) com.predic8.membrane.core.http(com.predic8.membrane.core.http) CacheBuilder(com.google.common.cache.CacheBuilder) HttpClient(com.predic8.membrane.core.transport.http.HttpClient) Cache(com.google.common.cache.Cache) StatisticCollector(com.predic8.membrane.core.rules.StatisticCollector) 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 37 with Header

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

the class ElasticSearchExchangeStore method getFromElasticSearchById.

private AbstractExchangeSnapshot getFromElasticSearchById(int id) {
    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" + "            \"id\": \"" + id + "\"\n" + "          }\n" + "        }\n" + "      ]\n" + "    }\n" + "  }\n" + "}").header("Content-Type", "application/json").buildExchange();
        exc = client.call(exc);
        Map res = responseToMap(exc);
        Map excJson = getSourceElementFromElasticSearchResponse(res).get(0);
        return mapper.readValue(mapper.writeValueAsString(excJson), AbstractExchangeSnapshot.class);
    } 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) UnknownHostException(java.net.UnknownHostException)

Example 38 with Header

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

the class ElasticSearchExchangeStore method sendToElasticSearch.

private void sendToElasticSearch(List<AbstractExchangeSnapshot> exchanges) throws Exception {
    StringBuilder data = exchanges.stream().map(exchange -> wrapForBulkOperationElasticSearch(index, type, getLocalMachineNameWithSuffix() + "-" + exchange.getId(), collectExchangeDataFrom(exchange))).collect(StringBuilder::new, (sb, str) -> sb.append(str), (sb1, sb2) -> sb1.append(sb2));
    Exchange elasticSearchExc = new Request.Builder().post(location + "/_bulk").header("Content-Type", "application/x-ndjson").body(data.toString()).buildExchange();
    client.call(elasticSearchExc);
}
Also used : java.util(java.util) AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange) LoggerFactory(org.slf4j.LoggerFactory) DynamicAbstractExchangeSnapshot(com.predic8.membrane.core.exchange.snapshots.DynamicAbstractExchangeSnapshot) InetAddress(java.net.InetAddress) Exchange(com.predic8.membrane.core.exchange.Exchange) MCElement(com.predic8.membrane.annot.MCElement) Interceptor(com.predic8.membrane.core.interceptor.Interceptor) RuleKey(com.predic8.membrane.core.rules.RuleKey) MCAttribute(com.predic8.membrane.annot.MCAttribute) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) AbstractExchangeSnapshot(com.predic8.membrane.core.exchange.snapshots.AbstractExchangeSnapshot) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) Rule(com.predic8.membrane.core.rules.Rule) TimeUnit(java.util.concurrent.TimeUnit) IOUtils(org.apache.commons.io.IOUtils) Stream(java.util.stream.Stream) com.predic8.membrane.core.http(com.predic8.membrane.core.http) CacheBuilder(com.google.common.cache.CacheBuilder) HttpClient(com.predic8.membrane.core.transport.http.HttpClient) Cache(com.google.common.cache.Cache) StatisticCollector(com.predic8.membrane.core.rules.StatisticCollector) AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange) Exchange(com.predic8.membrane.core.exchange.Exchange) CacheBuilder(com.google.common.cache.CacheBuilder)

Example 39 with Header

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

the class HeaderFilterInterceptor method handleMessage.

private void handleMessage(Message msg) {
    if (msg == null)
        return;
    Header h = msg.getHeader();
    if (h == null)
        return;
    for (HeaderField hf : h.getAllHeaderFields()) for (Rule r : rules) if (r.matches(hf.getHeaderName().toString())) {
        switch(r.getAction()) {
            case REMOVE:
                log.debug("Removing HTTP header " + hf.getHeaderName().toString());
                h.remove(hf);
                break;
            case KEEP:
                break;
        }
        break;
    }
}
Also used : Header(com.predic8.membrane.core.http.Header) HeaderField(com.predic8.membrane.core.http.HeaderField)

Example 40 with Header

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

the class AuthHead2BodyInterceptor method getUsername.

private Node getUsername(Document doc, Element header) {
    Element e = doc.createElement("username");
    e.appendChild(doc.createTextNode(((Element) doc.getElementsByTagNameNS(COM_NS, "userName").item(0)).getTextContent()));
    e.setAttributeNS(XSI_NS, "xsi:type", "xsd:string");
    return e;
}
Also used : Element(org.w3c.dom.Element) MCElement(com.predic8.membrane.annot.MCElement)

Aggregations

Exchange (com.predic8.membrane.core.exchange.Exchange)26 Header (com.predic8.membrane.core.http.Header)16 Request (com.predic8.membrane.core.http.Request)13 IOException (java.io.IOException)13 Response (com.predic8.membrane.core.http.Response)12 CacheBuilder (com.google.common.cache.CacheBuilder)8 Test (org.junit.Test)8 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)7 HttpClient (com.predic8.membrane.core.transport.http.HttpClient)6 UnknownHostException (java.net.UnknownHostException)6 MCElement (com.predic8.membrane.annot.MCElement)5 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)4 AbstractExchangeSnapshot (com.predic8.membrane.core.exchange.snapshots.AbstractExchangeSnapshot)4 DynamicAbstractExchangeSnapshot (com.predic8.membrane.core.exchange.snapshots.DynamicAbstractExchangeSnapshot)4 HeaderField (com.predic8.membrane.core.http.HeaderField)4 JsonFactory (com.fasterxml.jackson.core.JsonFactory)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Cache (com.google.common.cache.Cache)2