use of com.predic8.membrane.core.exchange.snapshots.AbstractExchangeSnapshot 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];
}
}
use of com.predic8.membrane.core.exchange.snapshots.AbstractExchangeSnapshot in project service-proxy by membrane.
the class DynamicAbstractExchangeSnapshot method addObservers.
public static void addObservers(AbstractExchange exc, AbstractExchangeSnapshot excCopy, Consumer<AbstractExchangeSnapshot> callback) {
MessageObserver obs = new MessageObserver() {
@Override
public void bodyRequested(AbstractBody body) {
}
@Override
public void bodyComplete(AbstractBody body) {
update(callback, excCopy, exc);
}
};
exc.addExchangeViewerListener(new AbstractExchangeViewerListener() {
@Override
public void addResponse(Response response) {
response.addObserver(obs);
}
@Override
public void setExchangeFinished() {
update(callback, excCopy, exc);
}
});
Stream.of(exc.getRequest(), exc.getResponse()).forEach(msg -> {
if (msg == null)
return;
if (msg.containsObserver(obs))
return;
msg.addObserver(obs);
});
update(callback, excCopy, exc);
}
use of com.predic8.membrane.core.exchange.snapshots.AbstractExchangeSnapshot 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);
}
}
use of com.predic8.membrane.core.exchange.snapshots.AbstractExchangeSnapshot in project service-proxy by membrane.
the class ElasticSearchExchangeStore method snap.
@Override
public void snap(AbstractExchange exc, Interceptor.Flow flow) {
AbstractExchangeSnapshot excCopy = null;
try {
if (flow == Interceptor.Flow.REQUEST) {
excCopy = new DynamicAbstractExchangeSnapshot(exc, this::addForElasticSearch);
addForElasticSearch(excCopy);
} else {
excCopy = getExchangeDtoById((int) exc.getId());
DynamicAbstractExchangeSnapshot.addObservers(exc, excCopy, this::addForElasticSearch);
excCopy = excCopy.updateFrom(exc);
addForElasticSearch(excCopy);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of com.predic8.membrane.core.exchange.snapshots.AbstractExchangeSnapshot 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);
}
Aggregations