@Override public SchemaMetadataInfo fetch(String name) throws SchemaNotFoundException { try { return getEntity(currentSchemaRegistryTargets().schemasTarget.path(name), SchemaMetadataInfo.class); } catch (NotFoundException e) { throw new SchemaNotFoundException(e); } }
@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 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 Collection<SchemaBranch> getSchemaBranches(String schemaName) throws SchemaNotFoundException { WebTarget target = currentSchemaRegistryTargets().schemasTarget.path(encode(schemaName) + "/branches"); Response response = Subject.doAs(subject, new PrivilegedAction<Response>() { @Override public Response run() { return target.request().get(); } }); int status = response.getStatus(); if (status == Response.Status.NOT_FOUND.getStatusCode()) { throw new SchemaNotFoundException(response.readEntity(String.class)); } else if (status != Response.Status.OK.getStatusCode()) { throw new RuntimeException(response.readEntity(String.class)); } return parseResponseAsEntities(response.readEntity(String.class), SchemaBranch.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 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); }
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 Collection<SchemaVersionInfo> getAllVersions(final String schemaName) throws SchemaNotFoundException { List<QueryParam> queryParams = Collections.singletonList(new QueryParam(SchemaVersionStorable.NAME, schemaName)); SchemaMetadataInfo schemaMetadataInfo = getSchemaMetadataInfo(schemaName); if (schemaMetadataInfo == null) { throw new SchemaNotFoundException("Schema not found with name " + schemaName); } Collection<SchemaVersionStorable> storables = storageManager.find(SchemaVersionStorable.NAME_SPACE, queryParams, Collections.singletonList(OrderByField.of(SchemaVersionStorable.VERSION, true))); List<SchemaVersionInfo> schemaVersionInfos; if (storables != null && !storables.isEmpty()) { schemaVersionInfos = storables .stream() .map(SchemaVersionStorable::toSchemaVersionInfo) .collect(Collectors.toList()); } else { schemaVersionInfos = Collections.emptyList(); } return schemaVersionInfos; }
private SchemaVersionInfo fetchSchemaVersionInfo(String schemaName, Integer version) throws SchemaNotFoundException { LOG.info("##### fetching schema version for name: [{}] version: [{}]", schemaName, version); SchemaVersionInfo schemaVersionInfo = null; if (SchemaVersionKey.LATEST_VERSION.equals(version)) { schemaVersionInfo = getLatestSchemaVersionInfo(schemaName); } else { List<QueryParam> queryParams = Lists.newArrayList( new QueryParam(SchemaVersionStorable.NAME, schemaName), new QueryParam(SchemaVersionStorable.VERSION, version.toString())); Collection<SchemaVersionStorable> versionedSchemas = storageManager.find(SchemaVersionStorable.NAME_SPACE, queryParams); if (versionedSchemas != null && !versionedSchemas.isEmpty()) { if (versionedSchemas.size() > 1) { LOG.warn("More than one schema exists with name: [{}] and version [{}]", schemaName, version); } schemaVersionInfo = versionedSchemas.iterator().next().toSchemaVersionInfo(); } else { throw new SchemaNotFoundException("No Schema version exists with name " + schemaName + " and version " + version); } } LOG.info("##### fetched schema version info [{}]", schemaVersionInfo); return schemaVersionInfo; }
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); } }
@GET @Path("/{schemaName}/versions/{version}") @Timed @Produces(MediaType.APPLICATION_JSON) public Response getSchemaForVersion(@PathParam("schemaName") String schemaName, @PathParam("version") String version, @Context SecurityContext securityContext) { try { LOG.info("Get schema:version [{}:{}]", schemaName, version); SchemaVersionInfo schemaVersionInfo = schemaRegistryClient.getSchemaVersionInfo(new SchemaVersionKey(schemaName, Integer.parseInt(version))); if (schemaVersionInfo != null) { LOG.debug("Received schema version from schema registry: [{}]", schemaVersionInfo); String schema = schemaVersionInfo.getSchemaText(); if (schema != null && !schema.isEmpty()) { schema = AvroStreamlineSchemaConverter.convertAvroSchemaToStreamlineSchema(schema); } LOG.debug("Converted schema: [{}]", schema); return WSUtils.respondEntity(schema, OK); } else { throw new SchemaNotFoundException("Version " + version + " of schema " + schemaName + " not found "); } } catch (SchemaNotFoundException e) { LOG.error("Schema not found: [{}]", schemaName, e); throw EntityNotFoundException.byName(schemaName); } catch (Exception e) { throw new RuntimeException(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; }
private ImmutablePair<SchemaVersionLifecycleContext, SchemaVersionLifecycleState> createSchemaVersionLifeCycleContextAndState(Long schemaVersionId) throws SchemaNotFoundException { // get the current state from storage for the given versionID // we can use a query to get max value for the column for a given schema-version-id but StorageManager does not // have API to take custom queries. Collection<SchemaVersionStateStorable> schemaVersionStates = storageManager.find(SchemaVersionStateStorable.NAME_SPACE, Collections.singletonList(new QueryParam(SchemaVersionStateStorable.SCHEMA_VERSION_ID, schemaVersionId.toString())), Collections.singletonList(OrderByField.of(SchemaVersionStateStorable.SEQUENCE, true))); if (schemaVersionStates.isEmpty()) { throw new SchemaNotFoundException("No schema versions found with id " + schemaVersionId); } SchemaVersionStateStorable stateStorable = schemaVersionStates.iterator().next(); SchemaVersionLifecycleState schemaVersionLifecycleState = schemaVersionLifecycleStateMachine.getStates() .get(stateStorable.getStateId()); SchemaVersionService schemaVersionService = createSchemaVersionService(); SchemaVersionLifecycleContext context = new SchemaVersionLifecycleContext(stateStorable.getSchemaVersionId(), stateStorable.getSequence(), schemaVersionService, schemaVersionLifecycleStateMachine, customSchemaStateExecutor); return new ImmutablePair<>(context, schemaVersionLifecycleState); }
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(); }
public SchemaVersionLifecycleContext createSchemaVersionLifeCycleContext(Long schemaVersionId, SchemaVersionLifecycleState schemaVersionLifecycleState) throws SchemaNotFoundException { // get the current state from storage for the given versionID // we can use a query to get max value for the column for a given schema-version-id but StorageManager does not // have API to take custom queries. List<QueryParam> queryParams = new ArrayList<>(); queryParams.add(new QueryParam(SchemaVersionStateStorable.SCHEMA_VERSION_ID, schemaVersionId.toString())); queryParams.add(new QueryParam(SchemaVersionStateStorable.STATE, schemaVersionLifecycleState.getId() .toString())); Collection<SchemaVersionStateStorable> schemaVersionStates = storageManager.find(SchemaVersionStateStorable.NAME_SPACE, queryParams, Collections.singletonList(OrderByField.of(SchemaVersionStateStorable.SEQUENCE, true))); if (schemaVersionStates.isEmpty()) { throw new SchemaNotFoundException("No schema versions found with id " + schemaVersionId); } SchemaVersionStateStorable stateStorable = schemaVersionStates.iterator().next(); SchemaVersionService schemaVersionService = createSchemaVersionService(); SchemaVersionLifecycleContext context = new SchemaVersionLifecycleContext(stateStorable.getSchemaVersionId(), stateStorable.getSequence(), schemaVersionService, schemaVersionLifecycleStateMachine, customSchemaStateExecutor); context.setDetails(stateStorable.getDetails()); return context; }
public SchemaIdVersion uploadSchemaVersion(final String schemaBranchName, final String schemaName, final String description, final InputStream schemaVersionInputStream) throws InvalidSchemaException, IncompatibleSchemaException, SchemaNotFoundException, SchemaBranchNotFoundException { SchemaMetadataInfo schemaMetadataInfo = getSchemaMetadataInfo(schemaName); if (schemaMetadataInfo == null) { throw new SchemaNotFoundException("Schema with name " + schemaName + " not found"); } StreamDataBodyPart streamDataBodyPart = new StreamDataBodyPart("file", schemaVersionInputStream); WebTarget target = currentSchemaRegistryTargets().schemasTarget.path(schemaName).path("/versions/upload").queryParam("branch",schemaBranchName); MultiPart multipartEntity = new FormDataMultiPart() .field("description", description, MediaType.APPLICATION_JSON_TYPE) .bodyPart(streamDataBodyPart); Entity<MultiPart> multiPartEntity = Entity.entity(multipartEntity, MediaType.MULTIPART_FORM_DATA); Response response = Subject.doAs(subject, new PrivilegedAction<Response>() { @Override public Response run() { return target.request().post(multiPartEntity, Response.class); } }); return handleSchemaIdVersionResponse(schemaMetadataInfo, response); }
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); }
SchemaMetadataInfo schemaMetadataInfo = schemaRegistry.getSchemaMetadataInfo(subject); if (schemaMetadataInfo == null) { throw new SchemaNotFoundException();