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.jcr.util;
35
36 import info.magnolia.link.LinkException;
37 import info.magnolia.link.LinkUtil;
38
39 import java.lang.reflect.Method;
40 import java.util.Collection;
41 import java.util.HashMap;
42 import java.util.LinkedHashSet;
43 import java.util.Map;
44 import java.util.Set;
45
46 import javax.jcr.Node;
47 import javax.jcr.PathNotFoundException;
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
54 import org.apache.commons.lang3.StringUtils;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57
58
59
60
61
62 public class ContentMap implements Map<String, Object> {
63
64 private final static Logger log = LoggerFactory.getLogger(ContentMap.class);
65
66 private final Node content;
67
68
69
70
71 private final Map<String, Method> specialProperties = new HashMap<String, Method>();
72
73 public ContentMap(Node content) {
74 if (content == null) {
75 throw new NullPointerException("ContentMap doesn't accept null content");
76 }
77
78 this.content = content;
79
80
81
82 Class<? extends Node> clazz = content.getClass();
83 try {
84 specialProperties.put("name", clazz.getMethod("getName", (Class<?>[]) null));
85 specialProperties.put("id", clazz.getMethod("getIdentifier", (Class<?>[]) null));
86 specialProperties.put("path", clazz.getMethod("getPath", (Class<?>[]) null));
87 specialProperties.put("depth", clazz.getMethod("getDepth", (Class<?>[]) null));
88 specialProperties.put("nodeType", clazz.getMethod("getPrimaryNodeType", (Class<?>[]) null));
89 } catch (SecurityException e) {
90 log.debug("Failed to gain access to Node get***() method. Check VM security settings. {}", e.getLocalizedMessage(), e);
91 } catch (NoSuchMethodException e) {
92 log.debug("Failed to retrieve get***() method of Node class. Check the classpath for conflicting version of JCR classes. {}", e.getLocalizedMessage(), e);
93 }
94 }
95
96 @Override
97 public boolean containsKey(Object key) {
98
99 String strKey = convertKey(key);
100
101 if (!isValidKey(strKey)) {
102 return false;
103 }
104
105 if (isSpecialProperty(strKey)) {
106 return true;
107 }
108
109 try {
110 return content.hasProperty(strKey);
111 } catch (RepositoryException e) {
112
113 }
114 return false;
115 }
116
117 private String convertKey(Object key) {
118 if (key == null) {
119 return null;
120 }
121 try {
122 return (String) key;
123 } catch (ClassCastException e) {
124 log.debug("Invalid key. Expected String, but got {}.", key.getClass().getName());
125 }
126 return null;
127 }
128
129 private boolean isValidKey(String strKey) {
130 return !StringUtils.isBlank(strKey);
131 }
132
133 public boolean isSpecialProperty(String strKey) {
134 if (!strKey.startsWith("@")) {
135 return false;
136 }
137 strKey = convertDeprecatedProps(strKey);
138 return specialProperties.containsKey(StringUtils.removeStart(strKey, "@"));
139 }
140
141
142
143
144
145 private String convertDeprecatedProps(String strKey) {
146
147 if ("@UUID".equals(strKey) || "@uuid".equals(strKey)) {
148 return "@id";
149 } else if ("@handle".equals(strKey)) {
150 return "@path";
151 } else if ("@level".equals(strKey)) {
152 return "@depth";
153 }
154 return strKey;
155 }
156
157 @Override
158 public Object get(Object key) {
159 String keyStr;
160 try {
161 keyStr = (String) key;
162 } catch (ClassCastException e) {
163 throw new ClassCastException("ContentMap accepts only String as a parameters, provided object was of type "
164 + (key == null ? "null" : key.getClass().getName()));
165 }
166
167 Object prop = getNodeProperty(keyStr);
168 if (prop == null) {
169 keyStr = convertDeprecatedProps(keyStr);
170 return getSpecialProperty(keyStr);
171 }
172 return prop;
173 }
174
175 private Object getSpecialProperty(String strKey) {
176 if (isSpecialProperty(strKey)) {
177 final Method method = specialProperties.get(StringUtils.removeStart(strKey, "@"));
178 try {
179 return method.invoke(content, null);
180 } catch (Exception e) {
181 throw new RuntimeException(e);
182 }
183 }
184 return null;
185
186 }
187
188 private Object getNodeProperty(String keyStr) {
189 try {
190 if (content.hasProperty(keyStr)) {
191 Property prop = content.getProperty(keyStr);
192 int type = prop.getType();
193 if (type == PropertyType.DATE) {
194 return prop.getDate();
195 } else if (type == PropertyType.BINARY) {
196
197
198 } else if (type == PropertyType.BOOLEAN) {
199 return prop.getBoolean();
200 } else if (type == PropertyType.LONG) {
201 return prop.getLong();
202 } else if (type == PropertyType.DOUBLE) {
203 return prop.getDouble();
204 } else if (type == PropertyType.REFERENCE) {
205 return prop.getNode();
206 } else if (prop.isMultiple()) {
207
208 Value[] values = prop.getValues();
209
210 String[] valueStrings = new String[values.length];
211
212 for (int j = 0; j < values.length; j++) {
213 try {
214 valueStrings[j] = values[j].getString();
215 } catch (RepositoryException e) {
216 log.debug(e.getMessage());
217 }
218 }
219
220 return valueStrings;
221 } else if (LinkUtil.UUID_PATTERN.matcher(prop.getString()).find()) {
222 try {
223 return info.magnolia.link.LinkUtil.convertLinksFromUUIDPattern(prop.getString());
224 } catch (LinkException e) {
225 log.warn("Failed to parse links with from {}", prop.getName(), e);
226 }
227 }
228
229 return prop.getString();
230 }
231
232 if (content.hasNode(keyStr)) {
233 return new ContentMap(content.getNode(keyStr));
234 }
235
236 } catch (PathNotFoundException e) {
237
238 } catch (RepositoryException e) {
239 log.warn("Failed to retrieve {} on {} with {}", keyStr, content, e.getMessage());
240 }
241
242 return null;
243 }
244
245 @Override
246 public int size() {
247 try {
248 return (int) (content.getProperties().getSize() + specialProperties.size());
249 } catch (RepositoryException e) {
250
251 }
252 return specialProperties.size();
253 }
254
255 @Override
256 public Set<String> keySet() {
257 Set<String> keys = new LinkedHashSet<>();
258 try {
259 PropertyIterator props = content.getProperties();
260 while (props.hasNext()) {
261 keys.add(props.nextProperty().getName());
262 }
263 } catch (RepositoryException e) {
264
265 }
266 for (String name : specialProperties.keySet()) {
267 keys.add(name);
268 }
269 return keys;
270 }
271
272 @Override
273 public Set<java.util.Map.Entry<String, Object>> entrySet() {
274 throw new UnsupportedOperationException("Entry collections are not supported");
275 }
276
277 @Override
278 public Collection<Object> values() {
279 throw new UnsupportedOperationException("Value collections are not supported");
280 }
281
282 @Override
283 public boolean containsValue(Object arg0) {
284 throw new UnsupportedOperationException("Value checks are not supported");
285 }
286
287 @Override
288 public boolean isEmpty() {
289
290 return false;
291 }
292
293 @Override
294 public void clear() {
295
296 }
297
298 @Override
299 public Object put(String arg0, Object arg1) {
300
301 return null;
302 }
303
304 @Override
305 public void putAll(Map<? extends String, ? extends Object> arg0) {
306
307 }
308
309 @Override
310 public Object remove(Object arg0) {
311
312 return null;
313 }
314
315 public Node getJCRNode() {
316 return content;
317 }
318 }