List of usage examples for org.apache.http.message BasicHttpResponse BasicHttpResponse
public BasicHttpResponse(StatusLine statusLine)
From source file:org.esigate.http.HttpClientRequestExecutorTest.java
/** * Test that we don't have a NullpointerException when forcing the caching (ttl). * //from w ww.j a v a2 s . c om * @throws Exception */ public void testForcedTtlWith302ResponseCode() throws Exception { properties = new Properties(); properties.put(Parameters.REMOTE_URL_BASE.getName(), "http://localhost:8080"); properties.put(Parameters.TTL.getName(), "1000"); createHttpClientRequestExecutor(); DriverRequest originalRequest = TestUtils.createDriverRequest(driver); OutgoingRequest request = httpClientRequestExecutor.createOutgoingRequest(originalRequest, "http://localhost:8080", true); HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_MOVED_TEMPORARILY, "Moved temporarily")); response.addHeader("Location", "http://www.foo.com"); mockConnectionManager.setResponse(response); HttpResponse result = httpClientRequestExecutor.execute(request); if (result.getEntity() != null) { result.getEntity().writeTo(new NullOutputStream()); // We should have had a NullpointerException } }
From source file:org.elasticsearch.client.RestHighLevelClientTests.java
public void testWrapResponseListenerOnResponseExceptionWithEntity() throws IOException { TrackingActionListener trackingActionListener = new TrackingActionListener(); ResponseListener responseListener = restHighLevelClient.wrapResponseListener( response -> response.getStatusLine().getStatusCode(), trackingActionListener, Collections.emptySet()); RestStatus restStatus = randomFrom(RestStatus.values()); HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(restStatus)); httpResponse.setEntity(// w ww.j a va 2 s . com new StringEntity("{\"error\":\"test error message\",\"status\":" + restStatus.getStatus() + "}", ContentType.APPLICATION_JSON)); 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("Elasticsearch exception [type=exception, reason=test error message]", elasticsearchException.getMessage()); assertEquals(restStatus, elasticsearchException.status()); assertSame(responseException, elasticsearchException.getSuppressed()[0]); }
From source file:org.esigate.http.HttpClientRequestExecutorTest.java
/** * Test Expires response header with ttl forced. * /* ww w .j a v a 2 s. c o m*/ * @throws Exception */ public void testExpiresResponseHeaderWithForcedTtl() throws Exception { properties = new Properties(); properties.put(Parameters.REMOTE_URL_BASE, "http://localhost:8080"); properties.put(Parameters.TTL, "1"); properties.put(Parameters.X_CACHE_HEADER, "true"); properties.put(Parameters.HEURISTIC_CACHING_ENABLED, "false"); createHttpClientRequestExecutor(); DriverRequest originalRequest = TestUtils.createDriverRequest(driver); OutgoingRequest request = httpClientRequestExecutor.createOutgoingRequest(originalRequest, "http://localhost:8080", false); HttpResponse response = new BasicHttpResponse( new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_OK, "OK")); response.addHeader("Date", "Mon, 10 Dec 2012 19:37:52 GMT"); response.addHeader("Last-Modified", "Mon, 10 Dec 2012 19:35:27 GMT"); response.addHeader("Expires", "Mon, 10 Dec 2012 20:35:27 GMT"); response.addHeader("Cache-Control", "private, no-cache, must-revalidate, proxy-revalidate"); response.setEntity(new StringEntity("test")); mockConnectionManager.setResponse(response); // First call to load the cache HttpResponse result = httpClientRequestExecutor.execute(request); assertNotNull(result.getFirstHeader("Expires")); assertNotNull(result.getFirstHeader("Cache-control")); assertEquals("public, max-age=1", (result.getFirstHeader("Cache-control").getValue())); // Same test with the cache entry // Change the response to check that the cache is used response = new BasicHttpResponse( new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_NOT_MODIFIED, "Not modified")); response.addHeader("Date", "Mon, 10 Dec 2012 19:37:52 GMT"); response.addHeader("Expires", "Mon, 10 Dec 2012 20:35:27 GMT"); response.addHeader("Cache-Control", "private, no-cache, must-revalidate, proxy-revalidate"); mockConnectionManager.setResponse(response); result = httpClientRequestExecutor.execute(request); // Check that the cache has been used assertTrue(result.getFirstHeader("X-cache").getValue(), result.getFirstHeader("X-cache").getValue().startsWith("HIT")); assertNotNull(result.getFirstHeader("Expires")); assertNotNull(result.getFirstHeader("Cache-control")); assertEquals("public, max-age=1", (result.getFirstHeader("Cache-control").getValue())); // Wait for a revalidation to occur Thread.sleep(ONE_SECOND); result = httpClientRequestExecutor.execute(request); // Check that the revalidation occurred assertNotNull(result.getFirstHeader("Expires")); assertNotNull(result.getFirstHeader("Cache-control")); assertEquals("public, max-age=1", (result.getFirstHeader("Cache-control").getValue())); assertTrue(result.getFirstHeader("X-cache").getValue(), result.getFirstHeader("X-cache").getValue().startsWith("VALIDATED")); }
From source file:org.elasticsearch.client.RestHighLevelClientTests.java
public void testWrapResponseListenerOnResponseExceptionWithBrokenEntity() throws IOException { {//w w w . j ava 2 s . c o m TrackingActionListener trackingActionListener = new TrackingActionListener(); ResponseListener responseListener = restHighLevelClient.wrapResponseListener( response -> response.getStatusLine().getStatusCode(), trackingActionListener, Collections.emptySet()); RestStatus restStatus = randomFrom(RestStatus.values()); HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(restStatus)); httpResponse.setEntity(new StringEntity("{\"error\":", ContentType.APPLICATION_JSON)); 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("Unable to parse response body", elasticsearchException.getMessage()); assertEquals(restStatus, elasticsearchException.status()); assertSame(responseException, elasticsearchException.getCause()); assertThat(elasticsearchException.getSuppressed()[0], instanceOf(JsonParseException.class)); } { TrackingActionListener trackingActionListener = new TrackingActionListener(); ResponseListener responseListener = restHighLevelClient.wrapResponseListener( response -> response.getStatusLine().getStatusCode(), trackingActionListener, Collections.emptySet()); RestStatus restStatus = randomFrom(RestStatus.values()); HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(restStatus)); httpResponse.setEntity( new StringEntity("{\"status\":" + restStatus.getStatus() + "}", ContentType.APPLICATION_JSON)); 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("Unable to parse response body", elasticsearchException.getMessage()); assertEquals(restStatus, elasticsearchException.status()); assertSame(responseException, elasticsearchException.getCause()); assertThat(elasticsearchException.getSuppressed()[0], instanceOf(IllegalStateException.class)); } }
From source file:org.esigate.http.HttpClientRequestExecutorTest.java
/** * Test that we do not return a 304 to a non-conditional request when ttl forced. * //www . java2s. c o m * @throws Exception */ public void testDoNotReturn304ForNonConditionalRequestWhenTtlSet() throws Exception { properties = new Properties(); properties.put(Parameters.REMOTE_URL_BASE, "http://localhost:8080"); properties.put(Parameters.TTL, "1"); properties.put(Parameters.X_CACHE_HEADER, "true"); createHttpClientRequestExecutor(); DriverRequest originalRequest = TestUtils.createDriverRequest(driver); OutgoingRequest request1 = httpClientRequestExecutor.createOutgoingRequest(originalRequest, "http://localhost:8080", false); request1.addHeader("If-None-Match", "etag"); HttpResponse response = new BasicHttpResponse( new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_NOT_MODIFIED, "Not modified")); response.addHeader("Date", "Mon, 10 Dec 2012 19:37:52 GMT"); response.addHeader("Etag", "etag"); response.addHeader("Cache-Control", "max-age=0"); mockConnectionManager.setResponse(response); // First request returns a 304 HttpResponse result1 = httpClientRequestExecutor.execute(request1); assertEquals(HttpStatus.SC_NOT_MODIFIED, result1.getStatusLine().getStatusCode()); assertTrue(result1.getFirstHeader("X-cache").getValue(), result1.getFirstHeader("X-cache").getValue().startsWith("MISS")); assertNull(result1.getEntity()); // Second request should use the cache and return a // 304 again HttpResponse result2 = httpClientRequestExecutor.execute(request1); assertEquals(HttpStatus.SC_NOT_MODIFIED, result1.getStatusLine().getStatusCode()); assertTrue(result2.getFirstHeader("X-cache").getValue(), result2.getFirstHeader("X-cache").getValue().startsWith("HIT")); assertNull(result2.getEntity()); HttpResponse response2 = new BasicHttpResponse( new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_OK, "Ok")); response2.addHeader("Date", "Mon, 10 Dec 2012 19:37:52 GMT"); response2.addHeader("Etag", "etag"); response2.addHeader("Cache-Control", "max-age=0"); response2.setEntity(new StringEntity("test")); mockConnectionManager.setResponse(response2); // Third request not conditional ! Should call backend server as we // don't have the entity in the cache. OutgoingRequest request2 = httpClientRequestExecutor.createOutgoingRequest(originalRequest, "http://localhost:8080", false); HttpResponse result3 = httpClientRequestExecutor.execute(request2); assertEquals(HttpStatus.SC_OK, result3.getStatusLine().getStatusCode()); assertTrue(result3.getFirstHeader("X-cache").getValue(), !result3.getFirstHeader("X-cache").getValue().startsWith("HIT")); assertNotNull(result3.getEntity()); }
From source file:org.elasticsearch.client.RestHighLevelClientTests.java
public void testWrapResponseListenerOnResponseExceptionWithIgnores() throws IOException { TrackingActionListener trackingActionListener = new TrackingActionListener(); ResponseListener responseListener = restHighLevelClient.wrapResponseListener( response -> response.getStatusLine().getStatusCode(), 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); //although we got an exception, we turn it into a successful response because the status code was provided among ignores assertNull(trackingActionListener.exception.get()); assertEquals(404, trackingActionListener.statusCode.get()); }
From source file:org.elasticsearch.client.RestHighLevelClientTests.java
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();/*w w w .j a v a 2 s .c om*/ assertEquals(RestStatus.NOT_FOUND, elasticsearchException.status()); assertSame(responseException, elasticsearchException.getCause()); assertEquals(responseException.getMessage(), elasticsearchException.getMessage()); }
From source file:org.apache.camel.component.olingo2.api.impl.Olingo2AppImpl.java
private Olingo2BatchResponse parseResponse(Edm edm, Map<String, String> contentIdLocationMap, Olingo2BatchRequest request, BatchSingleResponse response) throws EntityProviderException, ODataApplicationException { // validate HTTP status final int statusCode = Integer.parseInt(response.getStatusCode()); final String statusInfo = response.getStatusInfo(); final BasicHttpResponse httpResponse = new BasicHttpResponse( new BasicStatusLine(HttpVersion.HTTP_1_1, statusCode, statusInfo)); final Map<String, String> headers = response.getHeaders(); for (Map.Entry<String, String> entry : headers.entrySet()) { httpResponse.setHeader(entry.getKey(), entry.getValue()); }//from w w w.j a v a 2 s . c om ByteArrayInputStream content = null; try { if (response.getBody() != null) { final ContentType partContentType = receiveWithCharsetParameter( ContentType.parse(headers.get(HttpHeaders.CONTENT_TYPE)), Consts.UTF_8); final String charset = partContentType.getCharset().toString(); final String body = response.getBody(); content = body != null ? new ByteArrayInputStream(body.getBytes(charset)) : null; httpResponse.setEntity(new StringEntity(body, charset)); } AbstractFutureCallback.checkStatus(httpResponse); } catch (ODataApplicationException e) { return new Olingo2BatchResponse(statusCode, statusInfo, response.getContentId(), response.getHeaders(), e); } catch (UnsupportedEncodingException e) { return new Olingo2BatchResponse(statusCode, statusInfo, response.getContentId(), response.getHeaders(), e); } // resolve resource path and query params and parse batch part uri final String resourcePath = request.getResourcePath(); final String resolvedResourcePath; if (resourcePath.startsWith("$") && !(METADATA.equals(resourcePath) || BATCH.equals(resourcePath))) { resolvedResourcePath = findLocation(resourcePath, contentIdLocationMap); } else { final String resourceLocation = response.getHeader(HttpHeaders.LOCATION); resolvedResourcePath = resourceLocation != null ? resourceLocation.substring(serviceUri.length()) : resourcePath; } final Map<String, String> resolvedQueryParams = request instanceof Olingo2BatchQueryRequest ? ((Olingo2BatchQueryRequest) request).getQueryParams() : null; final UriInfoWithType uriInfo = parseUri(edm, resolvedResourcePath, resolvedQueryParams); // resolve response content final Object resolvedContent = content != null ? readContent(uriInfo, content) : null; return new Olingo2BatchResponse(statusCode, statusInfo, response.getContentId(), response.getHeaders(), resolvedContent); }
From source file:org.elasticsearch.client.RestHighLevelClientTests.java
public void testWrapResponseListenerOnResponseExceptionWithIgnoresErrorValidBody() 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)); httpResponse.setEntity(new StringEntity("{\"error\":\"test error message\",\"status\":404}", ContentType.APPLICATION_JSON)); 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();// www . j ava 2 s. c om assertEquals(RestStatus.NOT_FOUND, elasticsearchException.status()); assertSame(responseException, elasticsearchException.getSuppressed()[0]); assertEquals("Elasticsearch exception [type=exception, reason=test error message]", elasticsearchException.getMessage()); }