|
FileContextResourceLoader |
|
/* ** Luxor - XML User Interface Language (XUL) Toolkit ** Copyright (c) 2001, 2002 by Gerald Bauer ** ** This program is free software. ** ** You may redistribute it and/or modify it under the terms of the GNU ** General Public License as published by the Free Software Foundation. ** Version 2 of the license should be included with this distribution in ** the file LICENSE, as well as License.html. If the license is not ** included with this distribution, you may find a copy at the FSF web ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA. ** ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND, ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR ** REDISTRIBUTION OF THIS SOFTWARE. ** */ package luxor.http.loader; import java.io.*; import java.util.*; import luxor.core.*; import luxor.http.resource.*; import luxor.spi.*; import luxor.status.*; // fix: merge FileResourceLoader and FileContextResourceLoader into one to avoid // code duplication // or create a common base class public class FileContextResourceLoader implements WebResourceLoader { static Logger T = Logger.getLogger( FileContextResourceLoader.class ); /** * e.g. venus/test (note: no leading and trailing slashes) */ private String _context; private File _documentRootDir; /** * additional user-supplied http headers such as pragma: no-cache */ private List _httpHeader; private String _indexFileName = "index.html"; public FileContextResourceLoader( File documentRootDir, String context ) { this( documentRootDir, context, Collections.EMPTY_LIST ); } public FileContextResourceLoader( File documentRootDir, String context, List httpHeader ) { // note: context should have no leading and trailing slashes // eg. venus/test _documentRootDir = documentRootDir; _context = context; _httpHeader = httpHeader; } public String getContext() { return _context; } public WebResource getResource( String name ) { T.debug( "before: name=" + name ); // check if name starts with context String nameLower = name.toLowerCase(); String contextLower = _context.toLowerCase(); if( !nameLower.startsWith( "/" + contextLower + "/" ) ) return null; // no, different context // note: /context isn't legally allowed and shouldn't happen // when we cut of the context we need at least a leading /, that is, /context/ -> / name = name.substring( _context.length() + 1 ); T.debug( "after: name=" + name ); // note: this is the same code as in FileResourceLoader // fix: reuse code, delegate to FileResourceLoader // the only issue is DirectoryResource where we have // to pass the context along try { if( name.endsWith( "/" ) ) { // check if index filename exits // if it doesn't we create a directory view File indexFile = new File( _documentRootDir, name.substring( 1 ) + _indexFileName ); if( !indexFile.canRead() ) { // index file doesn't exists // index file doesn't exist // create directory // todo: put this in a method // todo: make it work for jar (not a high priority) // cut off leading / - leave trailing as it does no harm // note: for root there is only one / // fix: check if directory exists! File dir = new File( _documentRootDir, name.substring( 1 ) ); if( !dir.exists() ) return null; DirectoryResource res = new DirectoryResource( "/" + _context + name, dir, _httpHeader ); return res; } else { // index file does exist; fall through and treat it like an ordinary file name += _indexFileName; } } // strip leading / in request file File file = new File( _documentRootDir, name.substring( 1 ) ); if( !file.canRead() ) return null; FileResource res = new FileResource( name, file, _httpHeader ); return res; } catch( IOException ioex ) { Xul.error( "failed to retrieve resource '" + name + "': " + ioex.toString() ); return null; } } }
|
FileContextResourceLoader |
|