use of org.elasticsearch.action.ActionRequest in project elasticsearch by elastic.
the class RestHighLevelClient method performRequest.
<Req extends ActionRequest, Resp> Resp performRequest(Req request, CheckedFunction<Req, Request, IOException> requestConverter, CheckedFunction<Response, Resp, IOException> responseConverter, Set<Integer> ignores, Header... headers) throws IOException {
ActionRequestValidationException validationException = request.validate();
if (validationException != null) {
throw validationException;
}
Request req = requestConverter.apply(request);
Response response;
try {
response = client.performRequest(req.method, req.endpoint, req.params, req.entity, headers);
} catch (ResponseException e) {
if (ignores.contains(e.getResponse().getStatusLine().getStatusCode())) {
try {
return responseConverter.apply(e.getResponse());
} catch (Exception innerException) {
throw parseResponseException(e);
}
}
throw parseResponseException(e);
}
try {
return responseConverter.apply(response);
} catch (Exception e) {
throw new IOException("Unable to parse response body for " + response, e);
}
}
use of org.elasticsearch.action.ActionRequest in project elasticsearch by elastic.
the class RestHighLevelClient method performRequestAsync.
<Req extends ActionRequest, Resp> void performRequestAsync(Req request, CheckedFunction<Req, Request, IOException> requestConverter, CheckedFunction<Response, Resp, IOException> responseConverter, ActionListener<Resp> listener, Set<Integer> ignores, Header... headers) {
ActionRequestValidationException validationException = request.validate();
if (validationException != null) {
listener.onFailure(validationException);
return;
}
Request req;
try {
req = requestConverter.apply(request);
} catch (Exception e) {
listener.onFailure(e);
return;
}
ResponseListener responseListener = wrapResponseListener(responseConverter, listener, ignores);
client.performRequestAsync(req.method, req.endpoint, req.params, req.entity, responseListener, headers);
}
use of org.elasticsearch.action.ActionRequest in project elasticsearch by elastic.
the class ElasticsearchAssertions method assertVersionSerializable.
public static void assertVersionSerializable(Version version, Streamable streamable, NamedWriteableRegistry namedWriteableRegistry) {
try {
Streamable newInstance = tryCreateNewInstance(streamable);
if (newInstance == null) {
// can't create a new instance - we never modify a
return;
// streamable that comes in.
}
if (streamable instanceof ActionRequest) {
((ActionRequest) streamable).validate();
}
BytesReference orig;
try {
orig = serialize(version, streamable);
} catch (IllegalArgumentException e) {
// Can't serialize with this version so skip this test.
return;
}
StreamInput input = orig.streamInput();
if (namedWriteableRegistry != null) {
input = new NamedWriteableAwareStreamInput(input, namedWriteableRegistry);
}
input.setVersion(version);
newInstance.readFrom(input);
assertThat("Stream should be fully read with version [" + version + "] for streamable [" + streamable + "]", input.available(), equalTo(0));
BytesReference newBytes = serialize(version, streamable);
if (false == orig.equals(newBytes)) {
// The bytes are different. That is a failure. Lets try to throw a useful exception for debugging.
String message = "Serialization failed with version [" + version + "] bytes should be equal for streamable [" + streamable + "]";
// If the bytes are different then comparing BytesRef's toStrings will show you *where* they are different
assertEquals(message, orig.toBytesRef().toString(), newBytes.toBytesRef().toString());
// They bytes aren't different. Very very weird.
fail(message);
}
} catch (Exception ex) {
throw new RuntimeException("failed to check serialization - version [" + version + "] for streamable [" + streamable + "]", ex);
}
}
use of org.elasticsearch.action.ActionRequest in project camel by apache.
the class BulkRequestAggregationStrategy method aggregate.
@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
// Don't use getBody(Class<T>) here as we don't want to coerce the body type using a type converter.
Object objBody = newExchange.getIn().getBody();
if (!(objBody instanceof ActionRequest)) {
throw new InvalidPayloadRuntimeException(newExchange, ActionRequest.class);
}
ActionRequest newBody = (ActionRequest) objBody;
BulkRequest request;
if (oldExchange == null) {
request = new BulkRequest();
request.add(newBody);
newExchange.getIn().setBody(request);
return newExchange;
} else {
request = oldExchange.getIn().getBody(BulkRequest.class);
request.add(newBody);
return oldExchange;
}
}
Aggregations