www.pudn.com > jnp-src.rar > HttpFile.java


/*
 * Java Network Programming, Second Edition
 * Merlin Hughes, Michael Shoffner, Derek Hamner
 * Manning Publications Company; ISBN 188477749X
 *
 * http://nitric.com/jnp/
 *
 * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner;
 * all rights reserved; see license.txt for details.
 */

import java.io.*; 
 
public class HttpFile implements HttpProcessor { 
  protected File file; 
   
  public HttpFile (HttpInputStream in) throws IOException { 
    if (in.getMethod () == HTTP.METHOD_POST) 
      throw new HttpException (HTTP.STATUS_NOT_ALLOWED, 
                               "" + in.getMethod () + " " + in.getPath () + ""); 
    file = new File (HTTP.HTML_ROOT, HTTP.translateFilename (in.getPath ())); 
    if (in.getPath ().endsWith ("/")) 
      file = new File (file, HTTP.DEFAULT_INDEX); 
    if (!file.exists ()) 
      throw new HttpException (HTTP.STATUS_NOT_FOUND, 
                               "File " + in.getPath () + " not found."); 
    if (file.isDirectory ()) 
      throw new RedirectException (HTTP.STATUS_MOVED_PERMANENTLY, 
                                   in.getPath () + "/"); 
    if (!file.isFile () || !file.canRead ()) 
      throw new HttpException (HTTP.STATUS_FORBIDDEN, in.getPath ()); 
  } 
 
  public void processRequest (HttpOutputStream out) throws IOException { 
    out.setHeader ("Content-type", HTTP.guessMimeType (file.getName ())); 
    out.setHeader ("Content-length", String.valueOf (file.length ())); 
    if (out.sendHeaders ()) { 
      FileInputStream in = new FileInputStream (file); 
      out.write (in); 
      in.close (); 
    } 
  } 
}