Search in sources :

Example 46 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project FastDev4Android by jiangqqlmj.

the class HttpClientStack method setEntityIfNonEmptyBody.

/**
     * 如果request的请求体不为空,进行设置请求体信息
     * @param httpRequest
     * @param request
     * @throws AuthFailureError
     */
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)

Example 47 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project elasticsearch by elastic.

the class RemoteRequestBuilders method initialSearchEntity.

static HttpEntity initialSearchEntity(SearchRequest searchRequest, BytesReference query) {
    // EMPTY is safe here because we're not calling namedObject
    try (XContentBuilder entity = JsonXContent.contentBuilder();
        XContentParser queryParser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, query)) {
        entity.startObject();
        entity.field("query");
        {
            /* We're intentionally a bit paranoid here - copying the query as xcontent rather than writing a raw field. We don't want
                 * poorly written queries to escape. Ever. */
            entity.copyCurrentStructure(queryParser);
            XContentParser.Token shouldBeEof = queryParser.nextToken();
            if (shouldBeEof != null) {
                throw new ElasticsearchException("query was more than a single object. This first token after the object is [" + shouldBeEof + "]");
            }
        }
        if (searchRequest.source().fetchSource() != null) {
            entity.field("_source", searchRequest.source().fetchSource());
        } else {
            entity.field("_source", true);
        }
        entity.endObject();
        BytesRef bytes = entity.bytes().toBytesRef();
        return new ByteArrayEntity(bytes.bytes, bytes.offset, bytes.length, ContentType.APPLICATION_JSON);
    } catch (IOException e) {
        throw new ElasticsearchException("unexpected error building entity", e);
    }
}
Also used : ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) ElasticsearchException(org.elasticsearch.ElasticsearchException) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser) BytesRef(org.apache.lucene.util.BytesRef)

Example 48 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project gitblit by gitblit.

the class FilestoreServletTest method testDownloadMultiple.

@Test
public void testDownloadMultiple() throws Exception {
    FileUtils.delete(filestore().getStorageFolder());
    filestore().clearFilestoreCache();
    RepositoryModel r = gitblit().getRepositoryModel(repoName);
    UserModel u = new UserModel("admin");
    u.canAdmin = true;
    //No upload limit
    settings().overrideSetting(Keys.filestore.maxUploadSize, FilestoreManager.UNDEFINED_SIZE);
    final BlobInfo blob = new BlobInfo(512 * FileUtils.KB);
    //Emulate a pre-existing Git-LFS repository by using using internal pre-tested methods
    assertEquals(Status.Available, filestore().uploadBlob(blob.hash, blob.length, u, r, new ByteArrayInputStream(blob.blob)));
    final String batchURL = GitBlitSuite.url + repoLfs + "batch";
    final String downloadURL = GitBlitSuite.url + repoLfs + blob.hash;
    HttpClient client = HttpClientBuilder.create().build();
    HttpPost request = new HttpPost(batchURL);
    // add request header
    request.addHeader(HttpHeaders.ACCEPT, FilestoreServlet.GIT_LFS_META_MIME);
    request.addHeader(HttpHeaders.CONTENT_ENCODING, FilestoreServlet.GIT_LFS_META_MIME);
    String content = String.format("{%s:%s,%s:[{%s:%s,%s:%d},{%s:%s,%s:%d}]}", "\"operation\"", "\"download\"", "\"objects\"", "\"oid\"", "\"" + blob.hash + "\"", "\"size\"", blob.length, "\"oid\"", "\"" + SHA256_EG + "\"", "\"size\"", 0);
    HttpEntity entity = new ByteArrayEntity(content.getBytes("UTF-8"));
    request.setEntity(entity);
    HttpResponse response = client.execute(request);
    String responseMessage = IOUtils.toString(response.getEntity().getContent(), "UTF-8");
    assertEquals(200, response.getStatusLine().getStatusCode());
    String expectedContent = String.format("{%s:[{%s:%s,%s:%d,%s:{%s:{%s:%s}}},{%s:%s,%s:%d,%s:{%s:%s,%s:%d}}]}", "\"objects\"", "\"oid\"", "\"" + blob.hash + "\"", "\"size\"", blob.length, "\"actions\"", "\"download\"", "\"href\"", "\"" + downloadURL + "\"", "\"oid\"", "\"" + SHA256_EG + "\"", "\"size\"", 0, "\"error\"", "\"message\"", "\"Object not available\"", "\"code\"", 404);
    assertEquals(expectedContent, responseMessage);
}
Also used : UserModel(com.gitblit.models.UserModel) HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) RepositoryModel(com.gitblit.models.RepositoryModel) Test(org.junit.Test)

Example 49 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project gitblit by gitblit.

the class FilestoreServletTest method testMalformedUpload.

@Test
public void testMalformedUpload() throws Exception {
    FileUtils.delete(filestore().getStorageFolder());
    filestore().clearFilestoreCache();
    //No upload limit
    settings().overrideSetting(Keys.filestore.maxUploadSize, FilestoreManager.UNDEFINED_SIZE);
    final BlobInfo blob = new BlobInfo(512 * FileUtils.KB);
    final String initialUploadURL = GitBlitSuite.url + repoLfs + "batch";
    HttpClient client = HttpClientBuilder.create().build();
    HttpPost request = new HttpPost(initialUploadURL);
    // add request header
    request.addHeader(HttpHeaders.ACCEPT, FilestoreServlet.GIT_LFS_META_MIME);
    request.addHeader(HttpHeaders.CONTENT_ENCODING, FilestoreServlet.GIT_LFS_META_MIME);
    //Malformed JSON, comma instead of colon and unquoted strings
    String content = String.format("{%s:%s,%s:[{%s:%s,%s,%d}]}", "operation", "upload", "objects", "oid", blob.hash, "size", blob.length);
    HttpEntity entity = new ByteArrayEntity(content.getBytes("UTF-8"));
    request.setEntity(entity);
    HttpResponse response = client.execute(request);
    String responseMessage = IOUtils.toString(response.getEntity().getContent(), "UTF-8");
    assertEquals(400, response.getStatusLine().getStatusCode());
    String expectedError = String.format("{%s:%s,%s:%d}", "\"message\"", "\"Malformed Git-LFS request\"", "\"code\"", 400);
    assertEquals(expectedError, responseMessage);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test)

Example 50 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project elasticsearch by elastic.

the class RestHighLevelClientTests method createBinaryEntity.

private static HttpEntity createBinaryEntity(XContentBuilder xContentBuilder, ContentType contentType) throws IOException {
    try (XContentBuilder builder = xContentBuilder) {
        builder.startObject();
        builder.field("field", "value");
        builder.endObject();
        return new ByteArrayEntity(builder.bytes().toBytesRef().bytes, contentType);
    }
}
Also used : ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)78 HttpEntity (org.apache.http.HttpEntity)28 HttpPost (org.apache.http.client.methods.HttpPost)24 HttpResponse (org.apache.http.HttpResponse)20 IOException (java.io.IOException)17 ByteArrayOutputStream (java.io.ByteArrayOutputStream)16 JSONObject (org.json.JSONObject)11 Test (org.junit.Test)10 HttpClient (org.apache.http.client.HttpClient)9 InputStream (java.io.InputStream)7 JSONArray (org.json.JSONArray)7 URISyntaxException (java.net.URISyntaxException)5 Header (org.apache.http.Header)5 AbstractHttpEntity (org.apache.http.entity.AbstractHttpEntity)5 ContentType (org.apache.http.entity.ContentType)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