Search in sources :

Example 1 with JsonCreationException

use of org.opencastproject.adminui.exception.JsonCreationException in project opencast by opencast.

the class EndpointUtil method generateJSONObject.

/**
 * Returns a generated JSON object with key-value from given list.
 *
 * Note that JSONObject (and JSON in general) does not preserve key ordering,
 * so while the Map passed to this function may have ordered keys, the resulting
 * JSONObject is not ordered.
 *
 * @param list
 *          The source list for the JSON object
 * @return a JSON object containing the all the key-value as parameter
 * @throws JsonCreationException
 */
public static <T> JSONObject generateJSONObject(Map<String, T> list) throws JsonCreationException {
    JSONObject jsonList = new JSONObject();
    for (Entry<String, T> entry : list.entrySet()) {
        Object value = entry.getValue();
        if (value instanceof String) {
            jsonList.put(entry.getKey(), value);
        } else if (value instanceof JSONObject) {
            jsonList.put(entry.getKey(), value);
        } else if (value instanceof List) {
            Collection collection = (Collection) value;
            JSONArray jsonArray = new JSONArray();
            jsonArray.addAll(collection);
            jsonList.put(entry.getKey(), jsonArray);
        } else {
            throw new JsonCreationException("could not deal with " + value);
        }
    }
    return jsonList;
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) Collection(java.util.Collection) JSONObject(org.json.simple.JSONObject) JObject(com.entwinemedia.fn.data.json.JObject) List(java.util.List) JsonCreationException(org.opencastproject.adminui.exception.JsonCreationException)

Example 2 with JsonCreationException

use of org.opencastproject.adminui.exception.JsonCreationException in project opencast by opencast.

the class ListProvidersEndpoint method getList.

@GET
@Path("{source}.json")
@Produces(MediaType.APPLICATION_JSON)
@RestQuery(name = "list", description = "Provides key-value list from the given source", pathParameters = { @RestParameter(name = "source", description = "The source for the key-value list", isRequired = true, type = RestParameter.Type.STRING) }, restParameters = { @RestParameter(description = "The maximum number of items to return per page", isRequired = false, name = "limit", type = RestParameter.Type.INTEGER), @RestParameter(description = "The offset", isRequired = false, name = "offset", type = RestParameter.Type.INTEGER), @RestParameter(description = "Filters", isRequired = false, name = "filter", type = RestParameter.Type.STRING) }, reponses = { @RestResponse(description = "Returns the key-value list for the given source.", responseCode = HttpServletResponse.SC_OK) }, returnDescription = "")
public Response getList(@PathParam("source") final String source, @QueryParam("limit") final int limit, @QueryParam("filter") final String filter, @QueryParam("offset") final int offset, @Context HttpHeaders headers) {
    if (listProvidersService.hasProvider(source)) {
        ResourceListQueryImpl query = new ResourceListQueryImpl();
        query.setLimit(limit);
        query.setOffset(offset);
        addRequestFiltersToQuery(filter, query);
        Map<String, String> autocompleteList;
        try {
            autocompleteList = listProvidersService.getList(source, query, securityService.getOrganization(), false);
        } catch (ListProviderException e) {
            logger.error("Not able to get list from provider {}: {}", source, e);
            return SERVER_ERROR;
        }
        JSONObject jsonList;
        try {
            jsonList = generateJSONObject(autocompleteList);
        } catch (JsonCreationException e) {
            logger.error("Not able to generate resources list JSON from source {}: {}", source, e);
            return SERVER_ERROR;
        }
        return Response.ok(jsonList.toString()).build();
    }
    return NOT_FOUND;
}
Also used : EndpointUtil.generateJSONObject(org.opencastproject.adminui.endpoint.EndpointUtil.generateJSONObject) JSONObject(org.json.simple.JSONObject) ResourceListQueryImpl(org.opencastproject.index.service.resources.list.query.ResourceListQueryImpl) ListProviderException(org.opencastproject.index.service.exception.ListProviderException) JsonCreationException(org.opencastproject.adminui.exception.JsonCreationException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 3 with JsonCreationException

use of org.opencastproject.adminui.exception.JsonCreationException in project opencast by opencast.

the class ListProvidersEndpoint method getComponents.

@GET
@Path("components.json")
@Produces(MediaType.APPLICATION_JSON)
@RestQuery(name = "components", description = "Provides a set of constants lists (right now only eventCommentReasons) for use in the admin UI", reponses = { @RestResponse(description = "Returns a set of constants lists (right now only eventCommentReasons) for use in the admin UI", responseCode = HttpServletResponse.SC_OK) }, returnDescription = "")
public Response getComponents(@Context HttpHeaders headers) {
    String[] sources = { "eventCommentReasons" };
    ResourceListQuery query = new ResourceListQueryImpl();
    JSONObject list = new JSONObject();
    for (String source : sources) {
        if (listProvidersService.hasProvider(source)) {
            JSONObject subList;
            try {
                subList = generateJSONObject(listProvidersService.getList(source, query, securityService.getOrganization(), true));
                list.put(source, subList);
            } catch (JsonCreationException e) {
                logger.error("Not able to generate resources list JSON from source {}: {}", source, e);
                return SERVER_ERROR;
            } catch (ListProviderException e) {
                logger.error("Not able to get list from provider {}: {}", source, e);
                return SERVER_ERROR;
            }
        } else {
            return NOT_FOUND;
        }
    }
    return Response.ok(list.toString()).build();
}
Also used : EndpointUtil.generateJSONObject(org.opencastproject.adminui.endpoint.EndpointUtil.generateJSONObject) JSONObject(org.json.simple.JSONObject) ResourceListQueryImpl(org.opencastproject.index.service.resources.list.query.ResourceListQueryImpl) ListProviderException(org.opencastproject.index.service.exception.ListProviderException) JsonCreationException(org.opencastproject.adminui.exception.JsonCreationException) ResourceListQuery(org.opencastproject.index.service.resources.list.api.ResourceListQuery) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Aggregations

JSONObject (org.json.simple.JSONObject)3 JsonCreationException (org.opencastproject.adminui.exception.JsonCreationException)3 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 EndpointUtil.generateJSONObject (org.opencastproject.adminui.endpoint.EndpointUtil.generateJSONObject)2 ListProviderException (org.opencastproject.index.service.exception.ListProviderException)2 ResourceListQueryImpl (org.opencastproject.index.service.resources.list.query.ResourceListQueryImpl)2 RestQuery (org.opencastproject.util.doc.rest.RestQuery)2 JObject (com.entwinemedia.fn.data.json.JObject)1 Collection (java.util.Collection)1 List (java.util.List)1 JSONArray (org.json.simple.JSONArray)1 ResourceListQuery (org.opencastproject.index.service.resources.list.api.ResourceListQuery)1