2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.jcr
;
18 import java
.io
.ByteArrayInputStream
;
19 import java
.io
.ByteArrayOutputStream
;
21 import java
.io
.FileInputStream
;
22 import java
.io
.IOException
;
23 import java
.io
.InputStream
;
24 import java
.net
.MalformedURLException
;
26 import java
.security
.Principal
;
27 import java
.text
.DateFormat
;
28 import java
.text
.ParseException
;
29 import java
.util
.ArrayList
;
30 import java
.util
.Calendar
;
31 import java
.util
.Collections
;
32 import java
.util
.Date
;
33 import java
.util
.GregorianCalendar
;
34 import java
.util
.Iterator
;
35 import java
.util
.List
;
37 import java
.util
.TreeMap
;
39 import javax
.jcr
.Binary
;
40 import javax
.jcr
.NamespaceRegistry
;
41 import javax
.jcr
.NoSuchWorkspaceException
;
42 import javax
.jcr
.Node
;
43 import javax
.jcr
.NodeIterator
;
44 import javax
.jcr
.Property
;
45 import javax
.jcr
.PropertyIterator
;
46 import javax
.jcr
.PropertyType
;
47 import javax
.jcr
.Repository
;
48 import javax
.jcr
.RepositoryException
;
49 import javax
.jcr
.Session
;
50 import javax
.jcr
.Value
;
51 import javax
.jcr
.Workspace
;
52 import javax
.jcr
.nodetype
.NodeType
;
53 import javax
.jcr
.observation
.EventListener
;
54 import javax
.jcr
.query
.Query
;
55 import javax
.jcr
.query
.QueryResult
;
56 import javax
.jcr
.security
.AccessControlEntry
;
57 import javax
.jcr
.security
.AccessControlList
;
58 import javax
.jcr
.security
.AccessControlManager
;
59 import javax
.jcr
.security
.AccessControlPolicy
;
60 import javax
.jcr
.security
.AccessControlPolicyIterator
;
61 import javax
.jcr
.security
.Privilege
;
63 import org
.apache
.commons
.io
.IOUtils
;
64 import org
.apache
.commons
.logging
.Log
;
65 import org
.apache
.commons
.logging
.LogFactory
;
66 import org
.argeo
.ArgeoException
;
67 import org
.argeo
.ArgeoMonitor
;
68 import org
.argeo
.util
.security
.DigestUtils
;
69 import org
.argeo
.util
.security
.SimplePrincipal
;
71 /** Utility methods to simplify common JCR operations. */
72 public class JcrUtils
implements ArgeoJcrConstants
{
74 final private static Log log
= LogFactory
.getLog(JcrUtils
.class);
77 * Not complete yet. See
78 * http://www.day.com/specs/jcr/2.0/3_Repository_Model.html#3.2.2%20Local
81 public final static char[] INVALID_NAME_CHARACTERS
= { '/', ':', '[', ']',
87 /** Prevents instantiation */
92 * Queries one single node.
94 * @return one single node or null if none was found
95 * @throws ArgeoException
96 * if more than one node was found
98 public static Node
querySingleNode(Query query
) {
99 NodeIterator nodeIterator
;
101 QueryResult queryResult
= query
.execute();
102 nodeIterator
= queryResult
.getNodes();
103 } catch (RepositoryException e
) {
104 throw new ArgeoException("Cannot execute query " + query
, e
);
107 if (nodeIterator
.hasNext())
108 node
= nodeIterator
.nextNode();
112 if (nodeIterator
.hasNext())
113 throw new ArgeoException("Query returned more than one node.");
117 /** Retrieves the node name from the provided path */
118 public static String
nodeNameFromPath(String path
) {
119 if (path
.equals("/"))
121 if (path
.charAt(0) != '/')
122 throw new ArgeoException("Path " + path
+ " must start with a '/'");
124 if (pathT
.charAt(pathT
.length() - 1) == '/')
125 pathT
= pathT
.substring(0, pathT
.length() - 2);
127 int index
= pathT
.lastIndexOf('/');
128 return pathT
.substring(index
+ 1);
131 /** Retrieves the parent path of the provided path */
132 public static String
parentPath(String path
) {
133 if (path
.equals("/"))
134 throw new ArgeoException("Root path '/' has no parent path");
135 if (path
.charAt(0) != '/')
136 throw new ArgeoException("Path " + path
+ " must start with a '/'");
138 if (pathT
.charAt(pathT
.length() - 1) == '/')
139 pathT
= pathT
.substring(0, pathT
.length() - 2);
141 int index
= pathT
.lastIndexOf('/');
142 return pathT
.substring(0, index
);
145 /** The provided data as a path ('/' at the end, not the beginning) */
146 public static String
dateAsPath(Calendar cal
) {
147 return dateAsPath(cal
, false);
151 * Creates a deep path based on a URL:
152 * http://subdomain.example.com/to/content?args =>
153 * com/example/subdomain/to/content
155 public static String
urlAsPath(String url
) {
157 URL u
= new URL(url
);
158 StringBuffer path
= new StringBuffer(url
.length());
160 path
.append(hostAsPath(u
.getHost()));
161 // we don't put port since it may not always be there and may change
162 path
.append(u
.getPath());
163 return path
.toString();
164 } catch (MalformedURLException e
) {
165 throw new ArgeoException("Cannot generate URL path for " + url
, e
);
169 /** Set the {@link NodeType#NT_ADDRESS} properties based on this URL. */
170 public static void urlToAddressProperties(Node node
, String url
) {
172 URL u
= new URL(url
);
173 node
.setProperty(Property
.JCR_PROTOCOL
, u
.getProtocol());
174 node
.setProperty(Property
.JCR_HOST
, u
.getHost());
175 node
.setProperty(Property
.JCR_PORT
, Integer
.toString(u
.getPort()));
176 node
.setProperty(Property
.JCR_PATH
, normalizePath(u
.getPath()));
177 } catch (Exception e
) {
178 throw new ArgeoException("Cannot set URL " + url
179 + " as nt:address properties", e
);
183 /** Build URL based on the {@link NodeType#NT_ADDRESS} properties. */
184 public static String
urlFromAddressProperties(Node node
) {
187 node
.getProperty(Property
.JCR_PROTOCOL
).getString(), node
188 .getProperty(Property
.JCR_HOST
).getString(),
189 (int) node
.getProperty(Property
.JCR_PORT
).getLong(), node
190 .getProperty(Property
.JCR_PATH
).getString());
192 } catch (Exception e
) {
193 throw new ArgeoException(
194 "Cannot get URL from nt:address properties of " + node
, e
);
202 /** Make sure that: starts with '/', do not end with '/', do not have '//' */
203 public static String
normalizePath(String path
) {
204 List
<String
> tokens
= tokenize(path
);
205 StringBuffer buf
= new StringBuffer(path
.length());
206 for (String token
: tokens
) {
210 return buf
.toString();
214 * Creates a path from a FQDN, inverting the order of the component:
215 * www.argeo.org => org.argeo.www
217 public static String
hostAsPath(String host
) {
218 StringBuffer path
= new StringBuffer(host
.length());
219 String
[] hostTokens
= host
.split("\\.");
220 for (int i
= hostTokens
.length
- 1; i
>= 0; i
--) {
221 path
.append(hostTokens
[i
]);
225 return path
.toString();
229 * Creates a path from a UUID (e.g. 6ebda899-217d-4bf1-abe4-2839085c8f3c =>
230 * 6ebda899-217d/4bf1/abe4/2839085c8f3c/). '/' at the end, not the beginning
232 public static String
uuidAsPath(String uuid
) {
233 StringBuffer path
= new StringBuffer(uuid
.length());
234 String
[] tokens
= uuid
.split("-");
235 for (int i
= 0; i
< tokens
.length
; i
++) {
236 path
.append(tokens
[i
]);
240 return path
.toString();
244 * The provided data as a path ('/' at the end, not the beginning)
249 * whether to add hour as well
251 public static String
dateAsPath(Calendar cal
, Boolean addHour
) {
252 StringBuffer buf
= new StringBuffer(14);
254 buf
.append(cal
.get(Calendar
.YEAR
));
257 int month
= cal
.get(Calendar
.MONTH
) + 1;
264 int day
= cal
.get(Calendar
.DAY_OF_MONTH
);
272 int hour
= cal
.get(Calendar
.HOUR_OF_DAY
);
279 return buf
.toString();
283 /** Converts in one call a string into a gregorian calendar. */
284 public static Calendar
parseCalendar(DateFormat dateFormat
, String value
) {
286 Date date
= dateFormat
.parse(value
);
287 Calendar calendar
= new GregorianCalendar();
288 calendar
.setTime(date
);
290 } catch (ParseException e
) {
291 throw new ArgeoException("Cannot parse " + value
292 + " with date format " + dateFormat
, e
);
297 /** The last element of a path. */
298 public static String
lastPathElement(String path
) {
299 if (path
.charAt(path
.length() - 1) == '/')
300 throw new ArgeoException("Path " + path
+ " cannot end with '/'");
301 int index
= path
.lastIndexOf('/');
303 throw new ArgeoException("Cannot find last path element for "
305 return path
.substring(index
+ 1);
309 * Call {@link Node#getName()} without exceptions (useful in super
312 public static String
getNameQuietly(Node node
) {
314 return node
.getName();
315 } catch (RepositoryException e
) {
316 throw new ArgeoException("Cannot get name from " + node
, e
);
321 * Call {@link Node#getProperty(String)} without exceptions (useful in super
324 public static String
getStringPropertyQuietly(Node node
, String propertyName
) {
326 return node
.getProperty(propertyName
).getString();
327 } catch (RepositoryException e
) {
328 throw new ArgeoException("Cannot get name from " + node
, e
);
333 * Routine that get the child with this name, adding id it does not already
336 public static Node
getOrAdd(Node parent
, String childName
,
337 String childPrimaryNodeType
) throws RepositoryException
{
338 return parent
.hasNode(childName
) ? parent
.getNode(childName
) : parent
339 .addNode(childName
, childPrimaryNodeType
);
343 * Routine that get the child with this name, adding id it does not already
346 public static Node
getOrAdd(Node parent
, String childName
)
347 throws RepositoryException
{
348 return parent
.hasNode(childName
) ? parent
.getNode(childName
) : parent
352 /** Convert a {@link NodeIterator} to a list of {@link Node} */
353 public static List
<Node
> nodeIteratorToList(NodeIterator nodeIterator
) {
354 List
<Node
> nodes
= new ArrayList
<Node
>();
355 while (nodeIterator
.hasNext()) {
356 nodes
.add(nodeIterator
.nextNode());
366 * Concisely get the string value of a property or null if this node doesn't
369 public static String
get(Node node
, String propertyName
) {
371 if (!node
.hasProperty(propertyName
))
373 return node
.getProperty(propertyName
).getString();
374 } catch (RepositoryException e
) {
375 throw new ArgeoException("Cannot get property " + propertyName
380 /** Concisely get the boolean value of a property */
381 public static Boolean
check(Node node
, String propertyName
) {
383 return node
.getProperty(propertyName
).getBoolean();
384 } catch (RepositoryException e
) {
385 throw new ArgeoException("Cannot get property " + propertyName
390 /** Concisely get the bytes array value of a property */
391 public static byte[] getBytes(Node node
, String propertyName
) {
393 return getBinaryAsBytes(node
.getProperty(propertyName
));
394 } catch (RepositoryException e
) {
395 throw new ArgeoException("Cannot get property " + propertyName
400 /** Creates the nodes making path, if they don't exist. */
401 public static Node
mkdirs(Session session
, String path
) {
402 return mkdirs(session
, path
, null, null, false);
406 * use {@link #mkdirs(Session, String, String, String, Boolean)} instead.
411 public static Node
mkdirs(Session session
, String path
, String type
,
412 Boolean versioning
) {
413 return mkdirs(session
, path
, type
, type
, false);
418 * the type of the leaf node
420 public static Node
mkdirs(Session session
, String path
, String type
) {
421 return mkdirs(session
, path
, type
, null, false);
425 * Create sub nodes relative to a parent node
428 * the type of the leaf node
430 public static Node
mkdirs(Node parentNode
, String relativePath
,
432 return mkdirs(parentNode
, relativePath
, nodeType
, null);
436 * Create sub nodes relative to a parent node
439 * the type of the leaf node
441 public static Node
mkdirs(Node parentNode
, String relativePath
,
442 String nodeType
, String intermediaryNodeType
) {
443 List
<String
> tokens
= tokenize(relativePath
);
444 Node currParent
= parentNode
;
446 for (int i
= 0; i
< tokens
.size(); i
++) {
447 String name
= tokens
.get(i
);
448 if (parentNode
.hasNode(name
)) {
449 currParent
= currParent
.getNode(name
);
451 if (i
!= (tokens
.size() - 1)) {// intermediary
452 currParent
= currParent
.addNode(name
,
453 intermediaryNodeType
);
455 currParent
= currParent
.addNode(name
, nodeType
);
460 } catch (RepositoryException e
) {
461 throw new ArgeoException("Cannot mkdirs relative path "
462 + relativePath
+ " from " + parentNode
, e
);
467 * Synchronized and save is performed, to avoid race conditions in
468 * initializers leading to duplicate nodes.
470 public synchronized static Node
mkdirsSafe(Session session
, String path
,
473 if (session
.hasPendingChanges())
474 throw new ArgeoException(
475 "Session has pending changes, save them first.");
476 Node node
= mkdirs(session
, path
, type
);
479 } catch (RepositoryException e
) {
480 discardQuietly(session
);
481 throw new ArgeoException("Cannot safely make directories", e
);
485 public synchronized static Node
mkdirsSafe(Session session
, String path
) {
486 return mkdirsSafe(session
, path
, null);
490 * Creates the nodes making path, if they don't exist. This is up to the
491 * caller to save the session. Use with caution since it can create
492 * duplicate nodes if used concurrently.
494 public static Node
mkdirs(Session session
, String path
, String type
,
495 String intermediaryNodeType
, Boolean versioning
) {
497 if (path
.equals('/'))
498 return session
.getRootNode();
500 if (session
.itemExists(path
)) {
501 Node node
= session
.getNode(path
);
503 if (type
!= null && !node
.isNodeType(type
)
504 && !node
.getPath().equals("/"))
505 throw new ArgeoException("Node " + node
506 + " exists but is of type "
507 + node
.getPrimaryNodeType().getName()
508 + " not of type " + type
);
509 // TODO: check versioning
513 StringBuffer current
= new StringBuffer("/");
514 Node currentNode
= session
.getRootNode();
515 Iterator
<String
> it
= tokenize(path
).iterator();
516 while (it
.hasNext()) {
517 String part
= it
.next();
518 current
.append(part
).append('/');
519 if (!session
.itemExists(current
.toString())) {
520 if (!it
.hasNext() && type
!= null)
521 currentNode
= currentNode
.addNode(part
, type
);
522 else if (it
.hasNext() && intermediaryNodeType
!= null)
523 currentNode
= currentNode
.addNode(part
,
524 intermediaryNodeType
);
526 currentNode
= currentNode
.addNode(part
);
528 currentNode
.addMixin(NodeType
.MIX_VERSIONABLE
);
529 if (log
.isTraceEnabled())
530 log
.debug("Added folder " + part
+ " as " + current
);
532 currentNode
= (Node
) session
.getItem(current
.toString());
536 } catch (RepositoryException e
) {
537 discardQuietly(session
);
538 throw new ArgeoException("Cannot mkdirs " + path
, e
);
543 /** Convert a path to the list of its tokens */
544 public static List
<String
> tokenize(String path
) {
545 List
<String
> tokens
= new ArrayList
<String
>();
546 boolean optimized
= false;
548 String
[] rawTokens
= path
.split("/");
549 for (String token
: rawTokens
) {
550 if (!token
.equals(""))
554 StringBuffer curr
= new StringBuffer();
555 char[] arr
= path
.toCharArray();
556 chars
: for (int i
= 0; i
< arr
.length
; i
++) {
559 if (i
== 0 || (i
== arr
.length
- 1))
561 if (curr
.length() > 0) {
562 tokens
.add(curr
.toString());
563 curr
= new StringBuffer();
568 if (curr
.length() > 0) {
569 tokens
.add(curr
.toString());
570 curr
= new StringBuffer();
573 return Collections
.unmodifiableList(tokens
);
577 * Safe and repository implementation independent registration of a
580 public static void registerNamespaceSafely(Session session
, String prefix
,
583 registerNamespaceSafely(session
.getWorkspace()
584 .getNamespaceRegistry(), prefix
, uri
);
585 } catch (RepositoryException e
) {
586 throw new ArgeoException("Cannot find namespace registry", e
);
591 * Safe and repository implementation independent registration of a
594 public static void registerNamespaceSafely(NamespaceRegistry nr
,
595 String prefix
, String uri
) {
597 String
[] prefixes
= nr
.getPrefixes();
598 for (String pref
: prefixes
)
599 if (pref
.equals(prefix
)) {
600 String registeredUri
= nr
.getURI(pref
);
601 if (!registeredUri
.equals(uri
))
602 throw new ArgeoException("Prefix " + pref
603 + " already registered for URI "
605 + " which is different from provided URI "
610 nr
.registerNamespace(prefix
, uri
);
611 } catch (RepositoryException e
) {
612 throw new ArgeoException("Cannot register namespace " + uri
613 + " under prefix " + prefix
, e
);
617 /** Recursively outputs the contents of the given node. */
618 public static void debug(Node node
) {
622 /** Recursively outputs the contents of the given node. */
623 public static void debug(Node node
, Log log
) {
625 // First output the node path
626 log
.debug(node
.getPath());
627 // Skip the virtual (and large!) jcr:system subtree
628 if (node
.getName().equals("jcr:system")) {
632 // Then the children nodes (recursive)
633 NodeIterator it
= node
.getNodes();
634 while (it
.hasNext()) {
635 Node childNode
= it
.nextNode();
636 debug(childNode
, log
);
639 // Then output the properties
640 PropertyIterator properties
= node
.getProperties();
641 // log.debug("Property are : ");
643 properties
: while (properties
.hasNext()) {
644 Property property
= properties
.nextProperty();
645 if (property
.getType() == PropertyType
.BINARY
)
646 continue properties
;// skip
647 if (property
.getDefinition().isMultiple()) {
648 // A multi-valued property, print all values
649 Value
[] values
= property
.getValues();
650 for (int i
= 0; i
< values
.length
; i
++) {
651 log
.debug(property
.getPath() + "="
652 + values
[i
].getString());
655 // A single-valued property
656 log
.debug(property
.getPath() + "=" + property
.getString());
659 } catch (Exception e
) {
660 log
.error("Could not debug " + node
, e
);
665 /** Logs the effective access control policies */
666 public static void logEffectiveAccessPolicies(Node node
) {
668 logEffectiveAccessPolicies(node
.getSession(), node
.getPath());
669 } catch (RepositoryException e
) {
670 log
.error("Cannot log effective access policies of " + node
, e
);
674 /** Logs the effective access control policies */
675 public static void logEffectiveAccessPolicies(Session session
, String path
) {
676 if (!log
.isDebugEnabled())
680 AccessControlPolicy
[] effectivePolicies
= session
681 .getAccessControlManager().getEffectivePolicies(path
);
682 if (effectivePolicies
.length
> 0) {
683 for (AccessControlPolicy policy
: effectivePolicies
) {
684 if (policy
instanceof AccessControlList
) {
685 AccessControlList acl
= (AccessControlList
) policy
;
686 log
.debug("Access control list for " + path
+ "\n"
687 + accessControlListSummary(acl
));
691 log
.debug("No effective access control policy for " + path
);
693 } catch (RepositoryException e
) {
694 log
.error("Cannot log effective access policies of " + path
, e
);
698 /** Returns a human-readable summary of this access control list. */
699 public static String
accessControlListSummary(AccessControlList acl
) {
700 StringBuffer buf
= new StringBuffer("");
702 for (AccessControlEntry ace
: acl
.getAccessControlEntries()) {
703 buf
.append('\t').append(ace
.getPrincipal().getName())
705 for (Privilege priv
: ace
.getPrivileges())
706 buf
.append("\t\t").append(priv
.getName()).append('\n');
708 return buf
.toString();
709 } catch (RepositoryException e
) {
710 throw new ArgeoException("Cannot write summary of " + acl
, e
);
715 * Copies recursively the content of a node to another one. Do NOT copy the
716 * property values of {@link NodeType#MIX_CREATED} and
717 * {@link NodeType#MIX_LAST_MODIFIED}, but update the
718 * {@link Property#JCR_LAST_MODIFIED} and
719 * {@link Property#JCR_LAST_MODIFIED_BY} properties if the target node has
720 * the {@link NodeType#MIX_LAST_MODIFIED} mixin.
722 public static void copy(Node fromNode
, Node toNode
) {
724 if (toNode
.getDefinition().isProtected())
727 // process properties
728 PropertyIterator pit
= fromNode
.getProperties();
729 properties
: while (pit
.hasNext()) {
730 Property fromProperty
= pit
.nextProperty();
731 String propertyName
= fromProperty
.getName();
732 if (toNode
.hasProperty(propertyName
)
733 && toNode
.getProperty(propertyName
).getDefinition()
737 if (fromProperty
.getDefinition().isProtected())
740 if (propertyName
.equals("jcr:created")
741 || propertyName
.equals("jcr:createdBy")
742 || propertyName
.equals("jcr:lastModified")
743 || propertyName
.equals("jcr:lastModifiedBy"))
746 if (fromProperty
.isMultiple()) {
747 toNode
.setProperty(propertyName
, fromProperty
.getValues());
749 toNode
.setProperty(propertyName
, fromProperty
.getValue());
753 // update jcr:lastModified and jcr:lastModifiedBy in toNode in case
754 // they existed, before adding the mixins
755 updateLastModified(toNode
);
758 for (NodeType mixinType
: fromNode
.getMixinNodeTypes()) {
759 toNode
.addMixin(mixinType
.getName());
762 // process children nodes
763 NodeIterator nit
= fromNode
.getNodes();
764 while (nit
.hasNext()) {
765 Node fromChild
= nit
.nextNode();
766 Integer index
= fromChild
.getIndex();
767 String nodeRelPath
= fromChild
.getName() + "[" + index
+ "]";
769 if (toNode
.hasNode(nodeRelPath
))
770 toChild
= toNode
.getNode(nodeRelPath
);
772 toChild
= toNode
.addNode(fromChild
.getName(), fromChild
773 .getPrimaryNodeType().getName());
774 copy(fromChild
, toChild
);
776 } catch (RepositoryException e
) {
777 throw new ArgeoException("Cannot copy " + fromNode
+ " to "
783 * Check whether all first-level properties (except jcr:* properties) are
784 * equal. Skip jcr:* properties
786 public static Boolean
allPropertiesEquals(Node reference
, Node observed
,
787 Boolean onlyCommonProperties
) {
789 PropertyIterator pit
= reference
.getProperties();
790 props
: while (pit
.hasNext()) {
791 Property propReference
= pit
.nextProperty();
792 String propName
= propReference
.getName();
793 if (propName
.startsWith("jcr:"))
796 if (!observed
.hasProperty(propName
))
797 if (onlyCommonProperties
)
801 // TODO: deal with multiple property values?
802 if (!observed
.getProperty(propName
).getValue()
803 .equals(propReference
.getValue()))
807 } catch (RepositoryException e
) {
808 throw new ArgeoException("Cannot check all properties equals of "
809 + reference
+ " and " + observed
, e
);
813 public static Map
<String
, PropertyDiff
> diffProperties(Node reference
,
815 Map
<String
, PropertyDiff
> diffs
= new TreeMap
<String
, PropertyDiff
>();
816 diffPropertiesLevel(diffs
, null, reference
, observed
);
821 * Compare the properties of two nodes. Recursivity to child nodes is not
822 * yet supported. Skip jcr:* properties.
824 static void diffPropertiesLevel(Map
<String
, PropertyDiff
> diffs
,
825 String baseRelPath
, Node reference
, Node observed
) {
827 // check removed and modified
828 PropertyIterator pit
= reference
.getProperties();
829 props
: while (pit
.hasNext()) {
830 Property p
= pit
.nextProperty();
831 String name
= p
.getName();
832 if (name
.startsWith("jcr:"))
835 if (!observed
.hasProperty(name
)) {
836 String relPath
= propertyRelPath(baseRelPath
, name
);
837 PropertyDiff pDiff
= new PropertyDiff(PropertyDiff
.REMOVED
,
838 relPath
, p
.getValue(), null);
839 diffs
.put(relPath
, pDiff
);
841 if (p
.isMultiple()) {
842 // FIXME implement multiple
844 Value referenceValue
= p
.getValue();
845 Value newValue
= observed
.getProperty(name
).getValue();
846 if (!referenceValue
.equals(newValue
)) {
847 String relPath
= propertyRelPath(baseRelPath
, name
);
848 PropertyDiff pDiff
= new PropertyDiff(
849 PropertyDiff
.MODIFIED
, relPath
,
850 referenceValue
, newValue
);
851 diffs
.put(relPath
, pDiff
);
857 pit
= observed
.getProperties();
858 props
: while (pit
.hasNext()) {
859 Property p
= pit
.nextProperty();
860 String name
= p
.getName();
861 if (name
.startsWith("jcr:"))
863 if (!reference
.hasProperty(name
)) {
864 if (p
.isMultiple()) {
865 // FIXME implement multiple
867 String relPath
= propertyRelPath(baseRelPath
, name
);
868 PropertyDiff pDiff
= new PropertyDiff(
869 PropertyDiff
.ADDED
, relPath
, null, p
.getValue());
870 diffs
.put(relPath
, pDiff
);
874 } catch (RepositoryException e
) {
875 throw new ArgeoException("Cannot diff " + reference
+ " and "
881 * Compare only a restricted list of properties of two nodes. No
885 public static Map
<String
, PropertyDiff
> diffProperties(Node reference
,
886 Node observed
, List
<String
> properties
) {
887 Map
<String
, PropertyDiff
> diffs
= new TreeMap
<String
, PropertyDiff
>();
889 Iterator
<String
> pit
= properties
.iterator();
891 props
: while (pit
.hasNext()) {
892 String name
= pit
.next();
893 if (!reference
.hasProperty(name
)) {
894 if (!observed
.hasProperty(name
))
896 Value val
= observed
.getProperty(name
).getValue();
898 // empty String but not null
899 if ("".equals(val
.getString()))
901 } catch (Exception e
) {
902 // not parseable as String, silent
904 PropertyDiff pDiff
= new PropertyDiff(PropertyDiff
.ADDED
,
906 diffs
.put(name
, pDiff
);
907 } else if (!observed
.hasProperty(name
)) {
908 PropertyDiff pDiff
= new PropertyDiff(PropertyDiff
.REMOVED
,
909 name
, reference
.getProperty(name
).getValue(), null);
910 diffs
.put(name
, pDiff
);
912 Value referenceValue
= reference
.getProperty(name
)
914 Value newValue
= observed
.getProperty(name
).getValue();
915 if (!referenceValue
.equals(newValue
)) {
916 PropertyDiff pDiff
= new PropertyDiff(
917 PropertyDiff
.MODIFIED
, name
, referenceValue
,
919 diffs
.put(name
, pDiff
);
923 } catch (RepositoryException e
) {
924 throw new ArgeoException("Cannot diff " + reference
+ " and "
930 /** Builds a property relPath to be used in the diff. */
931 private static String
propertyRelPath(String baseRelPath
,
932 String propertyName
) {
933 if (baseRelPath
== null)
936 return baseRelPath
+ '/' + propertyName
;
940 * Normalizes a name so that it can be stored in contexts not supporting
941 * names with ':' (typically databases). Replaces ':' by '_'.
943 public static String
normalize(String name
) {
944 return name
.replace(':', '_');
948 * Replaces characters which are invalid in a JCR name by '_'. Currently not
951 * @see JcrUtils#INVALID_NAME_CHARACTERS
953 public static String
replaceInvalidChars(String name
) {
954 return replaceInvalidChars(name
, '_');
958 * Replaces characters which are invalid in a JCR name. Currently not
961 * @see JcrUtils#INVALID_NAME_CHARACTERS
963 public static String
replaceInvalidChars(String name
, char replacement
) {
964 boolean modified
= false;
965 char[] arr
= name
.toCharArray();
966 for (int i
= 0; i
< arr
.length
; i
++) {
968 invalid
: for (char invalid
: INVALID_NAME_CHARACTERS
) {
970 arr
[i
] = replacement
;
977 return new String(arr
);
979 // do not create new object if unnecessary
984 * Removes forbidden characters from a path, replacing them with '_'
986 * @deprecated use {@link #replaceInvalidChars(String)} instead
988 public static String
removeForbiddenCharacters(String str
) {
989 return str
.replace('[', '_').replace(']', '_').replace('/', '_')
994 /** Cleanly disposes a {@link Binary} even if it is null. */
995 public static void closeQuietly(Binary binary
) {
1001 /** Retrieve a {@link Binary} as a byte array */
1002 public static byte[] getBinaryAsBytes(Property property
) {
1003 ByteArrayOutputStream out
= new ByteArrayOutputStream();
1004 InputStream in
= null;
1005 Binary binary
= null;
1007 binary
= property
.getBinary();
1008 in
= binary
.getStream();
1009 IOUtils
.copy(in
, out
);
1010 return out
.toByteArray();
1011 } catch (Exception e
) {
1012 throw new ArgeoException("Cannot read binary " + property
1015 IOUtils
.closeQuietly(out
);
1016 IOUtils
.closeQuietly(in
);
1017 closeQuietly(binary
);
1021 /** Writes a {@link Binary} from a byte array */
1022 public static void setBinaryAsBytes(Node node
, String property
, byte[] bytes
) {
1023 InputStream in
= null;
1024 Binary binary
= null;
1026 in
= new ByteArrayInputStream(bytes
);
1027 binary
= node
.getSession().getValueFactory().createBinary(in
);
1028 node
.setProperty(property
, binary
);
1029 } catch (Exception e
) {
1030 throw new ArgeoException("Cannot read binary " + property
1033 IOUtils
.closeQuietly(in
);
1034 closeQuietly(binary
);
1039 * Creates depth from a string (typically a username) by adding levels based
1040 * on its first characters: "aBcD",2 => a/aB
1042 public static String
firstCharsToPath(String str
, Integer nbrOfChars
) {
1043 if (str
.length() < nbrOfChars
)
1044 throw new ArgeoException("String " + str
1045 + " length must be greater or equal than " + nbrOfChars
);
1046 StringBuffer path
= new StringBuffer("");
1047 StringBuffer curr
= new StringBuffer("");
1048 for (int i
= 0; i
< nbrOfChars
; i
++) {
1049 curr
.append(str
.charAt(i
));
1051 if (i
< nbrOfChars
- 1)
1054 return path
.toString();
1058 * Discards the current changes in the session attached to this node. To be
1059 * used typically in a catch block.
1061 * @see #discardQuietly(Session)
1063 public static void discardUnderlyingSessionQuietly(Node node
) {
1065 discardQuietly(node
.getSession());
1066 } catch (RepositoryException e
) {
1067 log
.warn("Cannot quietly discard session of node " + node
+ ": "
1073 * Discards the current changes in a session by calling
1074 * {@link Session#refresh(boolean)} with <code>false</code>, only logging
1075 * potential errors when doing so. To be used typically in a catch block.
1077 public static void discardQuietly(Session session
) {
1079 if (session
!= null)
1080 session
.refresh(false);
1081 } catch (RepositoryException e
) {
1082 log
.warn("Cannot quietly discard session " + session
+ ": "
1088 * Login to a workspace with implicit credentials, creates the workspace
1089 * with these credentials if it does not already exist.
1091 public static Session
loginOrCreateWorkspace(Repository repository
,
1092 String workspaceName
) throws RepositoryException
{
1093 Session workspaceSession
= null;
1094 Session defaultSession
= null;
1097 workspaceSession
= repository
.login(workspaceName
);
1098 } catch (NoSuchWorkspaceException e
) {
1099 // try to create workspace
1100 defaultSession
= repository
.login();
1101 defaultSession
.getWorkspace().createWorkspace(workspaceName
);
1102 workspaceSession
= repository
.login(workspaceName
);
1104 return workspaceSession
;
1106 logoutQuietly(defaultSession
);
1110 /** Logs out the session, not throwing any exception, even if it is null. */
1111 public static void logoutQuietly(Session session
) {
1113 if (session
!= null)
1114 if (session
.isLive())
1116 } catch (Exception e
) {
1122 * Convenient method to add a listener. uuids passed as null, deep=true,
1123 * local=true, only one node type
1125 public static void addListener(Session session
, EventListener listener
,
1126 int eventTypes
, String basePath
, String nodeType
) {
1128 session
.getWorkspace()
1129 .getObservationManager()
1136 nodeType
== null ?
null : new String
[] { nodeType
},
1138 } catch (RepositoryException e
) {
1139 throw new ArgeoException("Cannot add JCR listener " + listener
1140 + " to session " + session
, e
);
1144 /** Removes a listener without throwing exception */
1145 public static void removeListenerQuietly(Session session
,
1146 EventListener listener
) {
1147 if (session
== null || !session
.isLive())
1150 session
.getWorkspace().getObservationManager()
1151 .removeEventListener(listener
);
1152 } catch (RepositoryException e
) {
1158 * Quietly unregisters an {@link EventListener} from the udnerlying
1159 * workspace of this node.
1161 public static void unregisterQuietly(Node node
, EventListener eventListener
) {
1163 unregisterQuietly(node
.getSession().getWorkspace(), eventListener
);
1164 } catch (RepositoryException e
) {
1166 if (log
.isTraceEnabled())
1167 log
.trace("Could not unregister event listener "
1172 /** Quietly unregisters an {@link EventListener} from this workspace */
1173 public static void unregisterQuietly(Workspace workspace
,
1174 EventListener eventListener
) {
1175 if (eventListener
== null)
1178 workspace
.getObservationManager()
1179 .removeEventListener(eventListener
);
1180 } catch (RepositoryException e
) {
1182 if (log
.isTraceEnabled())
1183 log
.trace("Could not unregister event listener "
1189 * If this node is has the {@link NodeType#MIX_LAST_MODIFIED} mixin, it
1190 * updates the {@link Property#JCR_LAST_MODIFIED} property with the current
1191 * time and the {@link Property#JCR_LAST_MODIFIED_BY} property with the
1192 * underlying session user id. In Jackrabbit 2.x, <a
1193 * href="https://issues.apache.org/jira/browse/JCR-2233">these properties
1194 * are not automatically updated</a>, hence the need for manual update. The
1195 * session is not saved.
1197 public static void updateLastModified(Node node
) {
1199 if (!node
.isNodeType(NodeType
.MIX_LAST_MODIFIED
))
1200 node
.addMixin(NodeType
.MIX_LAST_MODIFIED
);
1201 node
.setProperty(Property
.JCR_LAST_MODIFIED
,
1202 new GregorianCalendar());
1203 node
.setProperty(Property
.JCR_LAST_MODIFIED_BY
, node
.getSession()
1205 } catch (RepositoryException e
) {
1206 throw new ArgeoException("Cannot update last modified on " + node
,
1211 /** Update lastModified recursively until this parent. */
1212 public static void updateLastModifiedAndParents(Node node
, String untilPath
) {
1214 if (!node
.getPath().startsWith(untilPath
))
1215 throw new ArgeoException(node
+ " is not under " + untilPath
);
1216 updateLastModified(node
);
1217 if (!node
.getPath().equals(untilPath
))
1218 updateLastModifiedAndParents(node
.getParent(), untilPath
);
1219 } catch (RepositoryException e
) {
1220 throw new ArgeoException("Cannot update lastModified from " + node
1221 + " until " + untilPath
, e
);
1226 * Returns a String representing the short version (see <a
1227 * href="http://jackrabbit.apache.org/node-type-notation.html"> Node type
1228 * Notation </a> attributes grammar) of the main business attributes of this
1229 * property definition
1233 public static String
getPropertyDefinitionAsString(Property prop
) {
1234 StringBuffer sbuf
= new StringBuffer();
1236 if (prop
.getDefinition().isAutoCreated())
1238 if (prop
.getDefinition().isMandatory())
1240 if (prop
.getDefinition().isProtected())
1242 if (prop
.getDefinition().isMultiple())
1244 } catch (RepositoryException re
) {
1245 throw new ArgeoException(
1246 "unexpected error while getting property definition as String",
1249 return sbuf
.toString();
1253 * Estimate the sub tree size from current node. Computation is based on the
1254 * Jcr {@link Property.getLength()} method. Note : it is not the exact size
1255 * used on the disk by the current part of the JCR Tree.
1258 public static long getNodeApproxSize(Node node
) {
1259 long curNodeSize
= 0;
1261 PropertyIterator pi
= node
.getProperties();
1262 while (pi
.hasNext()) {
1263 Property prop
= pi
.nextProperty();
1264 if (prop
.isMultiple()) {
1265 int nb
= prop
.getLengths().length
;
1266 for (int i
= 0; i
< nb
; i
++) {
1267 curNodeSize
+= (prop
.getLengths()[i
] > 0 ? prop
1268 .getLengths()[i
] : 0);
1271 curNodeSize
+= (prop
.getLength() > 0 ? prop
.getLength() : 0);
1274 NodeIterator ni
= node
.getNodes();
1275 while (ni
.hasNext())
1276 curNodeSize
+= getNodeApproxSize(ni
.nextNode());
1278 } catch (RepositoryException re
) {
1279 throw new ArgeoException(
1280 "Unexpected error while recursively determining node size.",
1290 * Convenience method for adding a single privilege to a principal (user or
1291 * role), typically jcr:all
1293 public synchronized static void addPrivilege(Session session
, String path
,
1294 String principal
, String privilege
) throws RepositoryException
{
1295 List
<Privilege
> privileges
= new ArrayList
<Privilege
>();
1296 privileges
.add(session
.getAccessControlManager().privilegeFromName(
1298 addPrivileges(session
, path
, new SimplePrincipal(principal
), privileges
);
1302 * Add privileges on a path to a {@link Principal}. The path must already
1303 * exist. Session is saved. Synchronized to prevent concurrent modifications
1306 public synchronized static Boolean
addPrivileges(Session session
,
1307 String path
, Principal principal
, List
<Privilege
> privs
)
1308 throws RepositoryException
{
1309 // make sure the session is in line with the persisted state
1310 session
.refresh(false);
1311 AccessControlManager acm
= session
.getAccessControlManager();
1312 AccessControlList acl
= getAccessControlList(acm
, path
);
1314 accessControlEntries
: for (AccessControlEntry ace
: acl
1315 .getAccessControlEntries()) {
1316 Principal currentPrincipal
= ace
.getPrincipal();
1317 if (currentPrincipal
.getName().equals(principal
.getName())) {
1318 Privilege
[] currentPrivileges
= ace
.getPrivileges();
1319 if (currentPrivileges
.length
!= privs
.size())
1320 break accessControlEntries
;
1321 for (int i
= 0; i
< currentPrivileges
.length
; i
++) {
1322 Privilege currP
= currentPrivileges
[i
];
1323 Privilege p
= privs
.get(i
);
1324 if (!currP
.getName().equals(p
.getName())) {
1325 break accessControlEntries
;
1332 Privilege
[] privileges
= privs
.toArray(new Privilege
[privs
.size()]);
1333 acl
.addAccessControlEntry(principal
, privileges
);
1334 acm
.setPolicy(path
, acl
);
1335 if (log
.isDebugEnabled()) {
1336 StringBuffer privBuf
= new StringBuffer();
1337 for (Privilege priv
: privs
)
1338 privBuf
.append(priv
.getName());
1339 log
.debug("Added privileges " + privBuf
+ " to "
1340 + principal
.getName() + " on " + path
+ " in '"
1341 + session
.getWorkspace().getName() + "'");
1343 session
.refresh(true);
1348 /** Gets access control list for this path, throws exception if not found */
1349 public synchronized static AccessControlList
getAccessControlList(
1350 AccessControlManager acm
, String path
) throws RepositoryException
{
1351 // search for an access control list
1352 AccessControlList acl
= null;
1353 AccessControlPolicyIterator policyIterator
= acm
1354 .getApplicablePolicies(path
);
1355 if (policyIterator
.hasNext()) {
1356 while (policyIterator
.hasNext()) {
1357 AccessControlPolicy acp
= policyIterator
1358 .nextAccessControlPolicy();
1359 if (acp
instanceof AccessControlList
)
1360 acl
= ((AccessControlList
) acp
);
1363 AccessControlPolicy
[] existingPolicies
= acm
.getPolicies(path
);
1364 for (AccessControlPolicy acp
: existingPolicies
) {
1365 if (acp
instanceof AccessControlList
)
1366 acl
= ((AccessControlList
) acp
);
1372 throw new ArgeoException("ACL not found at " + path
);
1375 /** Clear authorizations for a user at this path */
1376 public synchronized static void clearAccessControList(Session session
,
1377 String path
, String username
) throws RepositoryException
{
1378 AccessControlManager acm
= session
.getAccessControlManager();
1379 AccessControlList acl
= getAccessControlList(acm
, path
);
1380 for (AccessControlEntry ace
: acl
.getAccessControlEntries()) {
1381 if (ace
.getPrincipal().getName().equals(username
)) {
1382 acl
.removeAccessControlEntry(ace
);
1391 * Creates the nodes making the path as {@link NodeType#NT_FOLDER}
1393 public static Node
mkfolders(Session session
, String path
) {
1394 return mkdirs(session
, path
, NodeType
.NT_FOLDER
, NodeType
.NT_FOLDER
,
1399 * Copy only nt:folder and nt:file, without their additional types and
1403 * if true copies folders as well, otherwise only first level
1405 * @return how many files were copied
1407 public static Long
copyFiles(Node fromNode
, Node toNode
, Boolean recursive
,
1408 ArgeoMonitor monitor
) {
1411 Binary binary
= null;
1412 InputStream in
= null;
1414 NodeIterator fromChildren
= fromNode
.getNodes();
1415 while (fromChildren
.hasNext()) {
1416 if (monitor
!= null && monitor
.isCanceled())
1417 throw new ArgeoException(
1418 "Copy cancelled before it was completed");
1420 Node fromChild
= fromChildren
.nextNode();
1421 String fileName
= fromChild
.getName();
1422 if (fromChild
.isNodeType(NodeType
.NT_FILE
)) {
1423 if (monitor
!= null)
1424 monitor
.subTask("Copy " + fileName
);
1425 binary
= fromChild
.getNode(Node
.JCR_CONTENT
)
1426 .getProperty(Property
.JCR_DATA
).getBinary();
1427 in
= binary
.getStream();
1428 copyStreamAsFile(toNode
, fileName
, in
);
1429 IOUtils
.closeQuietly(in
);
1430 closeQuietly(binary
);
1433 toNode
.getSession().save();
1436 if (log
.isDebugEnabled())
1437 log
.debug("Copied file " + fromChild
.getPath());
1438 if (monitor
!= null)
1440 } else if (fromChild
.isNodeType(NodeType
.NT_FOLDER
)
1443 if (toNode
.hasNode(fileName
)) {
1444 toChildFolder
= toNode
.getNode(fileName
);
1445 if (!toChildFolder
.isNodeType(NodeType
.NT_FOLDER
))
1446 throw new ArgeoException(toChildFolder
1447 + " is not of type nt:folder");
1449 toChildFolder
= toNode
.addNode(fileName
,
1450 NodeType
.NT_FOLDER
);
1453 toNode
.getSession().save();
1456 + copyFiles(fromChild
, toChildFolder
, recursive
,
1461 } catch (RepositoryException e
) {
1462 throw new ArgeoException("Cannot copy files between " + fromNode
1463 + " and " + toNode
);
1465 // in case there was an exception
1466 IOUtils
.closeQuietly(in
);
1467 closeQuietly(binary
);
1472 * Iteratively count all file nodes in subtree, inefficient but can be
1473 * useful when query are poorly supported, such as in remoting.
1475 public static Long
countFiles(Node node
) {
1476 Long localCount
= 0l;
1478 for (NodeIterator nit
= node
.getNodes(); nit
.hasNext();) {
1479 Node child
= nit
.nextNode();
1480 if (child
.isNodeType(NodeType
.NT_FOLDER
))
1481 localCount
= localCount
+ countFiles(child
);
1482 else if (child
.isNodeType(NodeType
.NT_FILE
))
1483 localCount
= localCount
+ 1;
1485 } catch (RepositoryException e
) {
1486 throw new ArgeoException("Cannot count all children of " + node
);
1492 * Copy a file as an nt:file, assuming an nt:folder hierarchy. The session
1495 * @return the created file node
1497 public static Node
copyFile(Node folderNode
, File file
) {
1498 InputStream in
= null;
1500 in
= new FileInputStream(file
);
1501 return copyStreamAsFile(folderNode
, file
.getName(), in
);
1502 } catch (IOException e
) {
1503 throw new ArgeoException("Cannot copy file " + file
+ " under "
1506 IOUtils
.closeQuietly(in
);
1510 /** Copy bytes as an nt:file */
1511 public static Node
copyBytesAsFile(Node folderNode
, String fileName
,
1513 InputStream in
= null;
1515 in
= new ByteArrayInputStream(bytes
);
1516 return copyStreamAsFile(folderNode
, fileName
, in
);
1517 } catch (Exception e
) {
1518 throw new ArgeoException("Cannot copy file " + fileName
+ " under "
1521 IOUtils
.closeQuietly(in
);
1526 * Copy a stream as an nt:file, assuming an nt:folder hierarchy. The session
1529 * @return the created file node
1531 public static Node
copyStreamAsFile(Node folderNode
, String fileName
,
1533 Binary binary
= null;
1537 if (folderNode
.hasNode(fileName
)) {
1538 fileNode
= folderNode
.getNode(fileName
);
1539 if (!fileNode
.isNodeType(NodeType
.NT_FILE
))
1540 throw new ArgeoException(fileNode
1541 + " is not of type nt:file");
1542 // we assume that the content node is already there
1543 contentNode
= fileNode
.getNode(Node
.JCR_CONTENT
);
1545 fileNode
= folderNode
.addNode(fileName
, NodeType
.NT_FILE
);
1546 contentNode
= fileNode
.addNode(Node
.JCR_CONTENT
,
1547 NodeType
.NT_RESOURCE
);
1549 binary
= contentNode
.getSession().getValueFactory()
1551 contentNode
.setProperty(Property
.JCR_DATA
, binary
);
1553 } catch (Exception e
) {
1554 throw new ArgeoException("Cannot create file node " + fileName
1555 + " under " + folderNode
, e
);
1557 closeQuietly(binary
);
1561 /** Computes the checksum of an nt:file */
1562 public static String
checksumFile(Node fileNode
, String algorithm
) {
1564 InputStream in
= null;
1566 data
= fileNode
.getNode(Node
.JCR_CONTENT
)
1567 .getProperty(Property
.JCR_DATA
).getBinary();
1568 in
= data
.getStream();
1569 return DigestUtils
.digest(algorithm
, in
);
1570 } catch (RepositoryException e
) {
1571 throw new ArgeoException("Cannot checksum file " + fileNode
, e
);
1573 IOUtils
.closeQuietly(in
);