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 *     Funsho David
018 */
019
020package org.nuxeo.ecm.restapi.server.jaxrs;
021
022import java.io.IOException;
023
024import javax.ws.rs.Consumes;
025import javax.ws.rs.POST;
026import javax.ws.rs.Path;
027import javax.ws.rs.PathParam;
028import javax.ws.rs.Produces;
029import javax.ws.rs.core.MediaType;
030import javax.ws.rs.core.Response;
031
032import org.nuxeo.ecm.core.bulk.BulkAdminService;
033import org.nuxeo.ecm.core.bulk.BulkService;
034import org.nuxeo.ecm.core.bulk.io.BulkParameters;
035import org.nuxeo.ecm.core.bulk.message.BulkCommand;
036import org.nuxeo.ecm.core.bulk.message.BulkStatus;
037import org.nuxeo.ecm.webengine.model.WebObject;
038import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
039import org.nuxeo.runtime.api.Framework;
040
041/**
042 * Bulk endpoint to perform bulk actions on a set of documents.
043 *
044 * @since 10.3
045 */
046@WebObject(type = "bulkAction")
047public class BulkActionObject extends DefaultObject {
048
049    protected String query;
050
051    @Override
052    public void initialize(Object... args) {
053        query = (String) args[0];
054    }
055
056    @POST
057    @Path("{actionId}")
058    @Consumes(MediaType.APPLICATION_JSON)
059    @Produces(MediaType.APPLICATION_JSON)
060    public Response executeBulkAction(@PathParam("actionId") String actionId, String actionParams)
061            throws IOException {
062
063        BulkAdminService admin = Framework.getService(BulkAdminService.class);
064        if (!admin.getActions().contains(actionId)) {
065            return Response.status(Response.Status.BAD_REQUEST).build();
066        }
067        if (!admin.isHttpEnabled(actionId) && !getContext().getPrincipal().isAdministrator()) {
068            return Response.status(Response.Status.FORBIDDEN).build();
069        }
070
071        String repository = getContext().getCoreSession().getRepositoryName();
072        String username = getContext().getPrincipal().getName();
073
074        BulkCommand command = new BulkCommand.Builder(actionId, query).repository(repository)
075                                                                      .user(username)
076                                                                      .params(BulkParameters.paramsToMap(actionParams))
077                                                                      .build();
078
079        BulkService service = Framework.getService(BulkService.class);
080        String commandId = service.submit(command);
081        BulkStatus status = service.getStatus(commandId);
082        return Response.status(Response.Status.ACCEPTED).entity(status).build();
083    }
084
085}