]> git.argeo.org Git - gpl/argeo-jcr.git/blob - org.argeo.cms.jcr/src/org/argeo/jcr/JcrxApi.java
Improve writing to a JCR Content
[gpl/argeo-jcr.git] / org.argeo.cms.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 * Get the XML text of this child node.
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 return getXmlValue(child);
41 } catch (RepositoryException e) {
42 throw new IllegalStateException("Cannot get " + name + " as XML text", e);
43 }
44 }
45
46 /**
47 * Get the XML text of this node.
48 */
49 public static String getXmlValue(Node node) {
50 try {
51 if (!node.hasNode(Jcr.JCR_XMLTEXT))
52 return null;
53 Node xmlText = node.getNode(Jcr.JCR_XMLTEXT);
54 if (!xmlText.hasProperty(Jcr.JCR_XMLCHARACTERS))
55 throw new IllegalArgumentException(
56 "Node " + xmlText + " has no " + Jcr.JCR_XMLCHARACTERS + " property");
57 return xmlText.getProperty(Jcr.JCR_XMLCHARACTERS).getString();
58 } catch (RepositoryException e) {
59 throw new IllegalStateException("Cannot get " + node + " as XML text", e);
60 }
61 }
62
63 /**
64 * Set as a subnode which will be exported as an XML element.
65 */
66 public static void setXmlValue(Node node, String name, String value) {
67 try {
68 if (node.hasNode(name)) {
69 Node child = node.getNode(name);
70 setXmlValue(child, value);
71 } else
72 node.addNode(name, JcrxType.JCRX_XMLVALUE).addNode(Jcr.JCR_XMLTEXT, JcrxType.JCRX_XMLTEXT)
73 .setProperty(Jcr.JCR_XMLCHARACTERS, value);
74 } catch (RepositoryException e) {
75 throw new JcrException("Cannot set " + name + " as XML text", e);
76 }
77 }
78
79 /** Set the value as XML characters. */
80 public static void setXmlValue(Node child, String value) {
81 try {
82 if (!child.hasNode(Jcr.JCR_XMLTEXT))
83 child.addNode(Jcr.JCR_XMLTEXT, JcrxType.JCRX_XMLTEXT);
84 child.getNode(Jcr.JCR_XMLTEXT).setProperty(Jcr.JCR_XMLCHARACTERS, value);
85 } catch (RepositoryException e) {
86 throw new JcrException("Cannot set " + child + " as XML text", e);
87 }
88 }
89
90 /**
91 * Add a checksum replacing the one which was previously set with the same
92 * length.
93 */
94 public static void addChecksum(Node node, String checksum) {
95 try {
96 if (!node.hasProperty(JcrxName.JCRX_SUM)) {
97 node.setProperty(JcrxName.JCRX_SUM, new String[] { checksum });
98 return;
99 } else {
100 int stringLength = checksum.length();
101 Property property = node.getProperty(JcrxName.JCRX_SUM);
102 List<Value> values = Arrays.asList(property.getValues());
103 Integer indexToRemove = null;
104 values: for (int i = 0; i < values.size(); i++) {
105 Value value = values.get(i);
106 if (value.getString().length() == stringLength) {
107 indexToRemove = i;
108 break values;
109 }
110 }
111 if (indexToRemove != null)
112 values.set(indexToRemove, node.getSession().getValueFactory().createValue(checksum));
113 else
114 values.add(0, node.getSession().getValueFactory().createValue(checksum));
115 property.setValue(values.toArray(new Value[values.size()]));
116 }
117 } catch (RepositoryException e) {
118 throw new JcrException("Cannot set checksum on " + node, e);
119 }
120 }
121
122 /** Replace all checksums. */
123 public static void setChecksums(Node node, List<String> checksums) {
124 try {
125 node.setProperty(JcrxName.JCRX_SUM, checksums.toArray(new String[checksums.size()]));
126 } catch (RepositoryException e) {
127 throw new JcrException("Cannot set checksums on " + node, e);
128 }
129 }
130
131 /** Replace all checksums. */
132 public static List<String> getChecksums(Node node) {
133 try {
134 List<String> res = new ArrayList<>();
135 if (!node.hasProperty(JcrxName.JCRX_SUM))
136 return res;
137 Property property = node.getProperty(JcrxName.JCRX_SUM);
138 for (Value value : property.getValues()) {
139 res.add(value.getString());
140 }
141 return res;
142 } catch (RepositoryException e) {
143 throw new JcrException("Cannot get checksums from " + node, e);
144 }
145 }
146
147 // /** Replace all checksums with this single one. */
148 // public static void setChecksum(Node node, String checksum) {
149 // setChecksums(node, Collections.singletonList(checksum));
150 // }
151
152 /** Retrieves the checksum with this algorithm, or null if not found. */
153 public static String getChecksum(Node node, String algorithm) {
154 int stringLength;
155 switch (algorithm) {
156 case MD5:
157 stringLength = LENGTH_MD5;
158 break;
159 case SHA1:
160 stringLength = LENGTH_SHA1;
161 break;
162 case SHA256:
163 stringLength = LENGTH_SHA256;
164 break;
165 case SHA512:
166 stringLength = LENGTH_SHA512;
167 break;
168 default:
169 throw new IllegalArgumentException("Unkown algorithm " + algorithm);
170 }
171 return getChecksum(node, stringLength);
172 }
173
174 /** Retrieves the checksum with this string length, or null if not found. */
175 public static String getChecksum(Node node, int stringLength) {
176 try {
177 if (!node.hasProperty(JcrxName.JCRX_SUM))
178 return null;
179 Property property = node.getProperty(JcrxName.JCRX_SUM);
180 for (Value value : property.getValues()) {
181 String str = value.getString();
182 if (str.length() == stringLength)
183 return str;
184 }
185 return null;
186 } catch (RepositoryException e) {
187 throw new IllegalStateException("Cannot get checksum for " + node, e);
188 }
189 }
190
191 }