2 * Copyright (C) 2007-2012 Mathieu Baudier
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 private final 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
);
198 /** Make sure that: starts with '/', do not end with '/', do not have '//' */
199 public static String
normalizePath(String path
) {
200 List
<String
> tokens
= tokenize(path
);
201 StringBuffer buf
= new StringBuffer(path
.length());
202 for (String token
: tokens
) {
206 return buf
.toString();
210 * Creates a path from a FQDN, inverting the order of the component:
211 * www.argeo.org => org.argeo.www
213 public static String
hostAsPath(String host
) {
214 StringBuffer path
= new StringBuffer(host
.length());
215 String
[] hostTokens
= host
.split("\\.");
216 for (int i
= hostTokens
.length
- 1; i
>= 0; i
--) {
217 path
.append(hostTokens
[i
]);
221 return path
.toString();
225 * The provided data as a path ('/' at the end, not the beginning)
230 * whether to add hour as well
232 public static String
dateAsPath(Calendar cal
, Boolean addHour
) {
233 StringBuffer buf
= new StringBuffer(14);
235 buf
.append(cal
.get(Calendar
.YEAR
));
238 int month
= cal
.get(Calendar
.MONTH
) + 1;
245 int day
= cal
.get(Calendar
.DAY_OF_MONTH
);
253 int hour
= cal
.get(Calendar
.HOUR_OF_DAY
);
260 return buf
.toString();
264 /** Converts in one call a string into a gregorian calendar. */
265 public static Calendar
parseCalendar(DateFormat dateFormat
, String value
) {
267 Date date
= dateFormat
.parse(value
);
268 Calendar calendar
= new GregorianCalendar();
269 calendar
.setTime(date
);
271 } catch (ParseException e
) {
272 throw new ArgeoException("Cannot parse " + value
273 + " with date format " + dateFormat
, e
);
278 /** The last element of a path. */
279 public static String
lastPathElement(String path
) {
280 if (path
.charAt(path
.length() - 1) == '/')
281 throw new ArgeoException("Path " + path
+ " cannot end with '/'");
282 int index
= path
.lastIndexOf('/');
284 throw new ArgeoException("Cannot find last path element for "
286 return path
.substring(index
+ 1);
290 * Routine that get the child with this name, adding id it does not already
293 public static Node
getOrAdd(Node parent
, String childName
,
294 String childPrimaryNodeType
) throws RepositoryException
{
295 return parent
.hasNode(childName
) ? parent
.getNode(childName
) : parent
296 .addNode(childName
, childPrimaryNodeType
);
300 * Routine that get the child with this name, adding id it does not already
303 public static Node
getOrAdd(Node parent
, String childName
)
304 throws RepositoryException
{
305 return parent
.hasNode(childName
) ? parent
.getNode(childName
) : parent
309 /** Convert a {@link NodeIterator} to a list of {@link Node} */
310 public static List
<Node
> nodeIteratorToList(NodeIterator nodeIterator
) {
311 List
<Node
> nodes
= new ArrayList
<Node
>();
312 while (nodeIterator
.hasNext()) {
313 nodes
.add(nodeIterator
.nextNode());
323 * Concisely get the string value of a property or null if this node doesn't
326 public static String
get(Node node
, String propertyName
) {
328 if (!node
.hasProperty(propertyName
))
330 return node
.getProperty(propertyName
).getString();
331 } catch (RepositoryException e
) {
332 throw new ArgeoException("Cannot get property " + propertyName
337 /** Concisely get the boolean value of a property */
338 public static Boolean
check(Node node
, String propertyName
) {
340 return node
.getProperty(propertyName
).getBoolean();
341 } catch (RepositoryException e
) {
342 throw new ArgeoException("Cannot get property " + propertyName
347 /** Concisely get the bytes array value of a property */
348 public static byte[] getBytes(Node node
, String propertyName
) {
350 return getBinaryAsBytes(node
.getProperty(propertyName
));
351 } catch (RepositoryException e
) {
352 throw new ArgeoException("Cannot get property " + propertyName
357 /** Creates the nodes making path, if they don't exist. */
358 public static Node
mkdirs(Session session
, String path
) {
359 return mkdirs(session
, path
, null, null, false);
363 * use {@link #mkdirs(Session, String, String, String, Boolean)} instead.
368 public static Node
mkdirs(Session session
, String path
, String type
,
369 Boolean versioning
) {
370 return mkdirs(session
, path
, type
, type
, false);
375 * the type of the leaf node
377 public static Node
mkdirs(Session session
, String path
, String type
) {
378 return mkdirs(session
, path
, type
, null, false);
382 * Synchronized and save is performed, to avoid race conditions in
383 * initializers leading to duplicate nodes.
385 public synchronized static Node
mkdirsSafe(Session session
, String path
,
388 if (session
.hasPendingChanges())
389 throw new ArgeoException(
390 "Session has pending changes, save them first.");
391 Node node
= mkdirs(session
, path
, type
);
394 } catch (RepositoryException e
) {
395 discardQuietly(session
);
396 throw new ArgeoException("Cannot safely make directories", e
);
400 public synchronized static Node
mkdirsSafe(Session session
, String path
) {
401 return mkdirsSafe(session
, path
, null);
405 * Creates the nodes making the path as {@link NodeType#NT_FOLDER}
407 public static Node
mkfolders(Session session
, String path
) {
408 return mkdirs(session
, path
, NodeType
.NT_FOLDER
, NodeType
.NT_FOLDER
,
413 * Creates the nodes making path, if they don't exist. This is up to the
414 * caller to save the session. Use with caution since it can create
415 * duplicate nodes if used concurrently.
417 public static Node
mkdirs(Session session
, String path
, String type
,
418 String intermediaryNodeType
, Boolean versioning
) {
420 if (path
.equals('/'))
421 return session
.getRootNode();
423 if (session
.itemExists(path
)) {
424 Node node
= session
.getNode(path
);
426 if (type
!= null && !node
.isNodeType(type
)
427 && !node
.getPath().equals("/"))
428 throw new ArgeoException("Node " + node
429 + " exists but is of type "
430 + node
.getPrimaryNodeType().getName()
431 + " not of type " + type
);
432 // TODO: check versioning
436 StringBuffer current
= new StringBuffer("/");
437 Node currentNode
= session
.getRootNode();
438 Iterator
<String
> it
= tokenize(path
).iterator();
439 while (it
.hasNext()) {
440 String part
= it
.next();
441 current
.append(part
).append('/');
442 if (!session
.itemExists(current
.toString())) {
443 if (!it
.hasNext() && type
!= null)
444 currentNode
= currentNode
.addNode(part
, type
);
445 else if (it
.hasNext() && intermediaryNodeType
!= null)
446 currentNode
= currentNode
.addNode(part
,
447 intermediaryNodeType
);
449 currentNode
= currentNode
.addNode(part
);
451 currentNode
.addMixin(NodeType
.MIX_VERSIONABLE
);
452 if (log
.isTraceEnabled())
453 log
.debug("Added folder " + part
+ " as " + current
);
455 currentNode
= (Node
) session
.getItem(current
.toString());
459 } catch (RepositoryException e
) {
460 discardQuietly(session
);
461 throw new ArgeoException("Cannot mkdirs " + path
, e
);
466 /** Convert a path to the list of its tokens */
467 public static List
<String
> tokenize(String path
) {
468 List
<String
> tokens
= new ArrayList
<String
>();
469 boolean optimized
= false;
471 String
[] rawTokens
= path
.split("/");
472 for (String token
: rawTokens
) {
473 if (!token
.equals(""))
477 StringBuffer curr
= new StringBuffer();
478 char[] arr
= path
.toCharArray();
479 chars
: for (int i
= 0; i
< arr
.length
; i
++) {
482 if (i
== 0 || (i
== arr
.length
- 1))
484 if (curr
.length() > 0) {
485 tokens
.add(curr
.toString());
486 curr
= new StringBuffer();
491 if (curr
.length() > 0) {
492 tokens
.add(curr
.toString());
493 curr
= new StringBuffer();
496 return Collections
.unmodifiableList(tokens
);
500 * Safe and repository implementation independent registration of a
503 public static void registerNamespaceSafely(Session session
, String prefix
,
506 registerNamespaceSafely(session
.getWorkspace()
507 .getNamespaceRegistry(), prefix
, uri
);
508 } catch (RepositoryException e
) {
509 throw new ArgeoException("Cannot find namespace registry", e
);
514 * Safe and repository implementation independent registration of a
517 public static void registerNamespaceSafely(NamespaceRegistry nr
,
518 String prefix
, String uri
) {
520 String
[] prefixes
= nr
.getPrefixes();
521 for (String pref
: prefixes
)
522 if (pref
.equals(prefix
)) {
523 String registeredUri
= nr
.getURI(pref
);
524 if (!registeredUri
.equals(uri
))
525 throw new ArgeoException("Prefix " + pref
526 + " already registered for URI "
528 + " which is different from provided URI "
533 nr
.registerNamespace(prefix
, uri
);
534 } catch (RepositoryException e
) {
535 throw new ArgeoException("Cannot register namespace " + uri
536 + " under prefix " + prefix
, e
);
540 /** Recursively outputs the contents of the given node. */
541 public static void debug(Node node
) {
545 /** Recursively outputs the contents of the given node. */
546 public static void debug(Node node
, Log log
) {
548 // First output the node path
549 log
.debug(node
.getPath());
550 // Skip the virtual (and large!) jcr:system subtree
551 if (node
.getName().equals("jcr:system")) {
555 // Then the children nodes (recursive)
556 NodeIterator it
= node
.getNodes();
557 while (it
.hasNext()) {
558 Node childNode
= it
.nextNode();
559 debug(childNode
, log
);
562 // Then output the properties
563 PropertyIterator properties
= node
.getProperties();
564 // log.debug("Property are : ");
566 properties
: while (properties
.hasNext()) {
567 Property property
= properties
.nextProperty();
568 if (property
.getType() == PropertyType
.BINARY
)
569 continue properties
;// skip
570 if (property
.getDefinition().isMultiple()) {
571 // A multi-valued property, print all values
572 Value
[] values
= property
.getValues();
573 for (int i
= 0; i
< values
.length
; i
++) {
574 log
.debug(property
.getPath() + "="
575 + values
[i
].getString());
578 // A single-valued property
579 log
.debug(property
.getPath() + "=" + property
.getString());
582 } catch (Exception e
) {
583 log
.error("Could not debug " + node
, e
);
588 /** Logs the effective access control policies */
589 public static void logEffectiveAccessPolicies(Node node
) {
591 logEffectiveAccessPolicies(node
.getSession(), node
.getPath());
592 } catch (RepositoryException e
) {
593 log
.error("Cannot log effective access policies of " + node
, e
);
597 /** Logs the effective access control policies */
598 public static void logEffectiveAccessPolicies(Session session
, String path
) {
599 if (!log
.isDebugEnabled())
603 AccessControlPolicy
[] effectivePolicies
= session
604 .getAccessControlManager().getEffectivePolicies(path
);
605 if (effectivePolicies
.length
> 0) {
606 for (AccessControlPolicy policy
: effectivePolicies
) {
607 if (policy
instanceof AccessControlList
) {
608 AccessControlList acl
= (AccessControlList
) policy
;
609 log
.debug("Access control list for " + path
+ "\n"
610 + accessControlListSummary(acl
));
614 log
.debug("No effective access control policy for " + path
);
616 } catch (RepositoryException e
) {
617 log
.error("Cannot log effective access policies of " + path
, e
);
621 /** Returns a human-readable summary of this access control list. */
622 public static String
accessControlListSummary(AccessControlList acl
) {
623 StringBuffer buf
= new StringBuffer("");
625 for (AccessControlEntry ace
: acl
.getAccessControlEntries()) {
626 buf
.append('\t').append(ace
.getPrincipal().getName())
628 for (Privilege priv
: ace
.getPrivileges())
629 buf
.append("\t\t").append(priv
.getName()).append('\n');
631 return buf
.toString();
632 } catch (RepositoryException e
) {
633 throw new ArgeoException("Cannot write summary of " + acl
, e
);
638 * Copies recursively the content of a node to another one. Do NOT copy the
639 * property values of {@link NodeType#MIX_CREATED} and
640 * {@link NodeType#MIX_LAST_MODIFIED}, but update the
641 * {@link Property#JCR_LAST_MODIFIED} and
642 * {@link Property#JCR_LAST_MODIFIED_BY} properties if the target node has
643 * the {@link NodeType#MIX_LAST_MODIFIED} mixin.
645 public static void copy(Node fromNode
, Node toNode
) {
647 if (toNode
.getDefinition().isProtected())
650 // process properties
651 PropertyIterator pit
= fromNode
.getProperties();
652 properties
: while (pit
.hasNext()) {
653 Property fromProperty
= pit
.nextProperty();
654 String propertyName
= fromProperty
.getName();
655 if (toNode
.hasProperty(propertyName
)
656 && toNode
.getProperty(propertyName
).getDefinition()
660 if (fromProperty
.getDefinition().isProtected())
663 if (propertyName
.equals("jcr:created")
664 || propertyName
.equals("jcr:createdBy")
665 || propertyName
.equals("jcr:lastModified")
666 || propertyName
.equals("jcr:lastModifiedBy"))
669 if (fromProperty
.isMultiple()) {
670 toNode
.setProperty(propertyName
, fromProperty
.getValues());
672 toNode
.setProperty(propertyName
, fromProperty
.getValue());
676 // update jcr:lastModified and jcr:lastModifiedBy in toNode in case
677 // they existed, before adding the mixins
678 updateLastModified(toNode
);
681 for (NodeType mixinType
: fromNode
.getMixinNodeTypes()) {
682 toNode
.addMixin(mixinType
.getName());
685 // process children nodes
686 NodeIterator nit
= fromNode
.getNodes();
687 while (nit
.hasNext()) {
688 Node fromChild
= nit
.nextNode();
689 Integer index
= fromChild
.getIndex();
690 String nodeRelPath
= fromChild
.getName() + "[" + index
+ "]";
692 if (toNode
.hasNode(nodeRelPath
))
693 toChild
= toNode
.getNode(nodeRelPath
);
695 toChild
= toNode
.addNode(fromChild
.getName(), fromChild
696 .getPrimaryNodeType().getName());
697 copy(fromChild
, toChild
);
699 } catch (RepositoryException e
) {
700 throw new ArgeoException("Cannot copy " + fromNode
+ " to "
706 * Copy only nt:folder and nt:file, without their additional types and
710 * if true copies folders as well, otherwise only first level
712 * @return how many files were copied
714 public static Long
copyFiles(Node fromNode
, Node toNode
, Boolean recursive
,
715 ArgeoMonitor monitor
) {
718 Binary binary
= null;
719 InputStream in
= null;
721 NodeIterator fromChildren
= fromNode
.getNodes();
722 while (fromChildren
.hasNext()) {
723 if (monitor
!= null && monitor
.isCanceled())
724 throw new ArgeoException(
725 "Copy cancelled before it was completed");
727 Node fromChild
= fromChildren
.nextNode();
728 String fileName
= fromChild
.getName();
729 if (fromChild
.isNodeType(NodeType
.NT_FILE
)) {
731 monitor
.subTask("Copy " + fileName
);
732 binary
= fromChild
.getNode(Node
.JCR_CONTENT
)
733 .getProperty(Property
.JCR_DATA
).getBinary();
734 in
= binary
.getStream();
735 copyStreamAsFile(toNode
, fileName
, in
);
736 IOUtils
.closeQuietly(in
);
737 JcrUtils
.closeQuietly(binary
);
740 toNode
.getSession().save();
743 if (log
.isDebugEnabled())
744 log
.debug("Copied file " + fromChild
.getPath());
747 } else if (fromChild
.isNodeType(NodeType
.NT_FOLDER
)
750 if (toNode
.hasNode(fileName
)) {
751 toChildFolder
= toNode
.getNode(fileName
);
752 if (!toChildFolder
.isNodeType(NodeType
.NT_FOLDER
))
753 throw new ArgeoException(toChildFolder
754 + " is not of type nt:folder");
756 toChildFolder
= toNode
.addNode(fileName
,
760 toNode
.getSession().save();
763 + copyFiles(fromChild
, toChildFolder
, recursive
,
768 } catch (RepositoryException e
) {
769 throw new ArgeoException("Cannot copy files between " + fromNode
772 // in case there was an exception
773 IOUtils
.closeQuietly(in
);
774 JcrUtils
.closeQuietly(binary
);
779 * Check whether all first-level properties (except jcr:* properties) are
780 * equal. Skip jcr:* properties
782 public static Boolean
allPropertiesEquals(Node reference
, Node observed
,
783 Boolean onlyCommonProperties
) {
785 PropertyIterator pit
= reference
.getProperties();
786 props
: while (pit
.hasNext()) {
787 Property propReference
= pit
.nextProperty();
788 String propName
= propReference
.getName();
789 if (propName
.startsWith("jcr:"))
792 if (!observed
.hasProperty(propName
))
793 if (onlyCommonProperties
)
797 // TODO: deal with multiple property values?
798 if (!observed
.getProperty(propName
).getValue()
799 .equals(propReference
.getValue()))
803 } catch (RepositoryException e
) {
804 throw new ArgeoException("Cannot check all properties equals of "
805 + reference
+ " and " + observed
, e
);
809 public static Map
<String
, PropertyDiff
> diffProperties(Node reference
,
811 Map
<String
, PropertyDiff
> diffs
= new TreeMap
<String
, PropertyDiff
>();
812 diffPropertiesLevel(diffs
, null, reference
, observed
);
817 * Compare the properties of two nodes. Recursivity to child nodes is not
818 * yet supported. Skip jcr:* properties.
820 static void diffPropertiesLevel(Map
<String
, PropertyDiff
> diffs
,
821 String baseRelPath
, Node reference
, Node observed
) {
823 // check removed and modified
824 PropertyIterator pit
= reference
.getProperties();
825 props
: while (pit
.hasNext()) {
826 Property p
= pit
.nextProperty();
827 String name
= p
.getName();
828 if (name
.startsWith("jcr:"))
831 if (!observed
.hasProperty(name
)) {
832 String relPath
= propertyRelPath(baseRelPath
, name
);
833 PropertyDiff pDiff
= new PropertyDiff(PropertyDiff
.REMOVED
,
834 relPath
, p
.getValue(), null);
835 diffs
.put(relPath
, pDiff
);
837 if (p
.isMultiple()) {
838 // FIXME implement multiple
840 Value referenceValue
= p
.getValue();
841 Value newValue
= observed
.getProperty(name
).getValue();
842 if (!referenceValue
.equals(newValue
)) {
843 String relPath
= propertyRelPath(baseRelPath
, name
);
844 PropertyDiff pDiff
= new PropertyDiff(
845 PropertyDiff
.MODIFIED
, relPath
,
846 referenceValue
, newValue
);
847 diffs
.put(relPath
, pDiff
);
853 pit
= observed
.getProperties();
854 props
: while (pit
.hasNext()) {
855 Property p
= pit
.nextProperty();
856 String name
= p
.getName();
857 if (name
.startsWith("jcr:"))
859 if (!reference
.hasProperty(name
)) {
860 if (p
.isMultiple()) {
861 // FIXME implement multiple
863 String relPath
= propertyRelPath(baseRelPath
, name
);
864 PropertyDiff pDiff
= new PropertyDiff(
865 PropertyDiff
.ADDED
, relPath
, null, p
.getValue());
866 diffs
.put(relPath
, pDiff
);
870 } catch (RepositoryException e
) {
871 throw new ArgeoException("Cannot diff " + reference
+ " and "
877 * Compare only a restricted list of properties of two nodes. No
881 public static Map
<String
, PropertyDiff
> diffProperties(Node reference
,
882 Node observed
, List
<String
> properties
) {
883 Map
<String
, PropertyDiff
> diffs
= new TreeMap
<String
, PropertyDiff
>();
885 Iterator
<String
> pit
= properties
.iterator();
887 props
: while (pit
.hasNext()) {
888 String name
= pit
.next();
889 if (!reference
.hasProperty(name
)) {
890 if (!observed
.hasProperty(name
))
892 Value val
= observed
.getProperty(name
).getValue();
894 // empty String but not null
895 if ("".equals(val
.getString()))
897 } catch (Exception e
) {
898 // not parseable as String, silent
900 PropertyDiff pDiff
= new PropertyDiff(PropertyDiff
.ADDED
,
902 diffs
.put(name
, pDiff
);
903 } else if (!observed
.hasProperty(name
)) {
904 PropertyDiff pDiff
= new PropertyDiff(PropertyDiff
.REMOVED
,
905 name
, reference
.getProperty(name
).getValue(), null);
906 diffs
.put(name
, pDiff
);
908 Value referenceValue
= reference
.getProperty(name
)
910 Value newValue
= observed
.getProperty(name
).getValue();
911 if (!referenceValue
.equals(newValue
)) {
912 PropertyDiff pDiff
= new PropertyDiff(
913 PropertyDiff
.MODIFIED
, name
, referenceValue
,
915 diffs
.put(name
, pDiff
);
919 } catch (RepositoryException e
) {
920 throw new ArgeoException("Cannot diff " + reference
+ " and "
926 /** Builds a property relPath to be used in the diff. */
927 private static String
propertyRelPath(String baseRelPath
,
928 String propertyName
) {
929 if (baseRelPath
== null)
932 return baseRelPath
+ '/' + propertyName
;
936 * Normalizes a name so that it can be stored in contexts not supporting
937 * names with ':' (typically databases). Replaces ':' by '_'.
939 public static String
normalize(String name
) {
940 return name
.replace(':', '_');
944 * Replaces characters which are invalid in a JCR name by '_'. Currently not
947 * @see JcrUtils#INVALID_NAME_CHARACTERS
949 public static String
replaceInvalidChars(String name
) {
950 return replaceInvalidChars(name
, '_');
954 * Replaces characters which are invalid in a JCR name. Currently not
957 * @see JcrUtils#INVALID_NAME_CHARACTERS
959 public static String
replaceInvalidChars(String name
, char replacement
) {
960 boolean modified
= false;
961 char[] arr
= name
.toCharArray();
962 for (int i
= 0; i
< arr
.length
; i
++) {
964 invalid
: for (char invalid
: INVALID_NAME_CHARACTERS
) {
966 arr
[i
] = replacement
;
973 return new String(arr
);
975 // do not create new object if unnecessary
980 * Removes forbidden characters from a path, replacing them with '_'
982 * @deprecated use {@link #replaceInvalidChars(String)} instead
984 public static String
removeForbiddenCharacters(String str
) {
985 return str
.replace('[', '_').replace(']', '_').replace('/', '_')
990 /** Cleanly disposes a {@link Binary} even if it is null. */
991 public static void closeQuietly(Binary binary
) {
997 /** Retrieve a {@link Binary} as a byte array */
998 public static byte[] getBinaryAsBytes(Property property
) {
999 ByteArrayOutputStream out
= new ByteArrayOutputStream();
1000 InputStream in
= null;
1001 Binary binary
= null;
1003 binary
= property
.getBinary();
1004 in
= binary
.getStream();
1005 IOUtils
.copy(in
, out
);
1006 return out
.toByteArray();
1007 } catch (Exception e
) {
1008 throw new ArgeoException("Cannot read binary " + property
1011 IOUtils
.closeQuietly(out
);
1012 IOUtils
.closeQuietly(in
);
1013 closeQuietly(binary
);
1017 /** Writes a {@link Binary} from a byte array */
1018 public static void setBinaryAsBytes(Node node
, String property
, byte[] bytes
) {
1019 InputStream in
= null;
1020 Binary binary
= null;
1022 in
= new ByteArrayInputStream(bytes
);
1023 binary
= node
.getSession().getValueFactory().createBinary(in
);
1024 node
.setProperty(property
, binary
);
1025 } catch (Exception e
) {
1026 throw new ArgeoException("Cannot read binary " + property
1029 IOUtils
.closeQuietly(in
);
1030 closeQuietly(binary
);
1035 * Copy a file as an nt:file, assuming an nt:folder hierarchy. The session
1038 * @return the created file node
1040 public static Node
copyFile(Node folderNode
, File file
) {
1041 InputStream in
= null;
1043 in
= new FileInputStream(file
);
1044 return copyStreamAsFile(folderNode
, file
.getName(), in
);
1045 } catch (IOException e
) {
1046 throw new ArgeoException("Cannot copy file " + file
+ " under "
1049 IOUtils
.closeQuietly(in
);
1053 /** Copy bytes as an nt:file */
1054 public static Node
copyBytesAsFile(Node folderNode
, String fileName
,
1056 InputStream in
= null;
1058 in
= new ByteArrayInputStream(bytes
);
1059 return copyStreamAsFile(folderNode
, fileName
, in
);
1060 } catch (Exception e
) {
1061 throw new ArgeoException("Cannot copy file " + fileName
+ " under "
1064 IOUtils
.closeQuietly(in
);
1069 * Copy a stream as an nt:file, assuming an nt:folder hierarchy. The session
1072 * @return the created file node
1074 public static Node
copyStreamAsFile(Node folderNode
, String fileName
,
1076 Binary binary
= null;
1080 if (folderNode
.hasNode(fileName
)) {
1081 fileNode
= folderNode
.getNode(fileName
);
1082 if (!fileNode
.isNodeType(NodeType
.NT_FILE
))
1083 throw new ArgeoException(fileNode
1084 + " is not of type nt:file");
1085 // we assume that the content node is already there
1086 contentNode
= fileNode
.getNode(Node
.JCR_CONTENT
);
1088 fileNode
= folderNode
.addNode(fileName
, NodeType
.NT_FILE
);
1089 contentNode
= fileNode
.addNode(Node
.JCR_CONTENT
,
1090 NodeType
.NT_RESOURCE
);
1092 binary
= contentNode
.getSession().getValueFactory()
1094 contentNode
.setProperty(Property
.JCR_DATA
, binary
);
1096 } catch (Exception e
) {
1097 throw new ArgeoException("Cannot create file node " + fileName
1098 + " under " + folderNode
, e
);
1100 closeQuietly(binary
);
1104 /** Computes the checksum of an nt:file */
1105 public static String
checksumFile(Node fileNode
, String algorithm
) {
1107 InputStream in
= null;
1109 data
= fileNode
.getNode(Node
.JCR_CONTENT
)
1110 .getProperty(Property
.JCR_DATA
).getBinary();
1111 in
= data
.getStream();
1112 return DigestUtils
.digest(algorithm
, in
);
1113 } catch (RepositoryException e
) {
1114 throw new ArgeoException("Cannot checksum file " + fileNode
, e
);
1116 IOUtils
.closeQuietly(in
);
1122 * Creates depth from a string (typically a username) by adding levels based
1123 * on its first characters: "aBcD",2 => a/aB
1125 public static String
firstCharsToPath(String str
, Integer nbrOfChars
) {
1126 if (str
.length() < nbrOfChars
)
1127 throw new ArgeoException("String " + str
1128 + " length must be greater or equal than " + nbrOfChars
);
1129 StringBuffer path
= new StringBuffer("");
1130 StringBuffer curr
= new StringBuffer("");
1131 for (int i
= 0; i
< nbrOfChars
; i
++) {
1132 curr
.append(str
.charAt(i
));
1134 if (i
< nbrOfChars
- 1)
1137 return path
.toString();
1141 * Discards the current changes in the session attached to this node. To be
1142 * used typically in a catch block.
1144 * @see #discardQuietly(Session)
1146 public static void discardUnderlyingSessionQuietly(Node node
) {
1148 discardQuietly(node
.getSession());
1149 } catch (RepositoryException e
) {
1150 log
.warn("Cannot quietly discard session of node " + node
+ ": "
1156 * Discards the current changes in a session by calling
1157 * {@link Session#refresh(boolean)} with <code>false</code>, only logging
1158 * potential errors when doing so. To be used typically in a catch block.
1160 public static void discardQuietly(Session session
) {
1162 if (session
!= null)
1163 session
.refresh(false);
1164 } catch (RepositoryException e
) {
1165 log
.warn("Cannot quietly discard session " + session
+ ": "
1171 * Login to a workspace with implicit credentials, creates the workspace
1172 * with these credentials if it does not already exist.
1174 public static Session
loginOrCreateWorkspace(Repository repository
,
1175 String workspaceName
) throws RepositoryException
{
1176 Session workspaceSession
= null;
1177 Session defaultSession
= null;
1180 workspaceSession
= repository
.login(workspaceName
);
1181 } catch (NoSuchWorkspaceException e
) {
1182 // try to create workspace
1183 defaultSession
= repository
.login();
1184 defaultSession
.getWorkspace().createWorkspace(workspaceName
);
1185 workspaceSession
= repository
.login(workspaceName
);
1187 return workspaceSession
;
1189 logoutQuietly(defaultSession
);
1193 /** Logs out the session, not throwing any exception, even if it is null. */
1194 public static void logoutQuietly(Session session
) {
1196 if (session
!= null)
1197 if (session
.isLive())
1199 } catch (Exception e
) {
1205 * Convenient method to add a listener. uuids passed as null, deep=true,
1206 * local=true, only one node type
1208 public static void addListener(Session session
, EventListener listener
,
1209 int eventTypes
, String basePath
, String nodeType
) {
1211 session
.getWorkspace()
1212 .getObservationManager()
1219 nodeType
== null ?
null : new String
[] { nodeType
},
1221 } catch (RepositoryException e
) {
1222 throw new ArgeoException("Cannot add JCR listener " + listener
1223 + " to session " + session
, e
);
1227 /** Removes a listener without throwing exception */
1228 public static void removeListenerQuietly(Session session
,
1229 EventListener listener
) {
1230 if (session
== null || !session
.isLive())
1233 session
.getWorkspace().getObservationManager()
1234 .removeEventListener(listener
);
1235 } catch (RepositoryException e
) {
1241 * Quietly unregisters an {@link EventListener} from the udnerlying
1242 * workspace of this node.
1244 public static void unregisterQuietly(Node node
, EventListener eventListener
) {
1246 unregisterQuietly(node
.getSession().getWorkspace(), eventListener
);
1247 } catch (RepositoryException e
) {
1249 if (log
.isTraceEnabled())
1250 log
.trace("Could not unregister event listener "
1255 /** Quietly unregisters an {@link EventListener} from this workspace */
1256 public static void unregisterQuietly(Workspace workspace
,
1257 EventListener eventListener
) {
1258 if (eventListener
== null)
1261 workspace
.getObservationManager()
1262 .removeEventListener(eventListener
);
1263 } catch (RepositoryException e
) {
1265 if (log
.isTraceEnabled())
1266 log
.trace("Could not unregister event listener "
1272 * If this node is has the {@link NodeType#MIX_LAST_MODIFIED} mixin, it
1273 * updates the {@link Property#JCR_LAST_MODIFIED} property with the current
1274 * time and the {@link Property#JCR_LAST_MODIFIED_BY} property with the
1275 * underlying session user id. In Jackrabbit 2.x, <a
1276 * href="https://issues.apache.org/jira/browse/JCR-2233">these properties
1277 * are not automatically updated</a>, hence the need for manual update. The
1278 * session is not saved.
1280 public static void updateLastModified(Node node
) {
1282 if (!node
.isNodeType(NodeType
.MIX_LAST_MODIFIED
))
1283 node
.addMixin(NodeType
.MIX_LAST_MODIFIED
);
1284 node
.setProperty(Property
.JCR_LAST_MODIFIED
,
1285 new GregorianCalendar());
1286 node
.setProperty(Property
.JCR_LAST_MODIFIED_BY
, node
.getSession()
1288 } catch (RepositoryException e
) {
1289 throw new ArgeoException("Cannot update last modified on " + node
,
1294 /** Update lastModified recursively until this parent. */
1295 public static void updateLastModifiedAndParents(Node node
, String untilPath
) {
1297 if (!node
.getPath().startsWith(untilPath
))
1298 throw new ArgeoException(node
+ " is not under " + untilPath
);
1299 updateLastModified(node
);
1300 if (!node
.getPath().equals(untilPath
))
1301 updateLastModifiedAndParents(node
.getParent(), untilPath
);
1302 } catch (RepositoryException e
) {
1303 throw new ArgeoException("Cannot update lastModified from " + node
1304 + " until " + untilPath
, e
);
1309 * Returns a String representing the short version (see <a
1310 * href="http://jackrabbit.apache.org/node-type-notation.html"> Node type
1311 * Notation </a> attributes grammar) of the main business attributes of this
1312 * property definition
1316 public static String
getPropertyDefinitionAsString(Property prop
) {
1317 StringBuffer sbuf
= new StringBuffer();
1319 if (prop
.getDefinition().isAutoCreated())
1321 if (prop
.getDefinition().isMandatory())
1323 if (prop
.getDefinition().isProtected())
1325 if (prop
.getDefinition().isMultiple())
1327 } catch (RepositoryException re
) {
1328 throw new ArgeoException(
1329 "unexpected error while getting property definition as String",
1332 return sbuf
.toString();
1336 * Estimate the sub tree size from current node. Computation is based on the
1337 * Jcr {@link Property.getLength()} method. Note : it is not the exact size
1338 * used on the disk by the current part of the JCR Tree.
1341 public static long getNodeApproxSize(Node node
) {
1342 long curNodeSize
= 0;
1344 PropertyIterator pi
= node
.getProperties();
1345 while (pi
.hasNext()) {
1346 Property prop
= pi
.nextProperty();
1347 if (prop
.isMultiple()) {
1348 int nb
= prop
.getLengths().length
;
1349 for (int i
= 0; i
< nb
; i
++) {
1350 curNodeSize
+= (prop
.getLengths()[i
] > 0 ? prop
1351 .getLengths()[i
] : 0);
1354 curNodeSize
+= (prop
.getLength() > 0 ? prop
.getLength() : 0);
1357 NodeIterator ni
= node
.getNodes();
1358 while (ni
.hasNext())
1359 curNodeSize
+= getNodeApproxSize(ni
.nextNode());
1361 } catch (RepositoryException re
) {
1362 throw new ArgeoException(
1363 "Unexpected error while recursively determining node size.",
1373 * Convenience method for adding a single privilege to a principal (user or
1374 * role), typically jcr:all
1376 public static void addPrivilege(Session session
, String path
,
1377 String principal
, String privilege
) throws RepositoryException
{
1378 List
<Privilege
> privileges
= new ArrayList
<Privilege
>();
1379 privileges
.add(session
.getAccessControlManager().privilegeFromName(
1381 addPrivileges(session
, path
, new SimplePrincipal(principal
), privileges
);
1385 * Add privileges on a path to a {@link Principal}. The path must already
1386 * exist. Session is saved.
1388 public static void addPrivileges(Session session
, String path
,
1389 Principal principal
, List
<Privilege
> privs
)
1390 throws RepositoryException
{
1391 AccessControlManager acm
= session
.getAccessControlManager();
1392 AccessControlList acl
= getAccessControlList(acm
, path
);
1393 acl
.addAccessControlEntry(principal
,
1394 privs
.toArray(new Privilege
[privs
.size()]));
1395 acm
.setPolicy(path
, acl
);
1396 if (log
.isDebugEnabled()) {
1397 StringBuffer privBuf
= new StringBuffer();
1398 for (Privilege priv
: privs
)
1399 privBuf
.append(priv
.getName());
1400 log
.debug("Added privileges " + privBuf
+ " to " + principal
1406 /** Gets access control list for this path, throws exception if not found */
1407 public static AccessControlList
getAccessControlList(
1408 AccessControlManager acm
, String path
) throws RepositoryException
{
1409 // search for an access control list
1410 AccessControlList acl
= null;
1411 AccessControlPolicyIterator policyIterator
= acm
1412 .getApplicablePolicies(path
);
1413 if (policyIterator
.hasNext()) {
1414 while (policyIterator
.hasNext()) {
1415 AccessControlPolicy acp
= policyIterator
1416 .nextAccessControlPolicy();
1417 if (acp
instanceof AccessControlList
)
1418 acl
= ((AccessControlList
) acp
);
1421 AccessControlPolicy
[] existingPolicies
= acm
.getPolicies(path
);
1422 for (AccessControlPolicy acp
: existingPolicies
) {
1423 if (acp
instanceof AccessControlList
)
1424 acl
= ((AccessControlList
) acp
);
1430 throw new ArgeoException("ACL not found at " + path
);
1433 /** Clear authorizations for a user at this path */
1434 public static void clearAccessControList(Session session
, String path
,
1435 String username
) throws RepositoryException
{
1436 AccessControlManager acm
= session
.getAccessControlManager();
1437 AccessControlList acl
= getAccessControlList(acm
, path
);
1438 for (AccessControlEntry ace
: acl
.getAccessControlEntries()) {
1439 if (ace
.getPrincipal().getName().equals(username
)) {
1440 acl
.removeAccessControlEntry(ace
);