use of io.netty.handler.codec.http.LastHttpContent in project cdap by caskdata.
the class AuditLogHandler method write.
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (logEntry == null) {
ctx.write(msg, promise);
return;
}
if (msg instanceof HttpResponse) {
HttpResponse response = (HttpResponse) msg;
logEntry.setResponse(response);
// If no need to log the response body, we can emit the audit log
if (!logResponseBody) {
emitAuditLog();
}
} else if (msg instanceof HttpContent && logResponseBody) {
ByteBuf content = ((HttpContent) msg).content();
if (content.isReadable()) {
logEntry.appendResponseBody(content.toString(StandardCharsets.UTF_8));
}
// If need to log the response body, emit the audit log when all response contents are received
if (msg instanceof LastHttpContent) {
emitAuditLog();
}
}
ctx.write(msg, promise);
}
use of io.netty.handler.codec.http.LastHttpContent in project sidewinder by srotya.
the class HTTPDataPointDecoder method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
try {
if (ResourceMonitor.getInstance().isReject()) {
logger.warning("Write rejected, insufficient memory");
if (writeResponse(request, ctx)) {
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
return;
}
if (msg instanceof HttpRequest) {
HttpRequest request = this.request = (HttpRequest) msg;
if (HttpUtil.is100ContinueExpected(request)) {
send100Continue(ctx);
}
QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.uri());
path = queryStringDecoder.path();
Map<String, List<String>> params = queryStringDecoder.parameters();
if (!params.isEmpty()) {
for (Entry<String, List<String>> p : params.entrySet()) {
String key = p.getKey();
if (key.equalsIgnoreCase("db")) {
dbName = p.getValue().get(0);
}
}
}
if (path != null && path.contains("query")) {
Gson gson = new Gson();
JsonObject obj = new JsonObject();
JsonArray ary = new JsonArray();
ary.add(new JsonObject());
obj.add("results", ary);
responseString.append(gson.toJson(obj));
}
}
if (msg instanceof HttpContent) {
HttpContent httpContent = (HttpContent) msg;
ByteBuf byteBuf = httpContent.content();
if (byteBuf.isReadable()) {
requestBuffer.append(byteBuf.toString(CharsetUtil.UTF_8));
}
if (msg instanceof LastHttpContent) {
if (dbName == null) {
responseString.append("Invalid database null");
logger.severe("Invalid database null");
} else {
String payload = requestBuffer.toString();
logger.fine("Request:" + payload);
List<Point> dps = InfluxDecoder.pointsFromString(dbName, payload);
meter.inc(dps.size());
for (Point dp : dps) {
try {
engine.writeDataPoint(dp);
logger.fine("Accepted:" + dp + "\t" + new Date(dp.getTimestamp()));
} catch (IOException e) {
logger.fine("Dropped:" + dp + "\t" + e.getMessage());
responseString.append("Dropped:" + dp);
}
}
}
if (writeResponse(request, ctx)) {
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
use of io.netty.handler.codec.http.LastHttpContent in project reactor-netty by reactor.
the class HttpClientOperationsTest method addEncoderReplaysLastHttp.
@Test
public void addEncoderReplaysLastHttp() throws Exception {
ByteBuf buf = Unpooled.copiedBuffer("{\"foo\":1}", CharsetUtil.UTF_8);
EmbeddedChannel channel = new EmbeddedChannel();
HttpClientOperations ops = new HttpClientOperations(channel, (response, request) -> null, handler);
ops.addHandler(new JsonObjectDecoder());
channel.writeInbound(new DefaultLastHttpContent(buf));
assertThat(channel.pipeline().names().iterator().next(), is("JsonObjectDecoder$extractor"));
Object content = channel.readInbound();
assertThat(content, instanceOf(ByteBuf.class));
((ByteBuf) content).release();
content = channel.readInbound();
assertThat(content, instanceOf(LastHttpContent.class));
((LastHttpContent) content).release();
assertThat(channel.readInbound(), nullValue());
}
use of io.netty.handler.codec.http.LastHttpContent in project reactor-netty by reactor.
the class HttpClientOperationsTest method addDecoderReplaysLastHttp.
@Test
public void addDecoderReplaysLastHttp() throws Exception {
ByteBuf buf = Unpooled.copiedBuffer("{\"foo\":1}", CharsetUtil.UTF_8);
EmbeddedChannel channel = new EmbeddedChannel();
HttpClientOperations ops = new HttpClientOperations(channel, (response, request) -> null, handler);
ops.addHandler(new JsonObjectDecoder());
channel.writeInbound(new DefaultLastHttpContent(buf));
assertThat(channel.pipeline().names().iterator().next(), is("JsonObjectDecoder$extractor"));
Object content = channel.readInbound();
assertThat(content, instanceOf(ByteBuf.class));
((ByteBuf) content).release();
content = channel.readInbound();
assertThat(content, instanceOf(LastHttpContent.class));
((LastHttpContent) content).release();
assertThat(channel.readInbound(), nullValue());
}
use of io.netty.handler.codec.http.LastHttpContent in project flink by apache.
the class HttpRequestHandler method channelRead0.
@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
try {
if (msg instanceof HttpRequest) {
currentRequest = (HttpRequest) msg;
currentRequestPath = null;
if (currentDecoder != null) {
currentDecoder.destroy();
currentDecoder = null;
}
if (currentRequest.getMethod() == HttpMethod.GET || currentRequest.getMethod() == HttpMethod.DELETE) {
// directly delegate to the router
ctx.fireChannelRead(currentRequest);
} else if (currentRequest.getMethod() == HttpMethod.POST) {
// POST comes in multiple objects. First the request, then the contents
// keep the request and path for the remaining objects of the POST request
currentRequestPath = new QueryStringDecoder(currentRequest.getUri(), ENCODING).path();
currentDecoder = new HttpPostRequestDecoder(DATA_FACTORY, currentRequest, ENCODING);
} else {
throw new IOException("Unsupported HTTP method: " + currentRequest.getMethod().name());
}
} else if (currentDecoder != null && msg instanceof HttpContent) {
// received new chunk, give it to the current decoder
HttpContent chunk = (HttpContent) msg;
currentDecoder.offer(chunk);
try {
while (currentDecoder.hasNext()) {
InterfaceHttpData data = currentDecoder.next();
if (data.getHttpDataType() == HttpDataType.FileUpload && tmpDir != null) {
DiskFileUpload file = (DiskFileUpload) data;
if (file.isCompleted()) {
String name = file.getFilename();
File target = new File(tmpDir, UUID.randomUUID() + "_" + name);
if (!tmpDir.exists()) {
logExternalUploadDirDeletion(tmpDir);
checkAndCreateUploadDir(tmpDir);
}
file.renameTo(target);
QueryStringEncoder encoder = new QueryStringEncoder(currentRequestPath);
encoder.addParam("filepath", target.getAbsolutePath());
encoder.addParam("filename", name);
currentRequest.setUri(encoder.toString());
}
}
}
} catch (EndOfDataDecoderException ignored) {
}
if (chunk instanceof LastHttpContent) {
HttpRequest request = currentRequest;
currentRequest = null;
currentRequestPath = null;
currentDecoder.destroy();
currentDecoder = null;
// fire next channel handler
ctx.fireChannelRead(request);
}
} else {
ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
}
} catch (Throwable t) {
currentRequest = null;
currentRequestPath = null;
if (currentDecoder != null) {
currentDecoder.destroy();
currentDecoder = null;
}
if (ctx.channel().isActive()) {
byte[] bytes = ExceptionUtils.stringifyException(t).getBytes(ENCODING);
DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR, Unpooled.wrappedBuffer(bytes));
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
ctx.writeAndFlush(response);
}
}
}
Aggregations