001/*
002 * (C) Copyright 2018 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *       Kevin Leturc <kleturc@nuxeo.com>
018 */
019package org.nuxeo.ecm.restapi.server.jaxrs;
020
021import javax.ws.rs.GET;
022import javax.ws.rs.PUT;
023import javax.ws.rs.Path;
024import javax.ws.rs.PathParam;
025import javax.ws.rs.Produces;
026import javax.ws.rs.core.MediaType;
027
028import org.nuxeo.ecm.core.bulk.BulkService;
029import org.nuxeo.ecm.core.bulk.message.BulkStatus;
030import org.nuxeo.ecm.core.bulk.message.BulkStatus.State;
031import org.nuxeo.ecm.webengine.model.WebObject;
032import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException;
033import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
034import org.nuxeo.runtime.api.Framework;
035
036/**
037 * Bulk endpoint to expose {@link BulkService}.
038 *
039 * @since 10.3
040 */
041@WebObject(type = "bulkActionFramework")
042public class BulkActionFrameworkObject extends DefaultObject {
043
044    @GET
045    @Path("{commandId}")
046    @Produces(MediaType.APPLICATION_JSON)
047    public BulkStatus getBulkStatus(@PathParam("commandId") String commandId) {
048        BulkStatus status = Framework.getService(BulkService.class).getStatus(commandId);
049        checkStatus(status);
050        return status;
051    }
052
053    @PUT
054    @Path("{commandId}/abort")
055    @Produces(MediaType.APPLICATION_JSON)
056    public BulkStatus abortBulkAction(@PathParam("commandId") String commandId) {
057        BulkStatus status = Framework.getService(BulkService.class).getStatus(commandId);
058        checkStatus(status);
059        return Framework.getService(BulkService.class).abort(commandId);
060    }
061
062    protected void checkStatus(BulkStatus status) {
063        if (status.getState() == State.UNKNOWN || !getContext().getPrincipal().isAdministrator()
064                && !getContext().getPrincipal().getName().equals(status.getUsername())) {
065            throw new WebResourceNotFoundException("Bulk command with id=" + status.getId() + " doesn't exist");
066        }
067    }
068}