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.annotations.Api; |
64 |
|
import io.swagger.annotations.ApiOperation; |
65 |
|
import io.swagger.annotations.ApiResponse; |
66 |
|
import io.swagger.annotations.ApiResponses; |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
@see |
75 |
|
|
76 |
|
@Api(value = "/commands/v2", description = "The commands API v2") |
77 |
|
@Path("/commands/v2") |
|
|
| 73.2% |
Uncovered Elements: 11 (41) |
Complexity: 12 |
Complexity Density: 0.44 |
|
78 |
|
public class CommandEndpoint extends AbstractCommandEndpoint<CommandEndpointDefinition> { |
79 |
|
|
80 |
|
private final Logger log = LoggerFactory.getLogger(getClass()); |
81 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
82 |
2 |
@Inject... |
83 |
|
public CommandEndpoint(final CommandEndpointDefinition commandEndpointDefinition, final CommandsManager commandsManager) { |
84 |
2 |
super(commandEndpointDefinition, commandsManager); |
85 |
|
} |
86 |
|
|
|
|
| 76.9% |
Uncovered Elements: 6 (26) |
Complexity: 6 |
Complexity Density: 0.3 |
|
87 |
2 |
@POST... |
88 |
|
@Path("/{catalogName}/{commandName:(.)*}") |
89 |
|
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) |
90 |
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) |
91 |
|
@ApiOperation(value = "Executes a command", notes = "Executes a command from a specific catalog") |
92 |
|
@ApiResponses(value = { |
93 |
|
@ApiResponse(code = 200, message = STATUS_MESSAGE_OK), |
94 |
|
@ApiResponse(code = 403, message = STATUS_MESSAGE_FORBIDDEN), |
95 |
|
@ApiResponse(code = 404, message = STATUS_MESSAGE_COMMAND_NOT_FOUND), |
96 |
|
@ApiResponse(code = 500, message = STATUS_MESSAGE_ERROR_OCCURRED) |
97 |
|
}) |
98 |
|
public Response executeCommand( |
99 |
|
@PathParam("catalogName") String catalogName, |
100 |
|
@PathParam("commandName") String commandName, |
101 |
|
Map<String, Object> commandMap) throws RepositoryException { |
102 |
|
|
103 |
2 |
if (!isCommandExecutableByCurrentUser(catalogName, commandName)) { |
104 |
0 |
log.error("Unauthorized access while executing command [{}] from catalog [{}] with commandMap [{}]", commandName, catalogName, commandMap); |
105 |
0 |
return Response.status(Response.Status.FORBIDDEN).build(); |
106 |
|
} |
107 |
|
|
108 |
2 |
Command command = commandsManager.getCommand(catalogName, commandName); |
109 |
|
|
110 |
2 |
if (command == null) { |
111 |
0 |
return Response.status(Response.Status.NOT_FOUND).build(); |
112 |
|
} |
113 |
|
|
114 |
2 |
Map<String, Object> resultMap = new HashMap<String, Object>(); |
115 |
2 |
Collection<String> contextParameters = getResponseContextParametersForCommand(catalogName, commandName); |
116 |
|
|
117 |
2 |
try { |
118 |
2 |
commandsManager.executeCommand(command, commandMap); |
119 |
1 |
resultMap.put("success", true); |
120 |
1 |
if (contextParameters != null && !contextParameters.isEmpty()) { |
121 |
1 |
for (String key : contextParameters) { |
122 |
2 |
resultMap.put(key, MgnlContext.getAttribute(key)); |
123 |
|
} |
124 |
|
} |
125 |
1 |
return Response.status(Response.Status.OK).entity(resultMap).build(); |
126 |
|
} catch (Exception e) { |
127 |
1 |
log.error("Error executing command [{}] from catalog [{}] with commandMap [{}]", commandName, catalogName, commandMap, e); |
128 |
1 |
resultMap.put("success", false); |
129 |
1 |
resultMap.put("exceptionMessage", e.getMessage()); |
130 |
1 |
resultMap.put("stackTrace", ExceptionUtils.getStackTrace(e)); |
131 |
1 |
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(resultMap).build(); |
132 |
|
} |
133 |
|
} |
134 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
135 |
0 |
@POST... |
136 |
|
@Path("/{commandName:(.)*}") |
137 |
|
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) |
138 |
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) |
139 |
|
@ApiOperation(value = "Executes a command", notes = "Executes a command from the default catalog") |
140 |
|
@ApiResponses(value = { |
141 |
|
@ApiResponse(code = 200, message = STATUS_MESSAGE_OK), |
142 |
|
@ApiResponse(code = 403, message = STATUS_MESSAGE_FORBIDDEN), |
143 |
|
@ApiResponse(code = 404, message = STATUS_MESSAGE_COMMAND_NOT_FOUND), |
144 |
|
@ApiResponse(code = 500, message = STATUS_MESSAGE_ERROR_OCCURRED) |
145 |
|
}) |
146 |
|
public Response executeCommand(@PathParam("commandName") String commandName, Map<String, Object> commandMap) throws RepositoryException { |
147 |
0 |
return executeCommand(null, commandName, commandMap); |
148 |
|
} |
149 |
|
|
150 |
|
|
151 |
|
@link |
152 |
|
|
|
|
| 66.7% |
Uncovered Elements: 3 (9) |
Complexity: 4 |
Complexity Density: 0.8 |
|
153 |
2 |
protected Collection<String> getResponseContextParametersForCommand(String catalogName, String commandName) {... |
154 |
2 |
if (commandName != null) { |
155 |
2 |
for (CommandDefinition commandDefinition : getEndpointDefinition().getEnabledCommands()) { |
156 |
2 |
if (StringUtils.equals(catalogName, commandDefinition.getCatalogName()) && StringUtils.equals(commandName, commandDefinition.getCommandName())) { |
157 |
2 |
return commandDefinition.getResponseContextParameters(); |
158 |
|
} |
159 |
|
} |
160 |
|
} |
161 |
0 |
return Collections.emptyList(); |
162 |
|
} |
163 |
|
} |