Search in sources :

Example 36 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project intellij-community by JetBrains.

the class RestService method createJsonReader.

@NotNull
protected static JsonReader createJsonReader(@NotNull FullHttpRequest request) {
    JsonReader reader = new JsonReader(new InputStreamReader(new ByteBufInputStream(request.content()), CharsetToolkit.UTF8_CHARSET));
    reader.setLenient(true);
    return reader;
}
Also used : InputStreamReader(java.io.InputStreamReader) JsonReader(com.google.gson.stream.JsonReader) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) NotNull(org.jetbrains.annotations.NotNull)

Example 37 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project intellij-community by JetBrains.

the class XmlRpcServerImpl method process.

@Override
public boolean process(@NotNull String path, @NotNull FullHttpRequest request, @NotNull ChannelHandlerContext context, @Nullable Map<String, Object> handlers) {
    if (!(path.isEmpty() || (path.length() == 1 && path.charAt(0) == '/') || path.equalsIgnoreCase("/rpc2"))) {
        return false;
    }
    if (request.method() != HttpMethod.POST) {
        return false;
    }
    ByteBuf content = request.content();
    if (content.readableBytes() == 0) {
        Responses.send(HttpResponseStatus.BAD_REQUEST, context.channel(), request);
        return true;
    }
    ByteBuf result;
    try (ByteBufInputStream in = new ByteBufInputStream(content)) {
        XmlRpcServerRequest xmlRpcServerRequest = new XmlRpcRequestProcessor().decodeRequest(in);
        if (StringUtil.isEmpty(xmlRpcServerRequest.getMethodName())) {
            LOG.warn("method name empty");
            return false;
        }
        Object response = invokeHandler(getHandler(xmlRpcServerRequest.getMethodName(), handlers == null ? handlerMapping : handlers), xmlRpcServerRequest);
        result = Unpooled.wrappedBuffer(new XmlRpcResponseProcessor().encodeResponse(response, CharsetToolkit.UTF8));
    } catch (SAXParseException e) {
        LOG.warn(e);
        Responses.send(HttpResponseStatus.BAD_REQUEST, context.channel(), request);
        return true;
    } catch (Throwable e) {
        context.channel().close();
        LOG.error(e);
        return true;
    }
    Responses.send(Responses.response("text/xml", result), context.channel(), request);
    return true;
}
Also used : SAXParseException(org.xml.sax.SAXParseException) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) ByteBuf(io.netty.buffer.ByteBuf)

Example 38 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project CorfuDB by CorfuDB.

the class JSONSerializer method deserialize.

/**
     * Deserialize an object from a given byte buffer.
     *
     * @param b The bytebuf to deserialize.
     * @return The deserialized object.
     */
@Override
public Object deserialize(ByteBuf b, CorfuRuntime rt) {
    int classNameLength = b.readShort();
    byte[] classNameBytes = new byte[classNameLength];
    b.readBytes(classNameBytes, 0, classNameLength);
    String className = new String(classNameBytes);
    if (className.equals("null")) {
        return null;
    } else if (className.equals("CorfuObject")) {
        int SMRClassNameLength = b.readShort();
        byte[] SMRClassNameBytes = new byte[SMRClassNameLength];
        b.readBytes(SMRClassNameBytes, 0, SMRClassNameLength);
        String SMRClassName = new String(SMRClassNameBytes);
        try {
            return rt.getObjectsView().build().setStreamID(new UUID(b.readLong(), b.readLong())).setType(Class.forName(SMRClassName)).open();
        } catch (ClassNotFoundException cnfe) {
            log.error("Exception during deserialization!", cnfe);
            throw new RuntimeException(cnfe);
        }
    } else {
        try (ByteBufInputStream bbis = new ByteBufInputStream(b)) {
            try (InputStreamReader r = new InputStreamReader(bbis)) {
                return gson.fromJson(r, Class.forName(className));
            }
        } catch (IOException | ClassNotFoundException ie) {
            log.error("Exception during deserialization!", ie);
            throw new RuntimeException(ie);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) UUID(java.util.UUID)

Example 39 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project flink by apache.

the class KvStateRequestSerializer method deserializeKvStateRequestFailure.

/**
	 * Deserializes the KvState request failure.
	 *
	 * @param buf Buffer to deserialize (expected to be positioned after header)
	 * @return Deserialized KvStateRequestFailure
	 */
public static KvStateRequestFailure deserializeKvStateRequestFailure(ByteBuf buf) throws IOException, ClassNotFoundException {
    long requestId = buf.readLong();
    Throwable cause;
    try (ByteBufInputStream bbis = new ByteBufInputStream(buf);
        ObjectInputStream in = new ObjectInputStream(bbis)) {
        cause = (Throwable) in.readObject();
    }
    return new KvStateRequestFailure(requestId, cause);
}
Also used : ByteBufInputStream(io.netty.buffer.ByteBufInputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 40 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project SpongeCommon by SpongePowered.

the class SpongeFavicon method decode.

private static BufferedImage decode(String encoded) throws IOException {
    checkArgument(encoded.startsWith(FAVICON_PREFIX), "Unknown favicon format");
    ByteBuf base64 = Unpooled.copiedBuffer(encoded.substring(FAVICON_PREFIX.length()), Charsets.UTF_8);
    try {
        ByteBuf buf = Base64.decode(base64);
        try {
            BufferedImage result = ImageIO.read(new ByteBufInputStream(buf));
            checkState(result.getWidth() == 64, "favicon must be 64 pixels wide");
            checkState(result.getHeight() == 64, "favicon must be 64 pixels high");
            return result;
        } finally {
            buf.release();
        }
    } finally {
        base64.release();
    }
}
Also used : ByteBufInputStream(io.netty.buffer.ByteBufInputStream) ByteBuf(io.netty.buffer.ByteBuf) BufferedImage(java.awt.image.BufferedImage)

Aggregations

ByteBufInputStream (io.netty.buffer.ByteBufInputStream)69 ByteBuf (io.netty.buffer.ByteBuf)22 IOException (java.io.IOException)22 InputStreamReader (java.io.InputStreamReader)18 BadRequestException (co.cask.cdap.common.BadRequestException)16 Reader (java.io.Reader)16 JsonSyntaxException (com.google.gson.JsonSyntaxException)11 InputStream (java.io.InputStream)10 Path (javax.ws.rs.Path)9 ObjectInputStream (java.io.ObjectInputStream)8 NamespaceId (co.cask.cdap.proto.id.NamespaceId)6 POST (javax.ws.rs.POST)6 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 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)3 ObjectOutputStream (java.io.ObjectOutputStream)3