Search in sources :

Example 16 with ByteBufInputStream

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufInputStream in project quarkus by quarkusio.

the class VertxRequestHandler method handle.

@Override
public void handle(RoutingContext routingContext) {
    String path = routingContext.request().path();
    if (path == null) {
        routingContext.fail(404);
        return;
    }
    // expects rootPath to end with '/'
    if (!path.startsWith(rootPath)) {
        routingContext.fail(404);
        return;
    }
    path = path.substring(rootPath.length());
    FunctionInvoker invoker = FunctionRecorder.registry.matchInvoker(path);
    if (invoker == null) {
        routingContext.fail(404);
        return;
    }
    if (routingContext.request().method() == HttpMethod.GET) {
        Object input = null;
        if (invoker.hasInput()) {
            QueryReader reader = (QueryReader) invoker.getBindingContext().get(QueryReader.class.getName());
            try {
                input = reader.readValue(routingContext.request().params().iterator());
            } catch (Exception e) {
                log.error("Failed to unmarshal input", e);
                routingContext.fail(400);
                return;
            }
        }
        Object finalInput = input;
        executor.execute(() -> {
            dispatch(routingContext, invoker, finalInput);
        });
    } else if (routingContext.request().method() == HttpMethod.POST) {
        routingContext.request().bodyHandler(buff -> {
            Object input = null;
            if (buff.length() > 0) {
                ByteBufInputStream in = new ByteBufInputStream(buff.getByteBuf());
                ObjectReader reader = (ObjectReader) invoker.getBindingContext().get(ObjectReader.class.getName());
                try {
                    input = reader.readValue((InputStream) in);
                } catch (Exception e) {
                    log.error("Failed to unmarshal input", e);
                    routingContext.fail(400);
                    return;
                }
            }
            Object finalInput = input;
            executor.execute(() -> {
                dispatch(routingContext, invoker, finalInput);
            });
        });
    } else {
        routingContext.fail(405);
        log.error("Must be POST or GET for: " + invoker.getName());
    }
}
Also used : RequestContextImpl(io.quarkus.funqy.runtime.RequestContextImpl) ManagedContext(io.quarkus.arc.ManagedContext) FunctionRecorder(io.quarkus.funqy.runtime.FunctionRecorder) Executor(java.util.concurrent.Executor) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) Logger(org.jboss.logging.Logger) QueryReader(io.quarkus.funqy.runtime.query.QueryReader) Vertx(io.vertx.core.Vertx) CDI(javax.enterprise.inject.spi.CDI) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) BeanContainer(io.quarkus.arc.runtime.BeanContainer) RoutingContext(io.vertx.ext.web.RoutingContext) CurrentIdentityAssociation(io.quarkus.security.identity.CurrentIdentityAssociation) FunctionInvoker(io.quarkus.funqy.runtime.FunctionInvoker) CurrentVertxRequest(io.quarkus.vertx.http.runtime.CurrentVertxRequest) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) HttpMethod(io.vertx.core.http.HttpMethod) SecurityIdentity(io.quarkus.security.identity.SecurityIdentity) QuarkusHttpUser(io.quarkus.vertx.http.runtime.security.QuarkusHttpUser) Handler(io.vertx.core.Handler) Instance(javax.enterprise.inject.Instance) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) InputStream(java.io.InputStream) QueryReader(io.quarkus.funqy.runtime.query.QueryReader) FunctionInvoker(io.quarkus.funqy.runtime.FunctionInvoker) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 17 with ByteBufInputStream

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufInputStream in project Protocol by CloudburstMC.

the class BedrockPacketHelper_v332 method readItem.

@Override
public ItemData readItem(ByteBuf buffer, BedrockSession session) {
    int id = VarInts.readInt(buffer);
    if (id == 0) {
        // We don't need to read anything extra.
        return ItemData.AIR;
    }
    int aux = VarInts.readInt(buffer);
    int damage = (short) (aux >> 8);
    if (damage == Short.MAX_VALUE)
        damage = -1;
    int count = aux & 0xff;
    int nbtSize = buffer.readShortLE();
    NbtMap compoundTag = null;
    if (nbtSize > 0) {
        try (NBTInputStream reader = NbtUtils.createReaderLE(new ByteBufInputStream(buffer.readSlice(nbtSize)))) {
            compoundTag = (NbtMap) reader.readTag();
        } catch (IOException e) {
            throw new IllegalStateException("Unable to load NBT data", e);
        }
    } else if (nbtSize == -1) {
        int tagCount = buffer.readUnsignedByte();
        if (tagCount != 1)
            throw new IllegalArgumentException("Expected 1 tag but got " + tagCount);
        try (NBTInputStream reader = NbtUtils.createNetworkReader(new ByteBufInputStream(buffer))) {
            compoundTag = (NbtMap) reader.readTag();
        } catch (IOException e) {
            throw new IllegalStateException("Unable to load NBT data", e);
        }
    }
    String[] canPlace = new String[VarInts.readInt(buffer)];
    for (int i = 0; i < canPlace.length; i++) {
        canPlace[i] = this.readString(buffer);
    }
    String[] canBreak = new String[VarInts.readInt(buffer)];
    for (int i = 0; i < canBreak.length; i++) {
        canBreak[i] = this.readString(buffer);
    }
    return ItemData.builder().id(id).damage(damage).count(count).tag(compoundTag).canPlace(canPlace).canBreak(canBreak).build();
}
Also used : NbtMap(com.nukkitx.nbt.NbtMap) NBTInputStream(com.nukkitx.nbt.NBTInputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException)

Example 18 with ByteBufInputStream

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufInputStream in project Protocol by CloudburstMC.

the class BedrockPacketHelper_v340 method readItem.

@Override
public ItemData readItem(ByteBuf buffer, BedrockSession session) {
    int id = VarInts.readInt(buffer);
    if (id == 0) {
        // We don't need to read anything extra.
        return ItemData.AIR;
    }
    int aux = VarInts.readInt(buffer);
    int damage = (short) (aux >> 8);
    if (damage == Short.MAX_VALUE)
        damage = -1;
    int count = aux & 0xff;
    int nbtSize = buffer.readShortLE();
    NbtMap compoundTag = null;
    if (nbtSize > 0) {
        try (NBTInputStream reader = NbtUtils.createReaderLE(new ByteBufInputStream(buffer.readSlice(nbtSize)))) {
            compoundTag = (NbtMap) reader.readTag();
        } catch (IOException e) {
            throw new IllegalStateException("Unable to load NBT data", e);
        }
    } else if (nbtSize == -1) {
        int tagCount = buffer.readUnsignedByte();
        if (tagCount != 1)
            throw new IllegalArgumentException("Expected 1 tag but got " + tagCount);
        try (NBTInputStream reader = NbtUtils.createNetworkReader(new ByteBufInputStream(buffer))) {
            compoundTag = (NbtMap) reader.readTag();
        } catch (IOException e) {
            throw new IllegalStateException("Unable to load NBT data", e);
        }
    }
    String[] canPlace = new String[VarInts.readInt(buffer)];
    for (int i = 0; i < canPlace.length; i++) {
        canPlace[i] = this.readString(buffer);
    }
    String[] canBreak = new String[VarInts.readInt(buffer)];
    for (int i = 0; i < canBreak.length; i++) {
        canBreak[i] = this.readString(buffer);
    }
    long blockingTicks = 0;
    if (this.isBlockingItem(id, session)) {
        blockingTicks = VarInts.readLong(buffer);
    }
    return ItemData.builder().id(id).damage(damage).count(count).tag(compoundTag).canPlace(canPlace).canBreak(canBreak).blockingTicks(blockingTicks).build();
}
Also used : NbtMap(com.nukkitx.nbt.NbtMap) NBTInputStream(com.nukkitx.nbt.NBTInputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException)

Example 19 with ByteBufInputStream

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufInputStream in project Protocol by CloudburstMC.

the class BedrockPacketHelper_v291 method readItem.

@Override
public ItemData readItem(ByteBuf buffer, BedrockSession session) {
    Preconditions.checkNotNull(buffer, "buffer");
    int id = VarInts.readInt(buffer);
    if (id == 0) {
        // We don't need to read anything extra.
        return ItemData.AIR;
    }
    int aux = VarInts.readInt(buffer);
    int damage = (short) (aux >> 8);
    if (damage == Short.MAX_VALUE)
        damage = -1;
    int count = aux & 0xff;
    short nbtSize = buffer.readShortLE();
    NbtMap compoundTag = null;
    if (nbtSize > 0) {
        try (NBTInputStream reader = NbtUtils.createReaderLE(new ByteBufInputStream(buffer.readSlice(nbtSize)))) {
            Object tag = reader.readTag();
            if (tag instanceof NbtMap) {
                compoundTag = (NbtMap) tag;
            }
        } catch (IOException e) {
            throw new IllegalStateException("Unable to load NBT data", e);
        }
    }
    String[] canPlace = readArray(buffer, new String[0], this::readString);
    String[] canBreak = readArray(buffer, new String[0], this::readString);
    return ItemData.builder().id(id).damage(damage).count(count).tag(compoundTag).canPlace(canPlace).canBreak(canBreak).build();
}
Also used : NbtMap(com.nukkitx.nbt.NbtMap) NBTInputStream(com.nukkitx.nbt.NBTInputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException)

Example 20 with ByteBufInputStream

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufInputStream in project yamcs by yamcs.

the class WebSocketFrameHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext nettyContext, WebSocketFrame frame) throws Exception {
    ClientMessage message;
    if (protobuf) {
        try (InputStream in = new ByteBufInputStream(frame.content())) {
            message = ClientMessage.newBuilder().mergeFrom(in).build();
        }
    } else {
        String json = frame.content().toString(StandardCharsets.UTF_8);
        JsonObject obj = JsonParser.parseString(json).getAsJsonObject();
        String messageType = obj.get("type").getAsString();
        switch(messageType) {
            case "state":
                message = jsonToClientMessage(obj, null);
                break;
            case "cancel":
                message = jsonToClientMessage(obj, CancelOptions.getDescriptor());
                break;
            default:
                Topic topic = matchTopic(messageType);
                if (topic == null) {
                    message = jsonToClientMessage(obj, null);
                    break;
                }
                message = jsonToClientMessage(obj, topic.getRequestPrototype().getDescriptorForType());
        }
    }
    try {
        switch(message.getType()) {
            case "state":
                dumpState(nettyContext);
                break;
            case "cancel":
                cancelCall(nettyContext, message);
                break;
            default:
                Topic topic = matchTopic(message.getType());
                if (topic == null) {
                    throw new NotFoundException("No topic '" + message.getType() + "'");
                }
                if (message.getCall() > 0) {
                    streamToExistingCall(nettyContext, message, topic);
                } else {
                    startNewContext(nettyContext, message, topic);
                }
        }
    } catch (HttpException e) {
        Reply reply = Reply.newBuilder().setReplyTo(message.getId()).setException(e.toMessage()).build();
        writeMessage(nettyContext, ServerMessage.newBuilder().setType("reply").setData(Any.pack(reply, HttpServer.TYPE_URL_PREFIX)).build());
    }
}
Also used : ByteBufInputStream(io.netty.buffer.ByteBufInputStream) InputStream(java.io.InputStream) JsonObject(com.google.gson.JsonObject) Reply(org.yamcs.protobuf.Reply) ClientMessage(org.yamcs.protobuf.ClientMessage) ByteBufInputStream(io.netty.buffer.ByteBufInputStream)

Aggregations

ByteBufInputStream (io.netty.buffer.ByteBufInputStream)247 IOException (java.io.IOException)94 ByteBuf (io.netty.buffer.ByteBuf)83 InputStreamReader (java.io.InputStreamReader)51 InputStream (java.io.InputStream)45 Reader (java.io.Reader)44 JsonSyntaxException (com.google.gson.JsonSyntaxException)31 BadRequestException (io.cdap.cdap.common.BadRequestException)26 Path (javax.ws.rs.Path)24 POST (javax.ws.rs.POST)19 Map (java.util.Map)17 BadRequestException (co.cask.cdap.common.BadRequestException)16 HashMap (java.util.HashMap)15 DataInputStream (java.io.DataInputStream)13 ObjectInputStream (java.io.ObjectInputStream)11 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)11 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)10 BufferedImage (java.awt.image.BufferedImage)10 List (java.util.List)10