use of io.netty.buffer.ByteBuf in project camel by apache.
the class LumberjackFrameDecoder method handleCompressedFrame.
private boolean handleCompressedFrame(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if (!in.isReadable(FRAME_COMPRESS_HEADER_LENGTH)) {
return false;
}
int compressedPayloadLength = in.readInt();
if (!in.isReadable(compressedPayloadLength)) {
return false;
}
// decompress payload
Inflater inflater = new Inflater();
if (in.hasArray()) {
inflater.setInput(in.array(), in.arrayOffset() + in.readerIndex(), compressedPayloadLength);
in.skipBytes(compressedPayloadLength);
} else {
byte[] array = new byte[compressedPayloadLength];
in.readBytes(array);
inflater.setInput(array);
}
while (!inflater.finished()) {
ByteBuf decompressed = ctx.alloc().heapBuffer(1024, 1024);
byte[] outArray = decompressed.array();
int count = inflater.inflate(outArray, decompressed.arrayOffset(), decompressed.writableBytes());
decompressed.writerIndex(count);
// put data in the pipeline
out.add(decompressed);
}
return true;
}
use of io.netty.buffer.ByteBuf in project flink by apache.
the class RuntimeMonitorHandler method respondAsLeader.
@Override
protected void respondAsLeader(ChannelHandlerContext ctx, Routed routed, ActorGateway jobManager) {
FullHttpResponse response;
try {
// we only pass the first element in the list to the handlers.
Map<String, String> queryParams = new HashMap<>();
for (String key : routed.queryParams().keySet()) {
queryParams.put(key, routed.queryParam(key));
}
Map<String, String> pathParams = new HashMap<>(routed.pathParams().size());
for (String key : routed.pathParams().keySet()) {
pathParams.put(key, URLDecoder.decode(routed.pathParams().get(key), ENCODING.toString()));
}
InetSocketAddress address = (InetSocketAddress) ctx.channel().localAddress();
queryParams.put(WEB_MONITOR_ADDRESS_KEY, (httpsEnabled ? "https://" : "http://") + address.getHostName() + ":" + address.getPort());
response = handler.handleRequest(pathParams, queryParams, jobManager);
} catch (NotFoundException e) {
// this should result in a 404 error code (not found)
ByteBuf message = e.getMessage() == null ? Unpooled.buffer(0) : Unpooled.wrappedBuffer(e.getMessage().getBytes(ENCODING));
response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND, message);
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
LOG.debug("Error while handling request", e);
} catch (Exception e) {
byte[] bytes = ExceptionUtils.stringifyException(e).getBytes(ENCODING);
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());
LOG.debug("Error while handling request", e);
}
response.headers().set(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
// Content-Encoding:utf-8
response.headers().set(HttpHeaders.Names.CONTENT_ENCODING, ENCODING.name());
KeepAliveWrite.flush(ctx, routed.request(), response);
}
use of io.netty.buffer.ByteBuf in project flink by apache.
the class KvStateRequestSerializer method serializeKvStateRequestFailure.
/**
* Allocates a buffer and serializes the KvState request failure into it.
*
* @param alloc ByteBuf allocator for the buffer to serialize message into
* @param requestId ID of the request responding to
* @param cause Failure cause
* @return Serialized KvState request failure message
* @throws IOException Serialization failures are forwarded
*/
public static ByteBuf serializeKvStateRequestFailure(ByteBufAllocator alloc, long requestId, Throwable cause) throws IOException {
ByteBuf buf = alloc.ioBuffer();
// Frame length is set at the end
buf.writeInt(0);
writeHeader(buf, KvStateRequestType.REQUEST_FAILURE);
// Message
buf.writeLong(requestId);
try (ByteBufOutputStream bbos = new ByteBufOutputStream(buf);
ObjectOutputStream out = new ObjectOutputStream(bbos)) {
out.writeObject(cause);
}
// Set frame length
int frameLength = buf.readableBytes() - 4;
buf.setInt(0, frameLength);
return buf;
}
use of io.netty.buffer.ByteBuf in project flink by apache.
the class KvStateRequestSerializer method serializeKvStateRequest.
// ------------------------------------------------------------------------
// Serialization
// ------------------------------------------------------------------------
/**
* Allocates a buffer and serializes the KvState request into it.
*
* @param alloc ByteBuf allocator for the buffer to
* serialize message into
* @param requestId ID for this request
* @param kvStateId ID of the requested KvState instance
* @param serializedKeyAndNamespace Serialized key and namespace to request
* from the KvState instance.
* @return Serialized KvState request message
*/
public static ByteBuf serializeKvStateRequest(ByteBufAllocator alloc, long requestId, KvStateID kvStateId, byte[] serializedKeyAndNamespace) {
// Header + request ID + KvState ID + Serialized namespace
int frameLength = HEADER_LENGTH + 8 + (8 + 8) + (4 + serializedKeyAndNamespace.length);
// +4 for frame length
ByteBuf buf = alloc.ioBuffer(frameLength + 4);
buf.writeInt(frameLength);
writeHeader(buf, KvStateRequestType.REQUEST);
buf.writeLong(requestId);
buf.writeLong(kvStateId.getLowerPart());
buf.writeLong(kvStateId.getUpperPart());
buf.writeInt(serializedKeyAndNamespace.length);
buf.writeBytes(serializedKeyAndNamespace);
return buf;
}
use of io.netty.buffer.ByteBuf in project flink by apache.
the class KvStateRequestSerializer method serializeKvStateRequestResult.
/**
* Allocates a buffer and serializes the KvState request result into it.
*
* @param alloc ByteBuf allocator for the buffer to serialize message into
* @param requestId ID for this request
* @param serializedResult Serialized Result
* @return Serialized KvState request result message
*/
public static ByteBuf serializeKvStateRequestResult(ByteBufAllocator alloc, long requestId, byte[] serializedResult) {
Preconditions.checkNotNull(serializedResult, "Serialized result");
// Header + request ID + serialized result
int frameLength = HEADER_LENGTH + 8 + 4 + serializedResult.length;
ByteBuf buf = alloc.ioBuffer(frameLength);
buf.writeInt(frameLength);
writeHeader(buf, KvStateRequestType.REQUEST_RESULT);
buf.writeLong(requestId);
buf.writeInt(serializedResult.length);
buf.writeBytes(serializedResult);
return buf;
}
Aggregations