use of io.netty.buffer.ByteBuf in project camel by apache.
the class DatagramPacketObjectEncoder method encode.
@Override
protected void encode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg, List<Object> out) throws Exception {
if (msg.content() instanceof Serializable) {
Serializable payload = (Serializable) msg.content();
ByteBuf buf = ctx.alloc().buffer();
delegateObjectEncoder.encode(ctx, payload, buf);
AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(buf, msg.recipient(), msg.sender());
out.add(addressedEnvelop);
}
}
use of io.netty.buffer.ByteBuf in project camel by apache.
the class DatagramPacketStringDecoder method decode.
@Override
protected void decode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg, List<Object> out) throws Exception {
if (msg.content() instanceof ByteBuf) {
ByteBuf payload = (ByteBuf) msg.content();
AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(payload.toString(charset), msg.recipient(), msg.sender());
out.add(addressedEnvelop);
}
}
use of io.netty.buffer.ByteBuf in project ratpack by ratpack.
the class NettyHandlerAdapter method newRequest.
private void newRequest(final ChannelHandlerContext ctx, final HttpRequest nettyRequest) throws Exception {
if (!nettyRequest.decoderResult().isSuccess()) {
sendError(ctx, HttpResponseStatus.BAD_REQUEST);
return;
}
Headers requestHeaders = new NettyHeadersBackedHeaders(nettyRequest.headers());
//Find the content length we will use this as an indicator of a body
Long contentLength = HttpUtil.getContentLength(nettyRequest, -1L);
String transferEncoding = requestHeaders.get(HttpHeaderNames.TRANSFER_ENCODING);
//If there is a content length or transfer encoding that indicates there is a body
boolean hasBody = (contentLength > 0) || (transferEncoding != null);
RequestBody requestBody = hasBody ? new RequestBody(contentLength, nettyRequest, ctx) : null;
final Channel channel = ctx.channel();
if (requestBody != null) {
channel.attr(BODY_ACCUMULATOR_KEY).set(requestBody);
}
InetSocketAddress remoteAddress = (InetSocketAddress) channel.remoteAddress();
InetSocketAddress socketAddress = (InetSocketAddress) channel.localAddress();
final DefaultRequest request = new DefaultRequest(Instant.now(), requestHeaders, nettyRequest.method(), nettyRequest.protocolVersion(), nettyRequest.uri(), remoteAddress, socketAddress, serverRegistry.get(ServerConfig.class), requestBody);
final HttpHeaders nettyHeaders = new DefaultHttpHeaders(false);
final MutableHeaders responseHeaders = new NettyHeadersBackedMutableHeaders(nettyHeaders);
final AtomicBoolean transmitted = new AtomicBoolean(false);
final DefaultResponseTransmitter responseTransmitter = new DefaultResponseTransmitter(transmitted, channel, nettyRequest, request, nettyHeaders, requestBody);
ctx.channel().attr(DefaultResponseTransmitter.ATTRIBUTE_KEY).set(responseTransmitter);
Action<Action<Object>> subscribeHandler = thing -> {
transmitted.set(true);
ctx.channel().attr(CHANNEL_SUBSCRIBER_ATTRIBUTE_KEY).set(thing);
};
final DefaultContext.RequestConstants requestConstants = new DefaultContext.RequestConstants(applicationConstants, request, channel, responseTransmitter, subscribeHandler);
final Response response = new DefaultResponse(responseHeaders, ctx.alloc(), responseTransmitter);
requestConstants.response = response;
DefaultContext.start(channel.eventLoop(), requestConstants, serverRegistry, handlers, execution -> {
if (requestBody != null) {
requestBody.close();
channel.attr(BODY_ACCUMULATOR_KEY).set(null);
}
if (!transmitted.get()) {
Handler lastHandler = requestConstants.handler;
StringBuilder description = new StringBuilder();
description.append("No response sent for ").append(request.getMethod().getName()).append(" request to ").append(request.getUri());
if (lastHandler != null) {
description.append(" (last handler: ");
if (lastHandler instanceof DescribingHandler) {
((DescribingHandler) lastHandler).describeTo(description);
} else {
DescribingHandlers.describeTo(lastHandler, description);
}
description.append(")");
}
String message = description.toString();
LOGGER.warn(message);
response.getHeaders().clear();
ByteBuf body;
if (development) {
CharBuffer charBuffer = CharBuffer.wrap(message);
body = ByteBufUtil.encodeString(ctx.alloc(), charBuffer, CharsetUtil.UTF_8);
response.contentType(HttpHeaderConstants.PLAIN_TEXT_UTF8);
} else {
body = Unpooled.EMPTY_BUFFER;
}
response.getHeaders().set(HttpHeaderConstants.CONTENT_LENGTH, body.readableBytes());
responseTransmitter.transmit(HttpResponseStatus.INTERNAL_SERVER_ERROR, body);
}
});
}
use of io.netty.buffer.ByteBuf in project ratpack by ratpack.
the class JsonRenderer method render.
@Override
public void render(Context ctx, JsonRender object) throws Exception {
ObjectWriter writer = object.getObjectWriter();
if (writer == null) {
writer = ctx.maybeGet(ObjectWriter.class).orElseGet(() -> ctx.get(ObjectMapper.class).writer());
}
Class<?> viewClass = object.getViewClass();
if (viewClass != null) {
writer = writer.withView(viewClass);
}
ByteBuf buffer = ctx.get(ByteBufAllocator.class).buffer();
OutputStream outputStream = new ByteBufOutputStream(buffer);
try {
writer.writeValue(outputStream, object.getObject());
} catch (JsonProcessingException e) {
buffer.release();
ctx.error(e);
return;
}
ctx.getResponse().contentTypeIfNotSet(HttpHeaderConstants.JSON).send(buffer);
}
use of io.netty.buffer.ByteBuf in project ratpack by ratpack.
the class ContentAggregatingRequestAction method addResponseHandlers.
@Override
protected void addResponseHandlers(ChannelPipeline p, Downstream<? super ReceivedResponse> downstream) {
p.addLast(AGGREGATOR_HANDLER_NAME, new HttpObjectAggregator(requestConfig.maxContentLength));
p.addLast(RESPONSE_HANDLER_NAME, new SimpleChannelInboundHandler<FullHttpResponse>(false) {
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse response) throws Exception {
response.touch();
dispose(ctx.pipeline(), response);
ByteBuf content = response.content();
execution.onComplete(() -> {
if (content.refCnt() > 0) {
content.release();
}
});
success(downstream, toReceivedResponse(response, content));
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
forceDispose(ctx.pipeline());
error(downstream, cause);
}
});
}
Aggregations