Search in sources :

Example 56 with ByteBufInputStream

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)));
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) InputStreamReader(java.io.InputStreamReader) LogReader(co.cask.cdap.logging.read.LogReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BadRequestException(co.cask.cdap.common.BadRequestException) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) AppRequest(co.cask.cdap.proto.artifact.AppRequest) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 57 with ByteBufInputStream

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) BadRequestException(co.cask.cdap.common.BadRequestException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf)

Example 58 with ByteBufInputStream

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);
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) HttpObject(io.netty.handler.codec.http.HttpObject) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) InputStream(java.io.InputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent)

Example 59 with ByteBufInputStream

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);
}
Also used : JsonRpcIdentifiableMessage(co.rsk.jsonrpc.JsonRpcIdentifiableMessage) RskJsonRpcRequest(co.rsk.rpc.modules.RskJsonRpcRequest) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonRpcError(co.rsk.jsonrpc.JsonRpcError) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) JsonRpcResultOrError(co.rsk.jsonrpc.JsonRpcResultOrError)

Example 60 with ByteBufInputStream

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) {
                    }
                }
            }
        }
    }
}
Also used : BiomeConfig(com.almuradev.almura.feature.biome.BiomeConfig) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

ByteBufInputStream (io.netty.buffer.ByteBufInputStream)78 IOException (java.io.IOException)28 ByteBuf (io.netty.buffer.ByteBuf)27 InputStreamReader (java.io.InputStreamReader)18 BadRequestException (co.cask.cdap.common.BadRequestException)16 Reader (java.io.Reader)16 InputStream (java.io.InputStream)15 JsonSyntaxException (com.google.gson.JsonSyntaxException)11 Path (javax.ws.rs.Path)9 ObjectInputStream (java.io.ObjectInputStream)8 DataInputStream (java.io.DataInputStream)7 POST (javax.ws.rs.POST)6 NamespaceId (co.cask.cdap.proto.id.NamespaceId)5 ByteBuffer (java.nio.ByteBuffer)5 Test (org.junit.jupiter.api.Test)5 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)4 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)4 ByteBufOutputStream (io.netty.buffer.ByteBufOutputStream)4 RpcException (org.apache.drill.exec.rpc.RpcException)4 UnsupportedTypeException (co.cask.cdap.api.data.schema.UnsupportedTypeException)3