Clover icon

Magnolia REST Content Delivery 2.1

  1. Project Clover database Fri Mar 16 2018 18:21:08 CET
  2. Package info.magnolia.rest.delivery.jcr.v1

File JcrDeliveryEndpoint.java

 

Coverage histogram

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

Code metrics

22
45
6
1
248
156
18
0.4
7.5
6
3

Classes

Class Line # Actions
JcrDeliveryEndpoint 90 45 0% 18 15
0.7945205679.5%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /**
2    * This file Copyright (c) 2017-2018 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.delivery.jcr.v1;
35   
36    import static java.util.stream.Collectors.toMap;
37   
38    import info.magnolia.cms.security.JCRSessionOp;
39    import info.magnolia.cms.util.PathUtil;
40    import info.magnolia.context.Context;
41    import info.magnolia.context.MgnlContext;
42    import info.magnolia.rest.AbstractEndpoint;
43    import info.magnolia.rest.delivery.jcr.NodesResult;
44    import info.magnolia.rest.delivery.jcr.QueryBuilder;
45    import info.magnolia.rest.delivery.jcr.filter.FilteringContentDecoratorBuilder;
46   
47    import java.util.Arrays;
48    import java.util.Collections;
49    import java.util.List;
50    import java.util.Map;
51   
52    import javax.inject.Inject;
53    import javax.inject.Provider;
54    import javax.jcr.Node;
55    import javax.jcr.NodeIterator;
56    import javax.jcr.RepositoryException;
57    import javax.jcr.Session;
58    import javax.jcr.Workspace;
59    import javax.jcr.query.Query;
60    import javax.jcr.query.QueryResult;
61    import javax.ws.rs.DefaultValue;
62    import javax.ws.rs.GET;
63    import javax.ws.rs.NotFoundException;
64    import javax.ws.rs.Path;
65    import javax.ws.rs.PathParam;
66    import javax.ws.rs.Produces;
67    import javax.ws.rs.QueryParam;
68    import javax.ws.rs.core.MediaType;
69    import javax.ws.rs.core.UriInfo;
70   
71    import org.apache.commons.lang3.StringUtils;
72   
73    /**
74    * The JCR Delivery endpoint serves JCR content in a concise JSON format.<br/>
75    * Content may be pages, components, stories or anything else that is stored in a workspace.
76    *
77    * <p>It offers two methods for consuming content:</p>
78    * <ul>
79    * <li>Reading a single node, by passing a specific node path</li>
80    * <li>Querying for nodes; passing query parameters to leverage pagination, sorting, full-text search, or filters</li>
81    * </ul>
82    * <p>The endpoint behavior can be configured with a {@link JcrDeliveryEndpointDefinition} to match specific workspaces or node types.
83    * Nodes are represented in the JSON output as plain object-graph, resembling the tree-structure of JCR nodes and properties.<br/>
84    * Additionally, UUID references to other workspaces can be resolved and expanded within returned records.</p>
85    *
86    * @deprecated since 2.1, consider to use {@link info.magnolia.rest.delivery.jcr.v2.JcrDeliveryEndpoint} instead for multiple endpoint support and better reference resolving.
87    */
88    @Deprecated
89    @Path("/delivery/{endpointPrefix}/v1")
 
90    public class JcrDeliveryEndpoint extends AbstractEndpoint<JcrDeliveryEndpointDefinition> {
91   
92    private static final String ENDPOINT_PARAM = "endpointPrefix";
93   
94    private static final String PATH_PARAM = "path";
95    private static final String NODE_TYPES_PARAM = "nodeTypes";
96    private static final String KEYWORD_PARAM = "q";
97    private static final String ORDER_BY_PARAM = "orderBy";
98    private static final String OFFSET_PARAM = "offset";
99    private static final String LIMIT_PARAM = "limit";
100   
101    private static final List<String> ENDPOINT_PARAMETERS = Collections.unmodifiableList(Arrays.asList(
102    NODE_TYPES_PARAM,
103    KEYWORD_PARAM,
104    ORDER_BY_PARAM,
105    OFFSET_PARAM,
106    LIMIT_PARAM
107    ));
108   
109    private final Map<String, WorkspaceParameters> params;
110    private final Provider<Context> contextProvider;
111   
112    @javax.ws.rs.core.Context
113    private UriInfo uriInfo;
114   
 
115  3 toggle @Inject
116    public JcrDeliveryEndpoint(JcrDeliveryEndpointDefinition endpointDefinition, Provider<Context> contextProvider) {
117  3 super(endpointDefinition);
118  3 this.contextProvider = contextProvider;
119  3 this.params = getEndpointDefinition().getParams();
120    }
121   
122    /**
123    * Returns a node including its properties and child nodes down to a certain depth.
124    */
 
125  2 toggle @GET
126    @Path("/{path:.*}")
127    @Produces({ MediaType.APPLICATION_JSON })
128    public Node readNode(
129    @PathParam(ENDPOINT_PARAM) String endpointPrefix,
130    @PathParam(PATH_PARAM) @DefaultValue("/") String path) throws RepositoryException {
131   
132  2 WorkspaceParameters param = params.get(endpointPrefix);
133   
134  2 if (param != null) {
135  2 String workspace = param.getWorkspace();
136  2 if (param.getWorkspace() == null) {
137    // if endpoint prefix is the same as the target workspace, don't need both
138  2 workspace = endpointPrefix;
139    }
140   
141  2 return doSessionOperation(workspace, param.isBypassWorkspaceAcls(), new JCRSessionOp<Node>(workspace) {
 
142  2 toggle @Override
143    public Node exec(Session session) throws RepositoryException {
144  2 String nodePath = PathUtil.createPath(param.getRootPath(), path);
145  2 Node node = session.getNode(nodePath);
146   
147  1 if (param.getNodeTypes() != null && !param.getNodeTypes().isEmpty()) {
148  1 boolean matchingNodeType = false;
149  1 for (String nodeType : param.getNodeTypes()) {
150  1 if (node.isNodeType(nodeType)) {
151  1 matchingNodeType = true;
152  1 break;
153    }
154    }
155  1 if (!matchingNodeType) {
156  0 throw new NotFoundException(String.format("Node '%s' does not match any configured node type", node));
157    }
158    }
159   
160  1 FilteringContentDecoratorBuilder decorators = new FilteringContentDecoratorBuilder()
161    .childNodeTypes(param.getChildNodeTypes())
162    .depth(param.getDepth())
163    .includeSystemProperties(param.isIncludeSystemProperties())
164    .references(param.getReferences());
165   
166  1 node = decorators.wrapNode(node);
167   
168  1 return node;
169    }
170    });
171    }
172   
173  0 throw new NotFoundException(String.format("No workspace-params entry for endpoint prefix '%s'", endpointPrefix));
174    }
175   
176    /**
177    * Returns a list of nodes.
178    */
 
179  1 toggle @GET
180    @Produces({ MediaType.APPLICATION_JSON })
181    public NodesResult queryNodes(@PathParam(ENDPOINT_PARAM) String endpointPrefix,
182    @QueryParam(KEYWORD_PARAM) String keyword,
183    @QueryParam(ORDER_BY_PARAM) String orderByParam,
184    @QueryParam(OFFSET_PARAM) Long offsetParam,
185    @QueryParam(LIMIT_PARAM) Long limitParam) throws RepositoryException {
186   
187  1 WorkspaceParameters param = params.get(endpointPrefix);
188   
189  1 if (param != null) {
190  1 String workspace = param.getWorkspace();
191  1 if (param.getWorkspace() == null) {
192    // if endpoint prefix is the same as the target workspace, don't need both
193  1 workspace = endpointPrefix;
194    }
195   
196  1 long offset = offsetParam == null ? 0 : offsetParam;
197  1 long limit = limitParam == null ? param.getLimit() : limitParam;
198  1 List<String> propertiesToOrderBy = StringUtils.isEmpty(orderByParam) ?
199    Collections.emptyList() :
200    Arrays.asList(StringUtils.split(orderByParam, ","));
201  1 Map<String, List<String>> filteringConditions = uriInfo.getQueryParameters().entrySet().stream()
202    .filter(entry -> !ENDPOINT_PARAMETERS.contains(entry.getKey()) && !entry.getValue().isEmpty())
203    .collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
204   
205  1 NodeIterator results = doSessionOperation(workspace, param.isBypassWorkspaceAcls(), new JCRSessionOp<NodeIterator>(workspace) {
 
206  1 toggle @Override
207    public NodeIterator exec(Session session) throws RepositoryException {
208  1 Workspace workspaceObj = session.getWorkspace();
209   
210  1 Query query = QueryBuilder.inWorkspace(workspaceObj)
211    .rootPath(param.getRootPath())
212    .nodeTypes(param.getNodeTypes())
213    .keyword(keyword)
214    .conditions(filteringConditions)
215    .orderBy(propertiesToOrderBy)
216    .offset(offset)
217    .limit(limit)
218    .build();
219   
220  1 QueryResult result = query.execute();
221  1 NodeIterator nodeIterator = result.getNodes();
222   
223  1 FilteringContentDecoratorBuilder decorators = new FilteringContentDecoratorBuilder()
224    .childNodeTypes(param.getChildNodeTypes())
225    .depth(param.getDepth())
226    .includeSystemProperties(param.isIncludeSystemProperties())
227    .references(param.getReferences());
228   
229  1 nodeIterator = decorators.wrapNodeIterator(nodeIterator);
230   
231  1 return nodeIterator;
232    }
233    });
234    //TODO total number of entries in result will be set later.
235  1 return new NodesResult(results, 0);
236    }
237   
238  0 throw new NotFoundException(String.format("No workspace-params entry for endpoint prefix '%s'", endpointPrefix));
239    }
240   
 
241  3 toggle protected <R> R doSessionOperation(String workspace, boolean bypassWorkspaceAcls, JCRSessionOp<R> operation) throws RepositoryException {
242  3 if (bypassWorkspaceAcls) {
243  0 return MgnlContext.doInSystemContext(operation);
244    }
245   
246  3 return operation.exec(contextProvider.get().getJCRSession(workspace));
247    }
248    }