@POST @Path("/subjects/{subject}") @ApiOperation(value = "Get schema information for the given schema subject and schema text", response = Schema.class, tags = OPERATION_GROUP_CONFLUENT_SR) @Timed @UnitOfWork public Response lookupSubjectVersion(@ApiParam(value = "Schema subject", required = true) @PathParam("subject") String subject, @ApiParam(value = "The schema ", required = true) String schema) { Response response; try { SchemaVersionInfo schemaVersionInfo = schemaRegistry.getSchemaVersionInfo(subject, schemaStringFromJson(schema).getSchema()); if (schemaVersionInfo != null) { response = WSUtils.respondEntity(new Schema(schemaVersionInfo.getName(), schemaVersionInfo.getVersion(), schemaVersionInfo.getId(), schemaVersionInfo.getSchemaText()), Response.Status.OK); } else { response = WSUtils.respond(Response.Status.NOT_FOUND, CatalogResponse.ResponseMessage.ENTITY_NOT_FOUND, subject); } } catch (InvalidSchemaException ex) { LOG.error("Given schema is invalid", ex); response = invalidSchemaError(); } catch (SchemaNotFoundException ex) { LOG.error("No schema found with subject [{}]", subject, ex); response = subjectNotFoundError(); } catch (Exception ex) { LOG.error("Encountered error while retrieving schema version with subject: [{}]", subject, ex); response = serverError(); } return response; }
@Override public List<Object> getResources() { Collection<Map<String, Object>> schemaProviders = (Collection<Map<String, Object>>) config.get(SCHEMA_PROVIDERS); DefaultSchemaRegistry schemaRegistry = new DefaultSchemaRegistry(storageManager, fileStorage, schemaProviders, haServerNotificationManager); schemaRegistry.init(config); SchemaRegistryVersion schemaRegistryVersion = SchemaRegistryServiceInfo.get().version(); LOG.info("SchemaRegistry is starting with {}", schemaRegistryVersion); SchemaRegistryResource schemaRegistryResource = new SchemaRegistryResource(schemaRegistry, leadershipParticipant, schemaRegistryVersion); ConfluentSchemaRegistryCompatibleResource confluentSchemaRegistryResource = new ConfluentSchemaRegistryCompatibleResource(schemaRegistry, leadershipParticipant); return Arrays.asList(schemaRegistryResource, confluentSchemaRegistryResource); }
@GET @Path("/schemas/ids/{id}") @ApiOperation(value = "Get schema version by id", response = Schema.class, tags = OPERATION_GROUP_CONFLUENT_SR) @Timed @UnitOfWork public Response getSchemaById(@ApiParam(value = "schema version id", required = true) @PathParam("id") Long id) { Response response; try { SchemaVersionInfo schemaVersionInfo = schemaRegistry.getSchemaVersionInfo(new SchemaIdVersion(id)); SchemaString schema = new SchemaString(); schema.setSchema(schemaVersionInfo.getSchemaText()); response = WSUtils.respondEntity(schema, Response.Status.OK); } catch (SchemaNotFoundException ex) { LOG.error("No schema version found with id [{}]", id, ex); response = schemaNotFoundError(); } catch (Exception ex) { LOG.error("Encountered error while retrieving Schema with id: [{}]", id, ex); response = serverError(); } return response; }
@GET @Path("/subjects/{subject}/versions") @ApiOperation(value = "Get all schema versions of given subject", response = Integer.class, responseContainer = "List", tags = OPERATION_GROUP_CONFLUENT_SR) @Timed @UnitOfWork public Response getAllVersions(@ApiParam(value = "subject", required = true) @PathParam("subject") String subject) { Response response; try { List<Integer> registeredSubjects = schemaRegistry.getAllVersions(subject) .stream() .map(SchemaVersionInfo::getVersion) .collect(Collectors.toList()); response = WSUtils.respondEntity(registeredSubjects, Response.Status.OK); } catch (SchemaNotFoundException ex) { LOG.error("No schema found with subject [{}]", subject, ex); response = subjectNotFoundError(); } catch (Exception ex) { LOG.error("Encountered error while retrieving all subjects", ex); response = serverError(); } return response; }
@Context UriInfo uriInfo) { LOG.info("registerSchema for [{}] is [{}]", subject); return handleLeaderAction(uriInfo, () -> { Response response; try { new SchemaVersion(schemaStringFromJson(schema).getSchema(), null)); response = invalidSchemaError(); } catch (IncompatibleSchemaException ex) { LOG.error("Incompatible schema error encountered while adding subject [{}]", subject, ex); response = incompatibleSchemaError(); } catch (UnsupportedSchemaTypeException ex) { LOG.error("Unsupported schema type encountered while adding subject [{}]", subject, ex); response = incompatibleSchemaError(); } catch (Exception ex) { LOG.error("Encountered error while adding subject [{}]", subject, ex); response = serverError();
response = versionNotFoundError(); } else { Schema schema = new Schema(schemaVersionInfo.getName(), response = subjectNotFoundError(); } catch (Exception ex) { LOG.error("Encountered error while retrieving all subjects", ex); response = serverError();
.request(MediaType.APPLICATION_JSON_TYPE) .get(); Assert.assertEquals(subjectNotFoundError().getEntity(), objectMapper.readValue(invalidSubjectResponse.readEntity(String.class), ErrorMessage.class)); .request(MediaType.APPLICATION_JSON_TYPE) .get(); Assert.assertEquals(versionNotFoundError().getEntity(), objectMapper.readValue(invalidVersionResponse.readEntity(String.class), ErrorMessage.class));
Assert.assertEquals(incompatibleSchemaError().getStatus(), response.getStatus()); ErrorMessage errorMessage = objectMapper.readValue(response.readEntity(String.class), ErrorMessage.class); Assert.assertEquals(((ErrorMessage) incompatibleSchemaError().getEntity()).getErrorCode(), errorMessage.getErrorCode()); Assert.assertEquals(invalidSchemaError().getStatus(), invalidSchemaResponse.getStatus()); ErrorMessage invalidSchemaErrorMessage = objectMapper.readValue(invalidSchemaResponse.readEntity(String.class), ErrorMessage.class); Assert.assertEquals(((ErrorMessage) invalidSchemaError().getEntity()).getErrorCode(), invalidSchemaErrorMessage.getErrorCode());
@Test public void testNonExistingSubject() throws Exception { // check non existing subject Response nonExistingSubjectResponse = rootTarget.path("/subjects/" + testName() + "/versions") .request(MediaType.APPLICATION_JSON_TYPE) .get(); ErrorMessage errorMessage = new ObjectMapper().readValue(nonExistingSubjectResponse.readEntity(String.class), ErrorMessage.class); Assert.assertEquals(subjectNotFoundError().getStatus(), nonExistingSubjectResponse.getStatus()); Assert.assertEquals(subjectNotFoundError().getEntity(), errorMessage); }
@Test public void testInValidSchemas() throws Exception { // add invalid schema Response invalidSchemaResponse = postSubjectSchema(testName(), fetchSchema("/device-unsupported-type.avsc")); Assert.assertEquals(invalidSchemaError().getStatus(), invalidSchemaResponse.getStatus()); ErrorMessage errorMessage = new ObjectMapper().readValue(invalidSchemaResponse.readEntity(String.class), ErrorMessage.class); Assert.assertEquals(invalidSchemaError().getEntity(), errorMessage); }
@Test public void testIncompatibleSchemas() throws Exception { String subject = testName(); String response = postSubjectSchema(subject, fetchSchema("/device.avsc")).readEntity(String.class); ObjectMapper objectMapper = new ObjectMapper(); long id = objectMapper.readValue(response, Id.class).getId(); Assert.assertTrue(id > 0); // add incompatible schema Response incompatSchemaResponse = postSubjectSchema(subject, fetchSchema("/device-incompat.avsc")); Assert.assertEquals(incompatibleSchemaError().getStatus(), incompatSchemaResponse.getStatus()); ErrorMessage errorMessage = objectMapper.readValue(incompatSchemaResponse.readEntity(String.class), ErrorMessage.class); Assert.assertEquals(incompatibleSchemaError().getEntity(), errorMessage); }
@GET @Path("/subjects") @ApiOperation(value = "Get all registered subjects", response = String.class, responseContainer = "List", tags = OPERATION_GROUP_CONFLUENT_SR) @Timed @UnitOfWork public Response getSubjects() { Response response; try { List<String> registeredSubjects = schemaRegistry.findSchemaMetadata(Collections.emptyMap()) .stream() .map(x -> x.getSchemaMetadata().getName()) .collect(Collectors.toList()); response = WSUtils.respondEntity(registeredSubjects, Response.Status.OK); } catch (Exception ex) { LOG.error("Encountered error while retrieving all subjects", ex); response = serverError(); } return response; }
@Context UriInfo uriInfo) { LOG.info("registerSchema for [{}] is [{}]", subject); return handleLeaderAction(uriInfo, () -> { Response response; try { new SchemaVersion(schemaStringFromJson(schema).getSchema(), null)); response = invalidSchemaError(); } catch (IncompatibleSchemaException ex) { LOG.error("Incompatible schema error encountered while adding subject [{}]", subject, ex); response = incompatibleSchemaError(); } catch (UnsupportedSchemaTypeException ex) { LOG.error("Unsupported schema type encountered while adding subject [{}]", subject, ex); response = incompatibleSchemaError(); } catch (Exception ex) { LOG.error("Encountered error while adding subject [{}]", subject, ex); response = serverError();
response = versionNotFoundError(); } else { Schema schema = new Schema(schemaVersionInfo.getName(), response = subjectNotFoundError(); } catch (Exception ex) { LOG.error("Encountered error while retrieving all subjects", ex); response = serverError();
@GET @Path("/subjects/{subject}/versions") @ApiOperation(value = "Get all schema versions of given subject", response = Integer.class, responseContainer = "List", tags = OPERATION_GROUP_CONFLUENT_SR) @Timed @UnitOfWork public Response getAllVersions(@ApiParam(value = "subject", required = true) @PathParam("subject") String subject) { Response response; try { List<Integer> registeredSubjects = schemaRegistry.getAllVersions(subject) .stream() .map(SchemaVersionInfo::getVersion) .collect(Collectors.toList()); response = WSUtils.respondEntity(registeredSubjects, Response.Status.OK); } catch (SchemaNotFoundException ex) { LOG.error("No schema found with subject [{}]", subject, ex); response = subjectNotFoundError(); } catch (Exception ex) { LOG.error("Encountered error while retrieving all subjects", ex); response = serverError(); } return response; }
@GET @Path("/subjects") @ApiOperation(value = "Get all registered subjects", response = String.class, responseContainer = "List", tags = OPERATION_GROUP_CONFLUENT_SR) @Timed @UnitOfWork public Response getSubjects() { Response response; try { List<String> registeredSubjects = schemaRegistry.findSchemaMetadata(Collections.emptyMap()) .stream() .map(x -> x.getSchemaMetadata().getName()) .collect(Collectors.toList()); response = WSUtils.respondEntity(registeredSubjects, Response.Status.OK); } catch (Exception ex) { LOG.error("Encountered error while retrieving all subjects", ex); response = serverError(); } return response; }
@POST @Path("/subjects/{subject}") @ApiOperation(value = "Get schema information for the given schema subject and schema text", response = Schema.class, tags = OPERATION_GROUP_CONFLUENT_SR) @Timed @UnitOfWork public Response lookupSubjectVersion(@ApiParam(value = "Schema subject", required = true) @PathParam("subject") String subject, @ApiParam(value = "The schema ", required = true) String schema) { Response response; try { SchemaVersionInfo schemaVersionInfo = schemaRegistry.getSchemaVersionInfo(subject, schemaStringFromJson(schema).getSchema()); if (schemaVersionInfo != null) { response = WSUtils.respondEntity(new Schema(schemaVersionInfo.getName(), schemaVersionInfo.getVersion(), schemaVersionInfo.getId(), schemaVersionInfo.getSchemaText()), Response.Status.OK); } else { response = WSUtils.respond(Response.Status.NOT_FOUND, CatalogResponse.ResponseMessage.ENTITY_NOT_FOUND, subject); } } catch (InvalidSchemaException ex) { LOG.error("Given schema is invalid", ex); response = invalidSchemaError(); } catch (SchemaNotFoundException ex) { LOG.error("No schema found with subject [{}]", subject, ex); response = subjectNotFoundError(); } catch (Exception ex) { LOG.error("Encountered error while retrieving schema version with subject: [{}]", subject, ex); response = serverError(); } return response; }
@GET @Path("/schemas/ids/{id}") @ApiOperation(value = "Get schema version by id", response = Schema.class, tags = OPERATION_GROUP_CONFLUENT_SR) @Timed @UnitOfWork public Response getSchemaById(@ApiParam(value = "schema version id", required = true) @PathParam("id") Long id) { Response response; try { SchemaVersionInfo schemaVersionInfo = schemaRegistry.getSchemaVersionInfo(new SchemaIdVersion(id)); SchemaString schema = new SchemaString(); schema.setSchema(schemaVersionInfo.getSchemaText()); response = WSUtils.respondEntity(schema, Response.Status.OK); } catch (SchemaNotFoundException ex) { LOG.error("No schema version found with id [{}]", id, ex); response = schemaNotFoundError(); } catch (Exception ex) { LOG.error("Encountered error while retrieving Schema with id: [{}]", id, ex); response = serverError(); } return response; }
@Override public List<Object> getResources() { Collection<Map<String, Object>> schemaProviders = (Collection<Map<String, Object>>) config.get(SCHEMA_PROVIDERS); DefaultSchemaRegistry schemaRegistry = new DefaultSchemaRegistry(storageManager, fileStorage, schemaProviders, haServerNotificationManager, new SchemaLockManager(transactionManager)); schemaRegistry.init(config); SchemaRegistryVersion schemaRegistryVersion = SchemaRegistryServiceInfo.get().version(); LOG.info("SchemaRegistry is starting with {}", schemaRegistryVersion); SchemaRegistryResource schemaRegistryResource = new SchemaRegistryResource(schemaRegistry, leadershipParticipant, schemaRegistryVersion); ConfluentSchemaRegistryCompatibleResource confluentSchemaRegistryResource = new ConfluentSchemaRegistryCompatibleResource(schemaRegistry, leadershipParticipant); return Arrays.asList(schemaRegistryResource, confluentSchemaRegistryResource); }