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.link;
35
36 import info.magnolia.cms.beans.config.ContentRepository;
37 import info.magnolia.cms.beans.config.URI2RepositoryManager;
38 import info.magnolia.cms.core.Content;
39 import info.magnolia.cms.core.HierarchyManager;
40 import info.magnolia.cms.core.NodeData;
41 import info.magnolia.context.MgnlContext;
42
43 import java.io.UnsupportedEncodingException;
44 import java.net.URLEncoder;
45 import java.util.regex.Matcher;
46 import java.util.regex.Pattern;
47
48 import javax.jcr.RepositoryException;
49
50 import org.apache.commons.lang.StringUtils;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54
55
56
57
58
59 public class LinkUtil {
60
61
62
63
64 public static final Pattern EXTERNAL_LINK_PATTERN = Pattern.compile("^(\\w*://|mailto:|javascript:).*");
65
66 public static final String DEFAULT_EXTENSION = "html";
67
68 public static final String DEFAULT_REPOSITORY = ContentRepository.WEBSITE;
69
70
71
72
73
74
75
76 public static final Pattern LINK_OR_IMAGE_PATTERN = Pattern.compile(
77 "(<(a|img|embed) " +
78 "[^>]*" +
79 "(href|src)[ ]*=[ ]*\")" +
80 "([^\"]*)" +
81 "(\"" +
82 "[^>]*" +
83 ">)");
84
85
86
87
88 private static final Logger log = LoggerFactory.getLogger(LinkUtil.class);
89
90
91
92
93
94
95
96 public static String convertUUIDtoHandle(String uuid, String repository) throws LinkException {
97 return LinkFactory.createLink(repository, uuid).getHandle();
98 }
99
100
101
102
103
104 public static String convertUUIDtoURI(String uuid, String repository) throws LinkException {
105 return LinkTransformerManager.getInstance().getAbsolute(false).transform(LinkFactory.createLink(repository, uuid));
106 }
107
108
109
110
111
112
113
114 public static String convertAbsoluteLinksToUUIDs(String html) {
115
116 Matcher matcher = LINK_OR_IMAGE_PATTERN.matcher(html);
117 StringBuffer res = new StringBuffer();
118 while (matcher.find()) {
119 final String href = matcher.group(4);
120 if (!isExternalLinkOrAnchor(href)) {
121 try {
122 Link link = LinkFactory.parseLink(href);
123 String linkStr = LinkFactory.toPattern(link);
124 linkStr = StringUtils.replace(linkStr, "\\", "\\\\");
125 linkStr = StringUtils.replace(linkStr, "$", "\\$");
126 matcher.appendReplacement(res, "$1" + linkStr + "$5");
127 }
128 catch (LinkException e) {
129
130
131 matcher.appendReplacement(res, "$0");
132 log.debug("can't parse link", e);
133 }
134 }
135 else{
136 matcher.appendReplacement(res, "$0");
137 }
138 }
139 matcher.appendTail(res);
140 return res.toString();
141 }
142
143
144
145
146
147
148
149
150
151
152 public static String convertLinksFromUUIDPattern(String str, LinkTransformer transformer) throws LinkException {
153 Matcher matcher = LinkFactory.UUID_PATTERN.matcher(str);
154 StringBuffer res = new StringBuffer();
155 while (matcher.find()) {
156 Link link = LinkFactory.createLink(matcher.group(1), matcher.group(2), matcher.group(5), matcher.group(7), matcher.group(8), matcher.group(10), matcher.group(12));
157 String replacement = transformer.transform(link);
158
159 replacement = StringUtils.replace(replacement, "\\", "\\\\");
160 replacement = StringUtils.replace(replacement,"$", "\\$");
161 matcher.appendReplacement(res, replacement);
162 }
163 matcher.appendTail(res);
164 return res.toString();
165 }
166
167 public static String convertLinksFromUUIDPattern(String str) throws LinkException {
168 LinkTransformer transformer = LinkTransformerManager.getInstance().getBrowserLink(null);
169 return convertLinksFromUUIDPattern(str, transformer);
170 }
171
172
173
174 public static boolean isInternalRelativeLink(String href) {
175
176 return !isExternalLinkOrAnchor(href) && !href.startsWith("/");
177 }
178
179
180
181
182 public static boolean isExternalLinkOrAnchor(String href) {
183 return LinkUtil.EXTERNAL_LINK_PATTERN.matcher(href).matches() || href.startsWith("#");
184 }
185
186
187
188
189
190
191
192 public static String makePathRelative(String url, String absolutePath){
193 String fromPath = StringUtils.substringBeforeLast(url, "/");
194 String toPath = StringUtils.substringBeforeLast(absolutePath, "/");
195
196
197 if (StringUtils.equals(fromPath, toPath) && StringUtils.endsWith(absolutePath, "/")) {
198 return ".";
199 }
200
201 String[] fromDirectories = StringUtils.split(fromPath, "/");
202 String[] toDirectories = StringUtils.split(toPath, "/");
203
204 int pos=0;
205 while(pos < fromDirectories.length && pos < toDirectories.length && fromDirectories[pos].equals(toDirectories[pos])){
206 pos++;
207 }
208
209 String rel = "";
210 for(int i=pos; i < fromDirectories.length; i++ ){
211 rel += "../";
212 }
213
214 for(int i=pos; i < toDirectories.length; i++ ){
215 rel = rel + toDirectories[i] + "/";
216 }
217
218 rel += StringUtils.substringAfterLast(absolutePath, "/");
219
220 return rel;
221 }
222
223
224
225
226
227
228 public static String mapPathToRepository(String path) {
229 String repository = URI2RepositoryManager.getInstance().getRepository(path);
230 if(StringUtils.isEmpty(repository)){
231 repository = DEFAULT_REPOSITORY;
232 }
233 return repository;
234 }
235
236
237
238
239
240
241 public static void addParameter(StringBuffer uri, String name, String value) {
242 if (uri.indexOf("?") < 0) {
243 uri.append('?');
244 } else {
245 uri.append('&');
246 }
247 uri.append(name).append('=');
248 try {
249 uri.append(URLEncoder.encode(value, "UTF-8"));
250 } catch (UnsupportedEncodingException e) {
251 throw new RuntimeException("It seems your system does not support UTF-8 !?", e);
252 }
253 }
254
255
256
257
258
259
260
261 public static String createAbsoluteLink(NodeData nodedata) throws LinkException {
262 if(nodedata == null || !nodedata.isExist()){
263 return null;
264 }
265 return LinkTransformerManager.getInstance().getAbsolute().transform(LinkFactory.createLink(nodedata));
266 }
267
268
269
270
271
272
273
274
275 public static String createAbsoluteLink(String repository, String uuid) throws RepositoryException {
276 HierarchyManager hm = MgnlContext.getHierarchyManager(repository);
277 Content node = hm.getContentByUUID(uuid);
278 return createAbsoluteLink(node);
279 }
280
281
282
283
284
285
286
287 public static String createAbsoluteLink(Content content) {
288 if(content == null){
289 return null;
290 }
291 return LinkTransformerManager.getInstance().getAbsolute().transform(LinkFactory.createLink(content));
292 }
293
294
295
296
297
298
299 public static String createExternalLink(Content content) {
300 if(content == null){
301 return null;
302 }
303 return LinkTransformerManager.getInstance().getCompleteUrl().transform(LinkFactory.createLink(content));
304 }
305
306
307
308
309
310
311
312 public static String createLink(Content node) {
313 if(node == null){
314 return null;
315 }
316 return LinkTransformerManager.getInstance().getBrowserLink(node.getHandle()).transform(LinkFactory.createLink(node));
317 }
318
319
320
321
322
323
324
325 public static String createLink(NodeData nodedata) throws LinkException {
326 if(nodedata == null || !nodedata.isExist()){
327 return null;
328 }
329 try {
330 return LinkTransformerManager.getInstance().getBrowserLink(nodedata.getParent().getHandle()).transform(LinkFactory.createLink(nodedata));
331 } catch (RepositoryException e) {
332 throw new LinkException(e.getMessage(), e);
333 }
334 }
335
336
337
338
339
340
341
342
343 public static String createLink(String repository, String uuid) throws RepositoryException {
344 HierarchyManager hm = MgnlContext.getHierarchyManager(repository);
345 Content node = hm.getContentByUUID(uuid);
346 return createLink(node);
347 }
348 }