/*
** 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 luxor.core.*;
import luxor.http.resource.*;
import luxor.spi.*;
import luxor.status.*;

public class FileResourceLoader implements WebResourceLoader
{
   static Logger T = Logger.getLogger( FileResourceLoader.class );

   private File _documentRootDir;
   private String _indexFileName = "index.html";

   public FileResourceLoader( File documentRootDir )
   {
      _documentRootDir = documentRootDir;
   }

   public WebResource getResource( String name )
   {
      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( name, dir );
               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 );
         return res;
      }
      catch( IOException ioex )
      {
         Xul.error( "failed to retrieve resource '" + name + "': " + ioex.toString() );
         return null;
      }
   }

}