Search in sources :

Example 21 with ByteBufferAccessor

use of org.apache.kafka.common.protocol.ByteBufferAccessor in project kafka by apache.

the class RequestHeader method parse.

public static RequestHeader parse(ByteBuffer buffer) {
    short apiKey = -1;
    try {
        // We derive the header version from the request api version, so we read that first.
        // The request api version is part of `RequestHeaderData`, so we reset the buffer position after the read.
        int position = buffer.position();
        apiKey = buffer.getShort();
        short apiVersion = buffer.getShort();
        short headerVersion = ApiKeys.forId(apiKey).requestHeaderVersion(apiVersion);
        buffer.position(position);
        RequestHeaderData headerData = new RequestHeaderData(new ByteBufferAccessor(buffer), headerVersion);
        // However, we treat a null client ID as equivalent to an empty client ID.
        if (headerData.clientId() == null) {
            headerData.setClientId("");
        }
        return new RequestHeader(headerData, headerVersion);
    } catch (UnsupportedVersionException e) {
        throw new InvalidRequestException("Unknown API key " + apiKey, e);
    } catch (Throwable ex) {
        throw new InvalidRequestException("Error parsing request header. Our best guess of the apiKey is: " + apiKey, ex);
    }
}
Also used : RequestHeaderData(org.apache.kafka.common.message.RequestHeaderData) InvalidRequestException(org.apache.kafka.common.errors.InvalidRequestException) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException)

Example 22 with ByteBufferAccessor

use of org.apache.kafka.common.protocol.ByteBufferAccessor in project kafka by apache.

the class DefaultKafkaPrincipalBuilder method deserialize.

@Override
public KafkaPrincipal deserialize(byte[] bytes) {
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    short version = buffer.getShort();
    if (version < DefaultPrincipalData.LOWEST_SUPPORTED_VERSION || version > DefaultPrincipalData.HIGHEST_SUPPORTED_VERSION) {
        throw new SerializationException("Invalid principal data version " + version);
    }
    DefaultPrincipalData data = new DefaultPrincipalData(new ByteBufferAccessor(buffer), version);
    return new KafkaPrincipal(data.type(), data.name(), data.tokenAuthenticated());
}
Also used : SerializationException(org.apache.kafka.common.errors.SerializationException) KafkaPrincipal(org.apache.kafka.common.security.auth.KafkaPrincipal) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) DefaultPrincipalData(org.apache.kafka.common.message.DefaultPrincipalData) ByteBuffer(java.nio.ByteBuffer)

Example 23 with ByteBufferAccessor

use of org.apache.kafka.common.protocol.ByteBufferAccessor in project kafka by apache.

the class MessageTest method verifyWriteRaisesUve.

private void verifyWriteRaisesUve(short version, String problemText, Message message) {
    ObjectSerializationCache cache = new ObjectSerializationCache();
    UnsupportedVersionException e = assertThrows(UnsupportedVersionException.class, () -> {
        int size = message.size(cache, version);
        ByteBuffer buf = ByteBuffer.allocate(size);
        ByteBufferAccessor byteBufferAccessor = new ByteBufferAccessor(buf);
        message.write(byteBufferAccessor, cache, version);
    });
    assertTrue(e.getMessage().contains(problemText), "Expected to get an error message about " + problemText + ", but got: " + e.getMessage());
}
Also used : ObjectSerializationCache(org.apache.kafka.common.protocol.ObjectSerializationCache) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ByteBuffer(java.nio.ByteBuffer) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException)

Example 24 with ByteBufferAccessor

use of org.apache.kafka.common.protocol.ByteBufferAccessor in project kafka by apache.

the class RawTaggedFieldWriterTest method testInvalidNextDefinedTag.

@Test
public void testInvalidNextDefinedTag() {
    List<RawTaggedField> tags = Arrays.asList(new RawTaggedField(2, new byte[] { 0x1, 0x2, 0x3 }), new RawTaggedField(5, new byte[] { 0x4, 0x5, 0x6 }), new RawTaggedField(7, new byte[] { 0x0 }));
    RawTaggedFieldWriter writer = RawTaggedFieldWriter.forFields(tags);
    assertEquals(3, writer.numFields());
    try {
        writer.writeRawTags(new ByteBufferAccessor(ByteBuffer.allocate(1024)), 2);
        fail("expected to get RuntimeException");
    } catch (RuntimeException e) {
        assertEquals("Attempted to use tag 2 as an undefined tag.", e.getMessage());
    }
}
Also used : ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) Test(org.junit.jupiter.api.Test)

Example 25 with ByteBufferAccessor

use of org.apache.kafka.common.protocol.ByteBufferAccessor in project kafka by apache.

the class RawTaggedFieldWriterTest method testWritingZeroRawTaggedFields.

@Test
public void testWritingZeroRawTaggedFields() {
    RawTaggedFieldWriter writer = RawTaggedFieldWriter.forFields(null);
    assertEquals(0, writer.numFields());
    ByteBufferAccessor accessor = new ByteBufferAccessor(ByteBuffer.allocate(0));
    writer.writeRawTags(accessor, Integer.MAX_VALUE);
}
Also used : ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) Test(org.junit.jupiter.api.Test)

Aggregations

ByteBufferAccessor (org.apache.kafka.common.protocol.ByteBufferAccessor)39 ByteBuffer (java.nio.ByteBuffer)24 Test (org.junit.jupiter.api.Test)23 ObjectSerializationCache (org.apache.kafka.common.protocol.ObjectSerializationCache)13 TopicPartition (org.apache.kafka.common.TopicPartition)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)4 Uuid (org.apache.kafka.common.Uuid)4 UnsupportedVersionException (org.apache.kafka.common.errors.UnsupportedVersionException)4 Collections (java.util.Collections)3 List (java.util.List)3 Map (java.util.Map)3 LeaderChangeMessage (org.apache.kafka.common.message.LeaderChangeMessage)3 UpdateMetadataEndpoint (org.apache.kafka.common.message.UpdateMetadataRequestData.UpdateMetadataEndpoint)3 Send (org.apache.kafka.common.network.Send)3 BufferUnderflowException (java.nio.BufferUnderflowException)2 Arrays.asList (java.util.Arrays.asList)2 Collections.emptyList (java.util.Collections.emptyList)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2