/** * Copyright 2014 Confluent Inc. * * 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 io.confluent.kafka.schemaregistry.rest.resources; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import javax.validation.Valid; import javax.validation.constraints.NotNull; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.HeaderParam; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.container.AsyncResponse; import javax.ws.rs.container.Suspended; import io.confluent.kafka.schemaregistry.client.rest.Versions; import io.confluent.kafka.schemaregistry.client.rest.entities.Schema; import io.confluent.kafka.schemaregistry.client.rest.entities.requests.RegisterSchemaRequest; import io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryException; import io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException; import io.confluent.kafka.schemaregistry.rest.exceptions.Errors; import io.confluent.kafka.schemaregistry.storage.KafkaSchemaRegistry; import io.confluent.rest.annotations.PerformanceMetric; @Path("/subjects") @Produces({Versions.SCHEMA_REGISTRY_V1_JSON_WEIGHTED, Versions.SCHEMA_REGISTRY_DEFAULT_JSON_WEIGHTED, Versions.JSON_WEIGHTED}) @Consumes({Versions.SCHEMA_REGISTRY_V1_JSON, Versions.SCHEMA_REGISTRY_DEFAULT_JSON, Versions.JSON, Versions.GENERIC_REQUEST}) public class SubjectsResource { private static final Logger log = LoggerFactory.getLogger(SubjectsResource.class); private final KafkaSchemaRegistry schemaRegistry; public SubjectsResource(KafkaSchemaRegistry schemaRegistry) { this.schemaRegistry = schemaRegistry; } @POST @Path("/{subject}") @PerformanceMetric("subjects.get-schema") public void lookUpSchemaUnderSubject(final @Suspended AsyncResponse asyncResponse, final @HeaderParam("Content-Type") String contentType, final @HeaderParam("Accept") String accept, @PathParam("subject") String subject, @QueryParam("deleted") boolean lookupDeletedSchema, @NotNull RegisterSchemaRequest request) { // returns version if the schema exists. Otherwise returns 404 Map<String, String> headerProperties = new HashMap<String, String>(); headerProperties.put("Content-Type", contentType); headerProperties.put("Accept", accept); Schema schema = new Schema(subject, 0, 0, request.getSchema()); io.confluent.kafka.schemaregistry.client.rest.entities.Schema matchingSchema = null; try { if (!schemaRegistry.listSubjects().contains(subject)) { throw Errors.subjectNotFoundException(); } matchingSchema = schemaRegistry.lookUpSchemaUnderSubject(subject, schema, lookupDeletedSchema); } catch (SchemaRegistryException e) { throw Errors.schemaRegistryException("Error while looking up schema under subject " + subject, e); } if (matchingSchema == null) { throw Errors.schemaNotFoundException(); } asyncResponse.resume(matchingSchema); } @GET @Valid @PerformanceMetric("subjects.list") public Set<String> list() { try { return schemaRegistry.listSubjects(); } catch (SchemaRegistryStoreException e) { throw Errors.storeException("Error while listing subjects", e); } catch (SchemaRegistryException e) { throw Errors.schemaRegistryException("Error while listing subjects", e); } } @DELETE @Path("/{subject}") @PerformanceMetric("subjects.delete-subject") public void deleteSubject(final @Suspended AsyncResponse asyncResponse, @PathParam("subject") String subject) { List<Integer> deletedVersions; try { if (!schemaRegistry.listSubjects().contains(subject)) { throw Errors.subjectNotFoundException(); } deletedVersions = schemaRegistry.deleteSubjectOrForward(subject); } catch (SchemaRegistryException e) { throw Errors.schemaRegistryException("Error while deleting the subject " + subject, e); } asyncResponse.resume(deletedVersions); } }