use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project elasticsearch by elastic.
the class RestHighLevelClientTests method testWrapResponseListenerOnResponseExceptionWithIgnoresErrorNoBody.
public void testWrapResponseListenerOnResponseExceptionWithIgnoresErrorNoBody() throws IOException {
TrackingActionListener trackingActionListener = new TrackingActionListener();
//response parsing throws exception while handling ignores. same as when GetResponse#fromXContent throws error when trying
//to parse a 404 response which contains an error rather than a valid document not found response.
ResponseListener responseListener = restHighLevelClient.wrapResponseListener(response -> {
throw new IllegalStateException();
}, trackingActionListener, Collections.singleton(404));
HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(RestStatus.NOT_FOUND));
Response response = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse);
ResponseException responseException = new ResponseException(response);
responseListener.onFailure(responseException);
assertThat(trackingActionListener.exception.get(), instanceOf(ElasticsearchException.class));
ElasticsearchException elasticsearchException = (ElasticsearchException) trackingActionListener.exception.get();
assertEquals(RestStatus.NOT_FOUND, elasticsearchException.status());
assertSame(responseException, elasticsearchException.getCause());
assertEquals(responseException.getMessage(), elasticsearchException.getMessage());
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project elasticsearch by elastic.
the class RestHighLevelClientTests method testPerformRequestOnResponseExceptionWithEntity.
public void testPerformRequestOnResponseExceptionWithEntity() throws IOException {
MainRequest mainRequest = new MainRequest();
CheckedFunction<MainRequest, Request, IOException> requestConverter = request -> new Request("GET", "/", Collections.emptyMap(), null);
RestStatus restStatus = randomFrom(RestStatus.values());
HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(restStatus));
httpResponse.setEntity(new StringEntity("{\"error\":\"test error message\",\"status\":" + restStatus.getStatus() + "}", ContentType.APPLICATION_JSON));
Response mockResponse = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse);
ResponseException responseException = new ResponseException(mockResponse);
when(restClient.performRequest(anyString(), anyString(), anyMapOf(String.class, String.class), anyObject(), anyVararg())).thenThrow(responseException);
ElasticsearchException elasticsearchException = expectThrows(ElasticsearchException.class, () -> restHighLevelClient.performRequest(mainRequest, requestConverter, response -> response.getStatusLine().getStatusCode(), Collections.emptySet()));
assertEquals("Elasticsearch exception [type=exception, reason=test error message]", elasticsearchException.getMessage());
assertEquals(restStatus, elasticsearchException.status());
assertSame(responseException, elasticsearchException.getSuppressed()[0]);
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project h2o-2 by h2oai.
the class GoogleAnalyticsThreadFactory method post.
@SuppressWarnings({ "rawtypes" })
public GoogleAnalyticsResponse post(GoogleAnalyticsRequest request) {
GoogleAnalyticsResponse response = new GoogleAnalyticsResponse();
if (!config.isEnabled()) {
return response;
}
BasicHttpResponse httpResponse = null;
try {
List<NameValuePair> postParms = new ArrayList<NameValuePair>();
//Log.debug("GA Processing " + request);
//Process the parameters
processParameters(request, postParms);
//Process custom dimensions
processCustomDimensionParameters(request, postParms);
//Process custom metrics
processCustomMetricParameters(request, postParms);
//Log.debug("GA Processed all parameters and sending the request " + postParms);
HttpPost httpPost = new HttpPost(config.getUrl());
try {
httpPost.setEntity(new UrlEncodedFormEntity(postParms, "UTF-8"));
} catch (UnsupportedEncodingException e) {
Log.warn("This systems doesn't support UTF-8!");
}
try {
httpResponse = (BasicHttpResponse) httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
//Log.trace("GA connectivity had a problem or the connectivity was aborted. "+e.toString());
} catch (IOException e) {
//Log.trace("GA connectivity suffered a protocol error. "+e.toString());
}
//Log.debug("GA response: " +httpResponse.toString());
response.setStatusCode(httpResponse.getStatusLine().getStatusCode());
response.setPostedParms(postParms);
try {
EntityUtils.consume(httpResponse.getEntity());
} catch (IOException e) {
/*consume quietly*/
}
if (config.isGatherStats()) {
gatherStats(request);
}
} catch (Exception e) {
if (e instanceof UnknownHostException) {
//Log.trace("Coudln't connect to GA. Internet may not be available. " + e.toString());
} else {
//Log.trace("Exception while sending the GA tracker request: " + request +". "+ e.toString());
}
}
return response;
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project iosched by google.
the class BasicNetworkTest method testHeadersAndPostParams.
public void testHeadersAndPostParams() throws Exception {
MockHttpStack mockHttpStack = new MockHttpStack();
BasicHttpResponse fakeResponse = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), 200, "OK");
fakeResponse.setEntity(new StringEntity("foobar"));
mockHttpStack.setResponseToReturn(fakeResponse);
BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
Request<String> request = new Request<String>(Request.Method.GET, "http://foo", null) {
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
return null;
}
@Override
protected void deliverResponse(String response) {
}
@Override
public Map<String, String> getHeaders() {
Map<String, String> result = new HashMap<String, String>();
result.put("requestheader", "foo");
return result;
}
@Override
public Map<String, String> getParams() {
Map<String, String> result = new HashMap<String, String>();
result.put("requestpost", "foo");
return result;
}
};
httpNetwork.performRequest(request);
assertEquals("foo", mockHttpStack.getLastHeaders().get("requestheader"));
assertEquals("requestpost=foo&", new String(mockHttpStack.getLastPostBody()));
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project okhttp by square.
the class OkApacheClient method transformResponse.
private static HttpResponse transformResponse(Response response) {
int code = response.code();
String message = response.message();
BasicHttpResponse httpResponse = new BasicHttpResponse(HTTP_1_1, code, message);
ResponseBody body = response.body();
InputStreamEntity entity = new InputStreamEntity(body.byteStream(), body.contentLength());
httpResponse.setEntity(entity);
Headers headers = response.headers();
for (int i = 0, size = headers.size(); i < size; i++) {
String name = headers.name(i);
String value = headers.value(i);
httpResponse.addHeader(name, value);
if ("Content-Type".equalsIgnoreCase(name)) {
entity.setContentType(value);
} else if ("Content-Encoding".equalsIgnoreCase(name)) {
entity.setContentEncoding(value);
}
}
return httpResponse;
}
Aggregations