]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.jcr/src/org/argeo/jcr/JcrxApi.java
Better deal with multiple user directories.
[lgpl/argeo-commons.git] / org.argeo.jcr / src / org / argeo / jcr / JcrxApi.java
1 package org.argeo.jcr;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.List;
6
7 import javax.jcr.Node;
8 import javax.jcr.Property;
9 import javax.jcr.RepositoryException;
10 import javax.jcr.Value;
11
12 /** Uilities around the JCR extensions. */
13 public class JcrxApi {
14 public final static String MD5 = "MD5";
15 public final static String SHA1 = "SHA1";
16 public final static String SHA256 = "SHA-256";
17 public final static String SHA512 = "SHA-512";
18
19 public final static String EMPTY_MD5 = "d41d8cd98f00b204e9800998ecf8427e";
20 public final static String EMPTY_SHA1 = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
21 public final static String EMPTY_SHA256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
22 public final static String EMPTY_SHA512 = "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e";
23
24 public final static int LENGTH_MD5 = EMPTY_MD5.length();
25 public final static int LENGTH_SHA1 = EMPTY_SHA1.length();
26 public final static int LENGTH_SHA256 = EMPTY_SHA256.length();
27 public final static int LENGTH_SHA512 = EMPTY_SHA512.length();
28
29 /*
30 * XML
31 */
32 /**
33 * Set as a subnode which will be exported as an XML element.
34 */
35 public static String getXmlValue(Node node, String name) {
36 try {
37 if (!node.hasNode(name))
38 return null;
39 Node child = node.getNode(name);
40 if (!child.hasNode(Jcr.JCR_XMLTEXT))
41 return null;
42 Node xmlText = child.getNode(Jcr.JCR_XMLTEXT);
43 if (!xmlText.hasProperty(Jcr.JCR_XMLCHARACTERS))
44 throw new IllegalArgumentException(
45 "Node " + xmlText + " has no " + Jcr.JCR_XMLCHARACTERS + " property");
46 return xmlText.getProperty(Jcr.JCR_XMLCHARACTERS).getString();
47 } catch (RepositoryException e) {
48 throw new IllegalStateException("Cannot get " + name + " as XML text", e);
49 }
50 }
51
52 /**
53 * Set as a subnode which will be exported as an XML element.
54 */
55 public static void setXmlValue(Node node, String name, String value) {
56 try {
57 if (node.hasNode(name)) {
58 Node child = node.getNode(name);
59 setXmlValue(node, child, value);
60 } else
61 node.addNode(name, JcrxType.JCRX_XMLVALUE).addNode(Jcr.JCR_XMLTEXT, JcrxType.JCRX_XMLTEXT)
62 .setProperty(Jcr.JCR_XMLCHARACTERS, value);
63 } catch (RepositoryException e) {
64 throw new JcrException("Cannot set " + name + " as XML text", e);
65 }
66 }
67
68 public static void setXmlValue(Node node, Node child, String value) {
69 try {
70 if (!child.hasNode(Jcr.JCR_XMLTEXT))
71 child.addNode(Jcr.JCR_XMLTEXT, JcrxType.JCRX_XMLTEXT);
72 child.getNode(Jcr.JCR_XMLTEXT).setProperty(Jcr.JCR_XMLCHARACTERS, value);
73 } catch (RepositoryException e) {
74 throw new JcrException("Cannot set " + child + " as XML text", e);
75 }
76 }
77
78 /**
79 * Add a checksum replacing the one which was previously set with the same
80 * length.
81 */
82 public static void addChecksum(Node node, String checksum) {
83 try {
84 if (!node.hasProperty(JcrxName.JCRX_SUM)) {
85 node.setProperty(JcrxName.JCRX_SUM, new String[] { checksum });
86 return;
87 } else {
88 int stringLength = checksum.length();
89 Property property = node.getProperty(JcrxName.JCRX_SUM);
90 List<Value> values = Arrays.asList(property.getValues());
91 Integer indexToRemove = null;
92 values: for (int i = 0; i < values.size(); i++) {
93 Value value = values.get(i);
94 if (value.getString().length() == stringLength) {
95 indexToRemove = i;
96 break values;
97 }
98 }
99 if (indexToRemove != null)
100 values.set(indexToRemove, node.getSession().getValueFactory().createValue(checksum));
101 else
102 values.add(0, node.getSession().getValueFactory().createValue(checksum));
103 property.setValue(values.toArray(new Value[values.size()]));
104 }
105 } catch (RepositoryException e) {
106 throw new JcrException("Cannot set checksum on " + node, e);
107 }
108 }
109
110 /** Replace all checksums. */
111 public static void setChecksums(Node node, List<String> checksums) {
112 try {
113 node.setProperty(JcrxName.JCRX_SUM, checksums.toArray(new String[checksums.size()]));
114 } catch (RepositoryException e) {
115 throw new JcrException("Cannot set checksums on " + node, e);
116 }
117 }
118
119 /** Replace all checksums. */
120 public static List<String> getChecksums(Node node) {
121 try {
122 List<String> res = new ArrayList<>();
123 if (!node.hasProperty(JcrxName.JCRX_SUM))
124 return res;
125 Property property = node.getProperty(JcrxName.JCRX_SUM);
126 for (Value value : property.getValues()) {
127 res.add(value.getString());
128 }
129 return res;
130 } catch (RepositoryException e) {
131 throw new JcrException("Cannot get checksums from " + node, e);
132 }
133 }
134
135 // /** Replace all checksums with this single one. */
136 // public static void setChecksum(Node node, String checksum) {
137 // setChecksums(node, Collections.singletonList(checksum));
138 // }
139
140 /** Retrieves the checksum with this algorithm, or null if not found. */
141 public static String getChecksum(Node node, String algorithm) {
142 int stringLength;
143 switch (algorithm) {
144 case MD5:
145 stringLength = LENGTH_MD5;
146 break;
147 case SHA1:
148 stringLength = LENGTH_SHA1;
149 break;
150 case SHA256:
151 stringLength = LENGTH_SHA256;
152 break;
153 case SHA512:
154 stringLength = LENGTH_SHA512;
155 break;
156 default:
157 throw new IllegalArgumentException("Unkown algorithm " + algorithm);
158 }
159 return getChecksum(node, stringLength);
160 }
161
162 /** Retrieves the checksum with this string length, or null if not found. */
163 public static String getChecksum(Node node, int stringLength) {
164 try {
165 if (!node.hasProperty(JcrxName.JCRX_SUM))
166 return null;
167 Property property = node.getProperty(JcrxName.JCRX_SUM);
168 for (Value value : property.getValues()) {
169 String str = value.getString();
170 if (str.length() == stringLength)
171 return str;
172 }
173 return null;
174 } catch (RepositoryException e) {
175 throw new IllegalStateException("Cannot get checksum for " + node, e);
176 }
177 }
178
179 }