@Override public CompletableFuture<List<ResponseMessage>> submitAsync(final RequestMessage requestMessage) throws Exception { final List<ResponseMessage> results = new ArrayList<>(); final CompletableFuture<List<ResponseMessage>> f = new CompletableFuture<>(); callbackResponseHandler.callback = response -> { if (f.isDone()) throw new RuntimeException("A terminating message was already encountered - no more messages should have been received"); results.add(response); // check if the current message is terminating - if it is then we can mark complete if (!response.getStatus().getCode().equals(ResponseStatusCode.PARTIAL_CONTENT)) { f.complete(results); } }; writeAndFlush(requestMessage); return f; }
/** * Writes a response message to the underlying channel while ensuring that at most one * {@link ResponseStatusCode#isFinalResponse() final} response is written. * <p>Note: this method should be used instead of writing to the channel directly when multiple threads * are expected to produce response messages concurrently.</p> * <p>Attempts to write more than one final response message will be ignored.</p> * @see #writeAndFlush(ResponseStatusCode, Object) */ public void writeAndFlush(final ResponseMessage message) { writeAndFlush(message.getStatus().getCode(), message); }
if (!o.getStatus().getCode().isSuccess()) errorMeter.mark(); if (null == session || !o.getStatus().getCode().isSuccess()) serialized = new Frame(serializer.serializeResponseAsBinary(o, ctx.alloc())); else if (null == session || !o.getStatus().getCode().isSuccess()) serialized = new Frame(textSerializer.serializeResponseAsString(o)); else
@Override public ByteBuf serializeResponseAsBinary(final ResponseMessage responseMessage, final ByteBufAllocator allocator) throws SerializationException { ByteBuf encodedMessage = null; try { final Kryo kryo = kryoThreadLocal.get(); try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) { final Output output = new Output(baos, bufferSize); final ResponseMessage msgToWrite = !serializeToString ? responseMessage : ResponseMessage.build(responseMessage.getRequestId()) .code(responseMessage.getStatus().getCode()) .statusAttributes(responseMessage.getStatus().getAttributes()) .responseMetaData(responseMessage.getResult().getMeta()) .result(serializeResultToString(responseMessage)) .statusMessage(responseMessage.getStatus().getMessage()).create(); kryo.writeObject(output, msgToWrite); final long size = output.total(); if (size > Integer.MAX_VALUE) throw new SerializationException(String.format("Message size of %s exceeds allocatable space", size)); output.flush(); encodedMessage = allocator.buffer((int) size); encodedMessage.writeBytes(baos.toByteArray()); } return encodedMessage; } catch (Exception ex) { if (encodedMessage != null) ReferenceCountUtil.release(encodedMessage); logger.warn(String.format("Response [%s] could not be serialized by %s.", responseMessage, AbstractGryoMessageSerializerV3d0.class.getName()), ex); throw new SerializationException(ex); } }
if (response.getStatus().getCode() == ResponseStatusCode.AUTHENTICATE) { final Attribute<SaslClient> saslClient = ((AttributeMap) channelHandlerContext).attr(saslClientKey); final Attribute<Subject> subject = ((AttributeMap) channelHandlerContext).attr(subjectKey);
@Override public ByteBuf serializeResponseAsBinary(final ResponseMessage responseMessage, final ByteBufAllocator allocator) throws SerializationException { ByteBuf encodedMessage = null; try { final Kryo kryo = kryoThreadLocal.get(); try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) { final Output output = new Output(baos, bufferSize); // request id - if present kryo.writeObjectOrNull(output, responseMessage.getRequestId() != null ? responseMessage.getRequestId() : null, UUID.class); // status output.writeShort(responseMessage.getStatus().getCode().getValue()); output.writeString(responseMessage.getStatus().getMessage()); kryo.writeClassAndObject(output, responseMessage.getStatus().getAttributes()); // result kryo.writeClassAndObject(output, serializeToString ? serializeResultToString(responseMessage) : responseMessage.getResult().getData()); kryo.writeClassAndObject(output, responseMessage.getResult().getMeta()); final long size = output.total(); if (size > Integer.MAX_VALUE) throw new SerializationException(String.format("Message size of %s exceeds allocatable space", size)); output.flush(); encodedMessage = allocator.buffer((int) size); encodedMessage.writeBytes(baos.toByteArray()); } return encodedMessage; } catch (Exception ex) { if (encodedMessage != null) ReferenceCountUtil.release(encodedMessage); logger.warn(String.format("Response [%s] could not be serialized by %s.", responseMessage, AbstractGryoMessageSerializerV1d0.class.getName()), ex); throw new SerializationException(ex); } }
@Override protected void channelRead0(final ChannelHandlerContext channelHandlerContext, final ResponseMessage response) throws Exception { try { final ResponseStatusCode statusCode = response.getStatus().getCode(); final ResultQueue queue = pending.get(response.getRequestId()); if (statusCode == ResponseStatusCode.SUCCESS || statusCode == ResponseStatusCode.PARTIAL_CONTENT) { final List<String> exceptions = attributes.containsKey(Tokens.STATUS_ATTRIBUTE_EXCEPTIONS) ? (List<String>) attributes.get(Tokens.STATUS_ATTRIBUTE_EXCEPTIONS) : null; queue.markError(new ResponseException(response.getStatus().getCode(), response.getStatus().getMessage(), exceptions, stackTrace, cleanStatusAttributes(attributes)));
public ByteBuf writeValue(final ResponseMessage value, final ByteBufAllocator allocator, final GraphBinaryWriter context, final boolean nullable) throws SerializationException { final ResponseResult result = value.getResult(); final ResponseStatus status = value.getStatus(); return allocator.compositeBuffer(8).addComponents(true, // Version allocator.buffer(1).writeByte(0x81), // Nullable request id context.writeValue(value.getRequestId(), allocator, true), // Status code context.writeValue(status.getCode().getValue(), allocator, false), // Nullable status message context.writeValue(status.getMessage(), allocator, true), // Status attributes context.writeValue(status.getAttributes(), allocator, false), // Result meta context.writeValue(result.getMeta(), allocator, false), // Fully-qualified value context.write(result.getData(), allocator)); } }
public void ser(final ResponseMessage responseMessage, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider, final TypeSerializer typeSerializer) throws IOException, JsonProcessingException { jsonGenerator.writeStartObject(); if (typeSerializer != null) jsonGenerator.writeStringField(GraphSONTokens.CLASS, HashMap.class.getName()); jsonGenerator.writeStringField(SerTokens.TOKEN_REQUEST, responseMessage.getRequestId() != null ? responseMessage.getRequestId().toString() : null); jsonGenerator.writeObjectFieldStart(SerTokens.TOKEN_STATUS); if (typeSerializer != null) jsonGenerator.writeStringField(GraphSONTokens.CLASS, HashMap.class.getName()); jsonGenerator.writeStringField(SerTokens.TOKEN_MESSAGE, responseMessage.getStatus().getMessage()); jsonGenerator.writeNumberField(SerTokens.TOKEN_CODE, responseMessage.getStatus().getCode().getValue()); jsonGenerator.writeObjectField(SerTokens.TOKEN_ATTRIBUTES, responseMessage.getStatus().getAttributes()); jsonGenerator.writeEndObject(); jsonGenerator.writeObjectFieldStart(SerTokens.TOKEN_RESULT); if (typeSerializer != null) jsonGenerator.writeStringField(GraphSONTokens.CLASS, HashMap.class.getName()); if (null == responseMessage.getResult().getData()) jsonGenerator.writeNullField(SerTokens.TOKEN_DATA); else GraphSONUtil.writeWithType(SerTokens.TOKEN_DATA, responseMessage.getResult().getData(), jsonGenerator, serializerProvider, typeSerializer); jsonGenerator.writeObjectField(SerTokens.TOKEN_META, responseMessage.getResult().getMeta()); jsonGenerator.writeEndObject(); jsonGenerator.writeEndObject(); } }
@Override public <O extends OutputShim> void write(final KryoShim<?, O> kryo, final O output, final ResponseMessage responseMessage) { kryo.writeObjectOrNull(output, responseMessage.getRequestId() != null ? responseMessage.getRequestId() : null, UUID.class); // status output.writeShort((short) responseMessage.getStatus().getCode().getValue()); output.writeString(responseMessage.getStatus().getMessage()); kryo.writeClassAndObject(output, responseMessage.getStatus().getAttributes()); // result kryo.writeClassAndObject(output, responseMessage.getResult().getData()); kryo.writeClassAndObject(output, responseMessage.getResult().getMeta()); }
public void ser(final ResponseMessage responseMessage, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider, final TypeSerializer typeSerializer) throws IOException { GraphSONUtil.writeStartObject(responseMessage, jsonGenerator, typeSerializer); jsonGenerator.writeStringField(SerTokens.TOKEN_REQUEST, responseMessage.getRequestId() != null ? responseMessage.getRequestId().toString() : null); jsonGenerator.writeFieldName(SerTokens.TOKEN_STATUS); GraphSONUtil.writeStartObject(responseMessage, jsonGenerator, typeSerializer); jsonGenerator.writeStringField(SerTokens.TOKEN_MESSAGE, responseMessage.getStatus().getMessage()); jsonGenerator.writeNumberField(SerTokens.TOKEN_CODE, responseMessage.getStatus().getCode().getValue()); jsonGenerator.writeObjectField(SerTokens.TOKEN_ATTRIBUTES, responseMessage.getStatus().getAttributes()); GraphSONUtil.writeEndObject(responseMessage, jsonGenerator, typeSerializer); jsonGenerator.writeFieldName(SerTokens.TOKEN_RESULT); GraphSONUtil.writeStartObject(responseMessage, jsonGenerator, typeSerializer); if (null == responseMessage.getResult().getData()){ jsonGenerator.writeNullField(SerTokens.TOKEN_DATA); } else { jsonGenerator.writeFieldName(SerTokens.TOKEN_DATA); final Object result = responseMessage.getResult().getData(); serializerProvider.findTypedValueSerializer(result.getClass(), true, null).serialize(result, jsonGenerator, serializerProvider); } jsonGenerator.writeObjectField(SerTokens.TOKEN_META, responseMessage.getResult().getMeta()); GraphSONUtil.writeEndObject(responseMessage, jsonGenerator, typeSerializer); GraphSONUtil.writeEndObject(responseMessage, jsonGenerator, typeSerializer); } }
@Override public CompletableFuture<List<ResponseMessage>> submitAsync(final RequestMessage requestMessage) throws Exception { final List<ResponseMessage> results = new ArrayList<>(); final CompletableFuture<List<ResponseMessage>> f = new CompletableFuture<>(); callbackResponseHandler.callback = response -> { if (f.isDone()) throw new RuntimeException("A terminating message was already encountered - no more messages should have been received"); results.add(response); // check if the current message is terminating - if it is then we can mark complete if (!response.getStatus().getCode().equals(ResponseStatusCode.PARTIAL_CONTENT)) { f.complete(results); } }; writeAndFlush(requestMessage); return f; }
/** * Writes a response message to the underlying channel while ensuring that at most one * {@link ResponseStatusCode#isFinalResponse() final} response is written. * <p>Note: this method should be used instead of writing to the channel directly when multiple threads * are expected to produce response messages concurrently.</p> * <p>Attempts to write more than one final response message will be ignored.</p> * @see #writeAndFlush(ResponseStatusCode, Object) */ public void writeAndFlush(final ResponseMessage message) { writeAndFlush(message.getStatus().getCode(), message); }
@Override public ByteBuf serializeResponseAsBinary(final ResponseMessage responseMessage, final ByteBufAllocator allocator) throws SerializationException { ByteBuf encodedMessage = null; try { final Kryo kryo = kryoThreadLocal.get(); try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) { final Output output = new Output(baos, bufferSize); final ResponseMessage msgToWrite = !serializeToString ? responseMessage : ResponseMessage.build(responseMessage.getRequestId()) .code(responseMessage.getStatus().getCode()) .statusAttributes(responseMessage.getStatus().getAttributes()) .responseMetaData(responseMessage.getResult().getMeta()) .result(serializeResultToString(responseMessage)) .statusMessage(responseMessage.getStatus().getMessage()).create(); kryo.writeObject(output, msgToWrite); final long size = output.total(); if (size > Integer.MAX_VALUE) throw new SerializationException(String.format("Message size of %s exceeds allocatable space", size)); output.flush(); encodedMessage = allocator.buffer((int) size); encodedMessage.writeBytes(baos.toByteArray()); } return encodedMessage; } catch (Exception ex) { if (encodedMessage != null) ReferenceCountUtil.release(encodedMessage); logger.warn(String.format("Response [%s] could not be serialized by %s.", responseMessage, AbstractGryoMessageSerializerV3d0.class.getName()), ex); throw new SerializationException(ex); } }
if (response.getStatus().getCode() == ResponseStatusCode.AUTHENTICATE) { final Attribute<SaslClient> saslClient = ((AttributeMap) channelHandlerContext).attr(saslClientKey); final Attribute<Subject> subject = ((AttributeMap) channelHandlerContext).attr(subjectKey);
@Override public ByteBuf serializeResponseAsBinary(final ResponseMessage responseMessage, final ByteBufAllocator allocator) throws SerializationException { ByteBuf encodedMessage = null; try { final Kryo kryo = kryoThreadLocal.get(); try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) { final Output output = new Output(baos, bufferSize); // request id - if present kryo.writeObjectOrNull(output, responseMessage.getRequestId() != null ? responseMessage.getRequestId() : null, UUID.class); // status output.writeShort(responseMessage.getStatus().getCode().getValue()); output.writeString(responseMessage.getStatus().getMessage()); kryo.writeClassAndObject(output, responseMessage.getStatus().getAttributes()); // result kryo.writeClassAndObject(output, serializeToString ? serializeResultToString(responseMessage) : responseMessage.getResult().getData()); kryo.writeClassAndObject(output, responseMessage.getResult().getMeta()); final long size = output.total(); if (size > Integer.MAX_VALUE) throw new SerializationException(String.format("Message size of %s exceeds allocatable space", size)); output.flush(); encodedMessage = allocator.buffer((int) size); encodedMessage.writeBytes(baos.toByteArray()); } return encodedMessage; } catch (Exception ex) { if (encodedMessage != null) ReferenceCountUtil.release(encodedMessage); logger.warn(String.format("Response [%s] could not be serialized by %s.", responseMessage, AbstractGryoMessageSerializerV1d0.class.getName()), ex); throw new SerializationException(ex); } }
public ByteBuf writeValue(final ResponseMessage value, final ByteBufAllocator allocator, final GraphBinaryWriter context, final boolean nullable) throws SerializationException { final ResponseResult result = value.getResult(); final ResponseStatus status = value.getStatus(); return allocator.compositeBuffer(8).addComponents(true, // Version allocator.buffer(1).writeByte(0x81), // Nullable request id context.writeValue(value.getRequestId(), allocator, true), // Status code context.writeValue(status.getCode().getValue(), allocator, false), // Nullable status message context.writeValue(status.getMessage(), allocator, true), // Status attributes context.writeValue(status.getAttributes(), allocator, false), // Result meta context.writeValue(result.getMeta(), allocator, false), // Fully-qualified value context.write(result.getData(), allocator)); } }
public void ser(final ResponseMessage responseMessage, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider, final TypeSerializer typeSerializer) throws IOException, JsonProcessingException { jsonGenerator.writeStartObject(); if (typeSerializer != null) jsonGenerator.writeStringField(GraphSONTokens.CLASS, HashMap.class.getName()); jsonGenerator.writeStringField(SerTokens.TOKEN_REQUEST, responseMessage.getRequestId() != null ? responseMessage.getRequestId().toString() : null); jsonGenerator.writeObjectFieldStart(SerTokens.TOKEN_STATUS); if (typeSerializer != null) jsonGenerator.writeStringField(GraphSONTokens.CLASS, HashMap.class.getName()); jsonGenerator.writeStringField(SerTokens.TOKEN_MESSAGE, responseMessage.getStatus().getMessage()); jsonGenerator.writeNumberField(SerTokens.TOKEN_CODE, responseMessage.getStatus().getCode().getValue()); jsonGenerator.writeObjectField(SerTokens.TOKEN_ATTRIBUTES, responseMessage.getStatus().getAttributes()); jsonGenerator.writeEndObject(); jsonGenerator.writeObjectFieldStart(SerTokens.TOKEN_RESULT); if (typeSerializer != null) jsonGenerator.writeStringField(GraphSONTokens.CLASS, HashMap.class.getName()); if (null == responseMessage.getResult().getData()) jsonGenerator.writeNullField(SerTokens.TOKEN_DATA); else GraphSONUtil.writeWithType(SerTokens.TOKEN_DATA, responseMessage.getResult().getData(), jsonGenerator, serializerProvider, typeSerializer); jsonGenerator.writeObjectField(SerTokens.TOKEN_META, responseMessage.getResult().getMeta()); jsonGenerator.writeEndObject(); jsonGenerator.writeEndObject(); } }
@Override public <O extends OutputShim> void write(final KryoShim<?, O> kryo, final O output, final ResponseMessage responseMessage) { kryo.writeObjectOrNull(output, responseMessage.getRequestId() != null ? responseMessage.getRequestId() : null, UUID.class); // status output.writeShort((short) responseMessage.getStatus().getCode().getValue()); output.writeString(responseMessage.getStatus().getMessage()); kryo.writeClassAndObject(output, responseMessage.getStatus().getAttributes()); // result kryo.writeClassAndObject(output, responseMessage.getResult().getData()); kryo.writeClassAndObject(output, responseMessage.getResult().getMeta()); }
public void ser(final ResponseMessage responseMessage, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider, final TypeSerializer typeSerializer) throws IOException { GraphSONUtil.writeStartObject(responseMessage, jsonGenerator, typeSerializer); jsonGenerator.writeStringField(SerTokens.TOKEN_REQUEST, responseMessage.getRequestId() != null ? responseMessage.getRequestId().toString() : null); jsonGenerator.writeFieldName(SerTokens.TOKEN_STATUS); GraphSONUtil.writeStartObject(responseMessage, jsonGenerator, typeSerializer); jsonGenerator.writeStringField(SerTokens.TOKEN_MESSAGE, responseMessage.getStatus().getMessage()); jsonGenerator.writeNumberField(SerTokens.TOKEN_CODE, responseMessage.getStatus().getCode().getValue()); jsonGenerator.writeObjectField(SerTokens.TOKEN_ATTRIBUTES, responseMessage.getStatus().getAttributes()); GraphSONUtil.writeEndObject(responseMessage, jsonGenerator, typeSerializer); jsonGenerator.writeFieldName(SerTokens.TOKEN_RESULT); GraphSONUtil.writeStartObject(responseMessage, jsonGenerator, typeSerializer); if (null == responseMessage.getResult().getData()){ jsonGenerator.writeNullField(SerTokens.TOKEN_DATA); } else { jsonGenerator.writeFieldName(SerTokens.TOKEN_DATA); final Object result = responseMessage.getResult().getData(); serializerProvider.findTypedValueSerializer(result.getClass(), true, null).serialize(result, jsonGenerator, serializerProvider); } jsonGenerator.writeObjectField(SerTokens.TOKEN_META, responseMessage.getResult().getMeta()); GraphSONUtil.writeEndObject(responseMessage, jsonGenerator, typeSerializer); GraphSONUtil.writeEndObject(responseMessage, jsonGenerator, typeSerializer); } }