@Override public SchemaMetadataInfo fetch(String name) throws SchemaNotFoundException { try { return getEntity(currentSchemaRegistryTargets().schemasTarget.path(name), SchemaMetadataInfo.class); } catch (NotFoundException e) { throw new SchemaNotFoundException(e); } }
private void checkSchemaText(String schemaText) throws InvalidSchemaException { if(schemaText == null || schemaText.trim().isEmpty()) { throw new InvalidSchemaException(); } }
@Override public void deleteSchemaBranch(Long schemaBranchId) throws SchemaBranchNotFoundException, InvalidSchemaBranchDeletionException { WebTarget target = currentSchemaRegistryTargets().schemasTarget.path("branch/"+schemaBranchId); Response response = Subject.doAs(subject, new PrivilegedAction<Response>() { @Override public Response run() { return target.request().delete(); } }); int status = response.getStatus(); if (status == Response.Status.NOT_FOUND.getStatusCode()) { throw new SchemaBranchNotFoundException(response.readEntity(String.class)); } else if (status == Response.Status.BAD_REQUEST.getStatusCode()) { throw new InvalidSchemaBranchDeletionException(response.readEntity(String.class)); } else if (status != Response.Status.OK.getStatusCode()) { throw new RuntimeException(response.readEntity(String.class)); } }
@Override public SchemaVersionMergeResult mergeSchemaVersion(Long schemaVersionId) throws SchemaNotFoundException, IncompatibleSchemaException { WebTarget target = currentSchemaRegistryTargets().schemasTarget.path(schemaVersionId + "/merge"); Response response = Subject.doAs(subject, new PrivilegedAction<Response>() { @Override public Response run() { return target.request().post(null); } }); int status = response.getStatus(); if (status == Response.Status.OK.getStatusCode()) { String msg = response.readEntity(String.class); return readEntity(msg, SchemaVersionMergeResult.class); } else if (status == Response.Status.NOT_FOUND.getStatusCode()) { throw new SchemaNotFoundException(response.readEntity(String.class)); } else if (status == Response.Status.BAD_REQUEST.getStatusCode()) { throw new IncompatibleSchemaException(response.readEntity(String.class)); } else { throw new RuntimeException(response.readEntity(String.class)); } }
@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)); } }
private SchemaIdVersion handleSchemaIdVersionResponse(SchemaMetadataInfo schemaMetadataInfo, Response response) throws IncompatibleSchemaException, InvalidSchemaException { int status = response.getStatus(); String msg = response.readEntity(String.class); if (status == Response.Status.BAD_REQUEST.getStatusCode() || status == Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()) { CatalogResponse catalogResponse = readCatalogResponse(msg); if (CatalogResponse.ResponseMessage.INCOMPATIBLE_SCHEMA.getCode() == catalogResponse.getResponseCode()) { throw new IncompatibleSchemaException(catalogResponse.getResponseMessage()); } else if (CatalogResponse.ResponseMessage.INVALID_SCHEMA.getCode() == catalogResponse.getResponseCode()) { throw new InvalidSchemaException(catalogResponse.getResponseMessage()); } else { throw new RuntimeException(catalogResponse.getResponseMessage()); } } Integer version = readEntity(msg, Integer.class); SchemaVersionInfo schemaVersionInfo = doGetSchemaVersionInfo(new SchemaVersionKey(schemaMetadataInfo.getSchemaMetadata() .getName(), version)); return new SchemaIdVersion(schemaMetadataInfo.getId(), version, schemaVersionInfo.getId()); }
private static void checkCompatibility(SchemaVersionService schemaVersionService, SchemaMetadata schemaMetadata, String toSchemaText, String fromSchemaText) throws IncompatibleSchemaException { CompatibilityResult compatibilityResult = schemaVersionService.checkForCompatibility(schemaMetadata, toSchemaText, fromSchemaText); if (!compatibilityResult.isCompatible()) { String errMsg = String.format("Given schema is not compatible with latest schema versions. \n" + "Error location: [%s] \n" + "Error encountered is: [%s]", compatibilityResult.getErrorLocation(), compatibilityResult.getErrorMessage()); throw new IncompatibleSchemaException(errMsg); } }
@Override public SchemaMetadataInfo fetch(Long id) throws SchemaNotFoundException { try { return getEntity(currentSchemaRegistryTargets().schemasByIdTarget.path(id.toString()), SchemaMetadataInfo.class); } catch (NotFoundException e) { throw new SchemaNotFoundException(e); } } };
private boolean handleSchemaLifeCycleResponse(Response response) throws SchemaNotFoundException, SchemaLifecycleException { boolean result; int status = response.getStatus(); if (status == Response.Status.OK.getStatusCode()) { result = response.readEntity(Boolean.class); } else if (status == Response.Status.NOT_FOUND.getStatusCode()) { throw new SchemaNotFoundException(response.readEntity(String.class)); } else if (status == Response.Status.BAD_REQUEST.getStatusCode()) { CatalogResponse catalogResponse = readCatalogResponse(response.readEntity(String.class)); if (catalogResponse.getResponseCode() == CatalogResponse.ResponseMessage.INCOMPATIBLE_SCHEMA.getCode()) { throw new SchemaLifecycleException(new IncompatibleSchemaException(catalogResponse.getResponseMessage())); } throw new SchemaLifecycleException(catalogResponse.getResponseMessage()); } else { throw new RuntimeException(response.readEntity(String.class)); } return result; }
@Override public byte[] getFingerprint(String schemaText) throws InvalidSchemaException { try { return MessageDigest.getInstance(MD5).digest(schemaText.getBytes(UTF_8)); } catch (Exception e) { throw new InvalidSchemaException(e); } }
private static void checkCompatibility(SchemaVersionService schemaVersionService, SchemaMetadata schemaMetadata, String toSchemaText, String fromSchemaText) throws IncompatibleSchemaException { CompatibilityResult compatibilityResult = schemaVersionService.checkForCompatibility(schemaMetadata, toSchemaText, fromSchemaText); if (!compatibilityResult.isCompatible()) { String errMsg = String.format("Given schema is not compatible with latest schema versions. \n" + "Error location: [%s] \n" + "Error encountered is: [%s]", compatibilityResult.getErrorLocation(), compatibilityResult.getErrorMessage()); throw new IncompatibleSchemaException(errMsg); } }
private void handleDeleteSchemaResponse(Response response) throws SchemaNotFoundException, SchemaLifecycleException { String msg = response.readEntity(String.class); switch (Response.Status.fromStatusCode(response.getStatus())) { case NOT_FOUND: throw new SchemaNotFoundException(msg); case BAD_REQUEST: throw new SchemaLifecycleException(msg); case INTERNAL_SERVER_ERROR: throw new RuntimeException(msg); } }
@Override public byte[] getFingerprint(String schemaText) throws InvalidSchemaException, SchemaNotFoundException { try { // generates fingerprint of canonical form of the given schema. Schema schema = new Schema.Parser().parse(getResultantSchema(schemaText)); return MessageDigest.getInstance("MD5").digest(normalize(schema).getBytes()); } catch (IOException e) { throw new InvalidSchemaException("Given schema is invalid", e); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } }
private SchemaIdVersion doAddSchemaVersion(String schemaBranchName, String schemaName, SchemaVersion schemaVersion) throws IncompatibleSchemaException, InvalidSchemaException, SchemaNotFoundException { SchemaMetadataInfo schemaMetadataInfo = getSchemaMetadataInfo(schemaName); if (schemaMetadataInfo == null) { throw new SchemaNotFoundException("Schema with name " + schemaName + " not found"); } WebTarget target = currentSchemaRegistryTargets().schemasTarget.path(schemaName).path("/versions").queryParam("branch", schemaBranchName); Response response = Subject.doAs(subject, new PrivilegedAction<Response>() { @Override public Response run() { return target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(schemaVersion), Response.class); } }); return handleSchemaIdVersionResponse(schemaMetadataInfo, response); }
@Override public byte[] getFingerprint(String schemaText) throws InvalidSchemaException, SchemaNotFoundException { try { // generates fingerprint of canonical form of the given schema. Schema schema = new Schema.Parser().parse(getResultantSchema(schemaText)); return MessageDigest.getInstance("MD5").digest(normalize(schema).getBytes()); } catch (IOException e) { throw new InvalidSchemaException("Given schema is invalid", e); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } }
public SchemaIdVersion addSchemaVersion(String schemaBranchName, String schemaName, SchemaVersion schemaVersion) throws SchemaNotFoundException, IncompatibleSchemaException, InvalidSchemaException, SchemaBranchNotFoundException { Preconditions.checkNotNull(schemaBranchName, "Schema branch name can't be null"); Preconditions.checkNotNull(schemaName, "schemaName can't be null"); Preconditions.checkNotNull(schemaVersion, "schemaVersion can't be null"); checkSchemaText(schemaVersion.getSchemaText()); // check whether there exists schema-metadata for schema-metadata-key SchemaMetadataInfo schemaMetadataInfo = getSchemaMetadataInfo(schemaName); if (schemaMetadataInfo != null) { return addSchemaVersion(schemaBranchName, schemaMetadataInfo, schemaVersion); } else { throw new SchemaNotFoundException("SchemaMetadata not found with the schemaName: " + schemaName); } }
public SchemaVersionInfo getRootVersion(SchemaBranch schemaBranch) throws SchemaNotFoundException { if (schemaBranch.getName().equals(SchemaBranch.MASTER_BRANCH)) { throw new SchemaNotFoundException(String.format("There is no root schema version attached to the schema branch '%s'", schemaBranch.getName())); } List<SchemaVersionInfo> sortedVersionInfo; try { sortedVersionInfo = getSortedSchemaVersions(schemaBranch.getId()); } catch (SchemaBranchNotFoundException e) { throw new RuntimeException(e); } if (sortedVersionInfo == null) throw new SchemaNotFoundException(String.format("There were no schema versions attached to schema branch '%s'", schemaBranch.getName())); return sortedVersionInfo.iterator().next(); }
public SchemaVersionInfo getSchemaVersionInfo(String schemaName, String schemaText) throws SchemaNotFoundException, InvalidSchemaException, SchemaBranchNotFoundException { SchemaMetadataInfo schemaMetadataInfo = getSchemaMetadataInfo(schemaName); if (schemaMetadataInfo == null) { throw new SchemaNotFoundException("No schema found for schema metadata key: " + schemaName); } return findSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadataInfo.getSchemaMetadata() .getType(), schemaText, schemaName); }
private SchemaVersionInfo fetchSchemaVersionInfo(Long id) throws SchemaNotFoundException { StorableKey storableKey = new StorableKey(SchemaVersionStorable.NAME_SPACE, SchemaVersionStorable.getPrimaryKey(id)); SchemaVersionStorable versionedSchema = storageManager.get(storableKey); if (versionedSchema == null) { throw new SchemaNotFoundException("No Schema version exists with id " + id); } return versionedSchema.toSchemaVersionInfo(); }
private SchemaVersionInfo retrieveSchemaVersionInfo(SchemaVersionKey schemaVersionKey) throws SchemaNotFoundException { String schemaName = schemaVersionKey.getSchemaName(); Integer version = schemaVersionKey.getVersion(); SchemaMetadataInfo schemaMetadataInfo = getSchemaMetadataInfo(schemaName); if (schemaMetadataInfo == null) { throw new SchemaNotFoundException("No SchemaMetadata exists with key: " + schemaName); } return fetchSchemaVersionInfo(schemaVersionKey.getSchemaName(), version); }