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());
}
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());
}
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);
}
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());
}
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;
}
Aggregations