Search in sources :

Example 1 with BulkOperationResult

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");
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) JSONArray(org.codehaus.jettison.json.JSONArray) HttpResponse(org.apache.http.HttpResponse) NotFoundException(org.opencastproject.util.NotFoundException) BulkOperationResult(org.opencastproject.rest.BulkOperationResult) SeriesException(org.opencastproject.series.api.SeriesException) ParseException(java.text.ParseException) SeriesException(org.opencastproject.series.api.SeriesException) WebApplicationException(javax.ws.rs.WebApplicationException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException)

Example 2 with BulkOperationResult

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();
}
Also used : JSONArray(org.json.simple.JSONArray) NotFoundException(org.opencastproject.util.NotFoundException) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) JObject(com.entwinemedia.fn.data.json.JObject) BulkOperationResult(org.opencastproject.rest.BulkOperationResult) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 3 with BulkOperationResult

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();
}
Also used : JSONArray(org.json.simple.JSONArray) NotFoundException(org.opencastproject.util.NotFoundException) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) BulkOperationResult(org.opencastproject.rest.BulkOperationResult) WebApplicationException(javax.ws.rs.WebApplicationException) SearchIndexException(org.opencastproject.matterhorn.search.SearchIndexException) SeriesException(org.opencastproject.series.api.SeriesException) IndexServiceException(org.opencastproject.index.service.exception.IndexServiceException) AclServiceException(org.opencastproject.authorization.xacml.manager.api.AclServiceException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 4 with BulkOperationResult

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);
}
Also used : BulkOperationResult(org.opencastproject.rest.BulkOperationResult) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with BulkOperationResult

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();
}
Also used : JSONArray(org.json.simple.JSONArray) NotFoundException(org.opencastproject.util.NotFoundException) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) JObject(com.entwinemedia.fn.data.json.JObject) BulkOperationResult(org.opencastproject.rest.BulkOperationResult) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) WebApplicationException(javax.ws.rs.WebApplicationException) EventCommentException(org.opencastproject.event.comment.EventCommentException) JSONException(org.codehaus.jettison.json.JSONException) JobEndpointException(org.opencastproject.adminui.exception.JobEndpointException) SearchIndexException(org.opencastproject.matterhorn.search.SearchIndexException) ParseException(java.text.ParseException) IndexServiceException(org.opencastproject.index.service.exception.IndexServiceException) UrlSigningException(org.opencastproject.security.urlsigning.exception.UrlSigningException) AclServiceException(org.opencastproject.authorization.xacml.manager.api.AclServiceException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) WorkflowStateException(org.opencastproject.workflow.api.WorkflowStateException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Aggregations

BulkOperationResult (org.opencastproject.rest.BulkOperationResult)6 NotFoundException (org.opencastproject.util.NotFoundException)5 POST (javax.ws.rs.POST)4 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 JSONArray (org.json.simple.JSONArray)4 JSONObject (org.json.simple.JSONObject)4 JSONParser (org.json.simple.parser.JSONParser)4 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)4 RestQuery (org.opencastproject.util.doc.rest.RestQuery)4 AclServiceException (org.opencastproject.authorization.xacml.manager.api.AclServiceException)3 IndexServiceException (org.opencastproject.index.service.exception.IndexServiceException)3 SearchIndexException (org.opencastproject.matterhorn.search.SearchIndexException)3 SeriesException (org.opencastproject.series.api.SeriesException)3 JObject (com.entwinemedia.fn.data.json.JObject)2 ParseException (java.text.ParseException)2 HttpResponse (org.apache.http.HttpResponse)1 HttpPost (org.apache.http.client.methods.HttpPost)1 StringEntity (org.apache.http.entity.StringEntity)1