org.apache.http.nio.entity.NByteArrayEntity Java Examples

The following examples show how to use org.apache.http.nio.entity.NByteArrayEntity. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpComponentsAsyncClientHttpRequest.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);
}
 
Example #2
Source File: HttpComponentsAsyncClientHttpRequest.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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);
}
 
Example #3
Source File: HttpComponentsAsyncClientHttpRequest.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@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);
}
 
Example #4
Source File: ElasticSearchRestDAOV5.java    From conductor with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example #5
Source File: ElasticSearchRestDAOV5.java    From conductor with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example #6
Source File: ElasticSearchRestDAOV6.java    From conductor with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example #7
Source File: ElasticSearchRestDAOV6.java    From conductor with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example #8
Source File: HttpComponentsAsyncClientHttpRequest.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #9
Source File: AsyncHttpService.java    From Tenable.io-SDK-for-Java with MIT License 5 votes vote down vote up
/**
 * 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 );
}
 
Example #10
Source File: ESUtils.java    From yuzhouwan with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example #11
Source File: HttpClientUtil.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
private static NByteArrayEntity byteBody(HttpReq config) {
	NByteArrayEntity entity = new NByteArrayEntity(config.body());

	if (config.contentType() != null) {
		entity.setContentType(config.contentType());
	}
	return entity;
}