use of org.apache.atlas.exception.AtlasBaseException in project atlas by apache.
the class EntityResource method addTrait.
/**
* Adds a new trait to an existing entity represented by a guid.
*
* @param guid globally unique identifier for the entity
*/
@POST
@Path("{guid}/traits")
@Consumes({ Servlets.JSON_MEDIA_TYPE, MediaType.APPLICATION_JSON })
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response addTrait(@Context HttpServletRequest request, @PathParam("guid") final String guid) {
if (LOG.isDebugEnabled()) {
LOG.debug("==> EntityResource.addTrait({})", guid);
}
String traitDefinition = null;
AtlasPerfTracer perf = null;
try {
if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "EntityResource.addTrait(" + guid + ")");
}
traitDefinition = Servlets.getRequestPayload(request);
if (LOG.isDebugEnabled()) {
LOG.debug("Adding trait={} for entity={} ", traitDefinition, guid);
}
List<String> guids = new ArrayList<String>() {
{
add(guid);
}
};
entitiesStore.addClassification(guids, restAdapters.toAtlasClassification(AtlasType.fromV1Json(traitDefinition, Struct.class)));
URI locationURI = getLocationURI(new ArrayList<String>() {
{
add(guid);
}
});
Map<String, Object> response = new HashMap<>();
response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId());
return Response.created(locationURI).entity(AtlasJson.toV1Json(response)).build();
} catch (AtlasBaseException e) {
LOG.error("Unable to add trait for entity={} traitDef={}", guid, traitDefinition, e);
throw toWebApplicationException(e);
} catch (IllegalArgumentException e) {
LOG.error("Unable to add trait for entity={} traitDef={}", guid, traitDefinition, e);
throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
} catch (WebApplicationException e) {
LOG.error("Unable to add trait for entity={} traitDef={}", guid, traitDefinition, e);
throw e;
} catch (Throwable e) {
LOG.error("Unable to add trait for entity={} traitDef={}", guid, traitDefinition, e);
throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
} finally {
AtlasPerfTracer.log(perf);
if (LOG.isDebugEnabled()) {
LOG.debug("<== EntityResource.addTrait({})", guid);
}
}
}
use of org.apache.atlas.exception.AtlasBaseException in project atlas by apache.
the class EntityResource method deleteEntities.
/**
* Delete entities from the repository identified by their guids (including their composite references)
* or
* Deletes a single entity identified by its type and unique attribute value from the repository (including their composite references)
*
* @param guids list of deletion candidate guids
* or
* @param entityType the entity type
* @param attribute the unique attribute used to identify the entity
* @param value the unique attribute value used to identify the entity
* @return response payload as json - including guids of entities(including composite references from that entity) that were deleted
*/
@DELETE
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response deleteEntities(@QueryParam("guid") List<String> guids, @QueryParam("type") String entityType, @QueryParam("property") final String attribute, @QueryParam("value") final String value) {
if (LOG.isDebugEnabled()) {
LOG.debug("==> EntityResource.deleteEntities({}, {}, {}, {})", guids, entityType, attribute, value);
}
AtlasPerfTracer perf = null;
try {
if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "EntityResource.deleteEntities(" + guids + ", " + entityType + ", " + attribute + ", " + value + ")");
}
EntityResult entityResult;
if (guids != null && !guids.isEmpty()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Deleting entities {}", guids);
}
EntityMutationResponse mutationResponse = entityREST.deleteByGuids(guids);
entityResult = restAdapters.toCreateUpdateEntitiesResult(mutationResponse).getEntityResult();
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Deleting entity type={} with property {}={}", entityType, attribute, value);
}
Map<String, Object> attributes = new HashMap<>();
attributes.put(attribute, value);
EntityMutationResponse mutationResponse = entitiesStore.deleteByUniqueAttributes(getEntityType(entityType), attributes);
entityResult = restAdapters.toCreateUpdateEntitiesResult(mutationResponse).getEntityResult();
}
if (LOG.isDebugEnabled()) {
LOG.debug("Deleted entity result: {}", entityResult);
}
String response = getResponse(entityResult);
return Response.ok(response).build();
} catch (AtlasBaseException e) {
LOG.error("Unable to delete entities {} {} {} {} ", guids, entityType, attribute, value, e);
throw toWebApplicationException(e);
} catch (AtlasException | IllegalArgumentException e) {
LOG.error("Unable to delete entities {} {} {} {} ", guids, entityType, attribute, value, e);
throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
} catch (WebApplicationException e) {
LOG.error("Unable to delete entities {} {} {} {} ", guids, entityType, attribute, value, e);
throw e;
} catch (Throwable e) {
LOG.error("Unable to delete entities {} {} {} {} ", guids, entityType, attribute, value, e);
throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
} finally {
AtlasPerfTracer.log(perf);
if (LOG.isDebugEnabled()) {
LOG.debug("<== EntityResource.deleteEntities({}, {}, {}, {})", guids, entityType, attribute, value);
}
}
}
use of org.apache.atlas.exception.AtlasBaseException in project atlas by apache.
the class EntityResource method getEntityDefinition.
/**
* Fetch the complete definition of an entity given its GUID.
*
* @param guid GUID for the entity
*/
@GET
@Path("{guid}")
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response getEntityDefinition(@PathParam("guid") String guid) {
if (LOG.isDebugEnabled()) {
LOG.debug("==> EntityResource.getEntityDefinition({})", guid);
}
AtlasPerfTracer perf = null;
try {
if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "EntityResource.getEntityDefinition(" + guid + ")");
}
if (LOG.isDebugEnabled()) {
LOG.debug("Fetching entity definition for guid={} ", guid);
}
guid = ParamChecker.notEmpty(guid, "guid cannot be null");
Referenceable entity = getEntity(guid);
Map<String, Object> response = new HashMap<>();
response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId());
Response.Status status = Response.Status.NOT_FOUND;
if (entity != null) {
response.put(AtlasClient.DEFINITION, entity);
status = Response.Status.OK;
} else {
response.put(AtlasClient.ERROR, Servlets.escapeJsonString(String.format("An entity with GUID={%s} does not exist", guid)));
}
return Response.status(status).entity(AtlasJson.toV1Json(response)).build();
} catch (IllegalArgumentException e) {
LOG.error("Bad GUID={} ", guid, e);
throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
} catch (AtlasBaseException e) {
LOG.error("Unable to get instance definition for GUID {}", guid, e);
throw toWebApplicationException(e);
} catch (WebApplicationException e) {
LOG.error("Unable to get instance definition for GUID {}", guid, e);
throw e;
} catch (Throwable e) {
LOG.error("Unable to get instance definition for GUID {}", guid, e);
throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
} finally {
AtlasPerfTracer.log(perf);
if (LOG.isDebugEnabled()) {
LOG.debug("<== EntityResource.getEntityDefinition({})", guid);
}
}
}
use of org.apache.atlas.exception.AtlasBaseException in project 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()");
}
entityJson = Servlets.getRequestPayload(request);
// Handle backward compatibility - if entities is not JSONArray, convert to JSONArray
String[] jsonStrings;
try {
ArrayNode jsonEntities = AtlasJson.parseToV1ArrayNode(entityJson);
jsonStrings = new String[jsonEntities.size()];
for (int i = 0; i < jsonEntities.size(); i++) {
jsonStrings[i] = AtlasJson.toV1Json(jsonEntities.get(i));
}
} catch (IOException e) {
jsonStrings = new String[1];
jsonStrings[0] = entityJson;
}
if (LOG.isDebugEnabled()) {
LOG.debug("Updating entities: count={}; entities-json={}", jsonStrings.length, entityJson);
}
AtlasEntitiesWithExtInfo entitiesInfo = restAdapters.toAtlasEntities(jsonStrings);
EntityMutationResponse mutationResponse = entityREST.createOrUpdate(entitiesInfo);
CreateUpdateEntitiesResult result = restAdapters.toCreateUpdateEntitiesResult(mutationResponse);
if (LOG.isDebugEnabled()) {
LOG.debug("Updated entities: {}", result.getEntityResult());
}
String 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 (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.exception.AtlasBaseException in project atlas by apache.
the class EntityResource method getEntityDefinitionByAttribute.
/**
* Fetch the complete definition of an entity given its qualified name.
*
* @param entityType
* @param attribute
* @param value
*/
public Response getEntityDefinitionByAttribute(String entityType, String attribute, String value) {
try {
if (LOG.isDebugEnabled()) {
LOG.debug("Fetching entity definition for type={}, qualified name={}", entityType, value);
}
entityType = ParamChecker.notEmpty(entityType, "Entity type cannot be null");
attribute = ParamChecker.notEmpty(attribute, "attribute name cannot be null");
value = ParamChecker.notEmpty(value, "attribute value cannot be null");
Map<String, Object> attributes = new HashMap<>();
attributes.put(attribute, value);
AtlasEntityWithExtInfo entityInfo;
try {
entityInfo = entitiesStore.getByUniqueAttributes(getEntityType(entityType), attributes);
} catch (AtlasBaseException e) {
LOG.error("Cannot find entity with type: {}, attribute: {} and value: {}", entityType, attribute, value);
throw toWebApplicationException(e);
}
Referenceable entity = null;
if (entityInfo != null) {
entity = restAdapters.getReferenceable(entityInfo);
}
Map<String, Object> response = new HashMap<>();
response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId());
Response.Status status = Response.Status.NOT_FOUND;
if (entity != null) {
response.put(AtlasClient.DEFINITION, entity);
status = Response.Status.OK;
} else {
response.put(AtlasClient.ERROR, Servlets.escapeJsonString(String.format("An entity with type={%s}, " + "qualifiedName={%s} does not exist", entityType, value)));
}
return Response.status(status).entity(AtlasJson.toV1Json(response)).build();
} catch (AtlasBaseException e) {
LOG.error("Unable to get instance definition for type={}, qualifiedName={}", entityType, value, e);
throw toWebApplicationException(e);
} catch (IllegalArgumentException e) {
LOG.error("Bad type={}, qualifiedName={}", entityType, value, e);
throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
} catch (WebApplicationException e) {
LOG.error("Unable to get instance definition for type={}, qualifiedName={}", entityType, value, e);
throw e;
} catch (Throwable e) {
LOG.error("Unable to get instance definition for type={}, qualifiedName={}", entityType, value, e);
throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
}
}
Aggregations