1 package de.jwi.servletutil; 2 3 24 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.util.Properties ; 28 29 import javax.servlet.ServletContext ; 30 import javax.servlet.http.HttpServlet ; 31 32 import de.jwi.servletutil.RealPath; 33 34 39 public class PathHelper extends HttpServlet 40 { 41 42 43 public static RealPath getHttpRealPath(ServletContext servletContext, String path,Properties dirmapping) throws IOException 44 { 45 String s; 46 47 48 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 f = new File (s); 61 if (f.exists()) { return new RealPath(RealPath.ISHTTPPATH, f.getPath(), 62 s); } 63 } 64 } 65 66 68 s = dirmapping.getProperty("/"); 69 70 if (s != null) 71 { 72 s = s + path; 73 File f = new File (s); 74 if (f.exists()) { return new RealPath(RealPath.ISHTTPPATH, f.getPath(), 75 s); } 76 } 77 78 79 81 ServletContext rootContext = servletContext.getContext("/"); 82 83 if (null != rootContext) 84 { 85 86 89 p = path.indexOf('/', 1); 90 91 s = p > 0 ? path.substring(0, p) : path; 92 93 ServletContext context = servletContext.getContext(s); 94 95 String relativePath = path; 96 97 String rootContextRealPath = rootContext.getRealPath("/"); 98 99 String contextName = "/"; 100 String contextRealPath = context.getRealPath("/"); 101 102 if (!(contextRealPath.equals(rootContextRealPath))) 103 { 104 108 relativePath = path.substring(path.indexOf('/', 1)); 109 contextName = path.substring(0, path.indexOf('/', 1)); 110 } 111 112 String realPath = null; 113 if (null != context) 114 { 115 realPath = context.getRealPath(relativePath); 116 117 if (!(new File (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 132 s = dirmapping.getProperty("/"); 133 if (s != null) 134 { 135 s = s + path; 136 File f = new File (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 filebase,String path) throws IOException 145 { 146 String 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 f = new File (s); 162 163 if (!(f.exists())) { return null; } 164 165 return new RealPath(false, f.getPath(), null); 166 167 } 168 169 } | Popular Tags |