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