Search in sources :

Example 1 with ValueConversionException

use of org.apache.atlas.typesystem.types.ValueConversionException 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 ValueConversionException

use of org.apache.atlas.typesystem.types.ValueConversionException 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 ValueConversionException

use of org.apache.atlas.typesystem.types.ValueConversionException in project incubator-atlas by apache.

the class GraphHelper method deserializeClassInstances.

public static ITypedReferenceableInstance[] deserializeClassInstances(TypeSystem typeSystem, String entityInstanceDefinition) throws AtlasException {
    try {
        JSONArray referableInstances = new JSONArray(entityInstanceDefinition);
        ITypedReferenceableInstance[] instances = new ITypedReferenceableInstance[referableInstances.length()];
        for (int index = 0; index < referableInstances.length(); index++) {
            Referenceable entityInstance = InstanceSerialization.fromJsonReferenceable(referableInstances.getString(index), true);
            ITypedReferenceableInstance typedInstrance = getTypedReferenceableInstance(typeSystem, entityInstance);
            instances[index] = typedInstrance;
        }
        return instances;
    } catch (ValueConversionException | TypeNotFoundException e) {
        throw e;
    } catch (Exception e) {
        // exception from deserializer
        LOG.error("Unable to deserialize json={}", entityInstanceDefinition, e);
        throw new IllegalArgumentException("Unable to deserialize json", e);
    }
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) TypeNotFoundException(org.apache.atlas.typesystem.exception.TypeNotFoundException) JSONArray(org.codehaus.jettison.json.JSONArray) ValueConversionException(org.apache.atlas.typesystem.types.ValueConversionException) RepositoryException(org.apache.atlas.repository.RepositoryException) EntityNotFoundException(org.apache.atlas.typesystem.exception.EntityNotFoundException) TypeNotFoundException(org.apache.atlas.typesystem.exception.TypeNotFoundException) AtlasException(org.apache.atlas.AtlasException) ValueConversionException(org.apache.atlas.typesystem.types.ValueConversionException)

Example 4 with ValueConversionException

use of org.apache.atlas.typesystem.types.ValueConversionException in project incubator-atlas by apache.

the class EntityResource method updateByUniqueAttribute.

/**
     * Adds/Updates given entity identified by its unique attribute( entityType, attributeName and value)
     * Updates support only partial update of an entity - Adds/updates any new values specified
     * Updates do not support removal of attribute values
     *
     * @param entityType the entity type
     * @param attribute the unique attribute used to identify the entity
     * @param value the unique attributes value
     * @param request The updated entity json
     * @return response payload as json
     * 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
@Path("qualifiedName")
@Consumes({ Servlets.JSON_MEDIA_TYPE, MediaType.APPLICATION_JSON })
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response updateByUniqueAttribute(@QueryParam("type") String entityType, @QueryParam("property") String attribute, @QueryParam("value") String value, @Context HttpServletRequest request) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> EntityResource.updateByUniqueAttribute({}, {}, {})", entityType, attribute, value);
    }
    AtlasPerfTracer perf = null;
    String entityJson = null;
    try {
        if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "EntityResource.updateByUniqueAttribute(" + entityType + ", " + attribute + ", " + value + ")");
        }
        entityJson = Servlets.getRequestPayload(request);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Partially updating entity by unique attribute {} {} {} {} ", entityType, attribute, value, entityJson);
        }
        Referenceable updatedEntity = InstanceSerialization.fromJsonReferenceable(entityJson, true);
        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);
        // update referenceable with Id if not specified in payload
        Id updateId = updatedEntity.getId();
        if (updateId != null && !updateId.isAssigned()) {
            String guid = AtlasGraphUtilsV1.getGuidByUniqueAttributes(getEntityType(entityType), attributes);
            updatedEntity.replaceWithNewId(new Id(guid, 0, updatedEntity.getTypeName()));
        }
        AtlasEntitiesWithExtInfo entitiesInfo = restAdapters.toAtlasEntity(updatedEntity);
        EntityMutationResponse mutationResponse = entitiesStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), true);
        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 partially update entity {} {}:{}.{}", entityJson, entityType, attribute, value, e);
        throw toWebApplicationException(e);
    } catch (ValueConversionException ve) {
        LOG.error("Unable to persist entity instance due to a deserialization error {} ", entityJson, ve);
        throw new WebApplicationException(Servlets.getErrorResponse(ve.getCause(), Response.Status.BAD_REQUEST));
    } catch (EntityExistsException e) {
        LOG.error("Unique constraint violation for entity {} ", entityJson, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.CONFLICT));
    } catch (EntityNotFoundException e) {
        LOG.error("An entity with type={} and qualifiedName={} does not exist {} ", entityType, value, entityJson, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.NOT_FOUND));
    } catch (AtlasException | IllegalArgumentException e) {
        LOG.error("Unable to partially update entity {} {}:{}.{}", entityJson, entityType, attribute, value, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to partially update entity {} {}:{}.{}", entityJson, entityType, attribute, value, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to partially update entity {} {}:{}.{}", entityJson, 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.updateByUniqueAttribute({}, {}, {})", entityType, attribute, value);
        }
    }
}
Also used : HashMap(java.util.HashMap) AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) EntityNotFoundException(org.apache.atlas.typesystem.exception.EntityNotFoundException) AtlasException(org.apache.atlas.AtlasException) AtlasEntityStream(org.apache.atlas.repository.store.graph.v1.AtlasEntityStream) EntityExistsException(org.apache.atlas.typesystem.exception.EntityExistsException) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) Referenceable(org.apache.atlas.typesystem.Referenceable) JSONObject(org.codehaus.jettison.json.JSONObject) CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) JSONObject(org.codehaus.jettison.json.JSONObject) Id(org.apache.atlas.typesystem.persistence.Id) ValueConversionException(org.apache.atlas.typesystem.types.ValueConversionException)

Example 5 with ValueConversionException

use of org.apache.atlas.typesystem.types.ValueConversionException in project incubator-atlas by apache.

the class StructInstance method set.

public void set(String attrName, Object val) throws AtlasException {
    AttributeInfo i = fieldMapping.fields.get(attrName);
    if (i == null) {
        throw new ValueConversionException(getTypeName(), val, "Unknown field " + attrName);
    }
    int pos = fieldMapping.fieldPos.get(attrName);
    int nullPos = fieldMapping.fieldNullPos.get(attrName);
    Object cVal = null;
    explicitSets[nullPos] = true;
    if (val != null && val instanceof Id) {
        ClassType clsType = TypeSystem.getInstance().getDataType(ClassType.class, i.dataType().getName());
        clsType.validateId((Id) val);
        cVal = val;
    } else {
        try {
            cVal = i.dataType().convert(val, i.multiplicity);
        } catch (ValueConversionException.NullConversionException e) {
            throw new ValueConversionException.NullConversionException("For field '" + attrName + "'", e);
        }
    }
    if (cVal == null) {
        nullFlags[nullPos] = true;
        return;
    }
    nullFlags[nullPos] = false;
    if (i.dataType() == DataTypes.BOOLEAN_TYPE) {
        bools[pos] = (Boolean) cVal;
    } else if (i.dataType() == DataTypes.BYTE_TYPE) {
        bytes[pos] = (Byte) cVal;
    } else if (i.dataType() == DataTypes.SHORT_TYPE) {
        shorts[pos] = (Short) cVal;
    } else if (i.dataType() == DataTypes.INT_TYPE) {
        ints[pos] = (Integer) cVal;
    } else if (i.dataType() == DataTypes.LONG_TYPE) {
        longs[pos] = (Long) cVal;
    } else if (i.dataType() == DataTypes.FLOAT_TYPE) {
        floats[pos] = (Float) cVal;
    } else if (i.dataType() == DataTypes.DOUBLE_TYPE) {
        doubles[pos] = (Double) cVal;
    } else if (i.dataType() == DataTypes.BIGINTEGER_TYPE) {
        bigIntegers[pos] = (BigInteger) cVal;
    } else if (i.dataType() == DataTypes.BIGDECIMAL_TYPE) {
        bigDecimals[pos] = (BigDecimal) cVal;
    } else if (i.dataType() == DataTypes.DATE_TYPE) {
        dates[pos] = (Date) cVal;
    } else if (i.dataType() == DataTypes.STRING_TYPE) {
        strings[pos] = (String) cVal;
    } else if (i.dataType().getTypeCategory() == DataTypes.TypeCategory.ENUM) {
        ints[pos] = ((EnumValue) cVal).ordinal;
    } else if (i.dataType().getTypeCategory() == DataTypes.TypeCategory.ARRAY) {
        arrays[pos] = (ImmutableList) cVal;
    } else if (i.dataType().getTypeCategory() == DataTypes.TypeCategory.MAP) {
        maps[pos] = (ImmutableMap) cVal;
    } else if (i.dataType().getTypeCategory() == DataTypes.TypeCategory.STRUCT || i.dataType().getTypeCategory() == DataTypes.TypeCategory.TRAIT) {
        structs[pos] = (StructInstance) cVal;
    } else if (i.dataType().getTypeCategory() == DataTypes.TypeCategory.CLASS) {
        if (cVal instanceof Id) {
            ids[pos] = (Id) cVal;
        } else {
            referenceables[pos] = (ReferenceableInstance) cVal;
        }
    } else {
        throw new AtlasException(String.format("Unknown datatype %s", i.dataType()));
    }
}
Also used : EnumValue(org.apache.atlas.typesystem.types.EnumValue) ClassType(org.apache.atlas.typesystem.types.ClassType) AtlasException(org.apache.atlas.AtlasException) Date(java.util.Date) ImmutableMap(com.google.common.collect.ImmutableMap) BigInteger(java.math.BigInteger) AttributeInfo(org.apache.atlas.typesystem.types.AttributeInfo) BigInteger(java.math.BigInteger) ValueConversionException(org.apache.atlas.typesystem.types.ValueConversionException)

Aggregations

AtlasException (org.apache.atlas.AtlasException)5 ValueConversionException (org.apache.atlas.typesystem.types.ValueConversionException)5 CreateUpdateEntitiesResult (org.apache.atlas.CreateUpdateEntitiesResult)3 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)3 AtlasEntitiesWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo)3 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)3 EntityExistsException (org.apache.atlas.typesystem.exception.EntityExistsException)3 AtlasPerfTracer (org.apache.atlas.utils.AtlasPerfTracer)3 JSONArray (org.codehaus.jettison.json.JSONArray)3 JSONObject (org.codehaus.jettison.json.JSONObject)3 Referenceable (org.apache.atlas.typesystem.Referenceable)2 EntityNotFoundException (org.apache.atlas.typesystem.exception.EntityNotFoundException)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 BigInteger (java.math.BigInteger)1 URI (java.net.URI)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 RepositoryException (org.apache.atlas.repository.RepositoryException)1 AtlasEntityStream (org.apache.atlas.repository.store.graph.v1.AtlasEntityStream)1 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)1