##### Coverage Report - org.apache.turbine.util.ServletUtils

Classes in this File | Line Coverage | Branch Coverage | Complexity
ServletUtils |
0% | 0/23
0% | 0/20
| 14.0;14

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