Search in sources :

Example 1 with HttpEntity

use of org.apache.http.HttpEntity in project elasticsearch by elastic.

the class Request method bulk.

static Request bulk(BulkRequest bulkRequest) throws IOException {
    Params parameters = Params.builder();
    parameters.withTimeout(bulkRequest.timeout());
    parameters.withRefreshPolicy(bulkRequest.getRefreshPolicy());
    // Bulk API only supports newline delimited JSON or Smile. Before executing
    // the bulk, we need to check that all requests have the same content-type
    // and this content-type is supported by the Bulk API.
    XContentType bulkContentType = null;
    for (int i = 0; i < bulkRequest.numberOfActions(); i++) {
        DocWriteRequest<?> request = bulkRequest.requests().get(i);
        DocWriteRequest.OpType opType = request.opType();
        if (opType == DocWriteRequest.OpType.INDEX || opType == DocWriteRequest.OpType.CREATE) {
            bulkContentType = enforceSameContentType((IndexRequest) request, bulkContentType);
        } else if (opType == DocWriteRequest.OpType.UPDATE) {
            UpdateRequest updateRequest = (UpdateRequest) request;
            if (updateRequest.doc() != null) {
                bulkContentType = enforceSameContentType(updateRequest.doc(), bulkContentType);
            }
            if (updateRequest.upsertRequest() != null) {
                bulkContentType = enforceSameContentType(updateRequest.upsertRequest(), bulkContentType);
            }
        }
    }
    if (bulkContentType == null) {
        bulkContentType = XContentType.JSON;
    }
    byte separator = bulkContentType.xContent().streamSeparator();
    ContentType requestContentType = ContentType.create(bulkContentType.mediaType());
    ByteArrayOutputStream content = new ByteArrayOutputStream();
    for (DocWriteRequest<?> request : bulkRequest.requests()) {
        DocWriteRequest.OpType opType = request.opType();
        try (XContentBuilder metadata = XContentBuilder.builder(bulkContentType.xContent())) {
            metadata.startObject();
            {
                metadata.startObject(opType.getLowercase());
                if (Strings.hasLength(request.index())) {
                    metadata.field("_index", request.index());
                }
                if (Strings.hasLength(request.type())) {
                    metadata.field("_type", request.type());
                }
                if (Strings.hasLength(request.id())) {
                    metadata.field("_id", request.id());
                }
                if (Strings.hasLength(request.routing())) {
                    metadata.field("_routing", request.routing());
                }
                if (Strings.hasLength(request.parent())) {
                    metadata.field("_parent", request.parent());
                }
                if (request.version() != Versions.MATCH_ANY) {
                    metadata.field("_version", request.version());
                }
                VersionType versionType = request.versionType();
                if (versionType != VersionType.INTERNAL) {
                    if (versionType == VersionType.EXTERNAL) {
                        metadata.field("_version_type", "external");
                    } else if (versionType == VersionType.EXTERNAL_GTE) {
                        metadata.field("_version_type", "external_gte");
                    } else if (versionType == VersionType.FORCE) {
                        metadata.field("_version_type", "force");
                    }
                }
                if (opType == DocWriteRequest.OpType.INDEX || opType == DocWriteRequest.OpType.CREATE) {
                    IndexRequest indexRequest = (IndexRequest) request;
                    if (Strings.hasLength(indexRequest.getPipeline())) {
                        metadata.field("pipeline", indexRequest.getPipeline());
                    }
                } else if (opType == DocWriteRequest.OpType.UPDATE) {
                    UpdateRequest updateRequest = (UpdateRequest) request;
                    if (updateRequest.retryOnConflict() > 0) {
                        metadata.field("_retry_on_conflict", updateRequest.retryOnConflict());
                    }
                    if (updateRequest.fetchSource() != null) {
                        metadata.field("_source", updateRequest.fetchSource());
                    }
                }
                metadata.endObject();
            }
            metadata.endObject();
            BytesRef metadataSource = metadata.bytes().toBytesRef();
            content.write(metadataSource.bytes, metadataSource.offset, metadataSource.length);
            content.write(separator);
        }
        BytesRef source = null;
        if (opType == DocWriteRequest.OpType.INDEX || opType == DocWriteRequest.OpType.CREATE) {
            IndexRequest indexRequest = (IndexRequest) request;
            BytesReference indexSource = indexRequest.source();
            XContentType indexXContentType = indexRequest.getContentType();
            try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, indexSource, indexXContentType)) {
                try (XContentBuilder builder = XContentBuilder.builder(bulkContentType.xContent())) {
                    builder.copyCurrentStructure(parser);
                    source = builder.bytes().toBytesRef();
                }
            }
        } else if (opType == DocWriteRequest.OpType.UPDATE) {
            source = XContentHelper.toXContent((UpdateRequest) request, bulkContentType, false).toBytesRef();
        }
        if (source != null) {
            content.write(source.bytes, source.offset, source.length);
            content.write(separator);
        }
    }
    HttpEntity entity = new ByteArrayEntity(content.toByteArray(), 0, content.size(), requestContentType);
    return new Request(HttpPost.METHOD_NAME, "/_bulk", parameters.getParams(), entity);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) ContentType(org.apache.http.entity.ContentType) HttpEntity(org.apache.http.HttpEntity) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) WriteRequest(org.elasticsearch.action.support.WriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) GetRequest(org.elasticsearch.action.get.GetRequest) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IndexRequest(org.elasticsearch.action.index.IndexRequest) VersionType(org.elasticsearch.index.VersionType) XContentType(org.elasticsearch.common.xcontent.XContentType) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) BytesRef(org.apache.lucene.util.BytesRef) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 2 with HttpEntity

use of org.apache.http.HttpEntity in project elasticsearch by elastic.

the class Request method index.

static Request index(IndexRequest indexRequest) {
    String method = Strings.hasLength(indexRequest.id()) ? HttpPut.METHOD_NAME : HttpPost.METHOD_NAME;
    boolean isCreate = (indexRequest.opType() == DocWriteRequest.OpType.CREATE);
    String endpoint = endpoint(indexRequest.index(), indexRequest.type(), indexRequest.id(), isCreate ? "_create" : null);
    Params parameters = Params.builder();
    parameters.withRouting(indexRequest.routing());
    parameters.withParent(indexRequest.parent());
    parameters.withTimeout(indexRequest.timeout());
    parameters.withVersion(indexRequest.version());
    parameters.withVersionType(indexRequest.versionType());
    parameters.withPipeline(indexRequest.getPipeline());
    parameters.withRefreshPolicy(indexRequest.getRefreshPolicy());
    parameters.withWaitForActiveShards(indexRequest.waitForActiveShards());
    BytesRef source = indexRequest.source().toBytesRef();
    ContentType contentType = ContentType.create(indexRequest.getContentType().mediaType());
    HttpEntity entity = new ByteArrayEntity(source.bytes, source.offset, source.length, contentType);
    return new Request(method, endpoint, parameters.getParams(), entity);
}
Also used : XContentType(org.elasticsearch.common.xcontent.XContentType) ContentType(org.apache.http.entity.ContentType) HttpEntity(org.apache.http.HttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) WriteRequest(org.elasticsearch.action.support.WriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) GetRequest(org.elasticsearch.action.get.GetRequest) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) BytesRef(org.apache.lucene.util.BytesRef)

Example 3 with HttpEntity

use of org.apache.http.HttpEntity in project elasticsearch by elastic.

the class Request method update.

static Request update(UpdateRequest updateRequest) throws IOException {
    String endpoint = endpoint(updateRequest.index(), updateRequest.type(), updateRequest.id(), "_update");
    Params parameters = Params.builder();
    parameters.withRouting(updateRequest.routing());
    parameters.withParent(updateRequest.parent());
    parameters.withTimeout(updateRequest.timeout());
    parameters.withRefreshPolicy(updateRequest.getRefreshPolicy());
    parameters.withWaitForActiveShards(updateRequest.waitForActiveShards());
    parameters.withDocAsUpsert(updateRequest.docAsUpsert());
    parameters.withFetchSourceContext(updateRequest.fetchSource());
    parameters.withRetryOnConflict(updateRequest.retryOnConflict());
    parameters.withVersion(updateRequest.version());
    parameters.withVersionType(updateRequest.versionType());
    // The Java API allows update requests with different content types
    // set for the partial document and the upsert document. This client
    // only accepts update requests that have the same content types set
    // for both doc and upsert.
    XContentType xContentType = null;
    if (updateRequest.doc() != null) {
        xContentType = updateRequest.doc().getContentType();
    }
    if (updateRequest.upsertRequest() != null) {
        XContentType upsertContentType = updateRequest.upsertRequest().getContentType();
        if ((xContentType != null) && (xContentType != upsertContentType)) {
            throw new IllegalStateException("Update request cannot have different content types for doc [" + xContentType + "]" + " and upsert [" + upsertContentType + "] documents");
        } else {
            xContentType = upsertContentType;
        }
    }
    if (xContentType == null) {
        xContentType = Requests.INDEX_CONTENT_TYPE;
    }
    BytesRef source = XContentHelper.toXContent(updateRequest, xContentType, false).toBytesRef();
    HttpEntity entity = new ByteArrayEntity(source.bytes, source.offset, source.length, ContentType.create(xContentType.mediaType()));
    return new Request(HttpPost.METHOD_NAME, endpoint, parameters.getParams(), entity);
}
Also used : XContentType(org.elasticsearch.common.xcontent.XContentType) HttpEntity(org.apache.http.HttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) WriteRequest(org.elasticsearch.action.support.WriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) GetRequest(org.elasticsearch.action.get.GetRequest) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) BytesRef(org.apache.lucene.util.BytesRef)

Example 4 with HttpEntity

use of org.apache.http.HttpEntity in project moco by dreamhead.

the class MocoJsonStandaloneTest method should_return_expected_json_response_based_on_specified_json_request_shortcut.

@Test
public void should_return_expected_json_response_based_on_specified_json_request_shortcut() throws IOException {
    runWithConfiguration("json.json");
    HttpResponse response = helper.getResponse(remoteUrl("/json_response_shortcut"));
    HttpEntity entity = response.getEntity();
    byte[] bytes = ByteStreams.toByteArray(entity.getContent());
    assertThat(new String(bytes), is("{\"foo\":\"bar\"}"));
    MediaType mediaType = MediaType.parse(entity.getContentType().getValue());
    assertThat(mediaType.type(), is("application"));
    assertThat(mediaType.subtype(), is("json"));
}
Also used : HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) MediaType(com.google.common.net.MediaType) Test(org.junit.Test)

Example 5 with HttpEntity

use of org.apache.http.HttpEntity in project elasticsearch by elastic.

the class RemoteScrollableHitSourceTests method testWrapExceptionToPreserveStatus.

public void testWrapExceptionToPreserveStatus() throws IOException {
    Exception cause = new Exception();
    // Successfully get the status without a body
    RestStatus status = randomFrom(RestStatus.values());
    ElasticsearchStatusException wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(status.getStatus(), null, cause);
    assertEquals(status, wrapped.status());
    assertEquals(cause, wrapped.getCause());
    assertEquals("No error body.", wrapped.getMessage());
    // Successfully get the status without a body
    HttpEntity okEntity = new StringEntity("test body", ContentType.TEXT_PLAIN);
    wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(status.getStatus(), okEntity, cause);
    assertEquals(status, wrapped.status());
    assertEquals(cause, wrapped.getCause());
    assertEquals("body=test body", wrapped.getMessage());
    // Successfully get the status with a broken body
    IOException badEntityException = new IOException();
    HttpEntity badEntity = mock(HttpEntity.class);
    when(badEntity.getContent()).thenThrow(badEntityException);
    wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(status.getStatus(), badEntity, cause);
    assertEquals(status, wrapped.status());
    assertEquals(cause, wrapped.getCause());
    assertEquals("Failed to extract body.", wrapped.getMessage());
    assertEquals(badEntityException, wrapped.getSuppressed()[0]);
    // Fail to get the status without a body
    int notAnHttpStatus = -1;
    assertNull(RestStatus.fromCode(notAnHttpStatus));
    wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(notAnHttpStatus, null, cause);
    assertEquals(RestStatus.INTERNAL_SERVER_ERROR, wrapped.status());
    assertEquals(cause, wrapped.getCause());
    assertEquals("Couldn't extract status [" + notAnHttpStatus + "]. No error body.", wrapped.getMessage());
    // Fail to get the status without a body
    wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(notAnHttpStatus, okEntity, cause);
    assertEquals(RestStatus.INTERNAL_SERVER_ERROR, wrapped.status());
    assertEquals(cause, wrapped.getCause());
    assertEquals("Couldn't extract status [" + notAnHttpStatus + "]. body=test body", wrapped.getMessage());
    // Fail to get the status with a broken body
    wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(notAnHttpStatus, badEntity, cause);
    assertEquals(RestStatus.INTERNAL_SERVER_ERROR, wrapped.status());
    assertEquals(cause, wrapped.getCause());
    assertEquals("Couldn't extract status [" + notAnHttpStatus + "]. Failed to extract body.", wrapped.getMessage());
    assertEquals(badEntityException, wrapped.getSuppressed()[0]);
}
Also used : StringEntity(org.apache.http.entity.StringEntity) RestStatus(org.elasticsearch.rest.RestStatus) HttpEntity(org.apache.http.HttpEntity) IOException(java.io.IOException) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) ContentTooLongException(org.apache.http.ContentTooLongException) ParsingException(org.elasticsearch.common.ParsingException) IOException(java.io.IOException) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException)

Aggregations

HttpEntity (org.apache.http.HttpEntity)1382 HttpResponse (org.apache.http.HttpResponse)508 IOException (java.io.IOException)478 HttpGet (org.apache.http.client.methods.HttpGet)391 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)310 HttpPost (org.apache.http.client.methods.HttpPost)300 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)284 Test (org.junit.Test)259 ArrayList (java.util.ArrayList)248 HashMap (java.util.HashMap)177 MultipartEntityBuilder (org.apache.http.entity.mime.MultipartEntityBuilder)164 InputStream (java.io.InputStream)162 StatusLine (org.apache.http.StatusLine)149 URI (java.net.URI)148 StringEntity (org.apache.http.entity.StringEntity)144 Header (org.apache.http.Header)134 HttpClient (org.apache.http.client.HttpClient)127 VolleyError (com.android.volley.VolleyError)120 ApiException (io.swagger.client.ApiException)120 Pair (io.swagger.client.Pair)120