Search in sources :

Example 11 with HttpEntity

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

the class RestClientSingleHostTests method testInternalHttpRequest.

/**
     * Verifies the content of the {@link HttpRequest} that's internally created and passed through to the http client
     */
@SuppressWarnings("unchecked")
public void testInternalHttpRequest() throws Exception {
    ArgumentCaptor<HttpAsyncRequestProducer> requestArgumentCaptor = ArgumentCaptor.forClass(HttpAsyncRequestProducer.class);
    int times = 0;
    for (String httpMethod : getHttpMethods()) {
        HttpUriRequest expectedRequest = performRandomRequest(httpMethod);
        verify(httpClient, times(++times)).<HttpResponse>execute(requestArgumentCaptor.capture(), any(HttpAsyncResponseConsumer.class), any(HttpClientContext.class), any(FutureCallback.class));
        HttpUriRequest actualRequest = (HttpUriRequest) requestArgumentCaptor.getValue().generateRequest();
        assertEquals(expectedRequest.getURI(), actualRequest.getURI());
        assertEquals(expectedRequest.getClass(), actualRequest.getClass());
        assertArrayEquals(expectedRequest.getAllHeaders(), actualRequest.getAllHeaders());
        if (expectedRequest instanceof HttpEntityEnclosingRequest) {
            HttpEntity expectedEntity = ((HttpEntityEnclosingRequest) expectedRequest).getEntity();
            if (expectedEntity != null) {
                HttpEntity actualEntity = ((HttpEntityEnclosingRequest) actualRequest).getEntity();
                assertEquals(EntityUtils.toString(expectedEntity), EntityUtils.toString(actualEntity));
            }
        }
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpEntity(org.apache.http.HttpEntity) HttpAsyncRequestProducer(org.apache.http.nio.protocol.HttpAsyncRequestProducer) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) HttpAsyncResponseConsumer(org.apache.http.nio.protocol.HttpAsyncResponseConsumer) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) FutureCallback(org.apache.http.concurrent.FutureCallback)

Example 12 with HttpEntity

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

the class RestHighLevelClient method parseResponseException.

/**
     * Converts a {@link ResponseException} obtained from the low level REST client into an {@link ElasticsearchException}.
     * If a response body was returned, tries to parse it as an error returned from Elasticsearch.
     * If no response body was returned or anything goes wrong while parsing the error, returns a new {@link ElasticsearchStatusException}
     * that wraps the original {@link ResponseException}. The potential exception obtained while parsing is added to the returned
     * exception as a suppressed exception. This method is guaranteed to not throw any exception eventually thrown while parsing.
     */
ElasticsearchStatusException parseResponseException(ResponseException responseException) {
    Response response = responseException.getResponse();
    HttpEntity entity = response.getEntity();
    ElasticsearchStatusException elasticsearchException;
    if (entity == null) {
        elasticsearchException = new ElasticsearchStatusException(responseException.getMessage(), RestStatus.fromCode(response.getStatusLine().getStatusCode()), responseException);
    } else {
        try {
            elasticsearchException = parseEntity(entity, BytesRestResponse::errorFromXContent);
            elasticsearchException.addSuppressed(responseException);
        } catch (Exception e) {
            RestStatus restStatus = RestStatus.fromCode(response.getStatusLine().getStatusCode());
            elasticsearchException = new ElasticsearchStatusException("Unable to parse response body", restStatus, responseException);
            elasticsearchException.addSuppressed(e);
        }
    }
    return elasticsearchException;
}
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) HttpEntity(org.apache.http.HttpEntity) RestStatus(org.elasticsearch.rest.RestStatus) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) IOException(java.io.IOException) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException)

Example 13 with HttpEntity

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

the class RequestTests method testUpdate.

public void testUpdate() throws IOException {
    XContentType xContentType = randomFrom(XContentType.values());
    Map<String, String> expectedParams = new HashMap<>();
    String index = randomAsciiOfLengthBetween(3, 10);
    String type = randomAsciiOfLengthBetween(3, 10);
    String id = randomAsciiOfLengthBetween(3, 10);
    UpdateRequest updateRequest = new UpdateRequest(index, type, id);
    updateRequest.detectNoop(randomBoolean());
    if (randomBoolean()) {
        BytesReference source = RandomObjects.randomSource(random(), xContentType);
        updateRequest.doc(new IndexRequest().source(source, xContentType));
        boolean docAsUpsert = randomBoolean();
        updateRequest.docAsUpsert(docAsUpsert);
        if (docAsUpsert) {
            expectedParams.put("doc_as_upsert", "true");
        }
    } else {
        updateRequest.script(new Script("_value + 1"));
        updateRequest.scriptedUpsert(randomBoolean());
    }
    if (randomBoolean()) {
        BytesReference source = RandomObjects.randomSource(random(), xContentType);
        updateRequest.upsert(new IndexRequest().source(source, xContentType));
    }
    if (randomBoolean()) {
        String routing = randomAsciiOfLengthBetween(3, 10);
        updateRequest.routing(routing);
        expectedParams.put("routing", routing);
    }
    if (randomBoolean()) {
        String parent = randomAsciiOfLengthBetween(3, 10);
        updateRequest.parent(parent);
        expectedParams.put("parent", parent);
    }
    if (randomBoolean()) {
        String timeout = randomTimeValue();
        updateRequest.timeout(timeout);
        expectedParams.put("timeout", timeout);
    } else {
        expectedParams.put("timeout", ReplicationRequest.DEFAULT_TIMEOUT.getStringRep());
    }
    if (randomBoolean()) {
        WriteRequest.RefreshPolicy refreshPolicy = randomFrom(WriteRequest.RefreshPolicy.values());
        updateRequest.setRefreshPolicy(refreshPolicy);
        if (refreshPolicy != WriteRequest.RefreshPolicy.NONE) {
            expectedParams.put("refresh", refreshPolicy.getValue());
        }
    }
    if (randomBoolean()) {
        int waitForActiveShards = randomIntBetween(0, 10);
        updateRequest.waitForActiveShards(waitForActiveShards);
        expectedParams.put("wait_for_active_shards", String.valueOf(waitForActiveShards));
    }
    if (randomBoolean()) {
        long version = randomLong();
        updateRequest.version(version);
        if (version != Versions.MATCH_ANY) {
            expectedParams.put("version", Long.toString(version));
        }
    }
    if (randomBoolean()) {
        VersionType versionType = randomFrom(VersionType.values());
        updateRequest.versionType(versionType);
        if (versionType != VersionType.INTERNAL) {
            expectedParams.put("version_type", versionType.name().toLowerCase(Locale.ROOT));
        }
    }
    if (randomBoolean()) {
        int retryOnConflict = randomIntBetween(0, 5);
        updateRequest.retryOnConflict(retryOnConflict);
        if (retryOnConflict > 0) {
            expectedParams.put("retry_on_conflict", String.valueOf(retryOnConflict));
        }
    }
    if (randomBoolean()) {
        randomizeFetchSourceContextParams(updateRequest::fetchSource, expectedParams);
    }
    Request request = Request.update(updateRequest);
    assertEquals("/" + index + "/" + type + "/" + id + "/_update", request.endpoint);
    assertEquals(expectedParams, request.params);
    assertEquals("POST", request.method);
    HttpEntity entity = request.entity;
    assertNotNull(entity);
    assertTrue(entity instanceof ByteArrayEntity);
    UpdateRequest parsedUpdateRequest = new UpdateRequest();
    XContentType entityContentType = XContentType.fromMediaTypeOrFormat(entity.getContentType().getValue());
    try (XContentParser parser = createParser(entityContentType.xContent(), entity.getContent())) {
        parsedUpdateRequest.fromXContent(parser);
    }
    assertEquals(updateRequest.scriptedUpsert(), parsedUpdateRequest.scriptedUpsert());
    assertEquals(updateRequest.docAsUpsert(), parsedUpdateRequest.docAsUpsert());
    assertEquals(updateRequest.detectNoop(), parsedUpdateRequest.detectNoop());
    assertEquals(updateRequest.fetchSource(), parsedUpdateRequest.fetchSource());
    assertEquals(updateRequest.script(), parsedUpdateRequest.script());
    if (updateRequest.doc() != null) {
        assertToXContentEquivalent(updateRequest.doc().source(), parsedUpdateRequest.doc().source(), xContentType);
    } else {
        assertNull(parsedUpdateRequest.doc());
    }
    if (updateRequest.upsertRequest() != null) {
        assertToXContentEquivalent(updateRequest.upsertRequest().source(), parsedUpdateRequest.upsertRequest().source(), xContentType);
    } else {
        assertNull(parsedUpdateRequest.upsertRequest());
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) Script(org.elasticsearch.script.Script) HttpEntity(org.apache.http.HttpEntity) HashMap(java.util.HashMap) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) WriteRequest(org.elasticsearch.action.support.WriteRequest) ReplicatedWriteRequest(org.elasticsearch.action.support.replication.ReplicatedWriteRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) WriteRequest(org.elasticsearch.action.support.WriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) BulkShardRequest(org.elasticsearch.action.bulk.BulkShardRequest) GetRequest(org.elasticsearch.action.get.GetRequest) ReplicatedWriteRequest(org.elasticsearch.action.support.replication.ReplicatedWriteRequest) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) ReplicationRequest(org.elasticsearch.action.support.replication.ReplicationRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) VersionType(org.elasticsearch.index.VersionType) XContentType(org.elasticsearch.common.xcontent.XContentType) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 14 with HttpEntity

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

the class RequestTests method testIndex.

public void testIndex() throws IOException {
    String index = randomAsciiOfLengthBetween(3, 10);
    String type = randomAsciiOfLengthBetween(3, 10);
    IndexRequest indexRequest = new IndexRequest(index, type);
    String id = randomBoolean() ? randomAsciiOfLengthBetween(3, 10) : null;
    indexRequest.id(id);
    Map<String, String> expectedParams = new HashMap<>();
    String method = "POST";
    if (id != null) {
        method = "PUT";
        if (randomBoolean()) {
            indexRequest.opType(DocWriteRequest.OpType.CREATE);
        }
    }
    setRandomTimeout(indexRequest, expectedParams);
    setRandomRefreshPolicy(indexRequest, expectedParams);
    // There is some logic around _create endpoint and version/version type
    if (indexRequest.opType() == DocWriteRequest.OpType.CREATE) {
        indexRequest.version(randomFrom(Versions.MATCH_ANY, Versions.MATCH_DELETED));
        expectedParams.put("version", Long.toString(Versions.MATCH_DELETED));
    } else {
        setRandomVersion(indexRequest, expectedParams);
        setRandomVersionType(indexRequest, expectedParams);
    }
    if (frequently()) {
        if (randomBoolean()) {
            String routing = randomAsciiOfLengthBetween(3, 10);
            indexRequest.routing(routing);
            expectedParams.put("routing", routing);
        }
        if (randomBoolean()) {
            String parent = randomAsciiOfLengthBetween(3, 10);
            indexRequest.parent(parent);
            expectedParams.put("parent", parent);
        }
        if (randomBoolean()) {
            String pipeline = randomAsciiOfLengthBetween(3, 10);
            indexRequest.setPipeline(pipeline);
            expectedParams.put("pipeline", pipeline);
        }
    }
    XContentType xContentType = randomFrom(XContentType.values());
    int nbFields = randomIntBetween(0, 10);
    try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) {
        builder.startObject();
        for (int i = 0; i < nbFields; i++) {
            builder.field("field_" + i, i);
        }
        builder.endObject();
        indexRequest.source(builder);
    }
    Request request = Request.index(indexRequest);
    if (indexRequest.opType() == DocWriteRequest.OpType.CREATE) {
        assertEquals("/" + index + "/" + type + "/" + id + "/_create", request.endpoint);
    } else if (id != null) {
        assertEquals("/" + index + "/" + type + "/" + id, request.endpoint);
    } else {
        assertEquals("/" + index + "/" + type, request.endpoint);
    }
    assertEquals(expectedParams, request.params);
    assertEquals(method, request.method);
    HttpEntity entity = request.entity;
    assertNotNull(entity);
    assertTrue(entity instanceof ByteArrayEntity);
    try (XContentParser parser = createParser(xContentType.xContent(), entity.getContent())) {
        assertEquals(nbFields, parser.map().size());
    }
}
Also used : XContentType(org.elasticsearch.common.xcontent.XContentType) HttpEntity(org.apache.http.HttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) HashMap(java.util.HashMap) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) WriteRequest(org.elasticsearch.action.support.WriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) BulkShardRequest(org.elasticsearch.action.bulk.BulkShardRequest) GetRequest(org.elasticsearch.action.get.GetRequest) ReplicatedWriteRequest(org.elasticsearch.action.support.replication.ReplicatedWriteRequest) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) ReplicationRequest(org.elasticsearch.action.support.replication.ReplicationRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 15 with HttpEntity

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

the class RestHighLevelClientTests method testParseEntity.

public void testParseEntity() throws IOException {
    {
        IllegalStateException ise = expectThrows(IllegalStateException.class, () -> restHighLevelClient.parseEntity(null, null));
        assertEquals("Response body expected but not returned", ise.getMessage());
    }
    {
        IllegalStateException ise = expectThrows(IllegalStateException.class, () -> restHighLevelClient.parseEntity(new StringEntity("", (ContentType) null), null));
        assertEquals("Elasticsearch didn't return the [Content-Type] header, unable to parse response body", ise.getMessage());
    }
    {
        StringEntity entity = new StringEntity("", ContentType.APPLICATION_SVG_XML);
        IllegalStateException ise = expectThrows(IllegalStateException.class, () -> restHighLevelClient.parseEntity(entity, null));
        assertEquals("Unsupported Content-Type: " + entity.getContentType().getValue(), ise.getMessage());
    }
    {
        CheckedFunction<XContentParser, String, IOException> entityParser = parser -> {
            assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
            assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
            assertTrue(parser.nextToken().isValue());
            String value = parser.text();
            assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
            return value;
        };
        HttpEntity jsonEntity = new StringEntity("{\"field\":\"value\"}", ContentType.APPLICATION_JSON);
        assertEquals("value", restHighLevelClient.parseEntity(jsonEntity, entityParser));
        HttpEntity yamlEntity = new StringEntity("---\nfield: value\n", ContentType.create("application/yaml"));
        assertEquals("value", restHighLevelClient.parseEntity(yamlEntity, entityParser));
        HttpEntity smileEntity = createBinaryEntity(SmileXContent.contentBuilder(), ContentType.create("application/smile"));
        assertEquals("value", restHighLevelClient.parseEntity(smileEntity, entityParser));
        HttpEntity cborEntity = createBinaryEntity(CborXContent.contentBuilder(), ContentType.create("application/cbor"));
        assertEquals("value", restHighLevelClient.parseEntity(cborEntity, entityParser));
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) ContentType(org.apache.http.entity.ContentType) XContentType(org.elasticsearch.common.xcontent.XContentType) HttpEntity(org.apache.http.HttpEntity) Matchers.anyString(org.mockito.Matchers.anyString) CheckedFunction(org.elasticsearch.common.CheckedFunction)

Aggregations

HttpEntity (org.apache.http.HttpEntity)1391 HttpResponse (org.apache.http.HttpResponse)509 IOException (java.io.IOException)483 HttpGet (org.apache.http.client.methods.HttpGet)391 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)315 HttpPost (org.apache.http.client.methods.HttpPost)305 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)163 URI (java.net.URI)153 StatusLine (org.apache.http.StatusLine)149 StringEntity (org.apache.http.entity.StringEntity)148 Header (org.apache.http.Header)136 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