use of io.netty.buffer.ByteBufInputStream in project cdap by caskdata.
the class PreviewHttpHandler method start.
@POST
@Path("/previews")
public void start(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId) throws Exception {
NamespaceId namespace = new NamespaceId(namespaceId);
AppRequest appRequest;
try (Reader reader = new InputStreamReader(new ByteBufInputStream(request.content()), StandardCharsets.UTF_8)) {
appRequest = GSON.fromJson(reader, AppRequest.class);
} catch (JsonSyntaxException e) {
throw new BadRequestException("Request body is invalid json: " + e.getMessage());
}
responder.sendString(HttpResponseStatus.OK, GSON.toJson(previewManager.start(namespace, appRequest)));
}
use of io.netty.buffer.ByteBufInputStream in project cdap by caskdata.
the class MetadataHttpHandler method readMetadata.
private Map<String, String> readMetadata(FullHttpRequest request) throws BadRequestException {
ByteBuf content = request.content();
if (!content.isReadable()) {
throw new BadRequestException("Unable to read metadata properties from the request.");
}
Map<String, String> metadata;
try (Reader reader = new InputStreamReader(new ByteBufInputStream(content), StandardCharsets.UTF_8)) {
metadata = GSON.fromJson(reader, MAP_STRING_STRING_TYPE);
} catch (IOException e) {
throw new BadRequestException("Unable to read metadata properties from the request.", e);
}
if (metadata == null) {
throw new BadRequestException("Null metadata was read from the request");
}
return metadata;
}
use of io.netty.buffer.ByteBufInputStream in project jocean-http by isdom.
the class RxNettys method httpObjectsAsBytes.
public static byte[] httpObjectsAsBytes(final Iterator<HttpObject> itr) throws IOException {
final CompositeByteBuf composite = Unpooled.compositeBuffer();
try {
while (itr.hasNext()) {
final HttpObject obj = itr.next();
if (obj instanceof HttpContent) {
composite.addComponent(((HttpContent) obj).content());
}
}
composite.setIndex(0, composite.capacity());
@SuppressWarnings("resource") final InputStream is = new ByteBufInputStream(composite);
final byte[] bytes = new byte[is.available()];
is.read(bytes);
return bytes;
} finally {
ReferenceCountUtil.release(composite);
}
}
use of io.netty.buffer.ByteBufInputStream in project rskj by rsksmart.
the class RskWebSocketJsonRpcHandler method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBufHolder msg) {
ByteBuf content = msg.copy().content();
try (ByteBufInputStream source = new ByteBufInputStream(content)) {
final JsonNode jsonNodeRequest = mapper.readTree(source);
RskWebSocketJsonParameterValidator.Result validationResult = parameterValidator.validate(jsonNodeRequest);
RskJsonRpcRequest request = mapper.treeToValue(jsonNodeRequest, RskJsonRpcRequest.class);
JsonRpcResultOrError resultOrError = null;
if (validationResult.isValid()) {
// TODO(mc) we should support the ModuleDescription method filters
resultOrError = request.accept(this, ctx);
} else {
resultOrError = new JsonRpcError(JsonRpcError.INVALID_PARAMS, validationResult.getMessage());
}
JsonRpcIdentifiableMessage response = resultOrError.responseFor(request.getId());
ctx.writeAndFlush(new TextWebSocketFrame(getJsonWithTypedId(jsonNodeRequest, response)));
return;
} catch (IOException e) {
LOGGER.trace("Not a known or valid JsonRpcRequest", e);
// We need to release this resource, netty only takes care about 'ByteBufHolder msg'
content.release(content.refCnt());
}
// delegate to the next handler if the message can't be matched to a known JSON-RPC request
ctx.fireChannelRead(msg);
}
use of io.netty.buffer.ByteBufInputStream in project Almura by AlmuraDev.
the class ClientboundBiomeInformationPacket method readFrom.
@Override
public void readFrom(final ChannelBuf buf) {
final int size = buf.readInteger();
this.biomes = new HashMap<>(size);
if (size > 0) {
for (int i = 0; i < size; i++) {
final int length = buf.readInteger();
ByteBufInputStream byteBufInputStream = null;
ObjectInputStream objectInputStream = null;
try {
final ByteBuf buffer = Unpooled.buffer(length);
buffer.writeBytes(buf.readBytes(length));
byteBufInputStream = new ByteBufInputStream(buffer, length);
objectInputStream = new ObjectInputStream(byteBufInputStream);
final BiomeConfig biomeConfig = (BiomeConfig) objectInputStream.readObject();
this.biomes.put(biomeConfig.getServerBiomeId(), biomeConfig);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (byteBufInputStream != null) {
try {
byteBufInputStream.close();
} catch (IOException ignored) {
}
}
if (objectInputStream != null) {
try {
objectInputStream.close();
} catch (IOException ignored) {
}
}
}
}
}
}
Aggregations