Search in sources :

Example 6 with HttpResponseMessage

use of com.netflix.zuul.message.http.HttpResponseMessage in project zuul by Netflix.

the class GZipResponseFilterTest method prepareResponseBody_NeedsGZipping_gzipDeflate.

@Test
public void prepareResponseBody_NeedsGZipping_gzipDeflate() throws Exception {
    originalRequestHeaders.set("Accept-Encoding", "gzip,deflate");
    byte[] originBody = "blah".getBytes();
    response.getHeaders().set("Content-Length", Integer.toString(originBody.length));
    // Force GZip for small response
    Mockito.when(filter.isRightSizeForGzip(response)).thenReturn(true);
    response.setHasBody(true);
    assertTrue(filter.shouldFilter(response));
    final HttpResponseMessage result = filter.apply(response);
    final HttpContent hc1 = filter.processContentChunk(response, new DefaultHttpContent(Unpooled.wrappedBuffer(originBody)).retain());
    final HttpContent hc2 = filter.processContentChunk(response, new DefaultLastHttpContent());
    final byte[] body = new byte[hc1.content().readableBytes() + hc2.content().readableBytes()];
    final int hc1Len = hc1.content().readableBytes();
    final int hc2Len = hc2.content().readableBytes();
    hc1.content().readBytes(body, 0, hc1Len);
    hc2.content().readBytes(body, hc1Len, hc2Len);
    String bodyStr;
    // Check body is a gzipped version of the origin body.
    try (ByteArrayInputStream bais = new ByteArrayInputStream(body);
        GZIPInputStream gzis = new GZIPInputStream(bais);
        ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        int b;
        while ((b = gzis.read()) != -1) {
            baos.write(b);
        }
        bodyStr = baos.toString("UTF-8");
    }
    assertEquals("blah", bodyStr);
    assertEquals("gzip", result.getHeaders().getFirst("Content-Encoding"));
    // Check Content-Length header has been removed
    assertEquals(0, result.getHeaders().getAll("Content-Length").size());
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) HttpResponseMessage(com.netflix.zuul.message.http.HttpResponseMessage) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Test(org.junit.Test)

Example 7 with HttpResponseMessage

use of com.netflix.zuul.message.http.HttpResponseMessage in project zuul by Netflix.

the class GZipResponseFilterTest method prepareResponseBody_NeedsGZipping.

@Test
public void prepareResponseBody_NeedsGZipping() throws Exception {
    originalRequestHeaders.set("Accept-Encoding", "gzip");
    byte[] originBody = "blah".getBytes();
    response.getHeaders().set("Content-Length", Integer.toString(originBody.length));
    // Force GZip for small response
    Mockito.when(filter.isRightSizeForGzip(response)).thenReturn(true);
    response.setHasBody(true);
    assertTrue(filter.shouldFilter(response));
    final HttpResponseMessage result = filter.apply(response);
    final HttpContent hc1 = filter.processContentChunk(response, new DefaultHttpContent(Unpooled.wrappedBuffer(originBody)).retain());
    final HttpContent hc2 = filter.processContentChunk(response, new DefaultLastHttpContent());
    final byte[] body = new byte[hc1.content().readableBytes() + hc2.content().readableBytes()];
    final int hc1Len = hc1.content().readableBytes();
    final int hc2Len = hc2.content().readableBytes();
    hc1.content().readBytes(body, 0, hc1Len);
    hc2.content().readBytes(body, hc1Len, hc2Len);
    String bodyStr;
    // Check body is a gzipped version of the origin body.
    try (ByteArrayInputStream bais = new ByteArrayInputStream(body);
        GZIPInputStream gzis = new GZIPInputStream(bais);
        ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        int b;
        while ((b = gzis.read()) != -1) {
            baos.write(b);
        }
        bodyStr = baos.toString("UTF-8");
    }
    assertEquals("blah", bodyStr);
    assertEquals("gzip", result.getHeaders().getFirst("Content-Encoding"));
    // Check Content-Length header has been removed
    assertEquals(0, result.getHeaders().getAll("Content-Length").size());
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) HttpResponseMessage(com.netflix.zuul.message.http.HttpResponseMessage) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Test(org.junit.Test)

Example 8 with HttpResponseMessage

use of com.netflix.zuul.message.http.HttpResponseMessage in project zuul by Netflix.

the class BaseZuulFilterRunner method handleException.

protected void handleException(final ZuulMessage zuulMesg, final String filterName, final Exception ex) {
    HttpRequestInfo zuulReq = null;
    if (zuulMesg instanceof HttpRequestMessage) {
        zuulReq = (HttpRequestMessage) zuulMesg;
    } else if (zuulMesg instanceof HttpResponseMessage) {
        zuulReq = ((HttpResponseMessage) zuulMesg).getInboundRequest();
    }
    final String path = (zuulReq != null) ? zuulReq.getPathAndQuery() : "-";
    final String method = (zuulReq != null) ? zuulReq.getMethod() : "-";
    final String errMesg = "Error with filter: " + filterName + ", path: " + path + ", method: " + method;
    logger.error(errMesg, ex);
    getChannelHandlerContext(zuulMesg).fireExceptionCaught(ex);
}
Also used : HttpResponseMessage(com.netflix.zuul.message.http.HttpResponseMessage) HttpRequestMessage(com.netflix.zuul.message.http.HttpRequestMessage) HttpRequestInfo(com.netflix.zuul.message.http.HttpRequestInfo)

Example 9 with HttpResponseMessage

use of com.netflix.zuul.message.http.HttpResponseMessage in project zuul by Netflix.

the class BaseZuulFilterRunner method addPerfMarkTags.

protected final void addPerfMarkTags(ZuulMessage inMesg) {
    HttpRequestInfo req = null;
    if (inMesg instanceof HttpRequestInfo) {
        req = (HttpRequestInfo) inMesg;
    }
    if (inMesg instanceof HttpResponseMessage) {
        HttpResponseMessage msg = (HttpResponseMessage) inMesg;
        req = msg.getOutboundRequest();
        attachTag("statuscode", msg.getStatus());
    }
    if (req != null) {
        attachTag("path", req, HttpRequestInfo::getPath);
        attachTag("originalhost", req, HttpRequestInfo::getOriginalHost);
    }
    attachTag("uuid", inMesg, m -> m.getContext().getUUID());
}
Also used : HttpResponseMessage(com.netflix.zuul.message.http.HttpResponseMessage) HttpRequestInfo(com.netflix.zuul.message.http.HttpRequestInfo)

Example 10 with HttpResponseMessage

use of com.netflix.zuul.message.http.HttpResponseMessage in project zuul by Netflix.

the class ZuulEndPointRunner method getEndpoint.

protected ZuulFilter<HttpRequestMessage, HttpResponseMessage> getEndpoint(final String endpointName, final HttpRequestMessage zuulRequest) {
    final SessionContext zuulCtx = zuulRequest.getContext();
    if (zuulCtx.getStaticResponse() != null) {
        return STATIC_RESPONSE_ENDPOINT;
    }
    if (endpointName == null) {
        return new MissingEndpointHandlingFilter("NO_ENDPOINT_NAME");
    }
    if (PROXY_ENDPOINT_FILTER_NAME.equals(endpointName)) {
        return newProxyEndpoint(zuulRequest);
    }
    final Endpoint<HttpRequestMessage, HttpResponseMessage> filter = getEndpointFilter(endpointName);
    if (filter == null) {
        return new MissingEndpointHandlingFilter(endpointName);
    }
    return filter;
}
Also used : HttpResponseMessage(com.netflix.zuul.message.http.HttpResponseMessage) MissingEndpointHandlingFilter(com.netflix.zuul.filters.endpoint.MissingEndpointHandlingFilter) HttpRequestMessage(com.netflix.zuul.message.http.HttpRequestMessage) SessionContext(com.netflix.zuul.context.SessionContext)

Aggregations

HttpResponseMessage (com.netflix.zuul.message.http.HttpResponseMessage)14 HttpRequestMessage (com.netflix.zuul.message.http.HttpRequestMessage)8 SessionContext (com.netflix.zuul.context.SessionContext)5 HttpContent (io.netty.handler.codec.http.HttpContent)5 Headers (com.netflix.zuul.message.Headers)3 HttpResponseMessageImpl (com.netflix.zuul.message.http.HttpResponseMessageImpl)3 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)3 TaskCloseable (io.perfmark.TaskCloseable)3 Test (org.junit.Test)3 ProxyEndpoint (com.netflix.zuul.filters.endpoint.ProxyEndpoint)2 HttpRequestInfo (com.netflix.zuul.message.http.HttpRequestInfo)2 CurrentPassport (com.netflix.zuul.passport.CurrentPassport)2 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 ClientException (com.netflix.client.ClientException)1 ZuulException (com.netflix.zuul.exception.ZuulException)1 ZuulFilter (com.netflix.zuul.filters.ZuulFilter)1 MissingEndpointHandlingFilter (com.netflix.zuul.filters.endpoint.MissingEndpointHandlingFilter)1