KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > jwi > servletutil > PathHelper


1 package de.jwi.servletutil;
2
3 /*
4  * jFM - Java Web File Manager
5  *
6  * Copyright (C) 2004 Juergen Weber
7  *
8  * This file is part of jFM.
9  *
10  * jFM is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * jFM is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with jFM; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston
23  */

24
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.Properties JavaDoc;
28
29 import javax.servlet.ServletContext JavaDoc;
30 import javax.servlet.http.HttpServlet JavaDoc;
31
32 import de.jwi.servletutil.RealPath;
33
34 /**
35  * @author Jürgen Weber
36  * Source file created on 20.04.2004
37  *
38  */

39 public class PathHelper extends HttpServlet JavaDoc
40 {
41
42
43     public static RealPath getHttpRealPath(ServletContext JavaDoc servletContext, String JavaDoc path,Properties JavaDoc dirmapping) throws IOException JavaDoc
44     {
45         String JavaDoc s;
46
47         
48         // check if path is of the form /x/.. and in a mapping
49

50         int p = path.indexOf('/', 1);
51         if (p > -1)
52         {
53             s = path.substring(1, p);
54
55             s = dirmapping.getProperty(s);
56
57             if (s != null)
58             {
59                 s = s + path.substring(p);
60                 File JavaDoc f = new File JavaDoc(s);
61                 if (f.exists()) { return new RealPath(RealPath.ISHTTPPATH, f.getPath(),
62                         s); }
63             }
64         }
65
66         // check if there is a "/= .. " mapping
67

68         s = dirmapping.getProperty("/");
69
70         if (s != null)
71         {
72             s = s + path;
73             File JavaDoc f = new File JavaDoc(s);
74             if (f.exists()) { return new RealPath(RealPath.ISHTTPPATH, f.getPath(),
75                     s); }
76         }
77         
78         
79         // no mapping, so try to get a context
80

81         ServletContext JavaDoc rootContext = servletContext.getContext("/");
82
83         if (null != rootContext)
84         {
85
86             // Work around for Tomcat bug: use only the potential context part of
87
// folderPath for getContext
88

89             p = path.indexOf('/', 1);
90
91             s = p > 0 ? path.substring(0, p) : path;
92
93             ServletContext JavaDoc context = servletContext.getContext(s);
94
95             String JavaDoc relativePath = path;
96
97             String JavaDoc rootContextRealPath = rootContext.getRealPath("/");
98
99             String JavaDoc contextName = "/";
100             String JavaDoc contextRealPath = context.getRealPath("/");
101
102             if (!(contextRealPath.equals(rootContextRealPath)))
103             {
104                 // not found in ROOT context, so folderPath is of the form
105
// /context/path
106
// take only /path as input for getRealPath
107

108                 relativePath = path.substring(path.indexOf('/', 1));
109                 contextName = path.substring(0, path.indexOf('/', 1));
110             }
111
112             String JavaDoc realPath = null;
113             if (null != context)
114             {
115                 realPath = context.getRealPath(relativePath);
116
117                 if (!(new File JavaDoc(realPath).exists())) { return null; }
118
119                 if ("/".equals(contextName))
120                 {
121                     contextName="";
122                 }
123                 
124                 return new RealPath(RealPath.ISHTTPPATH, realPath, contextName);
125             }
126             return null;
127         }
128         else
129         {
130             // try to get a / mapping
131

132             s = dirmapping.getProperty("/");
133             if (s != null)
134             {
135                 s = s + path;
136                 File JavaDoc f = new File JavaDoc(s);
137                 if (f.exists()) { return new RealPath(RealPath.ISHTTPPATH, f.getPath(),
138                         s); }
139             }
140         }
141         return null;
142     }
143
144     public static RealPath getFileRealPath(String JavaDoc filebase,String JavaDoc path) throws IOException JavaDoc
145     {
146         String JavaDoc s = filebase;
147     
148         if (null != s)
149         {
150             if (!s.endsWith("/"))
151             {
152                 s = s + "/";
153             }
154             s = s + path;
155         }
156         else
157         {
158             s = path;
159         }
160     
161         File JavaDoc f = new File JavaDoc(s);
162     
163         if (!(f.exists())) { return null; }
164     
165         return new RealPath(false, f.getPath(), null);
166     
167     }
168
169 }
Popular Tags