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.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 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
@link |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
@deprecated@link |
87 |
|
|
88 |
|
@Deprecated |
89 |
|
@Path("/delivery/{endpointPrefix}/v1") |
|
|
| 79.5% |
Uncovered Elements: 15 (73) |
Complexity: 18 |
Complexity Density: 0.4 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
115 |
3 |
@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 |
|
|
124 |
|
|
|
|
| 72.7% |
Uncovered Elements: 3 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
125 |
2 |
@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 |
|
|
138 |
2 |
workspace = endpointPrefix; |
139 |
|
} |
140 |
|
|
141 |
2 |
return doSessionOperation(workspace, param.isBypassWorkspaceAcls(), new JCRSessionOp<Node>(workspace) { |
|
|
| 78.9% |
Uncovered Elements: 4 (19) |
Complexity: 5 |
Complexity Density: 0.38 |
|
142 |
2 |
@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 |
|
|
178 |
|
|
|
|
| 72.7% |
Uncovered Elements: 6 (22) |
Complexity: 6 |
Complexity Density: 0.5 |
|
179 |
1 |
@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 |
|
|
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) { |
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
206 |
1 |
@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 |
|
|
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 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
241 |
3 |
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 |
|
} |