@Override public SchemaBranch createSchemaBranch(Long schemaVersionId, SchemaBranch schemaBranch) throws SchemaBranchAlreadyExistsException, SchemaNotFoundException { WebTarget target = currentSchemaRegistryTargets().schemasTarget.path("versionsById/"+schemaVersionId + "/branch"); Response response = Subject.doAs(subject, new PrivilegedAction<Response>() { @Override public Response run() { return target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(schemaBranch), Response.class); } }); int status = response.getStatus(); if (status == Response.Status.OK.getStatusCode()) { String msg = response.readEntity(String.class); SchemaBranch returnedSchemaBranch = readEntity(msg, SchemaBranch.class); return returnedSchemaBranch; } else if (status == Response.Status.BAD_REQUEST.getStatusCode()) { throw new SchemaNotFoundException(response.readEntity(String.class)); } else if (status == Response.Status.CONFLICT.getStatusCode()) { throw new SchemaBranchAlreadyExistsException(response.readEntity(String.class)); } else { throw new RuntimeException(response.readEntity(String.class)); } }
@Override public SchemaBranch createSchemaBranch(Long schemaVersionId, SchemaBranch schemaBranch) throws SchemaBranchAlreadyExistsException, SchemaNotFoundException { Preconditions.checkNotNull(schemaBranch.getName(), "Schema branch name can't be null"); SchemaVersionInfo schemaVersionInfo = schemaVersionLifecycleManager.getSchemaVersionInfo(new SchemaIdVersion(schemaVersionId)); SchemaBranchKey schemaBranchKey = new SchemaBranchKey(schemaBranch.getName(), schemaVersionInfo.getName()); SchemaBranch existingSchemaBranch = null; try { existingSchemaBranch = schemaBranchCache.get(SchemaBranchCache.Key.of(schemaBranchKey)); } catch (SchemaBranchNotFoundException e) { // Ignore this error } if (existingSchemaBranch != null) throw new SchemaBranchAlreadyExistsException(String.format("A schema branch with name : '%s' already exists", schemaBranch.getName())); SchemaBranchStorable schemaBranchStorable = SchemaBranchStorable.from(schemaBranch); schemaBranchStorable.setSchemaMetadataName(schemaVersionInfo.getName()); schemaBranchStorable.setId(storageManager.nextId(SchemaBranchStorable.NAME_SPACE)); storageManager.add(schemaBranchStorable); SchemaBranch persistedSchemaBranch; try { persistedSchemaBranch = schemaBranchCache.get(SchemaBranchCache.Key.of(schemaBranchKey)); } catch (SchemaBranchNotFoundException e) { throw new RuntimeException(String.format("Failed to fetch persisted schema branch : '%s' from the database", schemaBranch.getName())); } SchemaBranchVersionMapping schemaBranchVersionMapping = new SchemaBranchVersionMapping(persistedSchemaBranch.getId(), schemaVersionInfo.getId()); storageManager.add(schemaBranchVersionMapping); return persistedSchemaBranch; }