Search in sources :

Example 1 with HttpRequestFutureTask

use of org.apache.http.impl.client.HttpRequestFutureTask in project cas by apereo.

the class SimpleHttpClient method sendMessageToEndPoint.

@Override
public boolean sendMessageToEndPoint(final HttpMessage message) {
    Assert.notNull(this.httpClient);
    try {
        final HttpPost request = new HttpPost(message.getUrl().toURI());
        request.addHeader("Content-Type", message.getContentType());
        final StringEntity entity = new StringEntity(message.getMessage(), ContentType.create(message.getContentType()));
        request.setEntity(entity);
        final ResponseHandler<Boolean> handler = response -> response.getStatusLine().getStatusCode() == HttpStatus.SC_OK;
        LOGGER.debug("Created HTTP post message payload [{}]", request);
        final HttpRequestFutureTask<Boolean> task = this.requestExecutorService.execute(request, HttpClientContext.create(), handler);
        if (message.isAsynchronous()) {
            return true;
        }
        return task.get();
    } catch (final RejectedExecutionException e) {
        LOGGER.warn(e.getMessage(), e);
        return false;
    } catch (final Exception e) {
        LOGGER.debug(e.getMessage(), e);
        return false;
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestFutureTask(org.apache.http.impl.client.HttpRequestFutureTask) URL(java.net.URL) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) LoggerFactory(org.slf4j.LoggerFactory) HttpStatus(org.apache.http.HttpStatus) EntityUtils(org.apache.http.util.EntityUtils) FutureRequestExecutionService(org.apache.http.impl.client.FutureRequestExecutionService) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) HttpEntity(org.apache.http.HttpEntity) ContentType(org.apache.http.entity.ContentType) StringEntity(org.apache.http.entity.StringEntity) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Serializable(java.io.Serializable) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) HttpGet(org.apache.http.client.methods.HttpGet) DisposableBean(org.springframework.beans.factory.DisposableBean) ResponseHandler(org.apache.http.client.ResponseHandler) Collections(java.util.Collections) Assert(org.springframework.util.Assert) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) MalformedURLException(java.net.MalformedURLException)

Aggregations

Serializable (java.io.Serializable)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Collections (java.util.Collections)1 List (java.util.List)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 Collectors (java.util.stream.Collectors)1 IOUtils (org.apache.commons.io.IOUtils)1 HttpEntity (org.apache.http.HttpEntity)1 HttpStatus (org.apache.http.HttpStatus)1 ResponseHandler (org.apache.http.client.ResponseHandler)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpGet (org.apache.http.client.methods.HttpGet)1 HttpPost (org.apache.http.client.methods.HttpPost)1 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)1 ContentType (org.apache.http.entity.ContentType)1 StringEntity (org.apache.http.entity.StringEntity)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1 FutureRequestExecutionService (org.apache.http.impl.client.FutureRequestExecutionService)1