Search in sources :

Example 6 with ActionRequest

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);
    }
}
Also used : GetResponse(org.elasticsearch.action.get.GetResponse) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) MainResponse(org.elasticsearch.action.main.MainResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) MainRequest(org.elasticsearch.action.main.MainRequest) ActionRequest(org.elasticsearch.action.ActionRequest) GetRequest(org.elasticsearch.action.get.GetRequest) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) IOException(java.io.IOException) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException)

Example 7 with ActionRequest

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);
}
Also used : ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) MainRequest(org.elasticsearch.action.main.MainRequest) ActionRequest(org.elasticsearch.action.ActionRequest) GetRequest(org.elasticsearch.action.get.GetRequest) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) ElasticsearchException(org.elasticsearch.ElasticsearchException) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) IOException(java.io.IOException) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException)

Example 8 with ActionRequest

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);
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) ActionRequest(org.elasticsearch.action.ActionRequest) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.elasticsearch.common.io.stream.StreamInput) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) Streamable(org.elasticsearch.common.io.stream.Streamable) ElasticsearchException(org.elasticsearch.ElasticsearchException) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) IOException(java.io.IOException) ShardOperationFailedException(org.elasticsearch.action.ShardOperationFailedException)

Example 9 with ActionRequest

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;
    }
}
Also used : ActionRequest(org.elasticsearch.action.ActionRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) InvalidPayloadRuntimeException(org.apache.camel.InvalidPayloadRuntimeException)

Aggregations

ActionRequest (org.elasticsearch.action.ActionRequest)9 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)5 ActionRequestValidationException (org.elasticsearch.action.ActionRequestValidationException)4 IOException (java.io.IOException)3 ElasticsearchException (org.elasticsearch.ElasticsearchException)3 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)3 IndexRequest (org.elasticsearch.action.index.IndexRequest)3 InvalidPayloadRuntimeException (org.apache.camel.InvalidPayloadRuntimeException)2 ElasticsearchStatusException (org.elasticsearch.ElasticsearchStatusException)2 ActionListener (org.elasticsearch.action.ActionListener)2 ActionResponse (org.elasticsearch.action.ActionResponse)2 GetRequest (org.elasticsearch.action.get.GetRequest)2 MainRequest (org.elasticsearch.action.main.MainRequest)2 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashSet (java.util.HashSet)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1