diff --git a/pom.xml b/pom.xml
index c8bceeefe..af3bdcfd5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -44,6 +44,8 @@
spring-cloud-stream-integration-testsspring-cloud-stream-docsspring-cloud-stream-reactive
+ spring-cloud-stream-schema
+ spring-cloud-stream-schema-server
diff --git a/spring-cloud-stream-docs/src/main/asciidoc/spring-cloud-stream-overview.adoc b/spring-cloud-stream-docs/src/main/asciidoc/spring-cloud-stream-overview.adoc
index 1407b2d90..0dbdc4281 100644
--- a/spring-cloud-stream-docs/src/main/asciidoc/spring-cloud-stream-overview.adoc
+++ b/spring-cloud-stream-docs/src/main/asciidoc/spring-cloud-stream-overview.adoc
@@ -1333,6 +1333,205 @@ This is because the payload at the module's output channel is already a String s
While conversion is supported for both input and output channels, it is especially recommended to be used for the conversion of outbound messages.
For the conversion of inbound messages, especially when the target is a POJO, the `@StreamListener` support will perform the conversion automatically.
+=== Customizing message conversion
+
+Besides the conversions that it supports out of the box, Spring Cloud Stream also supports registering your own message conversion implementations.
+This allows you to send and receive data in a variety of custom formats, including binary, and associate them with specific `contentTypes`.
+In order to do so, you can create a class that extends `AbstractMessageConverter`
+
+=== Schema-based message converters
+
+Spring Cloud Stream provides support for schema-based message converters through its `spring-cloud-stream-schema` module.
+Currently, the only serialization format supported out of the box is Apache Avro, with more formats to be added in future versions.
+
+==== Apache Avro Message Converters
+
+The `spring-cloud-stream-schema` module contains two types of message converters that can be used for Apache Avro serialization:
+
+* converters using the class information of the serialized/deserialized objects, or a schema with a location known at startup;
+* converters using a schema registry - they locate the schemas at runtime, as well as dynamically registering new schemas as domain objects evolve.
+
+===== Converters with schema support
+
+The `AvroSchemaMessageConverter` supports serializing and deserializing messages either using a predefined schema or by using the schema information available in the class (either reflectively, or contained in the `SpecificRecord`).
+If the target type of the conversion is a `GenericRecord`, then a schema must be set.
+
+For using it, you can simply add it to the application context, optionally specifying one ore more `MimeTypes` to associate it with.
+The default `MimeType` is `application/avro`.
+Here is an example of configuring it in a processor application registering the Apache Avro, without a predefined schema:
+
+[source,java]
+----
+@EnableBinding(Sink.class)
+@SpringBootApplication
+public static class SinkApplication {
+
+ ...
+
+ @Bean
+ public MessageConverter userMessageConverter() throws IOException {
+ AvroSchemaMessageConverter avroSchemaMessageConverter {
+ return new AvroSchemaMessageConverter(MimeType.valueOf("avro/bytes");
+ }
+}
+----
+
+Conversely, here is an application that registers a converter with a predefined schema, to be found on the classpath:
+
+[source,java]
+----
+@EnableBinding(Sink.class)
+@SpringBootApplication
+public static class SinkApplication {
+
+ ...
+
+ @Bean
+ public MessageConverter userMessageConverter() throws IOException {
+ AvroSchemaMessageConverter avroSchemaMessageConverter {
+ MessageConverter converter = new AvroSchemaMessageConverter(MimeType.valueOf("avro/bytes");
+ converter.setSchemaLocation("classpath:schemas/User.avro");
+ return converter;
+ }
+}
+----
+
+In order to understand the schema registry client converter, we will describe the schema registry support first.
+
+=== Schema Registry Support
+
+Most serialization models, especially the ones that aim for portability across different platforms and languages, rely on a schema that describes how the data is serialized in the binary payload.
+In order to serialize the data and then to interpret it, both the sending and receiving sides must have access to a schema that describes the binary format.
+In certain cases, the schema can be inferred from the payload type on serialization, or from the target type on deserialization, but in a lot of cases applications benefit from having access to an explicit schema that describes the binary data format.
+A schema registry allows you to store schema information in a textual format (typically JSON) and makes that information accessible to various applications that need it to receive and send data in binary format.
+A schema is referenceable as a tuple consisting of:
+
+* a _subject_ that is the logical name of the schema;
+* the schema _version_;
+* the schema _format_ which describes the binary format of the data.
+
+==== Schema Registry Server
+
+Spring Cloud Stream provides a schema registry server implementation.
+In order to use it, you can simply add the `spring-cloud-stream-server` artifact to your project and use the `@EnableSchemaRegistryServer` annotation, adding the schema registry server REST controller to your application.
+This annotation is intended to be used with Spring Boot web applications, and the listening port of the server is controlled by the `server.port` setting.
+The `spring.cloud.stream.schema.server.path` setting can be used to control the root path of the schema server (especially when it is embedded in other applications).
+
+The schema registry server uses a relational database to store the schemas.
+ By default, it uses an embedded database.
+You can customize the schema storage using the http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-sql[Spring Boot SQL database and JDBC configuration options].
+
+A Spring Boot application enabling the schema registry looks as follows:
+
+[source,java]
+----
+@SpringBootApplication
+@EnableSchemaRegistryServer
+public class SchemaRegistryServerApplication {
+ public static void main(String[] args) {
+ SpringApplication.run(SchemaRegistryServerApplication.class, args);
+ }
+}
+----
+
+===== Schema Registry Server API
+
+The Schema Registry Server API consists of the following operations:
+
+====== `POST /`
+
+Register a new schema.
+
+Accepts JSON payload with the following fields:
+
+* `subject` the schema subject;
+* `format` the schema format;
+* `definition` the schema definition.
+
+Response is a schema object in JSON format, with the following fields:
+
+* `id` the schema id;
+* `subject` the schema subject;
+* `format` the schema format;
+* `version` the schema version;
+* `definition` the schema definition.
+
+====== `GET /{subject}/{format}/{version}`
+
+Retrieve an existing schema by its subject, format and version.
+
+Response is a schema object in JSON format, with the following fields:
+
+* `id` the schema id;
+* `subject` the schema subject;
+* `format` the schema format;
+* `version` the schema version;
+* `definition` the schema definition.
+
+====== `GET /schemas/{id}`
+
+Retrieve an existing schema by its id.
+
+Response is a schema object in JSON format, with the following fields:
+
+* `id` the schema id;
+* `subject` the schema subject;
+* `format` the schema format;
+* `version` the schema version;
+* `definition` the schema definition.
+
+==== Schema Registry Client
+
+The client-side abstraction for interacting with schema registry servers is the `SchemaRegistryClient` interface, with the following structure:
+
+[source,java]
+----
+public interface SchemaRegistryClient {
+
+ SchemaRegistrationResponse register(String subject, String format, String schema);
+
+ String fetch(SchemaReference schemaReference);
+
+ String fetch(Integer id);
+
+}
+----
+
+Spring Cloud Stream provides out of the box implementations for interacting with its own schema server, as well as for interacting with the Confluent Schema Registry.
+
+A client for the Spring Cloud Stream schema registry can be configured using the `@EnableSchemaRegistryClient` as follows:
+
+[source,java]
+----
+ @EnableBinding(Sink.class)
+ @SpringBootApplication
+ @EnableSchemaRegistryClient
+ public static class AvroSinkApplication {
+ ...
+ }
+----
+
+==== Avro Schema Registry Client Message Converters
+
+For Spring Boot applications that have a `SchemaRegistryClient` bean registered with the application context, Spring Cloud Stream will auto-configure an Apache Avro message converter that uses the schema registry client for schema management.
+This eases schema evolution, as applications that receive messages can get easy access to a writer schema that can be reconciled with their own reader schema.
+
+For outbound messages, the `MessageConverter` will be activated if the content type of the channel is set to `application/*+avro`, e.g.:
+
+[source,properties]
+----
+spring.cloud.stream.bindings.output.contentType=application/*+avro
+----
+
+During the outbound conversion, the message converter will try to infer the schemas of the outbound messages based on their type and register them to a subject based on the payload type using the `SchemaRegistryClient`.
+If an identical schema is already found, then a reference to it will be retrieved.
+If not, the schema will be registered and a new version number will be provided.
+The message will be sent with a `contentType` header using the scheme `application/[prefix].[subject].v[version]+avro`, where `prefix` is configurable and `subject` is deduced from the payload type.
+
+For example, a message of the type `User` may be sent as a binary payload with a content type of `application/vnd.user.v2+avro`, where `user` is the subject and `2` is the version number.
+
+When receiving messages, the converter will infer the schema reference from the header of the incoming message and will try to retrieve it. The schema will be used as the writer schema in the deserialization process.
+
=== `@StreamListener` and Message Conversion
The `@StreamListener` annotation provides a convenient way for converting incoming messages without the need to specify the content type of an input channel.
diff --git a/spring-cloud-stream-integration-tests/src/test/java/org/springframework/cloud/stream/config/MessageChannelConfigurerTests.java b/spring-cloud-stream-integration-tests/src/test/java/org/springframework/cloud/stream/config/MessageChannelConfigurerTests.java
index 2c48e7d82..a9c62ca5d 100644
--- a/spring-cloud-stream-integration-tests/src/test/java/org/springframework/cloud/stream/config/MessageChannelConfigurerTests.java
+++ b/spring-cloud-stream-integration-tests/src/test/java/org/springframework/cloud/stream/config/MessageChannelConfigurerTests.java
@@ -16,7 +16,6 @@
package org.springframework.cloud.stream.config;
-import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@@ -38,7 +37,6 @@ import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;
-import org.springframework.messaging.converter.CompositeMessageConverter;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.tuple.Tuple;
@@ -83,16 +81,15 @@ public class MessageChannelConfigurerTests {
@Test
public void testObjectMapperConfig() throws Exception {
- CompositeMessageConverter compositeMessageConverter = messageConverterFactory.getMessageConverterForType(MimeTypeUtils.APPLICATION_JSON);
- List converters = compositeMessageConverter.getConverters();
- for (MessageConverter converter : converters) {
- DirectFieldAccessor converterAccessor = new DirectFieldAccessor(converter);
- ObjectMapper objectMapper = (ObjectMapper) converterAccessor.getPropertyValue("objectMapper");
- // assert that the ObjectMapper used by the converters is compliant with the Boot configuration
- assertThat(!objectMapper.getSerializationConfig().isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)).withFailMessage("SerializationFeature 'WRITE_DATES_AS_TIMESTAMPS' should be disabled");
- // assert that the globally set bean is used by the converters
- assertThat(objectMapper).isSameAs(this.objectMapper);
- }
+ MessageConverter converter = messageConverterFactory.getMessageConverterForType(MimeTypeUtils
+ .APPLICATION_JSON);
+ DirectFieldAccessor converterAccessor = new DirectFieldAccessor(converter);
+ ObjectMapper objectMapper = (ObjectMapper) converterAccessor.getPropertyValue("objectMapper");
+ // assert that the ObjectMapper used by the converters is compliant with the Boot configuration
+ assertThat(!objectMapper.getSerializationConfig().isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS))
+ .withFailMessage("SerializationFeature 'WRITE_DATES_AS_TIMESTAMPS' should be disabled");
+ // assert that the globally set bean is used by the converters
+ assertThat(objectMapper).isSameAs(this.objectMapper);
}
@EnableBinding(Sink.class)
diff --git a/spring-cloud-stream-schema-server/pom.xml b/spring-cloud-stream-schema-server/pom.xml
new file mode 100644
index 000000000..08f9f2787
--- /dev/null
+++ b/spring-cloud-stream-schema-server/pom.xml
@@ -0,0 +1,67 @@
+
+
+
+
+
+ spring-cloud-stream-parent
+ org.springframework.cloud
+ 1.1.0.BUILD-SNAPSHOT
+
+ 4.0.0
+
+ spring-cloud-stream-schema-server
+ 1.1.0.BUILD-SNAPSHOT
+
+
+
+ org.springframework.cloud
+ spring-cloud-stream
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+ org.springframework.boot
+ spring-boot-starter-data-jpa
+
+
+ com.h2database
+ h2
+ 1.4.192
+
+
+ org.apache.avro
+ avro
+ 1.8.1
+
+
+ org.springframework.cloud
+ spring-cloud-stream-test-support
+ test
+
+
+ org.springframework.cloud
+ spring-cloud-stream-test-support-internal
+ test
+
+
+
+
\ No newline at end of file
diff --git a/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/EnableSchemaRegistryServer.java b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/EnableSchemaRegistryServer.java
new file mode 100644
index 000000000..f46fa5e92
--- /dev/null
+++ b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/EnableSchemaRegistryServer.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.cloud.stream.schema.server;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import org.springframework.cloud.stream.schema.server.config.SchemaServerConfiguration;
+import org.springframework.context.annotation.Import;
+
+/**
+ * Enables the schema registry server enpoints.
+ *
+ * @author Vinicius Carvalho
+ */
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+@Import(SchemaServerConfiguration.class)
+public @interface EnableSchemaRegistryServer {
+}
diff --git a/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/SchemaRegistryServerApplication.java b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/SchemaRegistryServerApplication.java
new file mode 100644
index 000000000..76b084281
--- /dev/null
+++ b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/SchemaRegistryServerApplication.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.cloud.stream.schema.server;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+/**
+ * @author Vinicius Carvalho
+ */
+@SpringBootApplication
+@EnableSchemaRegistryServer
+public class SchemaRegistryServerApplication {
+ public static void main(String[] args) {
+ SpringApplication.run(SchemaRegistryServerApplication.class, args);
+ }
+}
diff --git a/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/config/SchemaServerConfiguration.java b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/config/SchemaServerConfiguration.java
new file mode 100644
index 000000000..78912590c
--- /dev/null
+++ b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/config/SchemaServerConfiguration.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.cloud.stream.schema.server.config;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.cloud.stream.schema.server.controllers.ServerController;
+import org.springframework.cloud.stream.schema.server.repository.SchemaRepository;
+import org.springframework.cloud.stream.schema.server.support.AvroSchemaValidator;
+import org.springframework.cloud.stream.schema.server.support.SchemaValidator;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
+
+/**
+ * @author Vinicius Carvalho
+ */
+@Configuration
+@EnableJpaRepositories(basePackageClasses = SchemaRepository.class)
+@EnableConfigurationProperties(SchemaServerProperties.class)
+public class SchemaServerConfiguration {
+
+ @Bean
+ public ServerController serverController(SchemaRepository repository) {
+ return new ServerController(repository, schemaValidators());
+ }
+
+ @Bean
+ public Map schemaValidators() {
+ Map validatorMap = new HashMap<>();
+ validatorMap.put("avro", new AvroSchemaValidator());
+ return validatorMap;
+ }
+}
diff --git a/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/config/SchemaServerProperties.java b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/config/SchemaServerProperties.java
new file mode 100644
index 000000000..347663dca
--- /dev/null
+++ b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/config/SchemaServerProperties.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.springframework.cloud.stream.schema.server.config;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+/**
+ * @author Vinicius Carvalho
+ */
+@ConfigurationProperties("spring.cloud.stream.schema.server")
+public class SchemaServerProperties {
+
+ /**
+ * Prefix for configuration resource paths (default is empty). Useful when embedding
+ * in another application when you don't want to change the context path or servlet
+ * path.
+ */
+ private String path;
+
+ public String getPath() {
+ return this.path;
+ }
+
+ public void setPath(String path) {
+ this.path = path;
+ }
+}
diff --git a/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/controllers/ServerController.java b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/controllers/ServerController.java
new file mode 100644
index 000000000..1cfb0989a
--- /dev/null
+++ b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/controllers/ServerController.java
@@ -0,0 +1,144 @@
+/*
+ * Copyright 2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.springframework.cloud.stream.schema.server.controllers;
+
+import java.util.List;
+import java.util.Map;
+
+import org.springframework.cloud.stream.schema.server.model.Schema;
+import org.springframework.cloud.stream.schema.server.repository.SchemaRepository;
+import org.springframework.cloud.stream.schema.server.support.InvalidSchemaException;
+import org.springframework.cloud.stream.schema.server.support.SchemaNotFoundException;
+import org.springframework.cloud.stream.schema.server.support.SchemaValidator;
+import org.springframework.cloud.stream.schema.server.support.UnsupportedFormatException;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.util.Assert;
+import org.springframework.util.StringUtils;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.ResponseStatus;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.util.UriComponentsBuilder;
+
+/**
+ * @author Vinicius Carvalho
+ */
+@RestController
+@RequestMapping(path = "${spring.cloud.stream.schema.server.prefix:}")
+public class ServerController {
+
+ private final SchemaRepository repository;
+
+ private final Map validators;
+
+ public ServerController(SchemaRepository repository,
+ Map validators) {
+ Assert.notNull(repository, "cannot be null");
+ Assert.notEmpty(validators, "cannot be empty");
+ this.repository = repository;
+ this.validators = validators;
+ }
+
+ @RequestMapping(method = RequestMethod.POST, path = "/", consumes = "application/json", produces = "application/json")
+ public synchronized ResponseEntity register(@RequestBody Schema schema,
+ UriComponentsBuilder builder) {
+ SchemaValidator validator = this.validators.get(schema.getFormat());
+
+ if (validator == null) {
+ throw new UnsupportedFormatException(String.format(
+ "Invalid format, supported types are: %s",
+ StringUtils.collectionToCommaDelimitedString(this.validators.keySet())));
+ }
+
+ if (!validator.isValid(schema.getDefinition())) {
+ throw new InvalidSchemaException("Invalid schema");
+ }
+
+ Schema result;
+ List registeredEntities = this.repository.findBySubjectAndFormatOrderByVersion(
+ schema.getSubject(), schema.getFormat());
+ if (registeredEntities == null || registeredEntities.size() == 0) {
+ schema.setVersion(1);
+ result = this.repository.save(schema);
+ }
+ else {
+ result = validator.match(registeredEntities, schema.getDefinition());
+ if (result == null) {
+ schema.setVersion(
+ registeredEntities.get(registeredEntities.size() - 1).getVersion()
+ + 1);
+ result = this.repository.save(schema);
+ }
+
+ }
+
+ HttpHeaders headers = new HttpHeaders();
+ headers.add(HttpHeaders.LOCATION,
+ builder.path("/{subject}/{format}/v{version}")
+ .buildAndExpand(result.getSubject(), result.getFormat(),
+ result.getVersion())
+ .toString());
+ ResponseEntity response = new ResponseEntity<>(result, headers,
+ HttpStatus.CREATED);
+
+ return response;
+
+ }
+
+ @RequestMapping(method = RequestMethod.GET, produces = "application/json", path = "/{subject}/{format}/v{version}")
+ public ResponseEntity findOne(@PathVariable("subject") String subject,
+ @PathVariable("format") String format,
+ @PathVariable("version") Integer version) {
+ Schema schema = this.repository.findOneBySubjectAndFormatAndVersion(subject, format,
+ version);
+ if (schema == null) {
+ throw new SchemaNotFoundException("Could not find Schema");
+ }
+ return new ResponseEntity<>(schema, HttpStatus.OK);
+ }
+
+ @RequestMapping(method = RequestMethod.GET, produces = "application/json", path = "/schemas/{id}")
+ public ResponseEntity findOne(@PathVariable("id") Integer id) {
+ Schema schema = this.repository.findOne(id);
+ if (schema == null) {
+ throw new SchemaNotFoundException("Could not find Schema");
+ }
+ return new ResponseEntity<>(schema, HttpStatus.OK);
+ }
+
+ @ExceptionHandler(UnsupportedFormatException.class)
+ @ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "Format not supported")
+ public void unsupportedFormat(UnsupportedFormatException ex) {
+ }
+
+ @ExceptionHandler(InvalidSchemaException.class)
+ @ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "Invalid schema")
+ public void invalidSchema(InvalidSchemaException ex) {
+ }
+
+ @ExceptionHandler(SchemaNotFoundException.class)
+ @ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Schema not found")
+ public void schemaNotFound(SchemaNotFoundException ex) {
+ }
+
+}
diff --git a/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/model/Compatibility.java b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/model/Compatibility.java
new file mode 100644
index 000000000..3a3bb9fe2
--- /dev/null
+++ b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/model/Compatibility.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.springframework.cloud.stream.schema.server.model;
+
+/**
+ * @author Vinicius Carvalho
+ */
+public enum Compatibility {
+ BACKWARD, FORWARD, FULL, INCOMPATIBLE;
+}
diff --git a/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/model/Schema.java b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/model/Schema.java
new file mode 100644
index 000000000..28ee7db1c
--- /dev/null
+++ b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/model/Schema.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.cloud.stream.schema.server.model;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.Lob;
+
+/**
+ * @author Vinicius Carvalho
+ *
+ * Represents a persisted schema entity.
+ */
+@Entity
+public class Schema {
+
+ @Id
+ @GeneratedValue
+ @Column(name = "ID")
+ private Integer id;
+
+ @Column(name = "VERSION", nullable = false)
+ private Integer version;
+
+ @Column(name = "SUBJECT", nullable = false)
+ private String subject;
+
+ @Column(name = "FORMAT", nullable = false)
+ private String format;
+
+ @Lob
+ @Column(name = "DEFINITION", nullable = false, length = 8192)
+ private String definition;
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public Integer getVersion() {
+ return version;
+ }
+
+ public void setVersion(Integer version) {
+ this.version = version;
+ }
+
+ public String getSubject() {
+ return subject;
+ }
+
+ public void setSubject(String subject) {
+ this.subject = subject;
+ }
+
+ public String getFormat() {
+ return format;
+ }
+
+ public void setFormat(String format) {
+ this.format = format;
+ }
+
+ public String getDefinition() {
+ return definition;
+ }
+
+ public void setDefinition(String definition) {
+ this.definition = definition;
+ }
+}
diff --git a/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/repository/SchemaRepository.java b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/repository/SchemaRepository.java
new file mode 100644
index 000000000..638e1e299
--- /dev/null
+++ b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/repository/SchemaRepository.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.cloud.stream.schema.server.repository;
+
+import java.util.List;
+
+import org.springframework.cloud.stream.schema.server.model.Schema;
+import org.springframework.data.repository.PagingAndSortingRepository;
+
+/**
+ * @author Vinicius Carvalho
+ */
+public interface SchemaRepository extends PagingAndSortingRepository {
+
+ List findBySubjectAndFormatOrderByVersion(String subject,
+ String format);
+
+ Schema findOneBySubjectAndFormatAndVersion(String subject, String format,
+ Integer version);
+}
diff --git a/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/support/AvroSchemaValidator.java b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/support/AvroSchemaValidator.java
new file mode 100644
index 000000000..9da3ed3a9
--- /dev/null
+++ b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/support/AvroSchemaValidator.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.cloud.stream.schema.server.support;
+
+import java.util.List;
+
+import org.apache.avro.SchemaParseException;
+
+import org.springframework.cloud.stream.schema.server.model.Compatibility;
+import org.springframework.cloud.stream.schema.server.model.Schema;
+
+/**
+ * @author Vinicius Carvalho
+ */
+public class AvroSchemaValidator implements SchemaValidator {
+
+ @Override
+ public boolean isValid(String definition) {
+ boolean result = true;
+ try {
+ new org.apache.avro.Schema.Parser().parse(definition);
+ }
+ catch (SchemaParseException ex) {
+ result = false;
+ }
+ return result;
+ }
+
+ @Override
+ public Compatibility compatibilityCheck(String source, String other) {
+ return null;
+ }
+
+ @Override
+ public Schema match(List schemas, String definition) {
+ Schema result = null;
+ org.apache.avro.Schema source = new org.apache.avro.Schema.Parser()
+ .parse(definition);
+ for (Schema s : schemas) {
+ org.apache.avro.Schema target = new org.apache.avro.Schema.Parser()
+ .parse(s.getDefinition());
+ if (target.equals(source)) {
+ result = s;
+ break;
+ }
+ }
+ return result;
+ }
+
+ @Override
+ public String getFormat() {
+ return "avro";
+ }
+}
diff --git a/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/support/InvalidSchemaException.java b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/support/InvalidSchemaException.java
new file mode 100644
index 000000000..098f9f8d0
--- /dev/null
+++ b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/support/InvalidSchemaException.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright 2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.cloud.stream.schema.server.support;
+
+/**
+ * @author Vinicius Carvalho
+ */
+public class InvalidSchemaException extends RuntimeException {
+ public InvalidSchemaException(String message) {
+ super(message);
+ }
+}
diff --git a/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/support/SchemaNotFoundException.java b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/support/SchemaNotFoundException.java
new file mode 100644
index 000000000..b62620915
--- /dev/null
+++ b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/support/SchemaNotFoundException.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright 2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.cloud.stream.schema.server.support;
+
+/**
+ * @author Vinicius Carvalho
+ */
+public class SchemaNotFoundException extends RuntimeException {
+ public SchemaNotFoundException(String message) {
+ super(message);
+ }
+}
diff --git a/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/support/SchemaValidator.java b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/support/SchemaValidator.java
new file mode 100644
index 000000000..52d85d1c4
--- /dev/null
+++ b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/support/SchemaValidator.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.cloud.stream.schema.server.support;
+
+import java.util.List;
+
+import org.springframework.cloud.stream.schema.server.model.Compatibility;
+import org.springframework.cloud.stream.schema.server.model.Schema;
+
+/**
+ * @author Vinicius Carvalho
+ *
+ * Provides utility methods to validate, check compatibility and match schemas of
+ * different implementations
+ */
+public interface SchemaValidator {
+
+ /**
+ * Verifies if a definition is a valid schema
+ * @param definition - The textual representation of the schema file
+ * @return
+ */
+ boolean isValid(String definition);
+
+ /**
+ * Checks for compatibility between two schemas @see Compatibility class for types
+ * This method may not be supported for certain formats
+ * @param source - The textual representation of the schema to tested
+ * @param other - The textual representation of the other schema to tested
+ * @return
+ */
+ Compatibility compatibilityCheck(String source, String other);
+
+ /**
+ * Return the Schema that is represented by the definition.
+ * @param schemas List of schemas to be tested
+ * @param definition Textual representation of the schema
+ * @return A full Schema object with identifier and subject properties
+ */
+ Schema match(List schemas, String definition);
+
+ String getFormat();
+
+}
diff --git a/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/support/UnsupportedFormatException.java b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/support/UnsupportedFormatException.java
new file mode 100644
index 000000000..af0c12887
--- /dev/null
+++ b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/support/UnsupportedFormatException.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.cloud.stream.schema.server.support;
+
+/**
+ * @author Vinicius Carvalho
+ */
+public class UnsupportedFormatException extends RuntimeException {
+
+ public UnsupportedFormatException(String message) {
+ super(message);
+ }
+}
diff --git a/spring-cloud-stream-schema-server/src/main/resources/META-INF/spring.factories b/spring-cloud-stream-schema-server/src/main/resources/META-INF/spring.factories
new file mode 100644
index 000000000..e69de29bb
diff --git a/spring-cloud-stream-schema-server/src/main/resources/application.yml b/spring-cloud-stream-schema-server/src/main/resources/application.yml
new file mode 100644
index 000000000..7961552db
--- /dev/null
+++ b/spring-cloud-stream-schema-server/src/main/resources/application.yml
@@ -0,0 +1,5 @@
+spring:
+ application:
+ name: SchemaRegistryServer
+server:
+ port: 8990
\ No newline at end of file
diff --git a/spring-cloud-stream-schema-server/src/test/java/org/springframework/cloud/stream/schema/server/SchemaRegistryServerAvroTests.java b/spring-cloud-stream-schema-server/src/test/java/org/springframework/cloud/stream/schema/server/SchemaRegistryServerAvroTests.java
new file mode 100644
index 000000000..b8f886d49
--- /dev/null
+++ b/spring-cloud-stream-schema-server/src/test/java/org/springframework/cloud/stream/schema/server/SchemaRegistryServerAvroTests.java
@@ -0,0 +1,160 @@
+/*
+ * Copyright 2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.cloud.stream.schema.server;
+
+import java.util.List;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.context.TestConfiguration;
+import org.springframework.boot.test.web.client.TestRestTemplate;
+import org.springframework.cloud.stream.schema.server.model.Schema;
+import org.springframework.context.annotation.Bean;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.test.context.junit4.SpringRunner;
+
+/**
+ * @author Vinicius Carvalho
+ */
+@RunWith(SpringRunner.class)
+@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
+public class SchemaRegistryServerAvroTests {
+
+ final String USER_SCHEMA_V1 = "{\"namespace\": \"example.avro\",\n"
+ + " \"type\": \"record\",\n" + " \"name\": \"User\",\n" + " \"fields\": [\n"
+ + " {\"name\": \"name\", \"type\": \"string\"},\n"
+ + " {\"name\": \"favorite_number\", \"type\": [\"int\", \"null\"]}\n"
+ + " ]\n" + "}";
+
+ final String USER_SCHEMA_V2 = "{\"namespace\": \"example.avro\",\n"
+ + " \"type\": \"record\",\n" + " \"name\": \"User\",\n" + " \"fields\": [\n"
+ + " {\"name\": \"name\", \"type\": \"string\"},\n"
+ + " {\"name\": \"favorite_number\", \"type\": [\"int\", \"null\"]},\n"
+ + " {\"name\": \"favorite_color\", \"type\": [\"string\", \"null\"]}\n"
+ + " ]\n" + "}";
+
+ @Autowired
+ private TestRestTemplate client;
+
+ @Test
+ public void testUnsupportedFormat() throws Exception {
+ Schema schema = new Schema();
+ schema.setFormat("spring");
+ schema.setSubject("boot");
+ ResponseEntity response = client.postForEntity("http://localhost:8990/",
+ schema, Schema.class);
+ Assert.assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
+ }
+
+ @Test
+ public void testInvalidSchema() throws Exception {
+ Schema schema = new Schema();
+ schema.setFormat("avro");
+ schema.setSubject("boot");
+ schema.setDefinition("{}");
+ ResponseEntity response = client.postForEntity("http://localhost:8990/",
+ schema, Schema.class);
+ Assert.assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
+ }
+
+ @Test
+ public void testUserSchemaV1() throws Exception {
+ Schema schema = new Schema();
+ schema.setFormat("avro");
+ schema.setSubject("org.springframework.cloud.stream.schema.User");
+ schema.setDefinition(USER_SCHEMA_V1);
+ ResponseEntity response = client.postForEntity("http://localhost:8990/",
+ schema, Schema.class);
+ Assert.assertTrue(response.getStatusCode().is2xxSuccessful());
+ Assert.assertEquals(new Integer(1), response.getBody().getVersion());
+ List location = response.getHeaders().get(HttpHeaders.LOCATION);
+ Assert.assertNotNull(location);
+ ResponseEntity persistedSchema = client.getForEntity(location.get(0),
+ Schema.class);
+ Assert.assertEquals(response.getBody().getId(),
+ persistedSchema.getBody().getId());
+
+ }
+
+ @Test
+ public void testUserSchemaV2() throws Exception {
+ Schema schema = new Schema();
+ schema.setFormat("avro");
+ schema.setSubject("org.springframework.cloud.stream.schema.User");
+ schema.setDefinition(USER_SCHEMA_V1);
+
+ Schema schema2 = new Schema();
+ schema2.setFormat("avro");
+ schema2.setSubject("org.springframework.cloud.stream.schema.User");
+ schema2.setDefinition(USER_SCHEMA_V2);
+
+ ResponseEntity response = client.postForEntity("http://localhost:8990/",
+ schema, Schema.class);
+ Assert.assertTrue(response.getStatusCode().is2xxSuccessful());
+ Assert.assertEquals(new Integer(1), response.getBody().getVersion());
+ List location = response.getHeaders().get(HttpHeaders.LOCATION);
+ Assert.assertNotNull(location);
+
+ ResponseEntity response2 = client.postForEntity("http://localhost:8990/",
+ schema2, Schema.class);
+ Assert.assertTrue(response.getStatusCode().is2xxSuccessful());
+ Assert.assertEquals(new Integer(2), response2.getBody().getVersion());
+ List location2 = response2.getHeaders().get(HttpHeaders.LOCATION);
+ Assert.assertNotNull(location2);
+
+ }
+
+ @Test
+ public void testIdempotentRegistration() throws Exception {
+ Schema schema = new Schema();
+ schema.setFormat("avro");
+ schema.setSubject("org.springframework.cloud.stream.schema.User");
+ schema.setDefinition(USER_SCHEMA_V1);
+ ResponseEntity response = client.postForEntity("http://localhost:8990/",
+ schema, Schema.class);
+ Assert.assertTrue(response.getStatusCode().is2xxSuccessful());
+ Assert.assertEquals(new Integer(1), response.getBody().getVersion());
+ List location = response.getHeaders().get(HttpHeaders.LOCATION);
+ Assert.assertNotNull(location);
+ ResponseEntity response2 = client.postForEntity("http://localhost:8990/",
+ schema, Schema.class);
+ Assert.assertEquals(response.getBody().getId(), response2.getBody().getId());
+
+ }
+
+ @Test
+ public void testSchemaNotfound() throws Exception {
+ ResponseEntity response = client
+ .getForEntity("http://localhost:8990/foo/avro/v42", Schema.class);
+ Assert.assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
+ }
+
+ @TestConfiguration
+ static class Config {
+ @Bean
+ public TestRestTemplate testRestTemplate() {
+ return new TestRestTemplate();
+ }
+ }
+
+}
diff --git a/spring-cloud-stream-schema/pom.xml b/spring-cloud-stream-schema/pom.xml
new file mode 100644
index 000000000..62170434d
--- /dev/null
+++ b/spring-cloud-stream-schema/pom.xml
@@ -0,0 +1,48 @@
+
+
+
+ spring-cloud-stream-parent
+ org.springframework.cloud
+ 1.1.0.BUILD-SNAPSHOT
+
+ 4.0.0
+
+ spring-cloud-stream-schema
+
+
+
+
+ org.springframework.cloud
+ spring-cloud-stream
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+ org.apache.avro
+ avro
+ 1.8.1
+ true
+
+
+ org.springframework.cloud
+ spring-cloud-stream-test-support
+ test
+
+
+ org.springframework.cloud
+ spring-cloud-stream-test-support-internal
+ test
+
+
+ org.springframework.cloud
+ spring-cloud-stream-schema-server
+ 1.1.0.BUILD-SNAPSHOT
+ test
+
+
+
diff --git a/spring-cloud-stream-schema/src/main/java/org/springframework/cloud/stream/schema/SchemaNotFoundException.java b/spring-cloud-stream-schema/src/main/java/org/springframework/cloud/stream/schema/SchemaNotFoundException.java
new file mode 100644
index 000000000..d02f957d5
--- /dev/null
+++ b/spring-cloud-stream-schema/src/main/java/org/springframework/cloud/stream/schema/SchemaNotFoundException.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.cloud.stream.schema;
+
+/**
+ * @author Vinicius Carvalho
+ */
+public class SchemaNotFoundException extends RuntimeException {
+
+ public SchemaNotFoundException(String message) {
+ super(message);
+ }
+}
diff --git a/spring-cloud-stream-schema/src/main/java/org/springframework/cloud/stream/schema/SchemaReference.java b/spring-cloud-stream-schema/src/main/java/org/springframework/cloud/stream/schema/SchemaReference.java
new file mode 100644
index 000000000..9ef5a628c
--- /dev/null
+++ b/spring-cloud-stream-schema/src/main/java/org/springframework/cloud/stream/schema/SchemaReference.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.cloud.stream.schema;
+
+import org.springframework.util.Assert;
+
+/**
+ * References a schema through its subject and version.
+ * @author Marius Bogoevici
+ */
+public class SchemaReference {
+
+ private String subject;
+
+ private int version;
+
+ private String format;
+
+ public SchemaReference(String subject, int version, String format) {
+ Assert.hasText(subject, "cannot be empty");
+ Assert.isTrue(version > 0, "must be a positive integer");
+ Assert.hasText(format, "cannot be empty");
+ this.subject = subject;
+ this.version = version;
+ this.format = format;
+ }
+
+ public String getSubject() {
+ return this.subject;
+ }
+
+ public void setSubject(String subject) {
+ Assert.hasText(subject, "cannot be empty");
+ this.subject = subject;
+ }
+
+ public int getVersion() {
+ return this.version;
+ }
+
+ public void setVersion(int version) {
+ Assert.isTrue(version > 0, "must be a positive integer");
+ this.version = version;
+ }
+
+ public String getFormat() {
+ return this.format;
+ }
+
+ public void setFormat(String format) {
+ Assert.hasText(format, "cannot be empty");
+ this.format = format;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+
+ SchemaReference that = (SchemaReference) o;
+
+ if (this.version != that.version) {
+ return false;
+ }
+ if (!this.subject.equals(that.subject)) {
+ return false;
+ }
+ return this.format.equals(that.format);
+
+ }
+
+ @Override
+ public int hashCode() {
+ int result = this.subject.hashCode();
+ result = 31 * result + this.version;
+ result = 31 * result + this.format.hashCode();
+ return result;
+ }
+
+ @Override
+ public String toString() {
+ return "SchemaReference{" +
+ "subject='" + this.subject + '\'' +
+ ", version=" + this.version +
+ ", format='" + this.format + '\'' +
+ '}';
+ }
+}
diff --git a/spring-cloud-stream-schema/src/main/java/org/springframework/cloud/stream/schema/SchemaRegistrationResponse.java b/spring-cloud-stream-schema/src/main/java/org/springframework/cloud/stream/schema/SchemaRegistrationResponse.java
new file mode 100644
index 000000000..5b98bbd1d
--- /dev/null
+++ b/spring-cloud-stream-schema/src/main/java/org/springframework/cloud/stream/schema/SchemaRegistrationResponse.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.cloud.stream.schema;
+
+/**
+ * @author Marius Bogoevici
+ */
+public class SchemaRegistrationResponse {
+
+ private long id;
+
+ private SchemaReference schemaReference;
+
+ public long getId() {
+ return this.id;
+ }
+
+ public void setId(long id) {
+ this.id = id;
+ }
+
+ public SchemaReference getSchemaReference() {
+ return this.schemaReference;
+ }
+
+ public void setSchemaReference(SchemaReference schemaReference) {
+ this.schemaReference = schemaReference;
+ }
+}
diff --git a/spring-cloud-stream-schema/src/main/java/org/springframework/cloud/stream/schema/avro/AbstractAvroMessageConverter.java b/spring-cloud-stream-schema/src/main/java/org/springframework/cloud/stream/schema/avro/AbstractAvroMessageConverter.java
new file mode 100644
index 000000000..7199c6c46
--- /dev/null
+++ b/spring-cloud-stream-schema/src/main/java/org/springframework/cloud/stream/schema/avro/AbstractAvroMessageConverter.java
@@ -0,0 +1,192 @@
+/*
+ * Copyright 2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.cloud.stream.schema.avro;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Collection;
+
+import org.apache.avro.Schema;
+import org.apache.avro.generic.GenericDatumReader;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.io.DatumReader;
+import org.apache.avro.io.DatumWriter;
+import org.apache.avro.io.Decoder;
+import org.apache.avro.io.DecoderFactory;
+import org.apache.avro.io.Encoder;
+import org.apache.avro.io.EncoderFactory;
+import org.apache.avro.reflect.ReflectDatumReader;
+import org.apache.avro.reflect.ReflectDatumWriter;
+import org.apache.avro.specific.SpecificDatumReader;
+import org.apache.avro.specific.SpecificDatumWriter;
+import org.apache.avro.specific.SpecificRecord;
+
+import org.springframework.core.io.Resource;
+import org.springframework.messaging.Message;
+import org.springframework.messaging.MessageHeaders;
+import org.springframework.messaging.converter.AbstractMessageConverter;
+import org.springframework.messaging.converter.MessageConversionException;
+import org.springframework.util.MimeType;
+
+/**
+ * Base class for Apache Avro {@link org.springframework.messaging.converter.MessageConverter} implementations.
+ * @author Marius Bogoevici
+ */
+public abstract class AbstractAvroMessageConverter extends AbstractMessageConverter {
+
+
+ protected AbstractAvroMessageConverter(MimeType supportedMimeType) {
+ super(supportedMimeType);
+ }
+
+ protected AbstractAvroMessageConverter(Collection supportedMimeTypes) {
+ super(supportedMimeTypes);
+ }
+
+ protected static Schema parseSchema(Resource r) throws IOException {
+ return new Schema.Parser().parse(r.getInputStream());
+ }
+
+ @Override
+ protected boolean canConvertFrom(Message> message, Class> targetClass) {
+ return super.canConvertFrom(message, targetClass) && (message.getPayload() instanceof byte[]);
+ }
+
+ @Override
+ protected Object convertFromInternal(Message> message, Class> targetClass, Object conversionHint) {
+ Object result = null;
+ try {
+ byte[] payload = (byte[]) message.getPayload();
+ ByteBuffer buf = ByteBuffer.wrap(payload);
+ MimeType mimeType = getContentTypeResolver().resolve(message.getHeaders());
+ if (mimeType == null) {
+ if (conversionHint instanceof MimeType) {
+ mimeType = (MimeType) conversionHint;
+ }
+ else {
+ return null;
+ }
+ }
+ buf.get(payload);
+ Schema writerSchema = resolveWriterSchemaForDeserialization(mimeType);
+ Schema readerSchema = resolveReaderSchemaForDeserialization(targetClass);
+ DatumReader