下面列出了怎么用org.apache.http.nio.entity.NByteArrayEntity的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected ListenableFuture<ClientHttpResponse> executeInternal(HttpHeaders headers, byte[] bufferedOutput)
throws IOException {
HttpComponentsClientHttpRequest.addHeaders(this.httpRequest, headers);
if (this.httpRequest instanceof HttpEntityEnclosingRequest) {
HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest;
HttpEntity requestEntity = new NByteArrayEntity(bufferedOutput);
entityEnclosingRequest.setEntity(requestEntity);
}
HttpResponseFutureCallback callback = new HttpResponseFutureCallback(this.httpRequest);
Future<HttpResponse> futureResponse = this.httpClient.execute(this.httpRequest, this.httpContext, callback);
return new ClientHttpResponseFuture(futureResponse, callback);
}
@Override
protected ListenableFuture<ClientHttpResponse> executeInternal(HttpHeaders headers, byte[] bufferedOutput)
throws IOException {
HttpComponentsClientHttpRequest.addHeaders(this.httpRequest, headers);
if (this.httpRequest instanceof HttpEntityEnclosingRequest) {
HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest;
HttpEntity requestEntity = new NByteArrayEntity(bufferedOutput);
entityEnclosingRequest.setEntity(requestEntity);
}
HttpResponseFutureCallback callback = new HttpResponseFutureCallback(this.httpRequest);
Future<HttpResponse> futureResponse = this.httpClient.execute(this.httpRequest, this.httpContext, callback);
return new ClientHttpResponseFuture(futureResponse, callback);
}
@Override
protected ListenableFuture<ClientHttpResponse> executeInternal(HttpHeaders headers, byte[] bufferedOutput)
throws IOException {
HttpComponentsClientHttpRequest.addHeaders(this.httpRequest, headers);
if (this.httpRequest instanceof HttpEntityEnclosingRequest) {
HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest;
HttpEntity requestEntity = new NByteArrayEntity(bufferedOutput);
entityEnclosingRequest.setEntity(requestEntity);
}
HttpResponseFutureCallback callback = new HttpResponseFutureCallback(this.httpRequest);
Future<HttpResponse> futureResponse = this.httpClient.execute(this.httpRequest, this.httpContext, callback);
return new ClientHttpResponseFuture(futureResponse, callback);
}
/**
* Initializes the index with the required templates and mappings.
*/
private void initIndex() throws Exception {
//0. Add the tasklog template
if (doesResourceNotExist("/_template/tasklog_template")) {
logger.info("Creating the index template 'tasklog_template'");
InputStream stream = ElasticSearchDAOV5.class.getResourceAsStream("/template_tasklog.json");
byte[] templateSource = IOUtils.toByteArray(stream);
HttpEntity entity = new NByteArrayEntity(templateSource, ContentType.APPLICATION_JSON);
try {
elasticSearchAdminClient.performRequest(HttpMethod.PUT, "/_template/tasklog_template", Collections.emptyMap(), entity);
} catch (IOException e) {
logger.error("Failed to initialize tasklog_template", e);
}
}
}
/**
* Adds a mapping type to an index if it does not exist.
*
* @param index The name of the index.
* @param mappingType The name of the mapping type.
* @param mappingFilename The name of the mapping file to use to add the mapping if it does not exist.
* @throws IOException If an error occurred during requests to ES.
*/
private void addMappingToIndex(final String index, final String mappingType, final String mappingFilename) throws IOException {
logger.info("Adding '{}' mapping to index '{}'...", mappingType, index);
String resourcePath = "/" + index + "/_mapping/" + mappingType;
if (doesResourceNotExist(resourcePath)) {
InputStream stream = ElasticSearchDAOV5.class.getResourceAsStream(mappingFilename);
byte[] mappingSource = IOUtils.toByteArray(stream);
HttpEntity entity = new NByteArrayEntity(mappingSource, ContentType.APPLICATION_JSON);
elasticSearchAdminClient.performRequest(HttpMethod.PUT, resourcePath, Collections.emptyMap(), entity);
logger.info("Added '{}' mapping", mappingType);
} else {
logger.info("Mapping '{}' already exists", mappingType);
}
}
/**
* Initializes the index with the required templates and mappings.
*/
private void initIndexTemplate(String type) {
String template = "template_" + type;
try {
if (doesResourceNotExist("/_template/" + template)) {
logger.info("Creating the index template '" + template + "'");
InputStream stream = ElasticSearchDAOV6.class.getResourceAsStream("/" + template + ".json");
byte[] templateSource = IOUtils.toByteArray(stream);
HttpEntity entity = new NByteArrayEntity(templateSource, ContentType.APPLICATION_JSON);
elasticSearchAdminClient
.performRequest(HttpMethod.PUT, "/_template/" + template, Collections.emptyMap(), entity);
}
} catch (Exception e) {
logger.error("Failed to init " + template, e);
}
}
/**
* Adds a mapping type to an index if it does not exist.
*
* @param index The name of the index.
* @param mappingType The name of the mapping type.
* @param mappingFilename The name of the mapping file to use to add the mapping if it does not exist.
* @throws IOException If an error occurred during requests to ES.
*/
private void addMappingToIndex(final String index, final String mappingType, final String mappingFilename)
throws IOException {
logger.info("Adding '{}' mapping to index '{}'...", mappingType, index);
String resourcePath = "/" + index + "/_mapping/" + mappingType;
if (doesResourceNotExist(resourcePath)) {
HttpEntity entity = new NByteArrayEntity(loadTypeMappingSource(mappingFilename).getBytes(),
ContentType.APPLICATION_JSON);
elasticSearchAdminClient.performRequest(HttpMethod.PUT, resourcePath, Collections.emptyMap(), entity);
logger.info("Added '{}' mapping", mappingType);
} else {
logger.info("Mapping '{}' already exists", mappingType);
}
}
@Override
protected ListenableFuture<ClientHttpResponse> executeInternal(HttpHeaders headers, byte[] bufferedOutput)
throws IOException {
HttpComponentsClientHttpRequest.addHeaders(this.httpRequest, headers);
if (this.httpRequest instanceof HttpEntityEnclosingRequest) {
HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest;
HttpEntity requestEntity = new NByteArrayEntity(bufferedOutput);
entityEnclosingRequest.setEntity(requestEntity);
}
final HttpResponseFutureCallback callback = new HttpResponseFutureCallback();
final Future<HttpResponse> futureResponse =
this.httpClient.execute(this.httpRequest, this.httpContext, callback);
return new ClientHttpResponseFuture(futureResponse, callback);
}
/**
* Makes an HTTP PUT request using the given URI and optional byte array body and headers.
*
* @param uri the URI to use for the PUT call
* @param contentType The content-type. Use helper function org.apache.http.entity.ContentType.create() to generate
* @param body Optional, can be null. An byte array that will be PUTed as is
* @param headers Optional, can be null. One or more HTTP headers, typically created by instancing org.apache.http.message.BasicHeader
* @return the resulting HttpFuture instance
*/
public HttpFuture doPut( URI uri, ContentType contentType, byte[] body, Header[] headers ) {
HttpPut httpPut = new HttpPut( uri );
if( body != null ) {
httpPut.setEntity( new NByteArrayEntity( body, contentType ) );
}
if( headers != null && headers.length > 0 )
httpPut.setHeaders( headers );
return new HttpFuture( this, httpPut, asyncClient.execute( httpPut, null ), null );
}
public static HttpEntity createEntity(String json) {
try {
byte[] bytes = SMILE_MAPPER.writeValueAsBytes(SMILE_MAPPER.readValue(json, Object.class));
return new NByteArrayEntity(bytes, ContentType.create(APPLICATION_JACKSON_SMILE));
} catch (Exception e) {
LOG.error("Cannot create entity!", e);
throw new RuntimeException("Cannot create entity!", e);
}
}
private static NByteArrayEntity byteBody(HttpReq config) {
NByteArrayEntity entity = new NByteArrayEntity(config.body());
if (config.contentType() != null) {
entity.setContentType(config.contentType());
}
return entity;
}