Search in sources :

Example 1 with InvalidSchemaDataException

use of org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException in project pulsar by apache.

the class SchemaRegistryServiceWithSchemaDataValidatorTest method testIsCompatibleWithBadSchemaData.

@Test
public void testIsCompatibleWithBadSchemaData() {
    String schemaId = "test-schema-id";
    SchemaCompatibilityStrategy strategy = SchemaCompatibilityStrategy.FULL;
    CompletableFuture<Boolean> future = new CompletableFuture<>();
    when(underlyingService.isCompatible(eq(schemaId), any(SchemaData.class), eq(strategy))).thenReturn(future);
    SchemaData schemaData = SchemaData.builder().type(SchemaType.BOOLEAN).data(new byte[10]).build();
    try {
        service.isCompatible(schemaId, schemaData, strategy).get();
        fail("Should fail isCompatible check");
    } catch (Exception e) {
        assertTrue(e.getCause() instanceof InvalidSchemaDataException);
    }
    verify(underlyingService, times(0)).isCompatible(eq(schemaId), same(schemaData), eq(strategy));
}
Also used : InvalidSchemaDataException(org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException) CompletableFuture(java.util.concurrent.CompletableFuture) SchemaData(org.apache.pulsar.common.protocol.schema.SchemaData) SchemaCompatibilityStrategy(org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy) InvalidSchemaDataException(org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException) Test(org.testng.annotations.Test)

Example 2 with InvalidSchemaDataException

use of org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException in project pulsar by yahoo.

the class SchemasResourceBase method postSchema.

public void postSchema(PostSchemaPayload payload, boolean authoritative, AsyncResponse response) {
    validateDestinationAndAdminOperation(authoritative);
    getSchemaCompatibilityStrategyAsync().thenAccept(schemaCompatibilityStrategy -> {
        byte[] data;
        if (SchemaType.KEY_VALUE.name().equals(payload.getType())) {
            try {
                data = DefaultImplementation.getDefaultImplementation().convertKeyValueDataStringToSchemaInfoSchema(payload.getSchema().getBytes(Charsets.UTF_8));
            } catch (IOException conversionError) {
                log.error("[{}] Failed to post schema for topic {}", clientAppId(), topicName, conversionError);
                response.resume(new RestException(conversionError));
                return;
            }
        } else {
            data = payload.getSchema().getBytes(Charsets.UTF_8);
        }
        pulsar().getSchemaRegistryService().putSchemaIfAbsent(getSchemaId(), SchemaData.builder().data(data).isDeleted(false).timestamp(clock.millis()).type(SchemaType.valueOf(payload.getType())).user(defaultIfEmpty(clientAppId(), "")).props(payload.getProperties()).build(), schemaCompatibilityStrategy).thenAccept(version -> response.resume(Response.accepted().entity(PostSchemaResponse.builder().version(version).build()).build())).exceptionally(error -> {
            Throwable throwable = FutureUtil.unwrapCompletionException(error);
            if (throwable instanceof IncompatibleSchemaException) {
                response.resume(Response.status(Response.Status.CONFLICT.getStatusCode(), throwable.getMessage()).build());
            } else if (throwable instanceof InvalidSchemaDataException) {
                response.resume(Response.status(422, /* Unprocessable Entity */
                throwable.getMessage()).build());
            } else {
                log.error("[{}] Failed to post schema for topic {}", clientAppId(), topicName, throwable);
                response.resume(new RestException(throwable));
            }
            return null;
        });
    }).exceptionally(error -> {
        Throwable throwable = FutureUtil.unwrapCompletionException(error);
        if (throwable instanceof RestException) {
            // Unprocessable Entity
            response.resume(Response.status(((RestException) throwable).getResponse().getStatus(), throwable.getMessage()).build());
        } else {
            log.error("[{}] Failed to post schema for topic {}", clientAppId(), topicName, throwable);
            response.resume(new RestException(throwable));
        }
        return null;
    });
}
Also used : GetAllVersionsSchemaResponse(org.apache.pulsar.common.protocol.schema.GetAllVersionsSchemaResponse) TopicName(org.apache.pulsar.common.naming.TopicName) IsCompatibilityResponse(org.apache.pulsar.common.protocol.schema.IsCompatibilityResponse) AdminResource(org.apache.pulsar.broker.admin.AdminResource) LoggerFactory(org.slf4j.LoggerFactory) PostSchemaResponse(org.apache.pulsar.common.protocol.schema.PostSchemaResponse) ByteBuffer(java.nio.ByteBuffer) SchemaType(org.apache.pulsar.common.schema.SchemaType) DefaultImplementation(org.apache.pulsar.client.internal.DefaultImplementation) MediaType(javax.ws.rs.core.MediaType) GetSchemaResponse(org.apache.pulsar.common.protocol.schema.GetSchemaResponse) RestException(org.apache.pulsar.broker.web.RestException) Objects.isNull(java.util.Objects.isNull) Charsets(com.google.common.base.Charsets) StringUtils.defaultIfEmpty(org.apache.commons.lang.StringUtils.defaultIfEmpty) Logger(org.slf4j.Logger) UTF_8(java.nio.charset.StandardCharsets.UTF_8) AsyncResponse(javax.ws.rs.container.AsyncResponse) IOException(java.io.IOException) DeleteSchemaResponse(org.apache.pulsar.common.protocol.schema.DeleteSchemaResponse) SchemaVersion(org.apache.pulsar.common.protocol.schema.SchemaVersion) Collectors(java.util.stream.Collectors) LongSchemaVersionResponse(org.apache.pulsar.common.protocol.schema.LongSchemaVersionResponse) List(java.util.List) FutureUtil(org.apache.pulsar.common.util.FutureUtil) Response(javax.ws.rs.core.Response) LongSchemaVersion(org.apache.pulsar.common.schema.LongSchemaVersion) Clock(java.time.Clock) SchemaData(org.apache.pulsar.common.protocol.schema.SchemaData) VisibleForTesting(com.google.common.annotations.VisibleForTesting) PostSchemaPayload(org.apache.pulsar.common.protocol.schema.PostSchemaPayload) SchemaAndMetadata(org.apache.pulsar.broker.service.schema.SchemaRegistry.SchemaAndMetadata) InvalidSchemaDataException(org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException) IncompatibleSchemaException(org.apache.pulsar.broker.service.schema.exceptions.IncompatibleSchemaException) IncompatibleSchemaException(org.apache.pulsar.broker.service.schema.exceptions.IncompatibleSchemaException) InvalidSchemaDataException(org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException) RestException(org.apache.pulsar.broker.web.RestException) IOException(java.io.IOException)

Example 3 with InvalidSchemaDataException

use of org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException in project incubator-pulsar by apache.

the class SchemaRegistryServiceWithSchemaDataValidatorTest method testIsCompatibleWithBadSchemaData.

@Test
public void testIsCompatibleWithBadSchemaData() {
    String schemaId = "test-schema-id";
    SchemaCompatibilityStrategy strategy = SchemaCompatibilityStrategy.FULL;
    CompletableFuture<Boolean> future = new CompletableFuture<>();
    when(underlyingService.isCompatible(eq(schemaId), any(SchemaData.class), eq(strategy))).thenReturn(future);
    SchemaData schemaData = SchemaData.builder().type(SchemaType.BOOLEAN).data(new byte[10]).build();
    try {
        service.isCompatible(schemaId, schemaData, strategy).get();
        fail("Should fail isCompatible check");
    } catch (Exception e) {
        assertTrue(e.getCause() instanceof InvalidSchemaDataException);
    }
    verify(underlyingService, times(0)).isCompatible(eq(schemaId), same(schemaData), eq(strategy));
}
Also used : InvalidSchemaDataException(org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException) CompletableFuture(java.util.concurrent.CompletableFuture) SchemaData(org.apache.pulsar.common.protocol.schema.SchemaData) SchemaCompatibilityStrategy(org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy) InvalidSchemaDataException(org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException) Test(org.testng.annotations.Test)

Example 4 with InvalidSchemaDataException

use of org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException in project incubator-pulsar by apache.

the class SchemaRegistryServiceWithSchemaDataValidatorTest method testPutSchemaIfAbsentWithBadSchemaData.

@Test
public void testPutSchemaIfAbsentWithBadSchemaData() {
    String schemaId = "test-schema-id";
    SchemaCompatibilityStrategy strategy = SchemaCompatibilityStrategy.FULL;
    CompletableFuture<SchemaVersion> future = new CompletableFuture<>();
    when(underlyingService.putSchemaIfAbsent(eq(schemaId), any(SchemaData.class), eq(strategy))).thenReturn(future);
    SchemaData schemaData = SchemaData.builder().type(SchemaType.BOOLEAN).data(new byte[10]).build();
    try {
        service.putSchemaIfAbsent(schemaId, schemaData, strategy).get();
        fail("Should fail putSchemaIfAbsent");
    } catch (Exception e) {
        assertTrue(e.getCause() instanceof InvalidSchemaDataException);
    }
    verify(underlyingService, times(0)).putSchemaIfAbsent(eq(schemaId), same(schemaData), eq(strategy));
}
Also used : InvalidSchemaDataException(org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException) CompletableFuture(java.util.concurrent.CompletableFuture) SchemaVersion(org.apache.pulsar.common.protocol.schema.SchemaVersion) SchemaData(org.apache.pulsar.common.protocol.schema.SchemaData) SchemaCompatibilityStrategy(org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy) InvalidSchemaDataException(org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException) Test(org.testng.annotations.Test)

Example 5 with InvalidSchemaDataException

use of org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException in project incubator-pulsar by apache.

the class SchemasResourceBase method postSchema.

public void postSchema(PostSchemaPayload payload, boolean authoritative, AsyncResponse response) {
    validateDestinationAndAdminOperation(authoritative);
    getSchemaCompatibilityStrategyAsync().thenAccept(schemaCompatibilityStrategy -> {
        byte[] data;
        if (SchemaType.KEY_VALUE.name().equals(payload.getType())) {
            try {
                data = DefaultImplementation.getDefaultImplementation().convertKeyValueDataStringToSchemaInfoSchema(payload.getSchema().getBytes(Charsets.UTF_8));
            } catch (IOException conversionError) {
                log.error("[{}] Failed to post schema for topic {}", clientAppId(), topicName, conversionError);
                response.resume(new RestException(conversionError));
                return;
            }
        } else {
            data = payload.getSchema().getBytes(Charsets.UTF_8);
        }
        pulsar().getSchemaRegistryService().putSchemaIfAbsent(getSchemaId(), SchemaData.builder().data(data).isDeleted(false).timestamp(clock.millis()).type(SchemaType.valueOf(payload.getType())).user(defaultIfEmpty(clientAppId(), "")).props(payload.getProperties()).build(), schemaCompatibilityStrategy).thenAccept(version -> response.resume(Response.accepted().entity(PostSchemaResponse.builder().version(version).build()).build())).exceptionally(error -> {
            Throwable throwable = FutureUtil.unwrapCompletionException(error);
            if (throwable instanceof IncompatibleSchemaException) {
                response.resume(Response.status(Response.Status.CONFLICT.getStatusCode(), throwable.getMessage()).build());
            } else if (throwable instanceof InvalidSchemaDataException) {
                response.resume(Response.status(422, /* Unprocessable Entity */
                throwable.getMessage()).build());
            } else {
                log.error("[{}] Failed to post schema for topic {}", clientAppId(), topicName, throwable);
                response.resume(new RestException(throwable));
            }
            return null;
        });
    }).exceptionally(error -> {
        Throwable throwable = FutureUtil.unwrapCompletionException(error);
        if (throwable instanceof RestException) {
            // Unprocessable Entity
            response.resume(Response.status(((RestException) throwable).getResponse().getStatus(), throwable.getMessage()).build());
        } else {
            log.error("[{}] Failed to post schema for topic {}", clientAppId(), topicName, throwable);
            response.resume(new RestException(throwable));
        }
        return null;
    });
}
Also used : GetAllVersionsSchemaResponse(org.apache.pulsar.common.protocol.schema.GetAllVersionsSchemaResponse) TopicName(org.apache.pulsar.common.naming.TopicName) IsCompatibilityResponse(org.apache.pulsar.common.protocol.schema.IsCompatibilityResponse) AdminResource(org.apache.pulsar.broker.admin.AdminResource) SchemaRegistryService(org.apache.pulsar.broker.service.schema.SchemaRegistryService) LoggerFactory(org.slf4j.LoggerFactory) PostSchemaResponse(org.apache.pulsar.common.protocol.schema.PostSchemaResponse) CompletableFuture(java.util.concurrent.CompletableFuture) ByteBuffer(java.nio.ByteBuffer) SchemaType(org.apache.pulsar.common.schema.SchemaType) DefaultImplementation(org.apache.pulsar.client.internal.DefaultImplementation) MediaType(javax.ws.rs.core.MediaType) GetSchemaResponse(org.apache.pulsar.common.protocol.schema.GetSchemaResponse) Pair(org.apache.commons.lang3.tuple.Pair) RestException(org.apache.pulsar.broker.web.RestException) Objects.isNull(java.util.Objects.isNull) Charsets(com.google.common.base.Charsets) StringUtils.defaultIfEmpty(org.apache.commons.lang.StringUtils.defaultIfEmpty) Logger(org.slf4j.Logger) SchemaCompatibilityStrategy(org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy) UTF_8(java.nio.charset.StandardCharsets.UTF_8) AsyncResponse(javax.ws.rs.container.AsyncResponse) IOException(java.io.IOException) DeleteSchemaResponse(org.apache.pulsar.common.protocol.schema.DeleteSchemaResponse) SchemaVersion(org.apache.pulsar.common.protocol.schema.SchemaVersion) Collectors(java.util.stream.Collectors) LongSchemaVersionResponse(org.apache.pulsar.common.protocol.schema.LongSchemaVersionResponse) List(java.util.List) FutureUtil(org.apache.pulsar.common.util.FutureUtil) Response(javax.ws.rs.core.Response) LongSchemaVersion(org.apache.pulsar.common.schema.LongSchemaVersion) Clock(java.time.Clock) SchemaData(org.apache.pulsar.common.protocol.schema.SchemaData) VisibleForTesting(com.google.common.annotations.VisibleForTesting) PostSchemaPayload(org.apache.pulsar.common.protocol.schema.PostSchemaPayload) SchemaAndMetadata(org.apache.pulsar.broker.service.schema.SchemaRegistry.SchemaAndMetadata) InvalidSchemaDataException(org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException) IncompatibleSchemaException(org.apache.pulsar.broker.service.schema.exceptions.IncompatibleSchemaException) IncompatibleSchemaException(org.apache.pulsar.broker.service.schema.exceptions.IncompatibleSchemaException) InvalidSchemaDataException(org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException) RestException(org.apache.pulsar.broker.web.RestException) IOException(java.io.IOException)

Aggregations

InvalidSchemaDataException (org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException)9 SchemaData (org.apache.pulsar.common.protocol.schema.SchemaData)9 CompletableFuture (java.util.concurrent.CompletableFuture)7 SchemaCompatibilityStrategy (org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy)7 SchemaVersion (org.apache.pulsar.common.protocol.schema.SchemaVersion)6 Test (org.testng.annotations.Test)6 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 Charsets (com.google.common.base.Charsets)3 IOException (java.io.IOException)3 ByteBuffer (java.nio.ByteBuffer)3 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)3 Clock (java.time.Clock)3 List (java.util.List)3 Objects.isNull (java.util.Objects.isNull)3 Collectors (java.util.stream.Collectors)3 AsyncResponse (javax.ws.rs.container.AsyncResponse)3 MediaType (javax.ws.rs.core.MediaType)3 Response (javax.ws.rs.core.Response)3 StringUtils.defaultIfEmpty (org.apache.commons.lang.StringUtils.defaultIfEmpty)3 AdminResource (org.apache.pulsar.broker.admin.AdminResource)3