Search in sources :

Example 26 with JSONException

use of org.codehaus.jettison.json.JSONException in project incubator-atlas by apache.

the class AtlasClient method getEntityAuditEvents.

/**
     * Get the entity audit events in decreasing order of timestamp for the given entity id
     * @param entityId entity id
     * @param startKey key for the first event to be returned, used for pagination
     * @param numResults number of results to be returned
     * @return list of audit events for the entity id
     * @throws AtlasServiceException
     */
public List<EntityAuditEvent> getEntityAuditEvents(String entityId, String startKey, short numResults) throws AtlasServiceException {
    WebResource resource = getResource(API.LIST_ENTITY_AUDIT, entityId, URI_ENTITY_AUDIT);
    if (StringUtils.isNotEmpty(startKey)) {
        resource = resource.queryParam(START_KEY, startKey);
    }
    resource = resource.queryParam(NUM_RESULTS, String.valueOf(numResults));
    JSONObject jsonResponse = callAPIWithResource(API.LIST_ENTITY_AUDIT, resource);
    return extractResults(jsonResponse, AtlasClient.EVENTS, new ExtractOperation<EntityAuditEvent, JSONObject>() {

        @Override
        EntityAuditEvent extractElement(JSONObject element) throws JSONException {
            return SerDe.GSON.fromJson(element.toString(), EntityAuditEvent.class);
        }
    });
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) WebResource(com.sun.jersey.api.client.WebResource) JSONException(org.codehaus.jettison.json.JSONException)

Example 27 with JSONException

use of org.codehaus.jettison.json.JSONException in project incubator-atlas by apache.

the class AtlasClient method updateType.

/**
     * Register the given type(meta model)
     * @param typeAsJson type definition a jaon
     * @return result json object
     * @throws AtlasServiceException
     */
public List<String> updateType(String typeAsJson) throws AtlasServiceException {
    LOG.debug("Updating type definition: {}", typeAsJson);
    JSONObject response = callAPIWithBody(API.UPDATE_TYPE, typeAsJson);
    List<String> results = extractResults(response, AtlasClient.TYPES, new ExtractOperation<String, JSONObject>() {

        @Override
        String extractElement(JSONObject element) throws JSONException {
            return element.getString(AtlasClient.NAME);
        }
    });
    LOG.debug("Update type definition returned results: {}", results);
    return results;
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) JSONException(org.codehaus.jettison.json.JSONException)

Example 28 with JSONException

use of org.codehaus.jettison.json.JSONException 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 29 with JSONException

use of org.codehaus.jettison.json.JSONException in project incubator-atlas by apache.

the class GraphBackedTypeStore method getAttributes.

private AttributeDefinition[] getAttributes(AtlasVertex vertex, String typeName) throws AtlasException {
    List<AttributeDefinition> attributes = new ArrayList<>();
    List<String> attrNames = GraphHelper.getListProperty(vertex, getPropertyKey(typeName));
    if (attrNames != null) {
        for (String attrName : attrNames) {
            try {
                String encodedPropertyKey = GraphHelper.encodePropertyKey(getPropertyKey(typeName, attrName));
                AttributeDefinition attrValue = AttributeInfo.fromJson((String) vertex.getJsonProperty(encodedPropertyKey));
                if (attrValue != null) {
                    attributes.add(attrValue);
                }
            } catch (JSONException e) {
                throw new AtlasException(e);
            }
        }
    }
    return attributes.toArray(new AttributeDefinition[attributes.size()]);
}
Also used : ArrayList(java.util.ArrayList) JSONException(org.codehaus.jettison.json.JSONException) AtlasException(org.apache.atlas.AtlasException)

Example 30 with JSONException

use of org.codehaus.jettison.json.JSONException in project incubator-atlas by apache.

the class DefaultMetadataService method createOrUpdateTypes.

private JSONObject createOrUpdateTypes(OperationType opType, String typeDefinition, boolean isUpdate) throws AtlasException {
    typeDefinition = ParamChecker.notEmpty(typeDefinition, "type definition");
    TypesDef typesDef = validateTypeDefinition(opType, typeDefinition);
    try {
        final TypeSystem.TransientTypeSystem transientTypeSystem = typeSystem.createTransientTypeSystem(typesDef, isUpdate);
        final Map<String, IDataType> typesAdded = transientTypeSystem.getTypesAdded();
        try {
            /* Create indexes first so that if index creation fails then we rollback
                   the typesystem and also do not persist the graph
                 */
            if (isUpdate) {
                onTypesUpdated(typesAdded);
            } else {
                onTypesAdded(typesAdded);
            }
            typeStore.store(transientTypeSystem, ImmutableList.copyOf(typesAdded.keySet()));
            typeSystem.commitTypes(typesAdded);
        } catch (Throwable t) {
            throw new AtlasException("Unable to persist types ", t);
        }
        return new JSONObject() {

            {
                put(AtlasClient.TYPES, typesAdded.keySet());
            }
        };
    } catch (JSONException e) {
        LOG.error("Unable to create response for types={}", typeDefinition, e);
        throw new AtlasException("Unable to create response ", e);
    }
}
Also used : TypesDef(org.apache.atlas.typesystem.TypesDef) JSONObject(org.codehaus.jettison.json.JSONObject) JSONException(org.codehaus.jettison.json.JSONException) AtlasException(org.apache.atlas.AtlasException)

Aggregations

JSONException (org.codehaus.jettison.json.JSONException)281 JSONObject (org.codehaus.jettison.json.JSONObject)256 Response (javax.ws.rs.core.Response)183 Builder (javax.ws.rs.client.Invocation.Builder)179 ResteasyClientBuilder (org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder)179 Test (org.testng.annotations.Test)174 BaseTest (org.xdi.oxauth.BaseTest)174 Parameters (org.testng.annotations.Parameters)171 RegisterRequest (org.xdi.oxauth.client.RegisterRequest)78 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)68 JSONArray (org.codehaus.jettison.json.JSONArray)44 RegisterResponse (org.xdi.oxauth.client.RegisterResponse)43 URISyntaxException (java.net.URISyntaxException)35 TokenRequest (org.xdi.oxauth.client.TokenRequest)35 ResponseType (org.xdi.oxauth.model.common.ResponseType)35 WebApplicationException (javax.ws.rs.WebApplicationException)18 IOException (java.io.IOException)17 OxAuthCryptoProvider (org.xdi.oxauth.model.crypto.OxAuthCryptoProvider)17 Path (javax.ws.rs.Path)14 Produces (javax.ws.rs.Produces)14