Clover icon

Magnolia REST Services 2.0-alpha2

  1. Project Clover database Tue Oct 10 2017 14:50:59 CEST
  2. Package info.magnolia.rest.service.command.v1

File CommandEndpoint.java

 

Coverage histogram

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

Code metrics

4
14
3
1
131
74
6
0.43
4.67
3
2

Classes

Class Line # Actions
CommandEndpoint 71 14 0% 6 4
0.809523881%
 

Contributing tests

This file is covered by 8 tests. .

Source view

1    /**
2    * This file Copyright (c) 2013-2017 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.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.annotations.Api;
56    import io.swagger.annotations.ApiOperation;
57    import io.swagger.annotations.ApiResponse;
58    import io.swagger.annotations.ApiResponses;
59   
60   
61    /**
62    * REST service for executing commands.
63    *
64    * Executable commands have to be white listed by adding them to the configuration.
65    *
66    * @deprecated since 1.1 use {@link info.magnolia.rest.service.command.v2.CommandEndpoint} instead.
67    */
68    @Api(value = "/commands/v1", description = "The commands API")
69    @Path("/commands/v1")
70    @Deprecated
 
71    public class CommandEndpoint extends AbstractCommandEndpoint<CommandEndpointDefinition> {
72   
73    private final Logger log = LoggerFactory.getLogger(getClass());
74   
 
75  8 toggle @Inject
76    public CommandEndpoint(final CommandEndpointDefinition commandEndpointDefinition, final CommandsManager commandsManager) {
77  8 super(commandEndpointDefinition, commandsManager);
78    }
79   
 
80  3 toggle @POST
81    @Path("/{catalogName}/{commandName:(.)*}")
82    @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
83    @ApiOperation(value = "Executes a command", notes = "Executes a command from a specific catalog")
84    @ApiResponses(value = {
85    @ApiResponse(code = 200, message = STATUS_MESSAGE_OK),
86    @ApiResponse(code = 403, message = STATUS_MESSAGE_FORBIDDEN),
87    @ApiResponse(code = 404, message = STATUS_MESSAGE_COMMAND_NOT_FOUND),
88    @ApiResponse(code = 500, message = 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  3 boolean commandResult;
96   
97  3 if (!isCommandExecutableByCurrentUser(catalogName, commandName)) {
98  1 log.error("Unauthorized access while executing command [{}] from catalog [{}] with commandMap [{}]", commandName, catalogName, commandMap);
99  1 return Response.status(Response.Status.FORBIDDEN).build();
100    }
101   
102  2 Command command = commandsManager.getCommand(catalogName, commandName);
103   
104  2 if (command == null) {
105  0 return Response.status(Response.Status.NOT_FOUND).build();
106    }
107   
108  2 try {
109  2 commandResult = commandsManager.executeCommand(command, commandMap);
110    } catch (Exception e) {
111  1 log.error("Error executing command [{}] from catalog [{}] with commandMap [{}]", commandName, catalogName, commandMap, e);
112  1 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).type(MediaType.TEXT_PLAIN_TYPE).build();
113    }
114   
115  1 return Response.status(Response.Status.OK).entity(!commandResult).type(MediaType.TEXT_PLAIN_TYPE).build();
116    }
117   
 
118  0 toggle @POST
119    @Path("/{commandName:(.)*}")
120    @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
121    @ApiOperation(value = "Executes a command", notes = "Executes a command from the default catalog")
122    @ApiResponses(value = {
123    @ApiResponse(code = 200, message = STATUS_MESSAGE_OK),
124    @ApiResponse(code = 403, message = STATUS_MESSAGE_FORBIDDEN),
125    @ApiResponse(code = 404, message = STATUS_MESSAGE_COMMAND_NOT_FOUND),
126    @ApiResponse(code = 500, message = STATUS_MESSAGE_ERROR_OCCURRED)
127    })
128    public Response executeCommand(@PathParam("commandName") String commandName, Map<String, Object> commandMap) throws RepositoryException {
129  0 return executeCommand(null, commandName, commandMap);
130    }
131    }