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);
}
}
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);
}
}
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);
}
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);
}
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);
}
}
Aggregations