1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.rest.service.node.v1; |
35 |
|
|
36 |
|
import info.magnolia.jcr.util.NodeTypes; |
37 |
|
import info.magnolia.jcr.util.NodeUtil; |
38 |
|
import info.magnolia.jcr.util.PropertyUtil; |
39 |
|
|
40 |
|
import java.io.ByteArrayInputStream; |
41 |
|
import java.io.IOException; |
42 |
|
import java.util.ArrayList; |
43 |
|
import java.util.Collections; |
44 |
|
import java.util.List; |
45 |
|
|
46 |
|
import javax.jcr.Node; |
47 |
|
import javax.jcr.NodeIterator; |
48 |
|
import javax.jcr.Property; |
49 |
|
import javax.jcr.PropertyIterator; |
50 |
|
import javax.jcr.PropertyType; |
51 |
|
import javax.jcr.RepositoryException; |
52 |
|
import javax.jcr.Value; |
53 |
|
import javax.jcr.ValueFactory; |
54 |
|
|
55 |
|
import org.apache.commons.codec.binary.Base64; |
56 |
|
import org.apache.commons.io.IOUtils; |
57 |
|
|
58 |
|
|
59 |
|
@link |
60 |
|
|
61 |
|
|
|
|
| 96.5% |
Uncovered Elements: 4 (115) |
Complexity: 27 |
Complexity Density: 0.33 |
|
62 |
|
public class RepositoryMarshaller { |
63 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
64 |
1 |
public RepositoryNode marshallNode(final Node node) throws RepositoryException {... |
65 |
1 |
return marshallNode(node, 0, Collections.<String>emptyList(), true); |
66 |
|
} |
67 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (29) |
Complexity: 9 |
Complexity Density: 0.47 |
|
68 |
17 |
public RepositoryNode marshallNode(final Node node, final int depth, final List<String> excludeNodeTypes, final boolean includeMeta) throws RepositoryException {... |
69 |
|
|
70 |
17 |
RepositoryNode data = new RepositoryNode(); |
71 |
17 |
data.setName(node.getName()); |
72 |
17 |
data.setPath(node.getPath()); |
73 |
17 |
data.setIdentifier(node.getIdentifier()); |
74 |
17 |
data.setType(node.getPrimaryNodeType().getName()); |
75 |
|
|
76 |
17 |
final PropertyIterator properties = node.getProperties(); |
77 |
40 |
while (properties.hasNext()) { |
78 |
23 |
Property property = properties.nextProperty(); |
79 |
23 |
String propertyName = property.getName(); |
80 |
|
|
81 |
23 |
if (!includeMeta && (propertyName.startsWith(NodeTypes.MGNL_PREFIX) || propertyName.startsWith(NodeTypes.JCR_PREFIX))) { |
82 |
2 |
continue; |
83 |
|
} |
84 |
|
|
85 |
21 |
data.getProperties().add(marshallProperty(property)); |
86 |
|
} |
87 |
|
|
88 |
17 |
if (depth > 0) { |
89 |
5 |
final NodeIterator nodes = node.getNodes(); |
90 |
11 |
while (nodes.hasNext()) { |
91 |
6 |
Node child = nodes.nextNode(); |
92 |
6 |
if (excludeNodeTypes == null || (!excludeNodeTypes.contains(child.getPrimaryNodeType().getName()))) { |
93 |
5 |
data.addNode(marshallNode(child, depth-1, excludeNodeTypes, includeMeta)); |
94 |
|
} |
95 |
|
} |
96 |
|
} |
97 |
|
|
98 |
17 |
return data; |
99 |
|
} |
100 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 2 |
Complexity Density: 0.18 |
|
101 |
23 |
public RepositoryProperty marshallProperty(Property property) throws RepositoryException {... |
102 |
|
|
103 |
23 |
RepositoryProperty data = new RepositoryProperty(); |
104 |
23 |
data.setName(property.getName()); |
105 |
23 |
data.setType(PropertyType.nameFromValue(property.getType())); |
106 |
23 |
data.setMultiple(property.isMultiple()); |
107 |
|
|
108 |
23 |
List<String> values = new ArrayList<String>(); |
109 |
23 |
if (property.isMultiple()) { |
110 |
5 |
for (Value propertyValue : property.getValues()) { |
111 |
11 |
values.add(getStringByValue(propertyValue)); |
112 |
|
} |
113 |
|
} else { |
114 |
18 |
values.add(getStringByValue(property.getValue())); |
115 |
|
} |
116 |
23 |
data.setValues(values); |
117 |
|
|
118 |
23 |
return data; |
119 |
|
} |
120 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
121 |
13 |
public void unmarshallProperties(final Node node, List<RepositoryProperty> properties) throws RepositoryException {... |
122 |
13 |
for (RepositoryProperty property : properties) { |
123 |
10 |
unmarshallProperty(node, property); |
124 |
|
} |
125 |
|
} |
126 |
|
|
|
|
| 92.7% |
Uncovered Elements: 3 (41) |
Complexity: 9 |
Complexity Density: 0.33 |
|
127 |
18 |
public void unmarshallProperty(Node node, RepositoryProperty property) throws RepositoryException {... |
128 |
|
|
129 |
18 |
ValueFactory valueFactory = node.getSession().getValueFactory(); |
130 |
|
|
131 |
18 |
if (property.isMultiple()) { |
132 |
|
|
133 |
|
|
134 |
|
|
135 |
4 |
Value[] values; |
136 |
4 |
if (property.getValues() != null) { |
137 |
4 |
values = new Value[property.getValues().size()]; |
138 |
4 |
int propertyType = PropertyType.valueFromName(property.getType()); |
139 |
4 |
int i = 0; |
140 |
4 |
for (String propertyValue : property.getValues()) { |
141 |
7 |
values[i++] = getValueByType(propertyType, propertyValue, valueFactory); |
142 |
|
} |
143 |
|
} else { |
144 |
0 |
values = new Value[0]; |
145 |
|
} |
146 |
|
|
147 |
4 |
String name = property.getName(); |
148 |
4 |
if (node.hasProperty(name)) { |
149 |
1 |
Property existingProperty = node.getProperty(name); |
150 |
1 |
if (!existingProperty.isMultiple()) { |
151 |
1 |
existingProperty.remove(); |
152 |
|
} |
153 |
|
} |
154 |
|
|
155 |
4 |
node.setProperty(property.getName(), values); |
156 |
|
|
157 |
|
} else { |
158 |
|
|
159 |
|
|
160 |
|
|
161 |
14 |
if (property.getValues() == null || property.getValues().size() != 1) { |
162 |
2 |
throw new IllegalArgumentException("Cannot set more than one value unless the property is multi-valued, [" + property.getName() + "] on [" + NodeUtil.getNodePathIfPossible(node) + "]"); |
163 |
|
} |
164 |
|
|
165 |
12 |
final int propertyType = PropertyType.valueFromName(property.getType()); |
166 |
12 |
final String propertyValue = property.getValues().get(0); |
167 |
12 |
Value value = getValueByType(propertyType, propertyValue, valueFactory); |
168 |
|
|
169 |
12 |
String name = property.getName(); |
170 |
12 |
if (node.hasProperty(name)) { |
171 |
3 |
Property existingProperty = node.getProperty(name); |
172 |
3 |
if (existingProperty.isMultiple()) { |
173 |
1 |
existingProperty.remove(); |
174 |
|
} |
175 |
|
} |
176 |
|
|
177 |
12 |
node.setProperty(property.getName(), value); |
178 |
|
} |
179 |
|
} |
180 |
|
|
181 |
|
|
182 |
|
@link |
183 |
|
@link |
184 |
|
@see |
185 |
|
|
|
|
| 92.3% |
Uncovered Elements: 1 (13) |
Complexity: 3 |
Complexity Density: 0.23 |
|
186 |
31 |
protected String getStringByValue(final Value propertyValue) throws RepositoryException {... |
187 |
31 |
String value; |
188 |
31 |
final int propertyType = propertyValue.getType(); |
189 |
|
|
190 |
31 |
switch (propertyType) { |
191 |
1 |
case PropertyType.BINARY: |
192 |
|
|
193 |
1 |
byte[] decodedPropertyValue; |
194 |
1 |
try { |
195 |
1 |
decodedPropertyValue = Base64.encodeBase64(IOUtils.toByteArray(propertyValue.getBinary().getStream())); |
196 |
|
} catch (IOException e) { |
197 |
0 |
throw new RepositoryException(e); |
198 |
|
} |
199 |
1 |
value = new String(decodedPropertyValue); |
200 |
1 |
break; |
201 |
30 |
default: |
202 |
30 |
value = propertyValue.getString(); |
203 |
|
} |
204 |
|
|
205 |
31 |
return value; |
206 |
|
} |
207 |
|
|
208 |
|
|
209 |
|
@link |
210 |
|
@link |
211 |
|
@see |
212 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
|
213 |
24 |
protected Value getValueByType(final int propertyType, final String propertyValue, final ValueFactory valueFactory) throws RepositoryException {... |
214 |
24 |
Value value; |
215 |
|
|
216 |
24 |
switch (propertyType) { |
217 |
1 |
case PropertyType.BINARY: |
218 |
|
|
219 |
1 |
byte[] decodedPropertyValue = Base64.decodeBase64(propertyValue); |
220 |
1 |
value = PropertyUtil.createValue(new ByteArrayInputStream(decodedPropertyValue), valueFactory); |
221 |
1 |
break; |
222 |
23 |
default: |
223 |
23 |
value = PropertyUtil.createValue(propertyValue, propertyType, valueFactory); |
224 |
|
} |
225 |
|
|
226 |
24 |
return value; |
227 |
|
} |
228 |
|
|
229 |
|
} |