1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.rest.service.command.v1;
35
36 import info.magnolia.commands.CommandsManager;
37 import info.magnolia.commands.chain.Command;
38 import info.magnolia.rest.service.command.AbstractCommandEndpoint;
39 import info.magnolia.rest.service.command.definition.CommandEndpointDefinition;
40
41 import java.util.Map;
42
43 import javax.inject.Inject;
44 import javax.jcr.RepositoryException;
45 import javax.ws.rs.Consumes;
46 import javax.ws.rs.POST;
47 import javax.ws.rs.Path;
48 import javax.ws.rs.PathParam;
49 import javax.ws.rs.core.MediaType;
50 import javax.ws.rs.core.Response;
51
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 import com.wordnik.swagger.annotations.Api;
56 import com.wordnik.swagger.annotations.ApiOperation;
57 import com.wordnik.swagger.annotations.ApiResponse;
58 import com.wordnik.swagger.annotations.ApiResponses;
59
60
61
62
63
64
65
66
67 @Api(value = "/commands/v1", description = "The commands API")
68 @Path("/commands/v1")
69 @Deprecated
70 public class CommandEndpoint extends AbstractCommandEndpoint<CommandEndpointDefinition> {
71
72 private final Logger log = LoggerFactory.getLogger(getClass());
73
74 @Inject
75 public CommandEndpoint(final CommandEndpointDefinition commandEndpointDefinition, final CommandsManager commandsManager) {
76 super(commandEndpointDefinition, commandsManager);
77 }
78
79 @POST
80 @Path("/{catalogName}/{commandName:(.)*}")
81 @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
82 @ApiOperation(value = "Executes a command", notes = "Executes a command from a specific catalog")
83 @ApiResponses(value = {
84 @ApiResponse(code = 200, message = STATUS_MESSAGE_OK),
85 @ApiResponse(code = 403, message = STATUS_MESSAGE_FORBIDDEN),
86 @ApiResponse(code = 404, message = STATUS_MESSAGE_COMMAND_NOT_FOUND),
87 @ApiResponse(code = 500, message = STATUS_MESSAGE_ERROR_OCCURRED)
88 })
89 public Response executeCommand(
90 @PathParam("catalogName") String catalogName,
91 @PathParam("commandName") String commandName,
92 Map<String, Object> commandMap) throws RepositoryException {
93
94 boolean commandResult;
95
96 if (!isCommandExecutableByCurrentUser(catalogName, commandName)) {
97 log.error("Unauthorized access while executing command '{}' from catalog '{}' with commandMap '{}'", commandName, catalogName, commandMap);
98 return Response.status(Response.Status.FORBIDDEN).build();
99 }
100
101 Command command = commandsManager.getCommand(catalogName, commandName);
102
103 if (command == null) {
104 return Response.status(Response.Status.NOT_FOUND).build();
105 }
106
107 try {
108 commandResult = commandsManager.executeCommand(command, commandMap);
109 } catch (Exception e) {
110 log.error("Error executing command '{}' from catalog '{}' with commandMap '{}'", commandName, catalogName, commandMap, e);
111 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).type(MediaType.TEXT_PLAIN_TYPE).build();
112 }
113
114 return Response.status(Response.Status.OK).entity(!commandResult).type(MediaType.TEXT_PLAIN_TYPE).build();
115 }
116
117 @POST
118 @Path("/{commandName:(.)*}")
119 @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
120 @ApiOperation(value = "Executes a command", notes = "Executes a command from the default catalog")
121 @ApiResponses(value = {
122 @ApiResponse(code = 200, message = STATUS_MESSAGE_OK),
123 @ApiResponse(code = 403, message = STATUS_MESSAGE_FORBIDDEN),
124 @ApiResponse(code = 404, message = STATUS_MESSAGE_COMMAND_NOT_FOUND),
125 @ApiResponse(code = 500, message = STATUS_MESSAGE_ERROR_OCCURRED)
126 })
127 public Response executeCommand(@PathParam("commandName") String commandName, Map<String, Object> commandMap) throws RepositoryException {
128 return executeCommand(null, commandName, commandMap);
129 }
130 }