/***********************************************************************\
//                                                                      *
//      DumpServlet.java                                        	*
//                                                                      *
//      Purpose: An HTTP servlet supporting file upload			*
//                                                                      *
//      Author:    Simon Brooke                                         *
//      Created:   27th December 2000                                   *
//	$Revision: 1.1 $; $Date: 2001/01/09 12:14:12 $		*
//                                                                      *
//***********************************************************************/

package uk.co.weft.maybeupload;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

/** Crude basic debugging tool. Dumps the request stream to the
 *  response so that you have a chance to see what's going on.
 *
 *
 *  @author Simon Brooke (simon@jasmine.org.uk)
 *  @version $Revision: 1.1 $
 *  This revision: $Author: simon $
 *  <pre>
 *  $Log: DumpServlet.java,v $
 *  Revision 1.1  2001/01/09 12:14:12  simon
 *  Now tested with:
 *  	Netscape Communicator 4.76/Linux 2.2
 *  	Konqueror 1.9.8/Linux 2.2
 *  	Microsoft Internet Explorer 5.00.2014.0216IC
 *  File upload (including binary file upload) works. Remaining known bug:
 *  all fields must have data...
 *
 *  Revision 1.1.1.1  2001/01/05 14:58:09  simon
 *  First cut - not yet tested
 *
 *  </pre>
 */

public class DumpServlet extends javax.servlet.http.HttpServlet
{
    /** Dump the request input stream onto the response output, byte
     *  for byte, for analysis/debugging purposes. */
    protected void service( HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException
    {
	resp.setContentType( "text/plain");
	int length = req.getContentLength();
	int read = 0;
	ServletInputStream in = req.getInputStream();
	ServletOutputStream out = resp.getOutputStream();
	byte[] buffer = new byte[ 1024];
	int n;

	do
	    {
		n = in.read( buffer, 0, 1024); 
		
		read += n;

		if ( n > 0)
		    {
			out.print( new String( buffer, 0, n));
		    }
	    }
	while ( read < length && n > 0);

	out.println( "\nThat's all, folks!");

	out.flush();
    }
}
