001/* 002 * (C) Copyright 2014 Nuxeo SA (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 * <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a> 018 * 019 */ 020 021package org.nuxeo.ecm.restapi.server.jaxrs.routing; 022 023import java.util.List; 024 025import javax.ws.rs.DELETE; 026import javax.ws.rs.GET; 027import javax.ws.rs.POST; 028import javax.ws.rs.Path; 029import javax.ws.rs.PathParam; 030import javax.ws.rs.Produces; 031import javax.ws.rs.QueryParam; 032import javax.ws.rs.core.MediaType; 033import javax.ws.rs.core.Response; 034import javax.ws.rs.core.Response.Status; 035 036import org.apache.commons.logging.Log; 037import org.apache.commons.logging.LogFactory; 038import org.nuxeo.ecm.core.api.DocumentModel; 039import org.nuxeo.ecm.core.api.IdRef; 040import org.nuxeo.ecm.core.api.NuxeoException; 041import org.nuxeo.ecm.core.api.NuxeoPrincipal; 042import org.nuxeo.ecm.platform.routing.api.DocumentRoute; 043import org.nuxeo.ecm.platform.routing.api.DocumentRoutingService; 044import org.nuxeo.ecm.platform.routing.core.api.DocumentRoutingEngineService; 045import org.nuxeo.ecm.platform.routing.core.impl.jsongraph.JsonGraphRoute; 046import org.nuxeo.ecm.platform.routing.core.io.WorkflowRequest; 047import org.nuxeo.ecm.webengine.WebException; 048import org.nuxeo.ecm.webengine.model.WebObject; 049import org.nuxeo.ecm.webengine.model.exceptions.WebSecurityException; 050import org.nuxeo.ecm.webengine.model.impl.DefaultObject; 051import org.nuxeo.runtime.api.Framework; 052 053/** 054 * @since 7.2 055 */ 056@WebObject(type = "workflow") 057@Produces(MediaType.APPLICATION_JSON) 058public class WorkflowObject extends DefaultObject { 059 060 private static Log log = LogFactory.getLog(WorkflowObject.class); 061 062 private DocumentRoutingService documentRoutingService; 063 064 @Override 065 protected void initialize(Object... args) { 066 documentRoutingService = Framework.getService(DocumentRoutingService.class); 067 } 068 069 @POST 070 public Response createWorkflowInstance(WorkflowRequest workflowRequest) { 071 final String workflowInstanceId = documentRoutingService.createNewInstance( 072 workflowRequest.getWorkflowModelName(), workflowRequest.getAttachedDocumentIds(), 073 workflowRequest.getVariables(), ctx.getCoreSession(), true); 074 DocumentModel workflowInstance = getContext().getCoreSession().getDocument(new IdRef(workflowInstanceId)); 075 DocumentRoute route = workflowInstance.getAdapter(DocumentRoute.class); 076 return Response.ok(route).status(Status.CREATED).build(); 077 } 078 079 @GET 080 @Path("{workflowInstanceId}") 081 public DocumentRoute getWorkflowInstance(@PathParam("workflowInstanceId") String workflowInstanceId) { 082 DocumentModel workflowInstance; 083 try { 084 workflowInstance = getContext().getCoreSession().getDocument(new IdRef(workflowInstanceId)); 085 return workflowInstance.getAdapter(DocumentRoute.class); 086 } catch (NuxeoException e) { 087 e.addInfo("Can not get workflow instance with id: " + workflowInstanceId); 088 throw e; 089 } 090 } 091 092 @GET 093 @Path("{workflowInstanceId}/graph") 094 public JsonGraphRoute getWorkflowGraph(@PathParam("workflowInstanceId") String workflowInstanceId) { 095 try { 096 return new JsonGraphRoute(getContext().getCoreSession(), workflowInstanceId, getContext().getLocale()); 097 } catch (NuxeoException e) { 098 e.addInfo("Can not get workflow instance graph with id: " + workflowInstanceId); 099 throw e; 100 } 101 } 102 103 @DELETE 104 @Path("{workflowInstanceId}") 105 public Response cancelWorkflowInstance(@PathParam("workflowInstanceId") String workflowInstanceId) { 106 DocumentModel workflowInstance; 107 DocumentRoute route; 108 try { 109 workflowInstance = getContext().getCoreSession().getDocument(new IdRef(workflowInstanceId)); 110 route = workflowInstance.getAdapter(DocumentRoute.class); 111 checkCancelGuards(route); 112 } catch (NuxeoException e) { 113 e.addInfo("Can not get workflow instance with id: " + workflowInstanceId); 114 throw e; 115 } 116 Framework.getService(DocumentRoutingEngineService.class).cancel(route, getContext().getCoreSession()); 117 return Response.ok().status(Status.NO_CONTENT).build(); 118 } 119 120 protected void checkCancelGuards(DocumentRoute route) { 121 NuxeoPrincipal currentUser = (NuxeoPrincipal) getContext().getCoreSession().getPrincipal(); 122 if (currentUser.isAdministrator() || currentUser.isMemberOf("powerusers")) { 123 return; 124 } 125 try { 126 if (currentUser.getName().equals(route.getInitiator())) { 127 return; 128 } 129 throw new WebSecurityException("You don't have the permission to cancel this workflow"); 130 } catch (NuxeoException e) { 131 throw WebException.wrap(e); 132 } 133 } 134 135 @GET 136 public List<DocumentRoute> getRunningWorkflowInstancesLaunchedByCurrentUser( 137 @QueryParam("workflowModelName") String worflowModelName) { 138 return documentRoutingService.getRunningWorkflowInstancesLaunchedByCurrentUser(getContext().getCoreSession(), 139 worflowModelName); 140 } 141 142}