1 20 21 package com.methodhead.res; 22 23 import org.apache.commons.io.FileUtils; 24 import java.io.File ; 25 import java.io.IOException ; 26 import javax.servlet.http.HttpServlet ; 27 import java.util.List ; 28 import java.util.Iterator ; 29 import com.methodhead.tree.FoldingTreeNode; 30 import javax.servlet.http.HttpServletRequest ; 31 import org.apache.struts.action.ActionMapping; 32 33 37 public class ResUtils { 38 39 41 43 45 47 51 public static boolean isSafeFileName( 52 String name ) { 53 54 return 55 !name.toLowerCase().endsWith( ".jsp" ) && 56 !name.toLowerCase().endsWith( ".do" ); 57 } 58 59 63 public static boolean isValidFileName( 64 String name ) { 65 66 return 67 ( name.indexOf( '/' ) == -1 ) && 68 ( name.indexOf( '\\' ) == -1 ); 69 } 70 71 75 public static boolean isValidPath( 76 String path ) { 77 78 if ( path == null ) 79 return false; 80 81 return path.indexOf( ".." ) == -1; 82 } 83 84 87 public static String cleanPath( 88 String path ) { 89 return path.replaceAll( "" + File.separator + "+", File.separator ); 90 } 91 92 96 public static String trimPath( 97 String path ) { 98 99 path = path.trim(); 100 101 if ( path.startsWith( "/" ) ) 102 path = path.substring( 1 ); 103 104 if ( path.endsWith( "/" ) ) 105 path = path.substring( 0, path.length() - 1 ); 106 107 return path; 108 } 109 110 119 public static boolean isPathDescendent( 120 String parentPath, 121 String path ) { 122 123 if ( ( parentPath == null ) || ( path == null ) ) 124 return false; 125 126 parentPath = trimPath( parentPath ); 127 path = trimPath( path ); 128 129 if ( path.startsWith( parentPath ) ) 130 return true; 131 132 return false; 133 } 134 135 143 public static void copyFile( 144 File from, 145 File to ) 146 throws 147 ResException, 148 IOException { 149 150 if ( from.isDirectory() ) { 151 if ( to.exists() ) { 152 if ( !to.isDirectory() ) 153 throw new ResException( 154 "Can't copy directory over non-directory: " + 155 to.getAbsolutePath() ); 156 } 157 else { 158 to.mkdir(); 159 } 160 161 File [] files = from.listFiles(); 165 166 for ( int i = 0; i < files.length; i++ ) 167 copyFile( files[ i ], new File ( to, files[ i ].getName() ) ); 168 } 169 else { 170 if ( to.exists() && to.isDirectory() ) 171 throw new ResException( 172 "Can't copy non-directory over directory: " + 173 to.getAbsolutePath() ); 174 175 FileUtils.copyFile( from, to ); 176 } 177 } 178 179 183 public static void deleteFile( 184 File file ) { 185 186 if ( file.isDirectory() ) { 187 188 File [] files = file.listFiles(); 189 for ( int i = 0; i < files.length; i++ ) { 190 deleteFile( files[ i ] ); 191 } 192 } 193 194 file.delete(); 195 } 196 197 201 public static File getFile( 202 HttpServlet servlet, 203 String path, 204 String fileName ) { 205 206 return 207 new File ( 208 servlet.getServletContext().getRealPath( 209 path + File.separator + fileName ) ); 210 } 211 212 216 public static String [] nodesToFileNames ( 217 List nodes ) { 218 219 String [] fileNames = new String [ nodes.size() ]; 220 int i = 0; 221 for ( Iterator iter = nodes.iterator(); iter.hasNext(); ) 222 fileNames[ i++ ] = ( ( FoldingTreeNode )iter.next() ).getLabel(); 223 224 return fileNames; 225 } 226 227 231 public static FileManager getFileManager( 232 ResPolicy policy, 233 HttpServletRequest request ) { 234 235 FileManager fileManager = 236 ( FileManager )request.getAttribute( ResGlobals.FILEMANAGER_KEY ); 237 238 if ( fileManager == null ) { 239 fileManager = policy.newFileManager(); 240 policy.initFileManager( request, fileManager ); 241 request.setAttribute( ResGlobals.FILEMANAGER_KEY, fileManager ); 242 } 243 244 return fileManager; 245 } 246 247 251 public static ResPolicy getPolicy( 252 ActionMapping mapping ) { 253 254 try { 255 return 256 ( ResPolicy )Class.forName( mapping.getParameter() ).newInstance(); 257 } 258 catch ( Exception e ) { 259 throw new ResException( 260 "Unexpected exception while instantiating \"" + 261 mapping.getParameter() +"\":" + e.toString() ); 262 } 263 } 264 265 269 public static FileTree getFileTree( 270 ResPolicy policy, 271 HttpServletRequest request ) { 272 273 FileTree fileTree = 274 ( FileTree )request.getSession().getAttribute( ResGlobals.FILETREE_KEY ); 275 276 if ( fileTree == null ) { 277 fileTree = policy.newFileTree(); 278 fileTree.build( getFileManager( policy, request ) ); 279 280 request.getSession().setAttribute( ResGlobals.FILETREE_KEY, fileTree ); 281 } 282 283 return fileTree; 284 } 285 286 288 } 290 | Popular Tags |