Java Code Examples for com.netflix.zuul.context.RequestContext#getResponseDataStream()
The following examples show how to use
com.netflix.zuul.context.RequestContext#getResponseDataStream() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: _SwaggerBasePathRewritingFilter.java From jhipster-ribbon-hystrix with GNU General Public License v3.0 | 7 votes |
private String rewriteBasePath(RequestContext context) { InputStream responseDataStream = context.getResponseDataStream(); String requestUri = RequestContext.getCurrentContext().getRequest().getRequestURI(); try { String response = CharStreams.toString(new InputStreamReader(responseDataStream)); if (response != null) { LinkedHashMap<String, Object> map = this.mapper.readValue(response, LinkedHashMap.class); String basePath = requestUri.replace(Swagger2Controller.DEFAULT_URL,""); map.put("basePath",basePath); log.debug("Swagger-docs: rewritten Base URL with correct micro-service route: {}", basePath); return mapper.writeValueAsString(map); } } catch (IOException e){ log.error("Swagger-docs filter error", e); } return null; }
Example 2
Source File: SwaggerBasePathRewritingFilterTest.java From java-microservices-examples with Apache License 2.0 | 6 votes |
@Test public void run_on_valid_response_gzip() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL); RequestContext context = RequestContext.getCurrentContext(); context.setRequest(request); MockHttpServletResponse response = new MockHttpServletResponse(); context.setResponseGZipped(true); context.setResponse(response); context.setResponseDataStream(new ByteArrayInputStream(gzipData("{\"basePath\":\"/\"}"))); filter.run(); assertEquals("UTF-8", response.getCharacterEncoding()); InputStream responseDataStream = new GZIPInputStream(context.getResponseDataStream()); String responseBody = IOUtils.toString(responseDataStream, StandardCharsets.UTF_8); assertEquals("{\"basePath\":\"/service1\"}", responseBody); }
Example 3
Source File: SwaggerBasePathRewritingFilterTest.java From tutorials with MIT License | 6 votes |
@Test public void run_on_valid_response_gzip() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL); RequestContext context = RequestContext.getCurrentContext(); context.setRequest(request); MockHttpServletResponse response = new MockHttpServletResponse(); context.setResponseGZipped(true); context.setResponse(response); context.setResponseDataStream(new ByteArrayInputStream(gzipData("{\"basePath\":\"/\"}"))); filter.run(); assertEquals("UTF-8", response.getCharacterEncoding()); InputStream responseDataStream = new GZIPInputStream(context.getResponseDataStream()); String responseBody = IOUtils.toString(responseDataStream, StandardCharsets.UTF_8); assertEquals("{\"basePath\":\"/service1\"}", responseBody); }
Example 4
Source File: ResponseLogFilter.java From tutorials with MIT License | 6 votes |
@Override public Object run() throws ZuulException { RequestContext context = RequestContext.getCurrentContext(); try (final InputStream responseDataStream = context.getResponseDataStream()) { if(responseDataStream == null) { logger.info("BODY: {}", ""); return null; } String responseData = CharStreams.toString(new InputStreamReader(responseDataStream, "UTF-8")); logger.info("BODY: {}", responseData); context.setResponseBody(responseData); } catch (Exception e) { throw new ZuulException(e, INTERNAL_SERVER_ERROR.value(), e.getMessage()); } return null; }
Example 5
Source File: SwaggerBasePathRewritingFilterTest.java From jhipster-registry with Apache License 2.0 | 6 votes |
@Test public void run_on_valid_response_gzip() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL); RequestContext context = RequestContext.getCurrentContext(); context.setRequest(request); MockHttpServletResponse response = new MockHttpServletResponse(); context.setResponseGZipped(true); context.setResponse(response); context.setResponseDataStream(new ByteArrayInputStream(gzipData("{\"basePath\":\"/\"}"))); filter.run(); assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8"); InputStream responseDataStream = new GZIPInputStream(context.getResponseDataStream()); String responseBody = IOUtils.toString(responseDataStream, StandardCharsets.UTF_8); assertThat(responseBody).isEqualTo("{\"basePath\":\"/service1\"}"); }
Example 6
Source File: SwaggerBasePathRewritingFilter.java From jhipster-ribbon-hystrix with GNU General Public License v3.0 | 6 votes |
private String rewriteBasePath(RequestContext context) { InputStream responseDataStream = context.getResponseDataStream(); String requestUri = RequestContext.getCurrentContext().getRequest().getRequestURI(); try { String response = CharStreams.toString(new InputStreamReader(responseDataStream)); if (response != null) { LinkedHashMap<String, Object> map = this.mapper.readValue(response, LinkedHashMap.class); String basePath = requestUri.replace(Swagger2Controller.DEFAULT_URL,""); map.put("basePath",basePath); log.debug("Swagger-docs: rewritten Base URL with correct micro-service route: {}", basePath); return mapper.writeValueAsString(map); } } catch (IOException e){ log.error("Swagger-docs filter error", e); } return null; }
Example 7
Source File: ResponseFilter.java From demo-project with MIT License | 6 votes |
@Override public Object run(){ RequestContext ctx = RequestContext.getCurrentContext(); String id = ctx.getZuulRequestHeaders().get("id"); ctx.getResponse().addHeader("id", id); try { BufferedReader reader = new BufferedReader(new InputStreamReader(ctx.getResponseDataStream())); String response = reader.readLine(); LOGGER.info("响应为:{}", response); //写到输出流中,本来可以由zuul框架来操作,但是我们已经读取了输入流,zuul读不到数据了,所以要手动写响应到response ctx.getResponse().setHeader("Content-Type","application/json;charset=utf-8"); ctx.getResponse().getWriter().write(response); } catch (Exception e) { } return null; }
Example 8
Source File: ResponseFilter.java From demo-project with MIT License | 6 votes |
@Override public Object run(){ RequestContext ctx = RequestContext.getCurrentContext(); String id = ctx.getZuulRequestHeaders().get("id"); ctx.getResponse().addHeader("id", id); try { BufferedReader reader = new BufferedReader(new InputStreamReader(ctx.getResponseDataStream())); String response = reader.readLine(); LOGGER.info("响应为:{}", response); //写到输出流中,本来可以由zuul框架来操作,但是我们已经读取了输入流,zuul读不到数据了,所以要手动写响应到response ctx.getResponse().setHeader("Content-Type","application/json;charset=utf-8"); ctx.getResponse().getWriter().write(response); } catch (Exception e) { } return null; }
Example 9
Source File: SwaggerBasePathRewritingFilterTest.java From okta-jhipster-microservices-oauth-example with Apache License 2.0 | 6 votes |
@Test public void run_on_valid_response_gzip() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL); RequestContext context = RequestContext.getCurrentContext(); context.setRequest(request); MockHttpServletResponse response = new MockHttpServletResponse(); context.setResponseGZipped(true); context.setResponse(response); context.setResponseDataStream(new ByteArrayInputStream(gzipData("{\"basePath\":\"/\"}"))); filter.run(); assertEquals("UTF-8", response.getCharacterEncoding()); InputStream responseDataStream = new GZIPInputStream(context.getResponseDataStream()); String responseBody = IOUtils.toString(responseDataStream, StandardCharsets.UTF_8); assertEquals("{\"basePath\":\"/service1\"}", responseBody); }
Example 10
Source File: SwaggerBasePathRewritingFilterTest.java From flair-registry with Apache License 2.0 | 6 votes |
@Test public void run_on_valid_response_gzip() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL); RequestContext context = RequestContext.getCurrentContext(); context.setRequest(request); MockHttpServletResponse response = new MockHttpServletResponse(); context.setResponseGZipped(true); context.setResponse(response); context.setResponseDataStream(new ByteArrayInputStream(gzipData("{\"basePath\":\"/\"}"))); filter.run(); assertEquals("UTF-8", response.getCharacterEncoding()); InputStream responseDataStream = new GZIPInputStream(context.getResponseDataStream()); String responseBody = IOUtils.toString(responseDataStream, StandardCharsets.UTF_8); assertEquals("{\"basePath\":\"/service1\"}", responseBody); }
Example 11
Source File: CustomSendResponseFilter.java From heimdall with Apache License 2.0 | 5 votes |
@Override public boolean shouldFilter() { long startTime = System.currentTimeMillis(); RequestContext context = RequestContext.getCurrentContext(); boolean should = (!context.getZuulResponseHeaders().isEmpty() || context.getResponseDataStream() != null || context.getResponseBody() != null); // boolean should = super.shouldFilter(); long endTime = System.currentTimeMillis(); long duration = (endTime - startTime); detail.setTimeInMillisShould(duration); return should; }
Example 12
Source File: ModifyResponseBodyFilter.java From sample-zuul-filters with Apache License 2.0 | 5 votes |
public Object run() { try { RequestContext context = getCurrentContext(); InputStream stream = context.getResponseDataStream(); String body = StreamUtils.copyToString(stream, Charset.forName("UTF-8")); context.setResponseBody("Modified via setResponseBody(): "+body); } catch (IOException e) { rethrowRuntimeException(e); } return null; }
Example 13
Source File: ModifyResponseDataStreamFilter.java From sample-zuul-filters with Apache License 2.0 | 5 votes |
public Object run() { try { RequestContext context = getCurrentContext(); InputStream stream = context.getResponseDataStream(); String body = StreamUtils.copyToString(stream, Charset.forName("UTF-8")); body = "Modified via setResponseDataStream(): " + body; context.setResponseDataStream(new ByteArrayInputStream(body.getBytes("UTF-8"))); } catch (IOException e) { rethrowRuntimeException(e); } return null; }
Example 14
Source File: ResponseHelper.java From heimdall with Apache License 2.0 | 5 votes |
public static String getResponseBody(RequestContext context, Map<String, String> headers) throws Throwable { String content = headers.get(HttpHeaders.CONTENT_TYPE); String response = null; // if the content type is not defined by api server then permit to read the body. Prevent NPE if (content == null || content.isEmpty()) content = ""; String[] types = content.split(";"); if (!ContentTypeUtils.belongsToBlackList(types)) { try (InputStream stream = context.getResponseDataStream()) { response = StreamUtils.copyToString(stream, StandardCharsets.UTF_8); if (response.isEmpty() && context.getResponseBody() != null) { response = context.getResponseBody(); } if (Objects.isNull(response) || response.isEmpty()) { response = ""; } context.setResponseDataStream(new ByteArrayInputStream(response.getBytes(StandardCharsets.UTF_8))); } } return response; }
Example 15
Source File: DataFilter.java From xmfcn-spring-cloud with Apache License 2.0 | 5 votes |
/** * 修改输出数据流 */ private void modifyResponseBodyDataStream() { try { RequestContext context = getCurrentContext(); InputStream stream = context.getResponseDataStream(); String body = StreamUtils.copyToString(stream, Charset.forName("UTF-8")); body = "Modified via setResponseDataStream(): " + body; context.setResponseDataStream(new ByteArrayInputStream(body.getBytes("UTF-8"))); } catch (IOException e) { rethrowRuntimeException(e); } }
Example 16
Source File: DataFilter.java From xmfcn-spring-cloud with Apache License 2.0 | 5 votes |
/** * 修改输出数据 */ private void modifyResponseBody() { try { RequestContext context = getCurrentContext(); InputStream stream = context.getResponseDataStream(); String body = StreamUtils.copyToString(stream, Charset.forName("UTF-8")); context.setResponseBody("Modified via setResponseBody(): " + body); } catch (IOException e) { rethrowRuntimeException(e); } }
Example 17
Source File: ContentUrlRewritingFilter.java From moserp with Apache License 2.0 | 5 votes |
private String getResponseBody(RequestContext context) throws IOException { String responseData = null; if (context.getResponseBody() != null) { context.getResponse().setCharacterEncoding("UTF-8"); responseData = context.getResponseBody(); } else if (context.getResponseDataStream() != null) { context.getResponse().setCharacterEncoding("UTF-8"); try (final InputStream responseDataStream = context.getResponseDataStream()) { //FIXME What about character encoding of the stream (depends on the response content type)? responseData = CharStreams.toString(new InputStreamReader(responseDataStream)); } } return responseData; }
Example 18
Source File: BaseResponseFilter.java From convergent-ui with Apache License 2.0 | 5 votes |
@Override public boolean shouldFilter() { RequestContext ctx = RequestContext.getCurrentContext(); String contentType = getContentType(ctx); String verb = getVerb(ctx.getRequest()); return "text/html".equals(contentType) && "GET".equalsIgnoreCase(verb) && (!ctx.getZuulResponseHeaders().isEmpty() || ctx.getResponseDataStream() != null || ctx.getResponseBody() != null); }
Example 19
Source File: CacheUpdateFilter.java From ServiceComb-Company-WorkShop with Apache License 2.0 | 4 votes |
private boolean isSuccessfulFibonacciResponse(RequestContext context, String path) { return path.contains(pathInRequest()) && context.getResponseStatusCode() == SC_OK && context.sendZuulResponse() && (context.getResponseBody() != null || context.getResponseDataStream() != null); }