1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package spcp7.imagegallery.impl.alfresco.utils;
21
22 import java.util.Map;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25
26 import org.alfresco.webservice.util.ISO9075;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 import spcp7.imagegallery.abstractionlayer.face.persistence.PropertyModelFace;
31 import spcp7.imagegallery.impl.alfresco.AlfrescoContentRetrievalImpl;
32
33
34
35
36
37 public class AlfrescoUtils {
38 private final Pattern replaceAppPattern = Pattern
39 .compile(AlfrescoDefaultValues.ALFRESCO_SHORTEN_APP_NAMESPACE_REGEX_VALUE);
40 private final Pattern replaceCmPattern = Pattern
41 .compile(AlfrescoDefaultValues.ALFRESCO_SHORTEN_CM_SPACE_REGEX_VALUE);
42 private Log log = LogFactory.getLog(AlfrescoContentRetrievalImpl.class);
43
44 public AlfrescoUtils() {
45
46 }
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 public String normalizePathNamespace(String inputPath,
65 Map<String, PropertyModelFace> properties) {
66 if (properties != null
67 && properties
68 .containsKey(AlfrescoDefaultProperties.ALFRESCO_FULL_APP_NAMESPACE_REPLACEMENT
69 .toString())
70 && properties
71 .containsKey(AlfrescoDefaultProperties.ALFRESCO_FULL_CM_NAMESPACE_REPLACEMENT
72 .toString())) {
73 String appReplace = properties
74 .get(
75 AlfrescoDefaultProperties.ALFRESCO_FULL_APP_NAMESPACE_REPLACEMENT
76 .toString()).getWert();
77 String cmReplace = properties
78 .get(
79 AlfrescoDefaultProperties.ALFRESCO_FULL_CM_NAMESPACE_REPLACEMENT
80 .toString()).getWert();
81 if (appReplace != null && cmReplace != null) {
82 Matcher appMatcher = replaceAppPattern.matcher(inputPath);
83 inputPath = appMatcher.replaceAll(appReplace);
84 Matcher cmMatcher = replaceCmPattern.matcher(inputPath);
85 inputPath = cmMatcher.replaceAll(cmReplace);
86 }
87 } else {
88 log.error("We could not normalize path namespace"
89 + " because no properties were provided!");
90 }
91 return inputPath;
92 }
93
94
95
96
97
98
99
100
101
102
103 public String encodeAlfrescoAbbreviatedPathISO9075(String path) {
104
105
106 path = ISO9075.decode(path);
107 String slashSplit[] = path.split("/");
108 boolean slashPrefix = path.startsWith("/");
109 StringBuffer retVal = new StringBuffer();
110 String colonSplit[] = null;
111 for (int i = 0; i < slashSplit.length; i++) {
112 if (!slashPrefix) {
113 colonSplit = slashSplit[i].split(":");
114 } else {
115 if (i > 0) {
116 colonSplit = slashSplit[i].split(":");
117 }
118 }
119 if (colonSplit != null && colonSplit.length > 0) {
120 if (colonSplit.length == 1) {
121
122
123 retVal.append(ISO9075.encode(colonSplit[0]));
124 } else {
125
126
127 retVal.append(colonSplit[0]);
128 }
129 if (colonSplit.length == 2) {
130 retVal.append(":");
131 retVal.append(ISO9075.encode(colonSplit[1]));
132 } else if (colonSplit.length > 2) {
133 log
134 .error("Path could not be correctly encoded"
135 + " because we found two colons within a path element"
136 + " (eg app:testname.jpg. This is not allowed."
137 + " So please specify a name without a colon!");
138 }
139 }
140 if (i < slashSplit.length - 1) {
141 retVal.append("/");
142 }
143 }
144 return retVal.toString();
145 }
146
147 }