/*
** Rachel  - Resource Loading Toolkit for Web Start/JNLP
** 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 rachel.http;

import java.io.*;
import java.net.*;
import java.util.*;
import houston.*;
import rachel.http.loader.*;
import rachel.http.resource.*;
import rachel.util.*;

public class RequestProcessor implements Runnable
{
   static Logger T = Logger.getLogger( RequestProcessor.class );

   private static List _pool = new LinkedList();

   private WebResourceLoader _loader;

   public RequestProcessor( WebResourceLoader loader )
   {
      _loader = loader;
   }

   public static void processRequest( Socket request )
   {
      synchronized( _pool )
      {
         _pool.add( _pool.size(), request );
         _pool.notifyAll();
      }
   }

   public void run()
   {
      while( true )
      {
         Socket connection;
         synchronized( _pool )
         {
            while( _pool.isEmpty() )
            {
               try
               {
                  _pool.wait();
               }
               catch( InterruptedException ex )
               {
               }
            }
            connection = ( Socket ) _pool.remove( 0 );
         }

         try
         {
            OutputStream raw = new BufferedOutputStream( connection.getOutputStream() );
            Writer out = new OutputStreamWriter( raw );

            Reader in = new InputStreamReader( new BufferedInputStream( connection.getInputStream() ), "ASCII" );

            StringBuffer requestLineBuf = new StringBuffer();
            int c;
            while( true )
            {
               c = in.read();
               if( c == '\r' || c == '\n' )
                  break;
               requestLineBuf.append( ( char ) c );
            }

            String requestLine = requestLineBuf.toString();

            StringTokenizer st = new StringTokenizer( requestLine );
            String method = st.nextToken();

            WebResource resource = null;

            if( method.equals( Http.Method.GET ) )
            {
               String resourceName = st.nextToken();

               resource = _loader.getResource( resourceName );
            }
            else
            {
               // sorry, we only understand GET

               resource = new ErrorResource( Http.Error.NOT_IMPLEMENTED_501 );
            }

            out.write( resource.getHeader() );
            out.flush();

            // send the file; it may be an image or other binary data
            //   so use the underlying output stream instead of the writer
            raw.write( resource.getData() );
            raw.flush();
         }
         catch( IOException ioex )
         {
            Status.error( ioex.toString() );
         }
         finally
         {
            try
            {
               connection.close();
            }
            catch( IOException ioex )
            {
               Status.error( ioex.toString() );
            }
         }
      }
      // end while
   }
   // end run()

}