Search in sources :

Example 6 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity 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 7 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity 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 8 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project nanohttpd by NanoHttpd.

the class PutStreamIntegrationTest method testSimplePutRequest.

@Test
public void testSimplePutRequest() throws Exception {
    String expected = "This HttpPut request has a content-length of 48.";
    HttpPut httpput = new HttpPut("http://localhost:8192/");
    httpput.setEntity(new ByteArrayEntity(expected.getBytes()));
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = this.httpclient.execute(httpput, responseHandler);
    assertEquals("PUT:" + expected, responseBody);
}
Also used : ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 9 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project dubbo by alibaba.

the class HttpClientConnection method sendRequest.

public void sendRequest() throws IOException {
    request.setEntity(new ByteArrayEntity(output.toByteArray()));
    this.response = httpClient.execute(request);
}
Also used : ByteArrayEntity(org.apache.http.entity.ByteArrayEntity)

Example 10 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project SmartAndroidSource by jaychou2012.

the class HttpClientStack method setEntityIfNonEmptyBody.

private static void setEntityIfNonEmptyBody(HttpEntityEnclosingRequestBase httpRequest, Request<?> request) throws AuthFailureError {
    byte[] body = request.getBody();
    if (body != null) {
        HttpEntity entity = new ByteArrayEntity(body);
        httpRequest.setEntity(entity);
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity)

Aggregations

ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)74 HttpEntity (org.apache.http.HttpEntity)25 HttpPost (org.apache.http.client.methods.HttpPost)22 HttpResponse (org.apache.http.HttpResponse)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 IOException (java.io.IOException)13 JSONObject (org.json.JSONObject)10 Test (org.junit.Test)10 HttpClient (org.apache.http.client.HttpClient)8 JSONArray (org.json.JSONArray)7 InputStream (java.io.InputStream)6 AbstractHttpEntity (org.apache.http.entity.AbstractHttpEntity)5 BytesRef (org.apache.lucene.util.BytesRef)5 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)5 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)5 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)5 GetRequest (org.elasticsearch.action.get.GetRequest)5 IndexRequest (org.elasticsearch.action.index.IndexRequest)5 WriteRequest (org.elasticsearch.action.support.WriteRequest)5 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)5