return output.toByteArray();
}
@Override
protected void channelRead0(final ChannelHandlerContext ctx, final FullHttpRequest fullHttpRequest) throws Exception {
final ChannelFuture future;
if (HttpMethod.POST.equals(fullHttpRequest.getMethod())) {
final InputStream is =
new ByteArrayInputStream(gzipCompression(fullHttpRequest.content().toString(Charset.defaultCharset()).getBytes()));
final DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final PrintWriter writer = new PrintWriter(baos);
collector.doPost(HttpServletRequest.class.cast(Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[]{HttpServletRequest.class}, new InvocationHandler() {
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
if (Object.class.equals(method.getDeclaringClass())) {
return method.invoke(this, args);
}
if ("getInputStream".equals(method.getName())) {
return new ServletInputStream() {
@Override
public int read() throws IOException {
return is.read();
}
};
}
if ("getHeader".equals( method.getName()) && args[0].equals( "Content-Encoding" )) {
return "gzip";
}
if ("getHeader".equals( method.getName()) && args[0].equals( "Content-Type" )) {
return "foo";
}
throw new UnsupportedOperationException("not implemented: " + method.getName() + " for args: " + Arrays.asList(args));
}
})),
HttpServletResponse.class.cast(Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { HttpServletResponse.class}, new InvocationHandler() {
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
if (Object.class.equals(method.getDeclaringClass())) {
return method.invoke(this, args);
}
final String name = method.getName();
if ("setStatus".equals(name)) {
response.setStatus(HttpResponseStatus.valueOf(Integer.class.cast(args[0])));
return null;
} else if ("getWriter".equals(name)) {
return writer;
}
throw new UnsupportedOperationException("not implemented");
}
})));
response.content().writeBytes(baos.toByteArray());
future = ctx.writeAndFlush(response);
} else {
LOGGER.warning("Received " + fullHttpRequest.getMethod());
future = ctx.writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR));
}
future.addListener(ChannelFutureListener.CLOSE);
}