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()");
}
}
}
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()");
}
}
}
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);
}
}
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;
}
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);
}
}
Aggregations