Clover icon

Magnolia REST Services 1.1

  1. Project Clover database Thu Aug 13 2015 19:10:59 CEST
  2. Package info.magnolia.rest.service.command.v2

File CommandEndpoint.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart8.png
80% of files have more coverage

Code metrics

10
27
4
1
162
103
12
0.44
6.75
4
3

Classes

Class Line # Actions
CommandEndpoint 77 27 0% 12 11
0.7317073373.2%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /**
2    * This file Copyright (c) 2015 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
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.lang.StringUtils;
59    import org.apache.commons.lang.exception.ExceptionUtils;
60    import org.slf4j.Logger;
61    import org.slf4j.LoggerFactory;
62   
63    import com.wordnik.swagger.annotations.Api;
64    import com.wordnik.swagger.annotations.ApiOperation;
65    import com.wordnik.swagger.annotations.ApiResponse;
66    import com.wordnik.swagger.annotations.ApiResponses;
67   
68    /**
69    * REST service for executing commands.
70    *
71    * Executable commands have to be white listed by adding them to the configuration.
72    *
73    * @see info.magnolia.rest.service.command.definition.CommandEndpointDefinition
74    */
75    @Api(value = "/commands/v2", description = "The commands API v2")
76    @Path("/commands/v2")
 
77    public class CommandEndpoint extends AbstractCommandEndpoint<CommandEndpointDefinition> {
78   
79    private final Logger log = LoggerFactory.getLogger(getClass());
80   
 
81  2 toggle @Inject
82    public CommandEndpoint(final CommandEndpointDefinition commandEndpointDefinition, final CommandsManager commandsManager) {
83  2 super(commandEndpointDefinition, commandsManager);
84    }
85   
 
86  2 toggle @POST
87    @Path("/{catalogName}/{commandName:(.)*}")
88    @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
89    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
90    @ApiOperation(value = "Executes a command", notes = "Executes a command from a specific catalog")
91    @ApiResponses(value = {
92    @ApiResponse(code = 200, message = STATUS_MESSAGE_OK),
93    @ApiResponse(code = 403, message = STATUS_MESSAGE_FORBIDDEN),
94    @ApiResponse(code = 404, message = STATUS_MESSAGE_COMMAND_NOT_FOUND),
95    @ApiResponse(code = 500, message = STATUS_MESSAGE_ERROR_OCCURRED)
96    })
97    public Response executeCommand(
98    @PathParam("catalogName") String catalogName,
99    @PathParam("commandName") String commandName,
100    Map<String, Object> commandMap) throws RepositoryException {
101   
102  2 if (!isCommandExecutableByCurrentUser(catalogName, commandName)) {
103  0 log.error("Unauthorized access while executing command '{}' from catalog '{}' with commandMap '{}'", commandName, catalogName, commandMap);
104  0 return Response.status(Response.Status.FORBIDDEN).build();
105    }
106   
107  2 Command command = commandsManager.getCommand(catalogName, commandName);
108   
109  2 if (command == null) {
110  0 return Response.status(Response.Status.NOT_FOUND).build();
111    }
112   
113  2 Map<String, Object> resultMap = new HashMap<String, Object>();
114  2 Collection<String> contextParameters = getResponseContextParametersForCommand(catalogName, commandName);
115   
116  2 try {
117  2 commandsManager.executeCommand(command, commandMap);
118  1 resultMap.put("success", true);
119  1 if (contextParameters != null && !contextParameters.isEmpty()) {
120  1 for (String key : contextParameters) {
121  2 resultMap.put(key, MgnlContext.getAttribute(key));
122    }
123    }
124  1 return Response.status(Response.Status.OK).entity(resultMap).build();
125    } catch (Exception e) {
126  1 log.error("Error executing command '{}' from catalog '{}' with commandMap '{}'", commandName, catalogName, commandMap, e);
127  1 resultMap.put("success", false);
128  1 resultMap.put("exceptionMessage", e.getMessage());
129  1 resultMap.put("stackTrace", ExceptionUtils.getStackTrace(e));
130  1 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(resultMap).build();
131    }
132    }
133   
 
134  0 toggle @POST
135    @Path("/{commandName:(.)*}")
136    @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
137    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
138    @ApiOperation(value = "Executes a command", notes = "Executes a command from the default catalog")
139    @ApiResponses(value = {
140    @ApiResponse(code = 200, message = STATUS_MESSAGE_OK),
141    @ApiResponse(code = 403, message = STATUS_MESSAGE_FORBIDDEN),
142    @ApiResponse(code = 404, message = STATUS_MESSAGE_COMMAND_NOT_FOUND),
143    @ApiResponse(code = 500, message = STATUS_MESSAGE_ERROR_OCCURRED)
144    })
145    public Response executeCommand(@PathParam("commandName") String commandName, Map<String, Object> commandMap) throws RepositoryException {
146  0 return executeCommand(null, commandName, commandMap);
147    }
148   
149    /**
150    * Returns context parameters for particular {@link Command} that will be added to the response after successful command execution.
151    */
 
152  2 toggle protected Collection<String> getResponseContextParametersForCommand(String catalogName, String commandName) {
153  2 if (commandName != null) {
154  2 for (CommandDefinition commandDefinition : getEndpointDefinition().getEnabledCommands()) {
155  2 if (StringUtils.equals(catalogName, commandDefinition.getCatalogName()) && StringUtils.equals(commandName, commandDefinition.getCommandName())) {
156  2 return commandDefinition.getResponseContextParameters();
157    }
158    }
159    }
160  0 return Collections.emptyList();
161    }
162    }