Search in sources :

Example 51 with CloseableHttpResponse

use of org.apache.http.client.methods.CloseableHttpResponse in project spring-boot by spring-projects.

the class AbstractHttpClientMockTests method mockMetadataGetError.

protected void mockMetadataGetError(int status, String message) throws IOException, JSONException {
    CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    mockHttpEntity(response, createJsonError(status, message).getBytes(), "application/json");
    mockStatus(response, status);
    given(this.http.execute(isA(HttpGet.class))).willReturn(response);
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 52 with CloseableHttpResponse

use of org.apache.http.client.methods.CloseableHttpResponse in project spring-boot by spring-projects.

the class InitializrServiceTests method loadMetadataNoContent.

@Test
public void loadMetadataNoContent() throws Exception {
    CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    mockStatus(response, 500);
    given(this.http.execute(isA(HttpGet.class))).willReturn(response);
    ProjectGenerationRequest request = new ProjectGenerationRequest();
    this.thrown.expect(ReportableException.class);
    this.thrown.expectMessage("No content received from server");
    this.invoker.generate(request);
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Test(org.junit.Test)

Example 53 with CloseableHttpResponse

use of org.apache.http.client.methods.CloseableHttpResponse in project spring-boot by spring-projects.

the class InitializrServiceTests method generateProjectNoContent.

@Test
public void generateProjectNoContent() throws Exception {
    mockSuccessfulMetadataGet(false);
    CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    mockStatus(response, 500);
    given(this.http.execute(isA(HttpGet.class))).willReturn(response);
    ProjectGenerationRequest request = new ProjectGenerationRequest();
    this.thrown.expect(ReportableException.class);
    this.thrown.expectMessage("No content received from server");
    this.invoker.generate(request);
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Test(org.junit.Test)

Example 54 with CloseableHttpResponse

use of org.apache.http.client.methods.CloseableHttpResponse in project jstorm by alibaba.

the class AlimonitorClient method httpPost.

private boolean httpPost(String url, String msg) {
    boolean ret = false;
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    CloseableHttpResponse response = null;
    try {
        HttpPost request = new HttpPost(url);
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("name", monitorName));
        nvps.add(new BasicNameValuePair("msg", msg));
        request.setEntity(new UrlEncodedFormEntity(nvps));
        response = httpClient.execute(request);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            LOG.info(EntityUtils.toString(entity));
        }
        EntityUtils.consume(entity);
        ret = true;
    } catch (Exception e) {
        LOG.error("Exception when sending http request to alimonitor", e);
    } finally {
        try {
            if (response != null)
                response.close();
            httpClient.close();
        } catch (Exception e) {
            LOG.error("Exception when closing httpclient", e);
        }
    }
    return ret;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpEntity(org.apache.http.HttpEntity) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ArrayList(java.util.ArrayList) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity)

Example 55 with CloseableHttpResponse

use of org.apache.http.client.methods.CloseableHttpResponse in project camel by apache.

the class Olingo2AppImpl method execute.

/**
     * public for unit test, not to be used otherwise
     */
public void execute(HttpUriRequest httpUriRequest, ContentType contentType, FutureCallback<HttpResponse> callback) {
    // add accept header when its not a form or multipart
    final String contentTypeString = contentType.toString();
    if (!ContentType.APPLICATION_FORM_URLENCODED.getMimeType().equals(contentType.getMimeType()) && !contentType.getMimeType().startsWith(MULTIPART_MIME_TYPE)) {
        // otherwise accept what is being sent
        httpUriRequest.addHeader(HttpHeaders.ACCEPT, contentTypeString);
    }
    // is something being sent?
    if (httpUriRequest instanceof HttpEntityEnclosingRequestBase && httpUriRequest.getFirstHeader(HttpHeaders.CONTENT_TYPE) == null) {
        httpUriRequest.addHeader(HttpHeaders.CONTENT_TYPE, contentTypeString);
    }
    // set user specified custom headers
    if (httpHeaders != null && !httpHeaders.isEmpty()) {
        for (Map.Entry<String, String> entry : httpHeaders.entrySet()) {
            httpUriRequest.setHeader(entry.getKey(), entry.getValue());
        }
    }
    // add client protocol version if not specified
    if (!httpUriRequest.containsHeader(ODataHttpHeaders.DATASERVICEVERSION)) {
        httpUriRequest.addHeader(ODataHttpHeaders.DATASERVICEVERSION, ODataServiceVersion.V20);
    }
    if (!httpUriRequest.containsHeader(MAX_DATA_SERVICE_VERSION)) {
        httpUriRequest.addHeader(MAX_DATA_SERVICE_VERSION, ODataServiceVersion.V30);
    }
    // execute request
    if (client instanceof CloseableHttpAsyncClient) {
        ((CloseableHttpAsyncClient) client).execute(httpUriRequest, callback);
    } else {
        // request synchronously
        try {
            CloseableHttpResponse result = ((CloseableHttpClient) client).execute(httpUriRequest);
            callback.completed(result);
        } catch (IOException e) {
            callback.failed(e);
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) IOException(java.io.IOException) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)536 HttpGet (org.apache.http.client.methods.HttpGet)242 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)171 StringEntity (org.apache.http.entity.StringEntity)127 JsonNode (com.fasterxml.jackson.databind.JsonNode)125 Test (org.junit.Test)125 HttpPost (org.apache.http.client.methods.HttpPost)107 IOException (java.io.IOException)105 HttpEntity (org.apache.http.HttpEntity)103 Deployment (org.activiti.engine.test.Deployment)87 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)67 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)57 InputStream (java.io.InputStream)53 HttpPut (org.apache.http.client.methods.HttpPut)50 Task (org.activiti.engine.task.Task)41 URI (java.net.URI)36 StatusLine (org.apache.http.StatusLine)34 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)34 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)31 BufferedReader (java.io.BufferedReader)30