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.v2;
35
36 import info.magnolia.commands.CommandsManager;
37 import info.magnolia.commands.chain.Command;
38 import info.magnolia.context.MgnlContext;
39 import info.magnolia.rest.service.command.AbstractCommandEndpoint;
40 import info.magnolia.rest.service.command.definition.CommandDefinition;
41 import info.magnolia.rest.service.command.definition.CommandEndpointDefinition;
42
43 import java.util.Collection;
44 import java.util.Collections;
45 import java.util.HashMap;
46 import java.util.Map;
47
48 import javax.inject.Inject;
49 import javax.jcr.RepositoryException;
50 import javax.ws.rs.Consumes;
51 import javax.ws.rs.POST;
52 import javax.ws.rs.Path;
53 import javax.ws.rs.PathParam;
54 import javax.ws.rs.Produces;
55 import javax.ws.rs.core.MediaType;
56 import javax.ws.rs.core.Response;
57
58 import org.apache.commons.lang3.StringUtils;
59 import org.apache.commons.lang3.exception.ExceptionUtils;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62
63 import io.swagger.v3.oas.annotations.Operation;
64 import io.swagger.v3.oas.annotations.responses.ApiResponse;
65 import io.swagger.v3.oas.annotations.responses.ApiResponses;
66
67
68
69
70
71
72
73
74
75 @Path("/commands/v2")
76 public class CommandEndpoint extends AbstractCommandEndpoint<CommandEndpointDefinition> {
77
78 private final Logger log = LoggerFactory.getLogger(getClass());
79
80 @Inject
81 public CommandEndpoint(final CommandEndpointDefinition commandEndpointDefinition, final CommandsManager commandsManager) {
82 super(commandEndpointDefinition, commandsManager);
83 }
84
85 @POST
86 @Path("/{catalogName}/{commandName:(.)*}")
87 @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
88 @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
89 @Operation(summary = "Executes a command", description = "Executes a command from a specific catalog")
90 @ApiResponses(value = {
91 @ApiResponse(responseCode = "200", description = STATUS_MESSAGE_OK),
92 @ApiResponse(responseCode = "403", description = STATUS_MESSAGE_FORBIDDEN),
93 @ApiResponse(responseCode = "404", description = STATUS_MESSAGE_COMMAND_NOT_FOUND),
94 @ApiResponse(responseCode = "500", description = STATUS_MESSAGE_ERROR_OCCURRED)
95 })
96 public Response executeCommand(
97 @PathParam("catalogName") String catalogName,
98 @PathParam("commandName") String commandName,
99 Map<String, Object> commandMap) throws RepositoryException {
100
101 if (!isCommandExecutableByCurrentUser(catalogName, commandName)) {
102 log.error("Unauthorized access while executing command [{}] from catalog [{}] with commandMap [{}]", commandName, catalogName, commandMap);
103 return Response.status(Response.Status.FORBIDDEN).build();
104 }
105
106 Command command = commandsManager.getCommand(catalogName, commandName);
107
108 if (command == null) {
109 return Response.status(Response.Status.NOT_FOUND).build();
110 }
111
112 Map<String, Object> resultMap = new HashMap<String, Object>();
113 Collection<String> contextParameters = getResponseContextParametersForCommand(catalogName, commandName);
114
115 try {
116 commandsManager.executeCommand(command, commandMap);
117 resultMap.put("success", true);
118 if (contextParameters != null && !contextParameters.isEmpty()) {
119 for (String key : contextParameters) {
120 resultMap.put(key, MgnlContext.getAttribute(key));
121 }
122 }
123 return Response.status(Response.Status.OK).entity(resultMap).build();
124 } catch (Exception e) {
125 log.error("Error executing command [{}] from catalog [{}] with commandMap [{}]", commandName, catalogName, commandMap, e);
126 resultMap.put("success", false);
127 resultMap.put("exceptionMessage", e.getMessage());
128 resultMap.put("stackTrace", ExceptionUtils.getStackTrace(e));
129 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(resultMap).build();
130 }
131 }
132
133 @POST
134 @Path("/{commandName:(.)*}")
135 @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
136 @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
137 @Operation(summary = "Executes a command", description = "Executes a command from the default catalog")
138 @ApiResponses(value = {
139 @ApiResponse(responseCode = "200", description = STATUS_MESSAGE_OK),
140 @ApiResponse(responseCode = "403", description = STATUS_MESSAGE_FORBIDDEN),
141 @ApiResponse(responseCode = "404", description = STATUS_MESSAGE_COMMAND_NOT_FOUND),
142 @ApiResponse(responseCode = "500", description = STATUS_MESSAGE_ERROR_OCCURRED)
143 })
144 public Response executeCommand(@PathParam("commandName") String commandName, Map<String, Object> commandMap) throws RepositoryException {
145 return executeCommand(null, commandName, commandMap);
146 }
147
148
149
150
151 protected Collection<String> getResponseContextParametersForCommand(String catalogName, String commandName) {
152 if (commandName != null) {
153 for (CommandDefinition commandDefinition : getEndpointDefinition().getEnabledCommands()) {
154 if (StringUtils.equals(catalogName, commandDefinition.getCatalogName()) && StringUtils.equals(commandName, commandDefinition.getCommandName())) {
155 return commandDefinition.getResponseContextParameters();
156 }
157 }
158 }
159 return Collections.emptyList();
160 }
161 }