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 |
|
@deprecated@link |
66 |
|
|
67 |
|
@Api(value = "/commands/v1", description = "The commands API") |
68 |
|
@Path("/commands/v1") |
69 |
|
@Deprecated |
|
|
| 81% |
Uncovered Elements: 4 (21) |
Complexity: 6 |
Complexity Density: 0.43 |
|
70 |
|
public class CommandEndpoint extends AbstractCommandEndpoint<CommandEndpointDefinition> { |
71 |
|
|
72 |
|
private final Logger log = LoggerFactory.getLogger(getClass()); |
73 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
74 |
8 |
@Inject... |
75 |
|
public CommandEndpoint(final CommandEndpointDefinition commandEndpointDefinition, final CommandsManager commandsManager) { |
76 |
8 |
super(commandEndpointDefinition, commandsManager); |
77 |
|
} |
78 |
|
|
|
|
| 87.5% |
Uncovered Elements: 2 (16) |
Complexity: 4 |
Complexity Density: 0.33 |
|
79 |
3 |
@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 |
3 |
boolean commandResult; |
95 |
|
|
96 |
3 |
if (!isCommandExecutableByCurrentUser(catalogName, commandName)) { |
97 |
1 |
log.error("Unauthorized access while executing command '{}' from catalog '{}' with commandMap '{}'", commandName, catalogName, commandMap); |
98 |
1 |
return Response.status(Response.Status.FORBIDDEN).build(); |
99 |
|
} |
100 |
|
|
101 |
2 |
Command command = commandsManager.getCommand(catalogName, commandName); |
102 |
|
|
103 |
2 |
if (command == null) { |
104 |
0 |
return Response.status(Response.Status.NOT_FOUND).build(); |
105 |
|
} |
106 |
|
|
107 |
2 |
try { |
108 |
2 |
commandResult = commandsManager.executeCommand(command, commandMap); |
109 |
|
} catch (Exception e) { |
110 |
1 |
log.error("Error executing command '{}' from catalog '{}' with commandMap '{}'", commandName, catalogName, commandMap, e); |
111 |
1 |
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).type(MediaType.TEXT_PLAIN_TYPE).build(); |
112 |
|
} |
113 |
|
|
114 |
1 |
return Response.status(Response.Status.OK).entity(!commandResult).type(MediaType.TEXT_PLAIN_TYPE).build(); |
115 |
|
} |
116 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
117 |
0 |
@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 |
0 |
return executeCommand(null, commandName, commandMap); |
129 |
|
} |
130 |
|
} |