Search in sources :

Example 1 with EntityExistsException

use of org.apache.atlas.typesystem.exception.EntityExistsException in project incubator-atlas by apache.

the class EntityResource method submit.

/**
     * Submits the entity definitions (instances).
     * The body contains the JSONArray of entity json. The service takes care of de-duping the entities based on any
     * unique attribute for the give type.
     */
@POST
@Consumes({ Servlets.JSON_MEDIA_TYPE, MediaType.APPLICATION_JSON })
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response submit(@Context HttpServletRequest request) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> EntityResource.submit()");
    }
    String entityJson = null;
    AtlasPerfTracer perf = null;
    try {
        if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "EntityResource.submit()");
        }
        String entities = Servlets.getRequestPayload(request);
        //Handle backward compatibility - if entities is not JSONArray, convert to JSONArray
        try {
            new JSONArray(entities);
        } catch (JSONException e) {
            final String finalEntities = entities;
            entities = new JSONArray() {

                {
                    put(finalEntities);
                }
            }.toString();
        }
        entityJson = AtlasClient.toString(new JSONArray(entities));
        if (LOG.isDebugEnabled()) {
            LOG.debug("submitting entities {} ", entityJson);
        }
        AtlasEntitiesWithExtInfo entitiesInfo = restAdapters.toAtlasEntities(entities);
        EntityMutationResponse mutationResponse = entityREST.createOrUpdate(entitiesInfo);
        final List<String> guids = restAdapters.getGuids(mutationResponse.getCreatedEntities());
        if (LOG.isDebugEnabled()) {
            LOG.debug("Created entities {}", guids);
        }
        final CreateUpdateEntitiesResult result = restAdapters.toCreateUpdateEntitiesResult(mutationResponse);
        JSONObject response = getResponse(result);
        URI locationURI = getLocationURI(guids);
        return Response.created(locationURI).entity(response).build();
    } catch (AtlasBaseException e) {
        LOG.error("Unable to persist entity instance entityDef={}", entityJson, e);
        throw toWebApplicationException(e);
    } catch (EntityExistsException e) {
        LOG.error("Unique constraint violation for entity entityDef={}", entityJson, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.CONFLICT));
    } catch (ValueConversionException ve) {
        LOG.error("Unable to persist entity instance due to a deserialization error entityDef={}", entityJson, ve);
        throw new WebApplicationException(Servlets.getErrorResponse(ve.getCause() != null ? ve.getCause() : ve, Response.Status.BAD_REQUEST));
    } catch (AtlasException | IllegalArgumentException e) {
        LOG.error("Unable to persist entity instance entityDef={}", entityJson, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to persist entity instance entityDef={}", entityJson, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to persist entity instance entityDef={}", entityJson, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    } finally {
        AtlasPerfTracer.log(perf);
        if (LOG.isDebugEnabled()) {
            LOG.debug("<== EntityResource.submit()");
        }
    }
}
Also used : AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) JSONArray(org.codehaus.jettison.json.JSONArray) JSONException(org.codehaus.jettison.json.JSONException) AtlasException(org.apache.atlas.AtlasException) URI(java.net.URI) EntityExistsException(org.apache.atlas.typesystem.exception.EntityExistsException) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) JSONObject(org.codehaus.jettison.json.JSONObject) CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) ValueConversionException(org.apache.atlas.typesystem.types.ValueConversionException)

Example 2 with EntityExistsException

use of org.apache.atlas.typesystem.exception.EntityExistsException in project incubator-atlas by apache.

the class EntityResource method updateEntities.

/**
     * Complete update of a set of entities - the values not specified will be replaced with null/removed
     * Adds/Updates given entities identified by its GUID or unique attribute
     * @return response payload as json
     */
@PUT
@Consumes({ Servlets.JSON_MEDIA_TYPE, MediaType.APPLICATION_JSON })
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response updateEntities(@Context HttpServletRequest request) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> EntityResource.updateEntities()");
    }
    String entityJson = null;
    AtlasPerfTracer perf = null;
    try {
        if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "EntityResource.updateEntities()");
        }
        final String entities = Servlets.getRequestPayload(request);
        entityJson = AtlasClient.toString(new JSONArray(entities));
        if (LOG.isDebugEnabled()) {
            LOG.info("updating entities {} ", entityJson);
        }
        AtlasEntitiesWithExtInfo entitiesInfo = restAdapters.toAtlasEntities(entities);
        EntityMutationResponse mutationResponse = entityREST.createOrUpdate(entitiesInfo);
        CreateUpdateEntitiesResult result = restAdapters.toCreateUpdateEntitiesResult(mutationResponse);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Updated entities: {}", result.getEntityResult());
        }
        JSONObject response = getResponse(result);
        return Response.ok(response).build();
    } catch (AtlasBaseException e) {
        LOG.error("Unable to persist entity instance entityDef={}", entityJson, e);
        throw toWebApplicationException(e);
    } catch (EntityExistsException e) {
        LOG.error("Unique constraint violation for entityDef={}", entityJson, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.CONFLICT));
    } catch (ValueConversionException ve) {
        LOG.error("Unable to persist entity instance due to a deserialization error entityDef={}", entityJson, ve);
        throw new WebApplicationException(Servlets.getErrorResponse(ve.getCause(), Response.Status.BAD_REQUEST));
    } catch (AtlasException | IllegalArgumentException e) {
        LOG.error("Unable to persist entity instance entityDef={}", entityJson, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to persist entity instance entityDef={}", entityJson, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to persist entity instance entityDef={}", entityJson, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    } finally {
        AtlasPerfTracer.log(perf);
        if (LOG.isDebugEnabled()) {
            LOG.debug("<== EntityResource.updateEntities()");
        }
    }
}
Also used : AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) JSONArray(org.codehaus.jettison.json.JSONArray) AtlasException(org.apache.atlas.AtlasException) EntityExistsException(org.apache.atlas.typesystem.exception.EntityExistsException) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) JSONObject(org.codehaus.jettison.json.JSONObject) CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) ValueConversionException(org.apache.atlas.typesystem.types.ValueConversionException)

Example 3 with EntityExistsException

use of org.apache.atlas.typesystem.exception.EntityExistsException in project incubator-atlas by apache.

the class DefaultTypeSystem method createEntity.

@Override
public String createEntity(ResourceDefinition definition, Request request) throws ResourceAlreadyExistsException {
    String typeName = definition.getTypeName();
    try {
        createClassType(definition, typeName, typeName + " Definition");
    } catch (ResourceAlreadyExistsException e) {
    // ok if type already exists
    }
    try {
        Referenceable entity = new Referenceable(typeName, request.getQueryProperties());
        //add Taxonomy Namespace
        entity.set(TaxonomyResourceProvider.NAMESPACE_ATTRIBUTE_NAME, TaxonomyResourceProvider.TAXONOMY_NS);
        ITypedReferenceableInstance typedInstance = metadataService.getTypedReferenceableInstance(entity);
        ITypedReferenceableInstance[] entitiesToCreate = Collections.singletonList(typedInstance).toArray(new ITypedReferenceableInstance[1]);
        final List<String> entities = metadataService.createEntities(entitiesToCreate).getCreatedEntities();
        return entities != null && entities.size() > 0 ? entities.get(0) : null;
    } catch (EntityExistsException e) {
        throw new ResourceAlreadyExistsException("Attempted to create an entity which already exists: " + request.getQueryProperties());
    } catch (AtlasException e) {
        throw new CatalogRuntimeException("An expected exception occurred creating an entity: " + e, e);
    }
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) CatalogRuntimeException(org.apache.atlas.catalog.exception.CatalogRuntimeException) ResourceAlreadyExistsException(org.apache.atlas.catalog.exception.ResourceAlreadyExistsException) AtlasException(org.apache.atlas.AtlasException) EntityExistsException(org.apache.atlas.typesystem.exception.EntityExistsException)

Example 4 with EntityExistsException

use of org.apache.atlas.typesystem.exception.EntityExistsException in project incubator-atlas by apache.

the class TypedInstanceToGraphMapper method addOrUpdateAttributesAndTraits.

private List<String> addOrUpdateAttributesAndTraits(Operation operation, List<ITypedReferenceableInstance> instances) throws AtlasException {
    List<String> guids = new ArrayList<>();
    for (ITypedReferenceableInstance instance : instances) {
        try {
            //new vertex, set all the properties
            String guid = addOrUpdateAttributesAndTraits(operation, instance);
            guids.add(guid);
        } catch (AtlasSchemaViolationException e) {
            throw new EntityExistsException(instance, e);
        }
    }
    return guids;
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) ArrayList(java.util.ArrayList) AtlasSchemaViolationException(org.apache.atlas.repository.graphdb.AtlasSchemaViolationException) EntityExistsException(org.apache.atlas.typesystem.exception.EntityExistsException)

Example 5 with EntityExistsException

use of org.apache.atlas.typesystem.exception.EntityExistsException in project incubator-atlas by apache.

the class GraphBackedMetadataRepository method createEntities.

@Override
@GraphTransaction
public CreateUpdateEntitiesResult createEntities(ITypedReferenceableInstance... entities) throws RepositoryException, EntityExistsException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("adding entities={}", entities);
    }
    try {
        TypedInstanceToGraphMapper instanceToGraphMapper = new TypedInstanceToGraphMapper(graphToInstanceMapper, deleteHandler);
        instanceToGraphMapper.mapTypedInstanceToGraph(TypedInstanceToGraphMapper.Operation.CREATE, entities);
        List<String> createdGuids = RequestContext.get().getCreatedEntityIds();
        CreateUpdateEntitiesResult result = new CreateUpdateEntitiesResult();
        EntityResult entityResult = new EntityResult(createdGuids, null, null);
        GuidMapping mapping = instanceToGraphMapper.createGuidMapping();
        result.setEntityResult(entityResult);
        result.setGuidMapping(mapping);
        return result;
    } catch (EntityExistsException e) {
        throw e;
    } catch (AtlasException e) {
        throw new RepositoryException(e);
    }
}
Also used : CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult) RepositoryException(org.apache.atlas.repository.RepositoryException) EntityResult(org.apache.atlas.model.legacy.EntityResult) AtlasException(org.apache.atlas.AtlasException) EntityExistsException(org.apache.atlas.typesystem.exception.EntityExistsException) GuidMapping(org.apache.atlas.model.instance.GuidMapping) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Aggregations

EntityExistsException (org.apache.atlas.typesystem.exception.EntityExistsException)6 AtlasException (org.apache.atlas.AtlasException)5 CreateUpdateEntitiesResult (org.apache.atlas.CreateUpdateEntitiesResult)4 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)3 AtlasEntitiesWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo)3 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)3 ValueConversionException (org.apache.atlas.typesystem.types.ValueConversionException)3 AtlasPerfTracer (org.apache.atlas.utils.AtlasPerfTracer)3 JSONObject (org.codehaus.jettison.json.JSONObject)3 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)2 Referenceable (org.apache.atlas.typesystem.Referenceable)2 JSONArray (org.codehaus.jettison.json.JSONArray)2 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 GraphTransaction (org.apache.atlas.annotation.GraphTransaction)1 CatalogRuntimeException (org.apache.atlas.catalog.exception.CatalogRuntimeException)1 ResourceAlreadyExistsException (org.apache.atlas.catalog.exception.ResourceAlreadyExistsException)1 GuidMapping (org.apache.atlas.model.instance.GuidMapping)1 EntityResult (org.apache.atlas.model.legacy.EntityResult)1