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