use of org.agrona.io.DirectBufferInputStream in project zeebe by zeebe-io.
the class PartitionsResponseTest method shouldEncodePartitions.
@Test
public void shouldEncodePartitions() throws Exception {
// given
final PartitionsResponse response = new PartitionsResponse();
response.addPartition(1, BufferUtil.wrapString("default-topic"));
response.addPartition(2, BufferUtil.wrapString("foo"));
response.addPartition(3, BufferUtil.wrapString("foo"));
// when
final UnsafeBuffer buf = new UnsafeBuffer(new byte[response.getLength()]);
response.write(buf, 0);
// then
final ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
final JsonNode deserializedResponse = objectMapper.readTree(new DirectBufferInputStream(buf));
assertThat(deserializedResponse.isObject()).isTrue();
assertThat(deserializedResponse.fieldNames()).containsExactly("partitions");
final JsonNode partitions = deserializedResponse.get("partitions");
assertThat(partitions.isArray()).isTrue();
assertThat(partitions.size()).isEqualTo(3);
final JsonNode partition3 = partitions.get(0);
assertThat(partition3.isObject()).isTrue();
assertThat(partition3.get("topic").textValue()).isEqualTo("default-topic");
assertThat(partition3.get("id").numberValue()).isEqualTo(1);
final JsonNode partition1 = partitions.get(1);
assertThat(partition1.isObject()).isTrue();
assertThat(partition1.get("topic").textValue()).isEqualTo("foo");
assertThat(partition1.get("id").numberValue()).isEqualTo(2);
final JsonNode partition2 = partitions.get(2);
assertThat(partition2.isObject()).isTrue();
assertThat(partition2.get("topic").textValue()).isEqualTo("foo");
assertThat(partition2.get("id").numberValue()).isEqualTo(3);
}
use of org.agrona.io.DirectBufferInputStream in project zeebe by zeebe-io.
the class ControlMessageResponse method wrap.
@Override
public void wrap(DirectBuffer responseBuffer, int offset, int length) {
messageHeaderDecoder.wrap(responseBuffer, 0);
if (messageHeaderDecoder.templateId() != responseDecoder.sbeTemplateId()) {
if (messageHeaderDecoder.templateId() == ErrorResponseDecoder.TEMPLATE_ID) {
errorResponse.wrap(responseBuffer, offset, length);
throw new RuntimeException("Unexpected error response from broker: " + errorResponse.getErrorCode() + " - " + errorResponse.getErrorData());
} else {
throw new RuntimeException("Unexpected response from broker. Template id " + messageHeaderDecoder.templateId());
}
}
responseDecoder.wrap(responseBuffer, messageHeaderDecoder.encodedLength(), messageHeaderDecoder.blockLength(), messageHeaderDecoder.version());
final int dataLength = responseDecoder.dataLength();
final int dataOffset = messageHeaderDecoder.encodedLength() + messageHeaderDecoder.blockLength() + ControlMessageResponseDecoder.dataHeaderLength();
try (InputStream is = new DirectBufferInputStream(responseBuffer, dataOffset, dataLength)) {
data = msgPackHelper.readMsgPack(is);
} catch (IOException e) {
LangUtil.rethrowUnchecked(e);
}
}
use of org.agrona.io.DirectBufferInputStream in project zeebe by zeebe-io.
the class SubscribedEvent method wrap.
@Override
public void wrap(DirectBuffer responseBuffer, int offset, int length) {
headerDecoder.wrap(responseBuffer, offset);
if (headerDecoder.templateId() != bodyDecoder.sbeTemplateId()) {
throw new RuntimeException("Unexpected response from broker.");
}
bodyDecoder.wrap(responseBuffer, offset + headerDecoder.encodedLength(), headerDecoder.blockLength(), headerDecoder.version());
final int eventLength = bodyDecoder.eventLength();
final int eventOffset = bodyDecoder.limit() + eventHeaderLength();
try (InputStream is = new DirectBufferInputStream(responseBuffer, offset + eventOffset, eventLength)) {
event = msgPackHelper.readMsgPack(is);
} catch (IOException e) {
LangUtil.rethrowUnchecked(e);
}
}
use of org.agrona.io.DirectBufferInputStream in project zeebe by zeebe-io.
the class CommandRequestHandler method getResult.
@Override
public EventImpl getResult(DirectBuffer buffer, int offset, int blockLength, int version) {
decoder.wrap(buffer, offset, blockLength, version);
final long key = decoder.key();
final int partitionId = decoder.partitionId();
final long position = decoder.position();
final int eventLength = decoder.eventLength();
final DirectBufferInputStream inStream = new DirectBufferInputStream(buffer, decoder.limit() + ExecuteCommandResponseDecoder.eventHeaderLength(), eventLength);
final EventImpl result;
try {
result = objectMapper.readValue(inStream, event.getClass());
} catch (Exception e) {
throw new ClientException("Cannot deserialize event in response", e);
}
result.setKey(key);
result.setPartitionId(partitionId);
result.setTopicName(event.getMetadata().getTopicName());
result.setEventPosition(position);
if (expectedState != null && !expectedState.equals(result.getState())) {
throw new ClientCommandRejectedException(errorFunction.apply(event, result));
}
return result;
}
use of org.agrona.io.DirectBufferInputStream in project zeebe by zeebe-io.
the class ControlMessageRequestHandler method getResult.
@SuppressWarnings({ "unchecked", "resource" })
@Override
public Object getResult(DirectBuffer buffer, int offset, int blockLength, int version) {
decoder.wrap(buffer, offset, blockLength, version);
final int dataLength = decoder.dataLength();
final DirectBufferInputStream inStream = new DirectBufferInputStream(buffer, decoder.limit() + ControlMessageRequestDecoder.dataHeaderLength(), dataLength);
final Object response;
try {
response = objectMapper.readValue(inStream, message.getResponseClass());
} catch (Exception e) {
throw new RuntimeException("Cannot deserialize event in response", e);
}
message.onResponse(response);
return response;
}
Aggregations