Package com.tinkerpop.gremlin.driver

Examples of com.tinkerpop.gremlin.driver.MessageSerializer


        final ByteBuf contentTypeBytes = channelHandlerContext.alloc().buffer(len);
        try {
            messageBytes.readBytes(contentTypeBytes);
            final String contentType = contentTypeBytes.toString(UTF8);
            final MessageSerializer serializer = select(contentType, Serializers.DEFAULT_REQUEST_SERIALIZER);

            channelHandlerContext.channel().attr(StateKey.SERIALIZER).set(serializer);
            channelHandlerContext.channel().attr(StateKey.USE_BINARY).set(true);
            try {
                objects.add(serializer.deserializeRequest(messageBytes.discardReadBytes()));
            } catch (SerializationException se) {
                objects.add(RequestMessage.INVALID);
            }
        } finally {
            contentTypeBytes.release();
View Full Code Here


    static final Meter errorMeter = MetricManager.INSTANCE.getMeter(name(GremlinServer.class, "errors"));
    private static final Charset UTF8 = Charset.forName("UTF-8");

    @Override
    protected void encode(final ChannelHandlerContext channelHandlerContext, final ResponseMessage responseMessage, final ByteBuf byteBuf) throws Exception {
        final MessageSerializer serializer = channelHandlerContext.channel().attr(StateKey.SERIALIZER).get();
        final boolean useBinary = channelHandlerContext.channel().attr(StateKey.USE_BINARY).get();

        try {
            if (useBinary) {
                if (responseMessage.getStatus().getCode().isSuccess())
                    byteBuf.writeBytes(serializer.serializeResponseAsBinary(responseMessage, channelHandlerContext.alloc()));
                else {
                    byteBuf.writeBytes(serializer.serializeResponseAsBinary(responseMessage, channelHandlerContext.alloc()));
                    final ResponseMessage terminator = ResponseMessage.build(responseMessage.getRequestId()).code(ResponseStatusCode.SUCCESS_TERMINATOR).create();
                    byteBuf.writeBytes(serializer.serializeResponseAsBinary(terminator, channelHandlerContext.alloc()));
                    errorMeter.mark();
                }
            } else {
                // the expectation is that the GremlinTextRequestDecoder will have placed a MessageTextSerializer
                // instance on the channel.
                final MessageTextSerializer textSerializer = (MessageTextSerializer) serializer;
                if (responseMessage.getStatus().getCode().isSuccess())
                    byteBuf.writeBytes(textSerializer.serializeResponseAsString(responseMessage).getBytes(UTF8));
                else {
                    byteBuf.writeBytes(textSerializer.serializeResponseAsString(responseMessage).getBytes(UTF8));
                    final ResponseMessage terminator = ResponseMessage.build(responseMessage.getRequestId()).code(ResponseStatusCode.SUCCESS_TERMINATOR).create();
                    byteBuf.writeBytes(textSerializer.serializeResponseAsString(terminator).getBytes(UTF8));
                    errorMeter.mark();
                }
            }
        } catch (Exception ex) {
            errorMeter.mark();
            logger.warn("The result [{}] in the request {} could not be serialized and returned.", responseMessage.getResult(), responseMessage.getRequestId(), ex);
            final String errorMessage = String.format("Error during serialization: %s",
                    ex.getCause() != null ? ex.getCause().getMessage() : ex.getMessage());
            final ResponseMessage error = ResponseMessage.build(responseMessage.getRequestId())
                    .statusMessage(errorMessage)
                    .code(ResponseStatusCode.SERVER_ERROR_SERIALIZATION).create();
            if (useBinary) {
                channelHandlerContext.write(serializer.serializeResponseAsBinary(error, channelHandlerContext.alloc()));
                final ResponseMessage terminator = ResponseMessage.build(responseMessage.getRequestId()).code(ResponseStatusCode.SUCCESS_TERMINATOR).create();
                channelHandlerContext.writeAndFlush(serializer.serializeResponseAsBinary(terminator, channelHandlerContext.alloc()));
            } else {
                final MessageTextSerializer textSerializer = (MessageTextSerializer) serializer;
                channelHandlerContext.write(textSerializer.serializeResponseAsString(error));
                final ResponseMessage terminator = ResponseMessage.build(responseMessage.getRequestId()).code(ResponseStatusCode.SUCCESS_TERMINATOR).create();
                channelHandlerContext.writeAndFlush(textSerializer.serializeResponseAsString(terminator));
View Full Code Here

                if (!MessageSerializer.class.isAssignableFrom(clazz)) {
                    logger.warn("The {} serialization class does not implement {} - it will not be available.", config.className, MessageSerializer.class.getCanonicalName());
                    return Optional.<MessageSerializer>empty();
                }

                final MessageSerializer serializer = (MessageSerializer) clazz.newInstance();
                if (config.config != null)
                    serializer.configure(config.config);

                return Optional.ofNullable(serializer);
            } catch (ClassNotFoundException cnfe) {
                logger.warn("Could not find configured serializer class - {} - it will not be available", config.className);
                return Optional.<MessageSerializer>empty();
            } catch (Exception ex) {
                logger.warn("Could not instantiate configured serializer class - {} - it will not be available.", config.className);
                return Optional.<MessageSerializer>empty();
            }
        }).filter(Optional::isPresent).map(Optional::get).flatMap(serializer ->
                        Stream.of(serializer.mimeTypesSupported()).map(mimeType -> Pair.with(mimeType, serializer))
        ).forEach(pair -> {
            final String mimeType = pair.getValue0().toString();
            final MessageSerializer serializer = pair.getValue1();
            if (serializers.containsKey(mimeType))
                logger.warn("{} already has {} configured.  It will not be replaced by {}. Check configuration for serializer duplication or other issues.",
                        mimeType, serializers.get(mimeType).getClass().getName(), serializer.getClass().getName());
            else {
                logger.info("Configured {} with {}", mimeType, pair.getValue1().getClass().getName());
                serializers.put(mimeType, serializer);
            }
        });
View Full Code Here

        final BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("nio-client-%d").build();
        group = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors(), threadFactory);
        final Bootstrap b = new Bootstrap().group(group);

        try {
            final MessageSerializer serializer = new KryoMessageSerializerV1d0();
            b.channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(final SocketChannel ch) {
                            final ChannelPipeline p = ch.pipeline();
View Full Code Here

        try {
            final WebSocketClientHandler wsHandler =
                    new WebSocketClientHandler(
                            WebSocketClientHandshakerFactory.newHandshaker(
                                    uri, WebSocketVersion.V13, null, false, new DefaultHttpHeaders()));
            final MessageSerializer serializer = new KryoMessageSerializerV1d0();
            b.channel(NioSocketChannel.class)
             .handler(new ChannelInitializer<SocketChannel>() {
                 @Override
                 protected void initChannel(final SocketChannel ch) {
                     final ChannelPipeline p = ch.pipeline();
View Full Code Here

        final ByteBuf contentTypeBytes = channelHandlerContext.alloc().buffer(lenOfContentType);
        try {
            byteBuf.readBytes(contentTypeBytes);
            final String contentType = contentTypeBytes.toString(UTF8);
            final MessageSerializer serializer = select(contentType, Serializers.DEFAULT_REQUEST_SERIALIZER);

            channelHandlerContext.channel().attr(StateKey.SERIALIZER).set(serializer);
            channelHandlerContext.channel().attr(StateKey.USE_BINARY).set(true);
            try {
                objects.add(serializer.deserializeRequest(byteBuf.discardReadBytes()));
            } catch (SerializationException se) {
                objects.add(RequestMessage.INVALID);
            }
        } finally {
            contentTypeBytes.release();
View Full Code Here

    private static final Logger logger = LoggerFactory.getLogger(WsGremlinResponseEncoder.class);
    static final Meter errorMeter = MetricManager.INSTANCE.getMeter(name(GremlinServer.class, "errors"));

    @Override
    protected void encode(final ChannelHandlerContext channelHandlerContext, final ResponseMessage o, final List<Object> objects) throws Exception {
        final MessageSerializer serializer = channelHandlerContext.channel().attr(StateKey.SERIALIZER).get();
        final boolean useBinary = channelHandlerContext.channel().attr(StateKey.USE_BINARY).get();
        final Session session = channelHandlerContext.channel().attr(StateKey.SESSION).get();

        try {
            if (useBinary) {
                final ByteBuf serialized;

                // if the request came in on a session then the serialization must occur in that same thread.
                if (null == session)
                    serialized = serializer.serializeResponseAsBinary(o, channelHandlerContext.alloc());
                else
                    serialized = session.getExecutor().submit(() -> serializer.serializeResponseAsBinary(o, channelHandlerContext.alloc())).get();

                if (o.getStatus().getCode().isSuccess())
                    objects.add(new BinaryWebSocketFrame(serialized));
                else {
                    objects.add(new BinaryWebSocketFrame(serialized));
                    final ResponseMessage terminator = ResponseMessage.build(o.getRequestId()).code(ResponseStatusCode.SUCCESS_TERMINATOR).create();
                    objects.add(new BinaryWebSocketFrame(serializer.serializeResponseAsBinary(terminator, channelHandlerContext.alloc())));
                    errorMeter.mark();
                }
            } else {
                // the expectation is that the GremlinTextRequestDecoder will have placed a MessageTextSerializer
                // instance on the channel.
                final MessageTextSerializer textSerializer = (MessageTextSerializer) serializer;

                final String serialized;

                // if the request came in on a session then the serialization must occur in that same thread.
                if (null == session)
                    serialized = textSerializer.serializeResponseAsString(o);
                else
                    serialized = session.getExecutor().submit(() -> textSerializer.serializeResponseAsString(o)).get();

                if (o.getStatus().getCode().isSuccess())
                    objects.add(new TextWebSocketFrame(true, 0, serialized));
                else {
                    objects.add(new TextWebSocketFrame(true, 0, serialized));
                    final ResponseMessage terminator = ResponseMessage.build(o.getRequestId()).code(ResponseStatusCode.SUCCESS_TERMINATOR).create();
                    objects.add(new TextWebSocketFrame(true, 0, textSerializer.serializeResponseAsString(terminator)));
                    errorMeter.mark();
                }
            }
        } catch (Exception ex) {
            errorMeter.mark();
            logger.warn("The result [{}] in the request {} could not be serialized and returned.", o.getResult(), o.getRequestId(), ex);
            final String errorMessage = String.format("Error during serialization: %s",
                    ex.getCause() != null ? ex.getCause().getMessage() : ex.getMessage());
            final ResponseMessage error = ResponseMessage.build(o.getRequestId())
                    .statusMessage(errorMessage)
                    .code(ResponseStatusCode.SERVER_ERROR_SERIALIZATION).create();
            if (useBinary) {
                channelHandlerContext.write(new BinaryWebSocketFrame(serializer.serializeResponseAsBinary(error, channelHandlerContext.alloc())));
                final ResponseMessage terminator = ResponseMessage.build(o.getRequestId()).code(ResponseStatusCode.SUCCESS_TERMINATOR).create();
                channelHandlerContext.writeAndFlush(new BinaryWebSocketFrame(serializer.serializeResponseAsBinary(terminator, channelHandlerContext.alloc())));
            } else {
                final MessageTextSerializer textSerializer = (MessageTextSerializer) serializer;
                channelHandlerContext.write(new TextWebSocketFrame(textSerializer.serializeResponseAsString(error)));
                final ResponseMessage terminator = ResponseMessage.build(o.getRequestId()).code(ResponseStatusCode.SUCCESS_TERMINATOR).create();
                channelHandlerContext.writeAndFlush(new TextWebSocketFrame(textSerializer.serializeResponseAsString(terminator)));
View Full Code Here

TOP

Related Classes of com.tinkerpop.gremlin.driver.MessageSerializer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.