use of org.opencastproject.rest.BulkOperationResult in project opencast by opencast.
the class SeriesServiceRemoteImpl method updateOptOutStatus.
@Override
public void updateOptOutStatus(String seriesId, boolean optOut) throws NotFoundException, SeriesException {
HttpPost post = new HttpPost("/optOutSeries/" + optOut);
HttpResponse response = null;
try {
JSONArray seriesIds = new JSONArray();
seriesIds.put(seriesId);
post.setEntity(new StringEntity(seriesIds.toString()));
response = getResponse(post, SC_OK);
BulkOperationResult bulkOperationResult = new BulkOperationResult();
bulkOperationResult.fromJson(response.getEntity().getContent());
if (bulkOperationResult.getNotFound().size() > 0) {
throw new NotFoundException("Unable to find series with id " + seriesId);
} else if (bulkOperationResult.getServerError().size() > 0) {
throw new SeriesException("Unable to update series " + seriesId + " opt out status using the remote series services.");
}
} catch (Exception e) {
throw new SeriesException("Unable to assemble a remote series request for updating series " + seriesId + " with optOut status of " + optOut + " because:" + ExceptionUtils.getStackTrace(e));
} finally {
if (response != null) {
closeConnection(response);
}
}
throw new SeriesException("Unable to update series " + seriesId + " using the remote series services");
}
use of org.opencastproject.rest.BulkOperationResult in project opencast by opencast.
the class AbstractEventEndpoint method deleteEvents.
@POST
@Path("deleteEvents")
@Produces(MediaType.APPLICATION_JSON)
@RestQuery(name = "deleteevents", description = "Deletes a json list of events by their given ids e.g. [\"1dbe7255-e17d-4279-811d-a5c7ced689bf\", \"04fae22b-0717-4f59-8b72-5f824f76d529\"]", returnDescription = "Returns a JSON object containing a list of event ids that were deleted, not found or if there was a server error.", reponses = { @RestResponse(description = "Events have been deleted", responseCode = HttpServletResponse.SC_OK), @RestResponse(description = "The list of ids could not be parsed into a json list.", responseCode = HttpServletResponse.SC_BAD_REQUEST), @RestResponse(description = "If the current user is not authorized to perform this action", responseCode = HttpServletResponse.SC_UNAUTHORIZED) })
public Response deleteEvents(String eventIdsContent) throws UnauthorizedException {
if (StringUtils.isBlank(eventIdsContent)) {
return Response.status(Response.Status.BAD_REQUEST).build();
}
JSONParser parser = new JSONParser();
JSONArray eventIdsJsonArray;
try {
eventIdsJsonArray = (JSONArray) parser.parse(eventIdsContent);
} catch (org.json.simple.parser.ParseException e) {
logger.error("Unable to parse '{}' because: {}", eventIdsContent, ExceptionUtils.getStackTrace(e));
return Response.status(Response.Status.BAD_REQUEST).build();
} catch (ClassCastException e) {
logger.error("Unable to cast '{}' because: {}", eventIdsContent, ExceptionUtils.getStackTrace(e));
return Response.status(Response.Status.BAD_REQUEST).build();
}
BulkOperationResult result = new BulkOperationResult();
for (Object eventIdObject : eventIdsJsonArray) {
String eventId = eventIdObject.toString();
try {
if (!getIndexService().removeEvent(eventId)) {
result.addServerError(eventId);
} else {
result.addOk(eventId);
}
} catch (NotFoundException e) {
result.addNotFound(eventId);
}
}
return Response.ok(result.toJson()).build();
}
use of org.opencastproject.rest.BulkOperationResult in project opencast by opencast.
the class SeriesEndpoint method changeOptOuts.
@POST
@Path("optouts")
@Produces(MediaType.APPLICATION_JSON)
@RestQuery(name = "changeOptOuts", description = "Change the opt out status of many series", returnDescription = "A JSON array listing which series were or were not opted out.", restParameters = { @RestParameter(name = "seriesIds", description = "A JSON array of ids of the series to opt out or in", defaultValue = "[]", isRequired = true, type = RestParameter.Type.STRING), @RestParameter(name = "optout", description = "Whether to opt out or not either true or false.", defaultValue = "false", isRequired = true, type = RestParameter.Type.BOOLEAN) }, reponses = { @RestResponse(description = "Returns a JSON object with the results for the different opted out or in elements such as ok, notFound or error.", responseCode = HttpServletResponse.SC_OK), @RestResponse(description = "Unable to parse boolean value to opt out, or parse JSON array of opt out series", responseCode = HttpServletResponse.SC_BAD_REQUEST) })
public Response changeOptOuts(@FormParam("optout") boolean optout, @FormParam("seriesIds") String seriesIds) {
JSONParser parser = new JSONParser();
JSONArray seriesIdsArray;
try {
seriesIdsArray = (JSONArray) parser.parse(seriesIds);
} catch (org.json.simple.parser.ParseException e) {
logger.warn("Unable to parse series ids {} : {}", seriesIds, ExceptionUtils.getStackTrace(e));
return Response.status(Status.BAD_REQUEST).build();
} catch (NullPointerException e) {
logger.warn("Unable to parse series ids because it was null {}", seriesIds);
return Response.status(Status.BAD_REQUEST).build();
} catch (ClassCastException e) {
logger.warn("Unable to parse series ids because it was the wrong class {} : {}", seriesIds, ExceptionUtils.getStackTrace(e));
return Response.status(Status.BAD_REQUEST).build();
}
BulkOperationResult result = new BulkOperationResult();
for (Object seriesId : seriesIdsArray) {
try {
seriesService.updateOptOutStatus(seriesId.toString(), optout);
result.addOk(seriesId.toString());
} catch (NotFoundException e) {
result.addNotFound(seriesId.toString());
} catch (Exception e) {
logger.error("Could not update opt out status of series {}: {}", seriesId.toString(), ExceptionUtils.getStackTrace(e));
result.addServerError(seriesId.toString());
}
}
return Response.ok(result.toJson()).build();
}
use of org.opencastproject.rest.BulkOperationResult in project opencast by opencast.
the class SeriesEndpointTest method testDelete.
@Ignore
@Test
public void testDelete() throws Exception {
BulkOperationResult emptyResult = new BulkOperationResult();
BulkOperationResult foundResult = new BulkOperationResult();
foundResult.addOk("1");
foundResult.addOk("2");
foundResult.addOk("3");
foundResult.addNotFound("4");
foundResult.addServerError("5");
given().expect().statusCode(HttpStatus.SC_BAD_REQUEST).when().post(rt.host("/deleteSeries"));
given().body("{}").expect().statusCode(HttpStatus.SC_BAD_REQUEST).when().post(rt.host("/deleteSeries"));
String result = given().body("[]").expect().statusCode(HttpStatus.SC_OK).when().post(rt.host("/deleteSeries")).asString();
assertEquals(emptyResult.toJson(), result);
result = given().body("[1,2,3,4,5]").expect().statusCode(HttpStatus.SC_OK).when().post(rt.host("/deleteSeries")).asString();
assertEquals(foundResult.toJson(), result);
}
use of org.opencastproject.rest.BulkOperationResult in project opencast by opencast.
the class AbstractEventEndpoint method changeOptOuts.
@POST
@Path("optouts")
@Produces(MediaType.APPLICATION_JSON)
@RestQuery(name = "changeOptOuts", description = "Change the opt out status of many events", returnDescription = "A JSON array listing which events were or were not opted out.", restParameters = { @RestParameter(name = "eventIds", description = "A JSON array of ids of the events to opt out or in", defaultValue = "[]", isRequired = true, type = RestParameter.Type.STRING), @RestParameter(name = "optout", description = "Whether to opt out or not either true or false.", defaultValue = "false", isRequired = true, type = RestParameter.Type.BOOLEAN) }, reponses = { @RestResponse(description = "Returns a JSON object with the results for the different opted out or in elements such as ok, notFound or error.", responseCode = HttpServletResponse.SC_OK), @RestResponse(description = "Unable to parse boolean value to opt out, or parse JSON array of opt out events", responseCode = HttpServletResponse.SC_BAD_REQUEST) })
public Response changeOptOuts(@FormParam("optout") boolean optout, @FormParam("eventIds") String eventIds) {
JSONParser parser = new JSONParser();
JSONArray eventIdsArray;
try {
eventIdsArray = (JSONArray) parser.parse(eventIds);
} catch (org.json.simple.parser.ParseException e) {
logger.warn("Unable to parse event ids {} : {}", eventIds, ExceptionUtils.getStackTrace(e));
return Response.status(Status.BAD_REQUEST).build();
} catch (NullPointerException e) {
logger.warn("Unable to parse event ids because it was null {}", eventIds);
return Response.status(Status.BAD_REQUEST).build();
} catch (ClassCastException e) {
logger.warn("Unable to parse event ids because it was the wrong class {} : {}", eventIds, ExceptionUtils.getStackTrace(e));
return Response.status(Status.BAD_REQUEST).build();
}
BulkOperationResult result = new BulkOperationResult();
for (Object idObject : eventIdsArray) {
String eventId = idObject.toString();
try {
getIndexService().changeOptOutStatus(eventId, optout, getIndex());
result.addOk(eventId);
} catch (NotFoundException e) {
result.addNotFound(eventId);
} catch (Exception e) {
logger.error("Could not update opt out status of event {}: {}", eventId, ExceptionUtils.getStackTrace(e));
result.addServerError(eventId);
}
}
return Response.ok(result.toJson()).build();
}
Aggregations