@Override public int read() throws IOException { if (byteArrayInputStream == null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStream stream = null; try { try { stream = requestContext.getWorkers().writeTo( entity, entity.getClass(), null, null, response.getMediaType(), response.getMetadata(), requestContext.getPropertiesDelegate(), baos, Collections.<WriterInterceptor>emptyList()); } finally { if (stream != null) { stream.close(); } } } catch (IOException e) { // ignore } byteArrayInputStream = new ByteArrayInputStream(baos.toByteArray()); } return byteArrayInputStream.read(); } };
@Override public int read() throws IOException { if (byteArrayInputStream == null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStream stream = null; try { try { stream = requestContext.getWorkers().writeTo( entity, entity.getClass(), null, null, response.getMediaType(), response.getMetadata(), requestContext.getPropertiesDelegate(), baos, Collections.<WriterInterceptor>emptyList()); } finally { if (stream != null) { stream.close(); } } } catch (IOException e) { // ignore } byteArrayInputStream = new ByteArrayInputStream(baos.toByteArray()); } return byteArrayInputStream.read(); } };
@Override public int read() throws IOException { if (byteArrayInputStream == null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStream stream = null; try { try { stream = requestContext.getWorkers().writeTo( entity, entity.getClass(), null, null, response.getMediaType(), response.getMetadata(), requestContext.getPropertiesDelegate(), baos, Collections.<WriterInterceptor>emptyList()); } finally { if (stream != null) { stream.close(); } } } catch (IOException e) { // ignore } byteArrayInputStream = new ByteArrayInputStream(baos.toByteArray()); } return byteArrayInputStream.read(); } };
private Response doGetRequest(String location, String auth, String query) { String url = String.format("%s?%s", location, query); Response r = this.client.target(url) .request() .header(HttpHeaders.AUTHORIZATION, auth) .accept(MediaType.APPLICATION_JSON) .acceptEncoding(CompressInterceptor.GZIP) .get(); if (r.getMediaType() != null) { // Append charset assert MediaType.APPLICATION_JSON_TYPE.equals(r.getMediaType()); r.getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, r.getMediaType().withCharset(CHARSET)); } gremlinInputHistogram.update(query.length()); gremlinOutputHistogram.update(r.getLength()); return r; }
private Response doPostRequest(String location, String auth, String req) { Entity<?> body = Entity.entity(req, MediaType.APPLICATION_JSON); Response r = this.client.target(location) .request() .header(HttpHeaders.AUTHORIZATION, auth) .accept(MediaType.APPLICATION_JSON) .acceptEncoding(CompressInterceptor.GZIP) .post(body); if (r.getMediaType() != null) { // Append charset assert MediaType.APPLICATION_JSON_TYPE.equals(r.getMediaType()); r.getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, r.getMediaType().withCharset(CHARSET)); } gremlinInputHistogram.update(req.length()); gremlinOutputHistogram.update(r.getLength()); return r; }
@Override public String getMimeType() { MediaType mediaType = myResponse.getMediaType(); if (mediaType == null) { return null; } //Keep only type and subtype and do not include the parameters such as charset return new MediaType(mediaType.getType(), mediaType.getSubtype()).toString(); }
if (response.getMediaType() == null) if (response.getMediaType() == null) response.close(); if (response.getMediaType() == null) response.close();
/** * Update response for a forbidden access. */ private void updateForbiddenAccess(final HttpServletResponse response) throws IOException { final Response response2 = accessDeniedHelper.toResponse(new AccessDeniedException("")); response.setStatus(response2.getStatus()); response.setContentType(response2.getMediaType().toString()); response.getOutputStream().write(((String) response2.getEntity()).getBytes(StandardCharsets.UTF_8)); }
private static void checkJsonOkResponse(String expectedContent, Response r) { assertEquals(200, r.getStatus()); assertEquals(MediaType.APPLICATION_JSON_TYPE, r.getMediaType()); assertEquals(expectedContent, r.getEntity().toString()); } }
@Test public void testUiRootResource() { // Test that Swagger UI resources do not interfere with // application-specific ones and are accessible. WebClient uiClient = WebClient .create(getBaseUrl() + "/api-docs") .accept("*/*"); try (Response response = uiClient.get()) { String html = response.readEntity(String.class); assertThat(html, containsString("<!-- HTML")); assertThat(response.getMediaType(), equalTo(MediaType.TEXT_HTML_TYPE)); } }
@Test public void testUiResource() { // Test that Swagger UI resources do not interfere with // application-specific ones and are accessible. WebClient uiClient = WebClient .create(getBaseUrl() + "/swagger-ui.css") .accept("text/css"); try (Response response = uiClient.get()) { String css = response.readEntity(String.class); assertThat(css, containsString(".swagger-ui{")); assertThat(response.getMediaType(), equalTo(MediaType.valueOf("text/css"))); } }
@Test public void testResponseHasBeenReceivedWhenQueringAllBookAsAtomFeed() { Response r = createWebClient(getBasePath() + "/books/feed", "application/atom+xml").get(); assertEquals(Response.Status.OK.getStatusCode(), r.getStatus()); assertEquals("application/atom+xml", r.getMediaType().toString()); }
@Test public void testResponseHasBeenReceivedWhenQueringAllBookAsAtomFeed() { Response r = createWebClient(getBasePath() + "/books/feed", "application/atom+xml").get(); assertEquals(Response.Status.OK.getStatusCode(), r.getStatus()); assertEquals("application/atom+xml", r.getMediaType().toString()); }
@Test public void testResponseHasBeenReceivedWhenQueringAllBookAsAtomFeed() { Response r = createWebClient(getBasePath() + "/books/feed", "application/atom+xml").get(); assertEquals(Response.Status.OK.getStatusCode(), r.getStatus()); assertEquals("application/atom+xml", r.getMediaType().toString()); }
@Test public void testResponseHasBeenReceivedWhenQueringAllBookAsAtomFeed() { Response r = createWebClient(getBasePath() + "/books/feed", "application/atom+xml").get(); assertEquals(Response.Status.OK.getStatusCode(), r.getStatus()); assertEquals("application/atom+xml", r.getMediaType().toString()); }
@Test public void testUseMapperOnBus() { String address = "http://localhost:" + PORT + "/bookstore/mapperonbus"; WebClient wc = WebClient.create(address); Response r = wc.post(null); assertEquals(500, r.getStatus()); MediaType mt = r.getMediaType(); assertEquals("text/plain;charset=utf-8", mt.toString().toLowerCase()); assertEquals("the-mapper", r.getHeaderString("BusMapper")); assertEquals("BusMapperException", r.readEntity(String.class)); }
@Test public void testGetBookDescriptionHttpResponse() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/httpresponse"; WebClient wc = WebClient.create(address); WebClient.getConfig(wc).getInInterceptors().add(new LoggingInInterceptor()); Response r = wc.get(); assertEquals("text/plain", r.getMediaType().toString()); assertEquals("Good Book", r.readEntity(String.class)); }
@Test public void testStatusAngHeadersFromStream() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/books/statusFromStream"; WebClient wc = WebClient.create(address); wc.accept("text/xml"); Response r = wc.get(); assertEquals(503, r.getStatus()); assertEquals("text/custom+plain", r.getMediaType().toString()); assertEquals("CustomValue", r.getHeaderString("CustomHeader")); assertEquals("Response is not available", r.readEntity(String.class)); }
@Test public void testGetCustomBookText() { String address = "http://localhost:" + PORT + "/bookstore/customtext"; WebClient wc = WebClient.create(address); Response r = wc.accept("text/custom").get(); String name = r.readEntity(String.class); assertEquals("Good book", name); assertEquals("text/custom;charset=us-ascii", r.getMediaType().toString()); assertEquals("CustomValue", r.getHeaderString("CustomHeader")); }
private void validateResponse(WebClient wc) { Response response = wc.getResponse(); assertEquals("OK", response.getHeaderString("Response")); assertEquals("OK2", response.getHeaderString("Response2")); assertEquals("Dynamic", response.getHeaderString("DynamicResponse")); assertEquals("Dynamic2", response.getHeaderString("DynamicResponse2")); assertEquals("custom", response.getHeaderString("Custom")); assertEquals("simple", response.getHeaderString("Simple")); assertEquals("serverWrite", response.getHeaderString("ServerWriterInterceptor")); assertEquals("application/xml;charset=us-ascii", response.getMediaType().toString()); assertEquals("http://localhost/redirect", response.getHeaderString(HttpHeaders.LOCATION)); }