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.cms.core;
35
36 import info.magnolia.cms.core.search.QueryManager;
37 import info.magnolia.cms.core.search.QueryManagerImpl;
38 import info.magnolia.cms.security.AccessDeniedException;
39 import info.magnolia.cms.security.AccessManager;
40 import info.magnolia.cms.security.PermissionUtil;
41 import info.magnolia.jcr.RuntimeRepositoryException;
42 import info.magnolia.jcr.util.NodeUtil;
43 import info.magnolia.jcr.util.SessionUtil;
44 import info.magnolia.jcr.wrapper.JCRPropertiesFilteringNodeWrapper;
45 import info.magnolia.logging.AuditLoggingUtil;
46
47 import java.io.ObjectStreamField;
48 import java.io.Serializable;
49 import java.util.Collection;
50
51 import javax.jcr.ItemNotFoundException;
52 import javax.jcr.Node;
53 import javax.jcr.PathNotFoundException;
54 import javax.jcr.RepositoryException;
55 import javax.jcr.Session;
56 import javax.jcr.Workspace;
57
58 import org.apache.commons.lang.StringUtils;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
61
62
63
64
65
66
67
68
69
70
71 @Deprecated
72 public class DefaultHierarchyManager implements HierarchyManager, Serializable {
73
74 private static final long serialVersionUID = 223L;
75
76
77
78
79
80 private static final ObjectStreamField[] serialPersistentFields = {
81 new ObjectStreamField("userId", String.class),
82 new ObjectStreamField("repositoryName", String.class),
83 new ObjectStreamField("workspaceName", String.class),
84 };
85
86 private static final Logger log = LoggerFactory.getLogger(DefaultHierarchyManager.class);
87
88 private Session jcrSession;
89
90 public DefaultHierarchyManager(Session jcrSession) {
91 this.jcrSession = jcrSession;
92 }
93
94 public DefaultHierarchyManager(String userId, Session jcrSession, AccessManager ignoredAccessManager) throws RepositoryException {
95 this(userId, jcrSession);
96 }
97
98 public DefaultHierarchyManager(String ignoredUserId, Session jcrSession) throws RepositoryException {
99 this(jcrSession);
100 }
101
102
103
104
105 public DefaultHierarchyManager(Session jcrSession, String ignored) {
106 this.jcrSession = jcrSession;
107 }
108
109
110
111
112
113 protected void setAccessManager(AccessManager accessManager) {
114
115 throw new UnsupportedOperationException("Custom access managers are no longer supported. Use Repository level security checks instead.");
116 }
117
118
119
120
121
122 @Override
123 public AccessManager getAccessManager() {
124
125 throw new UnsupportedOperationException("Custom access managers are no longer supported. Use Repository level security checks instead.");
126 }
127
128
129
130
131
132 protected void setQueryManager(QueryManager queryManager) {
133 throw new UnsupportedOperationException("This Operation is no longer available.");
134 }
135
136 @Override
137 public QueryManager getQueryManager() {
138 try {
139 return new QueryManagerImpl(getJcrSession().getWorkspace().getQueryManager(), this);
140 } catch (RepositoryException e) {
141 throw new RuntimeRepositoryException(e);
142 }
143 }
144
145 private Node getRootNode() throws RepositoryException {
146 return jcrSession.getRootNode();
147 }
148
149 private String getWorkspaceName() {
150 return jcrSession.getWorkspace().getName();
151 }
152
153 protected Session getJcrSession() {
154 return this.jcrSession;
155 }
156
157 protected void setJcrSession(Session jcrSession) {
158 this.jcrSession = jcrSession;
159 }
160
161
162
163
164
165
166
167
168
169
170
171 @Override
172 public Content createContent(String path, String label, String contentType) throws PathNotFoundException,
173 RepositoryException, AccessDeniedException {
174 Content content = wrapAsContent(this.getRootNode(), this.getNodePath(path, label), contentType);
175 setMetaData(content.getMetaData());
176 AuditLoggingUtil.log( AuditLoggingUtil.ACTION_CREATE, getWorkspaceName(), content.getItemType(), content.getHandle());
177 return content;
178 }
179
180 protected Content wrapAsContent(Node rootNode, String path, String contentType) throws PathNotFoundException, RepositoryException, AccessDeniedException {
181 return new DefaultContent(rootNode, path, contentType);
182 }
183
184 private String getNodePath(String parent, String label) {
185 if (StringUtils.isEmpty(parent) || (parent.equals("/"))) {
186 return label;
187 }
188 if (!parent.endsWith("/")) {
189 parent = parent + "/";
190 }
191 return getNodePath(parent + label);
192 }
193
194 private String getNodePath(String path) {
195 if (path != null && path.startsWith("/")) {
196 return path.replaceFirst("/", StringUtils.EMPTY);
197 }
198 return path;
199 }
200
201
202
203
204
205 protected void setMetaData(MetaData md) throws RepositoryException, AccessDeniedException {
206 md.setCreationDate();
207 md.setModificationDate();
208 md.setAuthorId(this.jcrSession.getUserID());
209 }
210
211
212
213
214
215
216
217
218 @Override
219 public Content getContent(String path) throws PathNotFoundException, RepositoryException, AccessDeniedException {
220 if (path.equals("/")) {
221 return this.getRoot();
222 }
223 try {
224 return wrapAsContent(this.getRootNode(), getNodePath(path));
225 } catch (ItemNotFoundException e) {
226 this.getJcrSession().refresh(true);
227 return wrapAsContent(this.getRootNode(), getNodePath(path));
228 }
229 }
230
231 public Content wrapAsContent(Node rootNode, String path) throws AccessDeniedException, PathNotFoundException, RepositoryException {
232 return new DefaultContent(rootNode, path);
233 }
234
235
236
237
238
239
240
241
242
243 @Override
244 public Content getContent(String path, boolean create, ItemType type) throws AccessDeniedException,
245 RepositoryException {
246 Content node;
247 try {
248 node = getContent(path);
249 }
250 catch (PathNotFoundException e) {
251 if (create) {
252 node = this.createContent(StringUtils.substringBeforeLast(path, "/"), StringUtils.substringAfterLast(
253 path,
254 "/"), type.toString());
255 AuditLoggingUtil.log( AuditLoggingUtil.ACTION_CREATE, getWorkspaceName(), node.getItemType(), node.getHandle());
256 }
257 else {
258 throw e;
259 }
260 }
261 return node;
262 }
263
264
265
266
267
268
269
270
271
272 @Override
273 public NodeData getNodeData(String path) throws PathNotFoundException, RepositoryException, AccessDeniedException {
274 if (StringUtils.isEmpty(path)) {
275 return null;
276 }
277 final String nodePath = StringUtils.substringBeforeLast(path, "/");
278 final String nodeDataName = StringUtils.substringAfterLast(path, "/");
279 return getContent(nodePath).getNodeData(nodeDataName);
280 }
281
282
283
284
285
286
287
288
289
290
291
292
293 @Deprecated
294 public Content getPage(String path, String templateName) throws PathNotFoundException, RepositoryException,
295 AccessDeniedException {
296 Content page = getContent(path);
297 if (page.getTemplate().equals(templateName)) {
298 return page;
299 }
300 Content pageToBeFound = null;
301 try {
302 if (page.hasChildren()) {
303 Collection<Content> children = page.getChildren(ItemType.CONTENT.getSystemName());
304 for (Content child : children) {
305 if (child.getTemplate().equals(templateName)) {
306 return child;
307 }
308 if (child.hasChildren()) {
309 pageToBeFound = getPage(child.getHandle(), templateName);
310 }
311 if (pageToBeFound != null) {
312 return pageToBeFound;
313 }
314 }
315 }
316 }
317 catch (Exception e) {
318 log.error("Failed to get - " + path + " : " + e.getMessage(), e);
319 }
320 return pageToBeFound;
321 }
322
323
324
325
326
327
328
329
330 @Override
331 public void delete(String path) throws PathNotFoundException, RepositoryException, AccessDeniedException {
332 ItemType type = null;
333 if (this.isNodeData(path)) {
334 this.getNodeData(makeRelative(path)).delete();
335 }
336 else {
337 Node aNode = this.getRootNode().getNode(makeRelative(path));
338 aNode = NodeUtil.deepUnwrap(aNode, JCRPropertiesFilteringNodeWrapper.class);
339 if (aNode.hasProperty(ItemType.JCR_FROZEN_PRIMARY_TYPE)) {
340 type = new ItemType(aNode.getProperty(ItemType.JCR_FROZEN_PRIMARY_TYPE).getString());
341 }
342 type = new ItemType(aNode.getProperty(ItemType.JCR_PRIMARY_TYPE).getString());
343 aNode.remove();
344 }
345 AuditLoggingUtil.log( AuditLoggingUtil.ACTION_DELETE, getWorkspaceName(), type, path);
346 }
347
348 private String makeRelative(String path) {
349 return StringUtils.stripStart(path, "/");
350 }
351
352
353
354
355 @Override
356 public Content getRoot() throws RepositoryException, AccessDeniedException {
357 return (new DefaultContent(this.getRootNode()));
358 }
359
360
361
362
363
364
365
366 @Override
367 public boolean isExist(String path) {
368 if (!PermissionUtil.isGranted(jcrSession, path, Session.ACTION_READ)) {
369 return false;
370 }
371 try {
372 this.getJcrSession().refresh(true);
373 return this.getJcrSession().itemExists(path);
374 } catch (RepositoryException re) {
375
376 if (re.getCause().getClass().getName().equals("org.apache.jackrabbit.spi.commons.conversion.MalformedPathException")) {
377
378 log.debug("{} is not valid jcr path.", path);
379 } else {
380 log.error("Exception caught", re);
381 }
382 return false;
383 }
384 }
385
386 @Override
387 public boolean isGranted(String path, long oldPermissions) {
388 return PermissionUtil.isGranted(jcrSession, path, oldPermissions);
389 }
390
391
392
393
394
395
396 @Override
397 public boolean isNodeData(String path) throws AccessDeniedException {
398 boolean result = false;
399 String nodePath = getNodePath(path);
400 if (StringUtils.isEmpty(nodePath)) {
401 return false;
402 }
403 try {
404 result = this.getRootNode().hasProperty(nodePath);
405 if (!result) {
406
407 result = this.getRootNode().hasProperty(nodePath + "/" + ItemType.JCR_DATA);
408 }
409 }
410 catch (RepositoryException e) {
411
412 }
413 return result;
414 }
415
416
417
418
419
420
421 @Override
422 public Content getContentByUUID(String uuid) throws ItemNotFoundException, RepositoryException,
423 AccessDeniedException {
424 try {
425 return wrapAsContent(this.getJcrSession().getNodeByIdentifier(uuid));
426 } catch (ItemNotFoundException e) {
427
428 this.getJcrSession().refresh(true);
429 return wrapAsContent(this.getJcrSession().getNodeByIdentifier(uuid));
430 }
431 }
432
433 protected Content wrapAsContent(Node node) {
434 return new DefaultContent(node);
435 }
436
437
438
439
440 @Override
441 public Workspace getWorkspace() {
442 return getJcrSession().getWorkspace();
443 }
444
445
446
447
448
449
450
451
452 @Override
453 public void moveTo(String source, String destination) throws PathNotFoundException, RepositoryException,
454 AccessDeniedException {
455 this.getWorkspace().move(source, destination);
456 AuditLoggingUtil.log( AuditLoggingUtil.ACTION_MOVE, getWorkspaceName(), source, destination);
457 }
458
459
460
461
462
463
464
465
466 @Override
467 public void copyTo(String source, String destination) throws PathNotFoundException, RepositoryException,
468 AccessDeniedException {
469 this.getWorkspace().copy(source, destination);
470 AuditLoggingUtil.log( AuditLoggingUtil.ACTION_COPY, getWorkspaceName(), source, destination);
471 }
472
473
474
475
476
477 @Override
478 public void save() throws RepositoryException {
479 try {
480 this.getJcrSession().save();
481 }
482 catch (RepositoryException re) {
483
484 log.error(re.getMessage(), re);
485 throw re;
486 }
487 }
488
489
490
491
492 @Override
493 public boolean hasPendingChanges() throws RepositoryException {
494 return this.getJcrSession().hasPendingChanges();
495 }
496
497
498
499
500
501
502
503 @Override
504 public void refresh(boolean keepChanges) throws RepositoryException {
505 this.getJcrSession().refresh(keepChanges);
506 }
507
508 @Override
509 public String getName() {
510 return getWorkspaceName();
511 }
512
513 @Override
514 public int hashCode() {
515 final int prime = 31;
516 int result = 1;
517 result = prime * result + ((jcrSession == null) ? 0 : jcrSession.hashCode());
518 return result;
519 }
520
521 @Override
522 public boolean equals(Object obj) {
523 if (this == obj) {
524 return true;
525 }
526 if (obj == null) {
527 return false;
528 }
529 if (getClass() != obj.getClass()) {
530 return false;
531 }
532 DefaultHierarchyManager other = (DefaultHierarchyManager) obj;
533 if (jcrSession == null) {
534 if (other.jcrSession != null) {
535 return false;
536 }
537 } else if (!SessionUtil.hasSameUnderlyingSession(jcrSession, other.jcrSession)) {
538 return false;
539 }
540 return true;
541 }
542 }