Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ServletUtils |
|
| 13.0;13 |
1 | package org.apache.turbine.util; | |
2 | ||
3 | ||
4 | import java.io.File; | |
5 | ||
6 | /* | |
7 | * Licensed to the Apache Software Foundation (ASF) under one | |
8 | * or more contributor license agreements. See the NOTICE file | |
9 | * distributed with this work for additional information | |
10 | * regarding copyright ownership. The ASF licenses this file | |
11 | * to you under the Apache License, Version 2.0 (the | |
12 | * "License"); you may not use this file except in compliance | |
13 | * with the License. You may obtain a copy of the License at | |
14 | * | |
15 | * http://www.apache.org/licenses/LICENSE-2.0 | |
16 | * | |
17 | * Unless required by applicable law or agreed to in writing, | |
18 | * software distributed under the License is distributed on an | |
19 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
20 | * KIND, either express or implied. See the License for the | |
21 | * specific language governing permissions and limitations | |
22 | * under the License. | |
23 | */ | |
24 | ||
25 | ||
26 | import java.util.StringTokenizer; | |
27 | ||
28 | import javax.servlet.ServletConfig; | |
29 | import javax.servlet.ServletContext; | |
30 | ||
31 | import org.apache.commons.lang3.StringUtils; | |
32 | import org.apache.turbine.Turbine; | |
33 | ||
34 | /** | |
35 | * This is where common Servlet manipulation routines should go. | |
36 | * | |
37 | * @author <a href="mailto:gonzalo.diethelm@sonda.com">Gonzalo Diethelm</a> | |
38 | * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> | |
39 | * @version $Id: ServletUtils.java 1854688 2019-03-03 10:36:42Z tv $ | |
40 | */ | |
41 | 0 | public class ServletUtils |
42 | { | |
43 | /** | |
44 | * Expands a string that points to a relative path or path list, | |
45 | * leaving it as an absolute path based on the servlet context. | |
46 | * It will return null if the text is empty or the config object | |
47 | * is null. | |
48 | * | |
49 | * @param config The ServletConfig. | |
50 | * @param text The String containing a path or path list. | |
51 | * @return A String with the expanded path or path list. | |
52 | */ | |
53 | public static String expandRelative(ServletConfig config, | |
54 | String text) | |
55 | { | |
56 | 0 | if (StringUtils.isEmpty(text)) |
57 | { | |
58 | 0 | return text; |
59 | } | |
60 | ||
61 | 0 | if (config == null) |
62 | { | |
63 | 0 | return null; |
64 | } | |
65 | ||
66 | final String expandedText; | |
67 | ||
68 | // attempt to make it relative | |
69 | 0 | if (!text.startsWith("/") && !text.startsWith("./") |
70 | 0 | && !text.startsWith("\\") && !text.startsWith(".\\")) |
71 | { | |
72 | 0 | StringBuilder sb = new StringBuilder(); |
73 | 0 | sb.append("./"); |
74 | 0 | sb.append(text); |
75 | 0 | expandedText = sb.toString(); |
76 | 0 | } |
77 | else | |
78 | { | |
79 | 0 | expandedText = text; |
80 | } | |
81 | ||
82 | 0 | ServletContext context = config.getServletContext(); |
83 | 0 | String base = StringUtils.defaultIfEmpty(context.getRealPath("/"), |
84 | 0 | Turbine.getApplicationRoot()); |
85 | ||
86 | 0 | if (StringUtils.isEmpty(base)) |
87 | { | |
88 | 0 | return expandedText; |
89 | } | |
90 | ||
91 | 0 | StringTokenizer tokenizer = new StringTokenizer(expandedText, File.pathSeparator); |
92 | 0 | StringBuilder buffer = new StringBuilder(); |
93 | 0 | while (tokenizer.hasMoreTokens()) |
94 | { | |
95 | 0 | buffer.append(base).append(tokenizer.nextToken()); |
96 | 0 | if (tokenizer.hasMoreTokens()) |
97 | { | |
98 | 0 | buffer.append(File.pathSeparator); |
99 | } | |
100 | } | |
101 | 0 | return buffer.toString(); |
102 | } | |
103 | } |