Java Code Examples for com.netflix.zuul.context.RequestContext#setResponseDataStream()

The following examples show how to use com.netflix.zuul.context.RequestContext#setResponseDataStream() . 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 flair-registry with Apache License 2.0 7 votes vote down vote up
@Override
public Object run() {
    RequestContext context = RequestContext.getCurrentContext();

    context.getResponse().setCharacterEncoding("UTF-8");

    String rewrittenResponse = rewriteBasePath(context);
    if (context.getResponseGZipped()) {
        try {
            context.setResponseDataStream(new ByteArrayInputStream(gzipData(rewrittenResponse)));
        } catch (IOException e) {
            log.error("Swagger-docs filter error", e);
        }
    } else {
        context.setResponseBody(rewrittenResponse);
    }
    return null;
}
 
Example 2
Source File: SwaggerBasePathRewritingFilterTest.java    From flair-registry with Apache License 2.0 6 votes vote down vote up
@Test
public void run_on_valid_response() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL);
    RequestContext context = RequestContext.getCurrentContext();
    context.setRequest(request);

    MockHttpServletResponse response = new MockHttpServletResponse();
    context.setResponseGZipped(false);
    context.setResponse(response);

    InputStream in = IOUtils.toInputStream("{\"basePath\":\"/\"}", StandardCharsets.UTF_8);
    context.setResponseDataStream(in);

    filter.run();

    assertEquals("UTF-8", response.getCharacterEncoding());
    assertEquals("{\"basePath\":\"/service1\"}", context.getResponseBody());
}
 
Example 3
Source File: SwaggerBasePathRewritingFilterTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void run_on_valid_response() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL);
    RequestContext context = RequestContext.getCurrentContext();
    context.setRequest(request);

    MockHttpServletResponse response = new MockHttpServletResponse();
    context.setResponseGZipped(false);
    context.setResponse(response);

    InputStream in = IOUtils.toInputStream("{\"basePath\":\"/\"}", StandardCharsets.UTF_8);
    context.setResponseDataStream(in);

    filter.run();

    assertEquals("UTF-8", response.getCharacterEncoding());
    assertEquals("{\"basePath\":\"/service1\"}", context.getResponseBody());
}
 
Example 4
Source File: SwaggerBasePathRewritingFilter.java    From jhipster-registry with Apache License 2.0 6 votes vote down vote up
@Override
public Object run() {
    RequestContext context = RequestContext.getCurrentContext();

    context.getResponse().setCharacterEncoding("UTF-8");

    String rewrittenResponse = rewriteBasePath(context);
    if (context.getResponseGZipped()) {
        try {
            context.setResponseDataStream(new ByteArrayInputStream(gzipData(rewrittenResponse)));
        } catch (IOException e) {
            log.error("Swagger-docs filter error", e);
        }
    } else {
        context.setResponseBody(rewrittenResponse);
    }
    return null;
}
 
Example 5
Source File: _SwaggerBasePathRewritingFilterTest.java    From jhipster-ribbon-hystrix with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void run_on_valid_response() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL);
    RequestContext context = RequestContext.getCurrentContext();
    context.setRequest(request);

    MockHttpServletResponse response = new MockHttpServletResponse();
    context.setResponse(response);

    InputStream in = IOUtils.toInputStream("{\"basePath\":\"/\"}");
    context.setResponseDataStream(in);

    filter.run();

    assertEquals("UTF-8", response.getCharacterEncoding());
    assertEquals("{\"basePath\":\"/service1\"}", context.getResponseBody());
}
 
Example 6
Source File: SwaggerBasePathRewritingFilterTest.java    From jhipster-ribbon-hystrix with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void run_on_valid_response() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL);
    RequestContext context = RequestContext.getCurrentContext();
    context.setRequest(request);

    MockHttpServletResponse response = new MockHttpServletResponse();
    context.setResponse(response);

    InputStream in = IOUtils.toInputStream("{\"basePath\":\"/\"}");
    context.setResponseDataStream(in);

    filter.run();

    assertEquals("UTF-8", response.getCharacterEncoding());
    assertEquals("{\"basePath\":\"/service1\"}", context.getResponseBody());
}
 
Example 7
Source File: SwaggerBasePathRewritingFilterTest.java    From jhipster-microservices-example with Apache License 2.0 6 votes vote down vote up
@Test
public void run_on_valid_response() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL);
    RequestContext context = RequestContext.getCurrentContext();
    context.setRequest(request);

    MockHttpServletResponse response = new MockHttpServletResponse();
    context.setResponseGZipped(false);
    context.setResponse(response);

    InputStream in = IOUtils.toInputStream("{\"basePath\":\"/\"}", StandardCharsets.UTF_8);
    context.setResponseDataStream(in);

    filter.run();

    assertEquals("UTF-8", response.getCharacterEncoding());
    assertEquals("{\"basePath\":\"/service1\"}", context.getResponseBody());
}
 
Example 8
Source File: SwaggerBasePathRewritingFilterTest.java    From e-commerce-microservice with Apache License 2.0 6 votes vote down vote up
@Test
public void run_on_valid_response() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL);
    RequestContext context = RequestContext.getCurrentContext();
    context.setRequest(request);

    MockHttpServletResponse response = new MockHttpServletResponse();
    context.setResponseGZipped(false);
    context.setResponse(response);

    InputStream in = IOUtils.toInputStream("{\"basePath\":\"/\"}", StandardCharsets.UTF_8);
    context.setResponseDataStream(in);

    filter.run();

    assertEquals("UTF-8", response.getCharacterEncoding());
    assertEquals("{\"basePath\":\"/service1\"}", context.getResponseBody());
}
 
Example 9
Source File: SwaggerBasePathRewritingFilter.java    From e-commerce-microservice with Apache License 2.0 6 votes vote down vote up
@Override
public Object run() {
    RequestContext context = RequestContext.getCurrentContext();

    context.getResponse().setCharacterEncoding("UTF-8");

    String rewrittenResponse = rewriteBasePath(context);
    if (context.getResponseGZipped()) {
        try {
            context.setResponseDataStream(new ByteArrayInputStream(gzipData(rewrittenResponse)));
        } catch (IOException e) {
            log.error("Swagger-docs filter error", e);
        }
    } else {
        context.setResponseBody(rewrittenResponse);
    }
    return null;
}
 
Example 10
Source File: SwaggerBasePathRewritingFilterTest.java    From okta-jhipster-microservices-oauth-example with Apache License 2.0 6 votes vote down vote up
@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: SwaggerBasePathRewritingFilterTest.java    From tutorials with MIT License 6 votes vote down vote up
@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 12
Source File: SwaggerBasePathRewritingFilter.java    From okta-jhipster-microservices-oauth-example with Apache License 2.0 6 votes vote down vote up
@Override
public Object run() {
    RequestContext context = RequestContext.getCurrentContext();

    context.getResponse().setCharacterEncoding("UTF-8");

    String rewrittenResponse = rewriteBasePath(context);
    if (context.getResponseGZipped()) {
        try {
            context.setResponseDataStream(new ByteArrayInputStream(gzipData(rewrittenResponse)));
        } catch (IOException e) {
            log.error("Swagger-docs filter error", e);
        }
    } else {
        context.setResponseBody(rewrittenResponse);
    }
    return null;
}
 
Example 13
Source File: SwaggerBasePathRewritingFilterTest.java    From cubeai with Apache License 2.0 6 votes vote down vote up
@Test
public void run_on_valid_response() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL);
    RequestContext context = RequestContext.getCurrentContext();
    context.setRequest(request);

    MockHttpServletResponse response = new MockHttpServletResponse();
    context.setResponseGZipped(false);
    context.setResponse(response);

    InputStream in = IOUtils.toInputStream("{\"basePath\":\"/\"}", StandardCharsets.UTF_8);
    context.setResponseDataStream(in);

    filter.run();

    assertEquals("UTF-8", response.getCharacterEncoding());
    assertEquals("{\"basePath\":\"/service1\"}", context.getResponseBody());
}
 
Example 14
Source File: SwaggerBasePathRewritingFilter.java    From tutorials with MIT License 6 votes vote down vote up
@Override
public Object run() {
    RequestContext context = RequestContext.getCurrentContext();

    context.getResponse().setCharacterEncoding("UTF-8");

    String rewrittenResponse = rewriteBasePath(context);
    if (context.getResponseGZipped()) {
        try {
            context.setResponseDataStream(new ByteArrayInputStream(gzipData(rewrittenResponse)));
        } catch (IOException e) {
            log.error("Swagger-docs filter error", e);
        }
    } else {
        context.setResponseBody(rewrittenResponse);
    }
    return null;
}
 
Example 15
Source File: SwaggerBasePathRewritingFilterTest.java    From java-microservices-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void run_on_valid_response() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL);
    RequestContext context = RequestContext.getCurrentContext();
    context.setRequest(request);

    MockHttpServletResponse response = new MockHttpServletResponse();
    context.setResponseGZipped(false);
    context.setResponse(response);

    InputStream in = IOUtils.toInputStream("{\"basePath\":\"/\"}", StandardCharsets.UTF_8);
    context.setResponseDataStream(in);

    filter.run();

    assertEquals("UTF-8", response.getCharacterEncoding());
    assertEquals("{\"basePath\":\"/service1\"}", context.getResponseBody());
}
 
Example 16
Source File: SwaggerBasePathRewritingFilter.java    From java-microservices-examples with Apache License 2.0 6 votes vote down vote up
@Override
public Object run() {
    RequestContext context = RequestContext.getCurrentContext();

    context.getResponse().setCharacterEncoding("UTF-8");

    String rewrittenResponse = rewriteBasePath(context);
    if (context.getResponseGZipped()) {
        try {
            context.setResponseDataStream(new ByteArrayInputStream(gzipData(rewrittenResponse)));
        } catch (IOException e) {
            log.error("Swagger-docs filter error", e);
        }
    } else {
        context.setResponseBody(rewrittenResponse);
    }
    return null;
}
 
Example 17
Source File: SwaggerBasePathRewritingFilterTest.java    From flair-registry with Apache License 2.0 6 votes vote down vote up
@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 18
Source File: DataFilter.java    From xmfcn-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * 修改输出数据流
 */
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 19
Source File: ResponseHelper.java    From heimdall with Apache License 2.0 5 votes vote down vote up
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 20
Source File: ModifyResponseDataStreamFilter.java    From sample-zuul-filters with Apache License 2.0 5 votes vote down vote up
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;
}