1 package org.apache.turbine.util;
2
3
4 import java.io.File;
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import java.util.StringTokenizer;
27 import java.util.stream.Stream;
28
29 import javax.servlet.ServletConfig;
30 import javax.servlet.ServletContext;
31
32 import org.apache.commons.lang3.StringUtils;
33 import org.apache.turbine.Turbine;
34
35
36
37
38
39
40
41
42 public class ServletUtils
43 {
44
45
46
47
48
49
50
51
52
53
54 public static String expandRelative(ServletConfig config,
55 String text)
56 {
57 if (StringUtils.isEmpty(text))
58 {
59 return text;
60 }
61
62 if (config == null)
63 {
64 return null;
65 }
66
67 final String expandedText;
68
69
70 if (Stream.of("/", "./", "\\", ".\\").noneMatch(text::startsWith))
71 {
72 StringBuilder sb = new StringBuilder();
73 sb.append("./");
74 sb.append(text);
75 expandedText = sb.toString();
76 }
77 else
78 {
79 expandedText = text;
80 }
81
82 ServletContext context = config.getServletContext();
83 String base = StringUtils.defaultIfEmpty(context.getRealPath("/"),
84 Turbine.getApplicationRoot());
85
86 if (StringUtils.isEmpty(base))
87 {
88 return expandedText;
89 }
90
91 StringTokenizer tokenizer = new StringTokenizer(expandedText, File.pathSeparator);
92 StringBuilder buffer = new StringBuilder();
93 while (tokenizer.hasMoreTokens())
94 {
95 buffer.append(base).append(tokenizer.nextToken());
96 if (tokenizer.hasMoreTokens())
97 {
98 buffer.append(File.pathSeparator);
99 }
100 }
101 return buffer.toString();
102 }
103 }