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 io.swagger.v3.oas.annotations.Operation;
56 import io.swagger.v3.oas.annotations.responses.ApiResponse;
57 import io.swagger.v3.oas.annotations.responses.ApiResponses;
58 import io.swagger.v3.oas.annotations.tags.Tag;
59
60
61
62
63
64
65
66
67
68 @Path("/commands/v1")
69 @Tag(name = "Commands API")
70 @Deprecated
71 public class CommandEndpoint extends AbstractCommandEndpoint<CommandEndpointDefinition> {
72
73 private final Logger log = LoggerFactory.getLogger(getClass());
74
75 @Inject
76 public CommandEndpoint(final CommandEndpointDefinition commandEndpointDefinition, final CommandsManager commandsManager) {
77 super(commandEndpointDefinition, commandsManager);
78 }
79
80 @POST
81 @Path("/{catalogName}/{commandName:(.)*}")
82 @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
83 @Operation(summary = "Executes a command", description = "Executes a command from a specific catalog")
84 @ApiResponses(value = {
85 @ApiResponse(responseCode = "200", description = STATUS_MESSAGE_OK),
86 @ApiResponse(responseCode = "403", description = STATUS_MESSAGE_FORBIDDEN),
87 @ApiResponse(responseCode = "404", description = STATUS_MESSAGE_COMMAND_NOT_FOUND),
88 @ApiResponse(responseCode = "500", description = STATUS_MESSAGE_ERROR_OCCURRED)
89 })
90 public Response executeCommand(
91 @PathParam("catalogName") String catalogName,
92 @PathParam("commandName") String commandName,
93 Map<String, Object> commandMap) throws RepositoryException {
94
95 boolean commandResult;
96
97 if (!isCommandExecutableByCurrentUser(catalogName, commandName)) {
98 log.error("Unauthorized access while executing command [{}] from catalog [{}] with commandMap [{}]", commandName, catalogName, commandMap);
99 return Response.status(Response.Status.FORBIDDEN).build();
100 }
101
102 Command command = commandsManager.getCommand(catalogName, commandName);
103
104 if (command == null) {
105 return Response.status(Response.Status.NOT_FOUND).build();
106 }
107
108 try {
109 commandResult = commandsManager.executeCommand(command, commandMap);
110 } catch (Exception e) {
111 log.error("Error executing command [{}] from catalog [{}] with commandMap [{}]", commandName, catalogName, commandMap, e);
112 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).type(MediaType.TEXT_PLAIN_TYPE).build();
113 }
114
115 return Response.status(Response.Status.OK).entity(!commandResult).type(MediaType.TEXT_PLAIN_TYPE).build();
116 }
117
118 @POST
119 @Path("/{commandName:(.)*}")
120 @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
121 @Operation(summary = "Executes a command", description = "Executes a command from the default catalog")
122 @ApiResponses(value = {
123 @ApiResponse(responseCode = "200", description = STATUS_MESSAGE_OK),
124 @ApiResponse(responseCode = "403", description = STATUS_MESSAGE_FORBIDDEN),
125 @ApiResponse(responseCode = "404", description = STATUS_MESSAGE_COMMAND_NOT_FOUND),
126 @ApiResponse(responseCode = "500", description = STATUS_MESSAGE_ERROR_OCCURRED)
127 })
128 public Response executeCommand(@PathParam("commandName") String commandName, Map<String, Object> commandMap) throws RepositoryException {
129 return executeCommand(null, commandName, commandMap);
130 }
131 }