SnoopServlet.java

/**
 * SnoopServlet
 * 
 * Displays table of data available through the Servlet API.
 * 
 * by Paul Colton
 * (c) 1999 Allaire Corporation
 */

import java.io.*;
import java.util.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 * SimpleServlet extends JRunDemoServlet and displays
 * a simple text string.
 */
public class SnoopServlet extends JRunDemoServlet {

	public void service( HttpServletRequest req, HttpServletResponse res ) 
	  throws IOException {
		// Set the content-type to HTML
		res.setContentType( "text/html; charset=euc-jp" );

		// Get the PrintWriter for the servlet response
		PrintWriter out = res.getWriter();

		// Begin the demo page. This method is defined in JRunDemoServlet.
		generateDemoPageStart( out );

		// Build the demo page
		out.println( "<CENTER>" );
		out.println( "<TABLE BORDER=0 CELLPADDING=4>" );
                out.println("コンテキスト ");
                

        /**
		 * This next section makes repeated calls to the makeTableEntry method,
		 * passing the results of different method calls.
		 */
		out.println( makeTableEntry( req, res, "コンテキスト InitParameter データ", getContextParameterData( getServletContext(), res ), "a"  ) );
        out.println( makeTableEntry( req, res, "サーブレット InitParameter データ", getInitParameterData( this, res ), "b" ) );
		out.println( makeTableEntry( req, res, "要求データ", getRequestData( req ), "c"  ) );

		out.println( makeTableEntry( req, res, "ヘッダ データ", getHeaderData( req ), "d"  ) );
		out.println( makeTableEntry( req, res, "クッキー データ", getCookieData( req ), "e"  ) );

        out.println( makeTableEntry( req, res, "要求パラメータ (単一スタイル)", getRequestParameterData( req, res ), "f"  ));
        out.println( makeTableEntry( req, res, "要求パラメータ (複数スタイル)", getRequestParametersData( req, res ), "g"  ) );

        //Showing of application variables is commented out to prevent showing of data that
        //should probably not be shown to the world (classpaths, rootdirs, etc).
		//out.println( makeTableEntry( req, "Application Attributes", getAttributeData( getServletContext() )  )  );

		out.println( makeTableEntry( req, res, "セッションの属性", getSessionData( req, res ), "h"  ) );
        out.println( makeTableEntry( req, res, "要求の属性", getAttributeData( req ), "i"  ) );
		out.println( "</TABLE>" );
		out.println( " </CENTER> " );
		
		// End the demo page. This method is defined in JRunDemoServlet.
		generateDemoPageEnd( req, res, out );
	}
	
	/**
	 * makeTableEntry formats information from the the passed Vector
	 * into a table, using the passed title as the first row.
	 * This method also supports collapsible table rows.
	 */
	private String makeTableEntry( HttpServletRequest req, HttpServletResponse res, String title, Vector rows, String key )
	throws IOException  {

            String out = "";
        
            if( rows == null || rows.size() == 0 ) {
                out = "<TR BGCOLOR=\"" + TITLE_COLOR + "\">" +
			"<TD COLSPAN=2><FONT FACE=\"arial\">" + title + " - none</FONT></TD>";
			return out;
                }

                else {
		    out = "<TR BGCOLOR=\"" + TITLE_COLOR + "\">" +
			"<TD COLSPAN=2><FONT FACE=\"arial\">" + title;
                }
		
		HttpSession session = req.getSession( true );
		
		Hashtable state = (Hashtable) session.getAttribute( "state" );
		
		if( state == null ) {
			state = new Hashtable();
			session.setAttribute( "state", state );
		}
		
		
		Boolean b = (Boolean ) state.get( key );
		if( b == null) b = new Boolean( true );
		
		String toggle = req.getParameter( "toggle" );
                if( toggle != null) toggle = ParameterUtil.getParameterWithEncoding(req, res, "toggle" );                
		if( toggle != null && toggle.equals( key ) )
			if( b.booleanValue() == true )
				b = new Boolean( false );
			else
				b = new Boolean( true );
		
		state.put( key, b );

		if( b.booleanValue() == true )
			out += " - <A HREF=\"" + req.getRequestURI() + "?toggle=" + 
				   URLEncoder.encode( key ) + "\">Collapse</A>" +
				   "</FONT></TD>" + "</TR>";
		else
			out += " - <A HREF=\"" + req.getRequestURI() + "?toggle=" + 
				   URLEncoder.encode( key ) + "\">Expand</A>" +
				   "</FONT></TD>" + "</TR>";

		if( b.booleanValue() == true ) {
			for( int i = 0; i < rows.size(); i++ ) {
				Vector cols = (Vector) rows.elementAt( i );
				
				if( i  2 == 0 )
					out += "<TR BGCOLOR=\"" + ROW_COLOR + "\">";
				else
					out += "<TR BGCOLOR=\"" + ROW_ALT_COLOR + "\">";
				
				for( int j = 0; j < cols.size(); j++ ) {
					out += "<TD><FONT SIZE=-1 FACE=\"arial\">" + cols.elementAt( j ) + "</FONT></TD>";
				}
				
				out += "</TR>";
		  }
		}
		return out;
	}

	/**
	 * getInitParameterData returns a Vector of servlet initialization parameters.
	 * The output of this method is used as input to the makeTableEntry method.
	 */
     private Vector getInitParameterData( HttpServlet servlet, HttpServletResponse res )
	throws IOException  {
		Vector rows = new Vector();
		
		for( Enumeration e=servlet.getInitParameterNames(); e.hasMoreElements(); ) {
			String name = (String) e.nextElement();
			String value = new String(servlet.getInitParameter( name ));
			Vector cols = new Vector();
			cols.addElement( name );
			cols.addElement( value );
			rows.addElement( cols );
		}
		return rows;
	}

	/**
	 * getContextParameterData returns a Vector of JRun server initialization parameters.
	 * The output of this method is used as input to the makeTableEntry method.
	 */
     private Vector getContextParameterData( ServletContext context, HttpServletResponse res ) 
	throws IOException {
		Vector rows = new Vector();
		
		for( Enumeration e=context.getInitParameterNames(); e.hasMoreElements(); ) {
			String name = (String) e.nextElement();
			String value = new String(context.getInitParameter( name ));
			
			Vector cols = new Vector();
			cols.addElement( name );
			cols.addElement( value );
			rows.addElement( cols );
		}
		return rows;
	}
		
	/**
	 * getAttributeData(HttpServletRequest) returns a Vector of attributes 
	 * passed to the servlet. The output of this method is used as input to 
	 * the makeTableEntry method. Note that there is another version of getAttributeData,
	 * which takes ServletContext as an argument.
	 */
	private Vector getAttributeData( HttpServletRequest req )
	{
		Vector rows = new Vector();
		
		for( Enumeration e=req.getAttributeNames(); e.hasMoreElements(); ) {
			String name = (String) e.nextElement();
			Object value = req.getAttribute( name );
			
			Vector cols = new Vector();
			cols.addElement( name );
			cols.addElement( value );
			rows.addElement( cols );
		}
		return rows;
	}

	/**
	 * getAttributeData(ServletContext) returns a Vector of attributes 
	 * for the servlet context. The output of this method is used as input to 
	 * the makeTableEntry method. Note that there is another version of getAttributeData,
	 * which takes HttpServletRequest as an argument.
	 */
    private Vector getAttributeData( ServletContext sc ) {
		Vector rows = new Vector();
		
		for( Enumeration e=sc.getAttributeNames(); e.hasMoreElements(); ) {
			String name = (String) e.nextElement();
			Object value = sc.getAttribute( name );
			
			Vector cols = new Vector();
			cols.addElement( name );
			cols.addElement( value );
			rows.addElement( cols );
		}
		return rows;
	}
	
	/**
	 * getSessionData returns a Vector containing session information. 
	 * The output of this method is used as input to the makeTableEntry method.
	 */
	private Vector getSessionData( HttpServletRequest req, HttpServletResponse res ) {
		Vector rows = new Vector();
		
		HttpSession s = req.getSession( true );
		
		Vector cols = new Vector();
		cols.addElement( "Session ID" );
		cols.addElement( s.getId() );
		rows.addElement( cols );
				
		cols = new Vector();
		cols.addElement( "Creation Time" );
		cols.addElement( new Date( s.getCreationTime() ).toString() );
		rows.addElement( cols );
		
		cols = new Vector();
		cols.addElement( "Last Accessed Time" );
		cols.addElement( new Date( s.getLastAccessedTime() ).toString() );
		rows.addElement( cols );

		cols = new Vector();
		cols.addElement( "Max Inactive Interval" );
		cols.addElement( "" + s.getMaxInactiveInterval() );
		rows.addElement( cols );

		cols = new Vector();
		cols.addElement( "Is New Session" );
		cols.addElement( "" + s.isNew() );
		rows.addElement( cols );

		for( Enumeration e=s.getAttributeNames(); e.hasMoreElements(); ) {
			String name = (String) e.nextElement();
			Object value = s.getAttribute( name );

			cols = new Vector();
			cols.addElement( "Entry '" + name + "'" );
			cols.addElement(  "" + value );
			rows.addElement( cols );
		}

		return rows;
	}

	/**
	 * getRequestParameterData returns a Vector containing parameters from 
	 * the HttpRequest object. The output of this method is used as input to 
	 * the makeTableEntry method.
	 */
    private Vector getRequestParameterData( HttpServletRequest req, HttpServletResponse res ) 
	throws IOException {
		Vector rows = new Vector();
		
		for( Enumeration e=ParameterUtil.getParameterNamesWithEncoding(req, res); e.hasMoreElements(); ) {
			String name = (String) e.nextElement();
			Object value = ParameterUtil.getParameterWithEncoding(req, res, name );
			
			Vector cols = new Vector();
			cols.addElement( name );
			cols.addElement( value );
			rows.addElement( cols );
		}
		return rows;
	}

	/**
	 * getRequestParametersData returns a Vector containing parameters from the HttpRequest object. 
	 * The output of this method is used as input to the makeTableEntry method.
	 */
    private Vector getRequestParametersData( HttpServletRequest req, HttpServletResponse res ) 
	throws IOException {
		Vector rows = new Vector();
		
		for( Enumeration e=ParameterUtil.getParameterNamesWithEncoding(req, res); e.hasMoreElements(); ) {
			String name = (String) e.nextElement();
			String[] values = req.getParameterValues( name );
			
			Vector cols = new Vector();
			cols.addElement( name );
                        String value = "";
                        for(int i=0; i<values.length; i++){
                            String valuesd = new String((values[i]).getBytes("8859_1"), res.getCharacterEncoding());
                            value += "{" + valuesd  + "}";
                        }
			cols.addElement( value );
			rows.addElement( cols );
		}
		return rows;
	}
	
	/**
	 * getHeaderData returns a Vector containing HTTP headers from the 
	 * HttpRequest object. The output of this method is used as input 
	 * to the makeTableEntry method.
	 */
	private Vector getHeaderData( HttpServletRequest req ) {
		Vector rows = new Vector();
		
		for( Enumeration e=req.getHeaderNames(); e.hasMoreElements(); ) {
			String name = (String) e.nextElement();
			String value = req.getHeader( name );
			
			Vector cols = new Vector();
			cols.addElement( name );
			cols.addElement( value );
			rows.addElement( cols );
		}
		return rows;
	}

	/**
	 * getCookieData returns a Vector containing cookies from the 
	 * HttpRequest object. The output of this method is used as input 
	 * to the makeTableEntry method.
	 */
	private Vector getCookieData( HttpServletRequest req ) {
		Vector rows = new Vector();
		
		Cookie[] cc = req.getCookies();
		
		if( cc != null ) {
			for( int i = 0; i < cc.length; i++ ) {
				Cookie c = cc[i];
				
				Vector cols = new Vector();
				cols.addElement( c.getName() );
				cols.addElement( c.getValue() );
				rows.addElement( cols );
				
				cols = new Vector();
				cols.addElement( c.getName() + ": Comment" );
				cols.addElement( c.getComment() );
				rows.addElement( cols );
				
				cols = new Vector();
				cols.addElement( c.getName() + ": Domain" );
				cols.addElement( c.getDomain() );
				rows.addElement( cols );

				cols = new Vector();
				cols.addElement( c.getName() + ": Max age" );
				cols.addElement( "" + c.getMaxAge() );
				rows.addElement( cols );

				cols = new Vector();
				cols.addElement( c.getName() + ": Path" );
				cols.addElement( c.getPath() );
				rows.addElement( cols );
				
				cols = new Vector();
				cols.addElement( c.getName() + ": Is secure" );
				cols.addElement( "" + c.getSecure() );
				rows.addElement( cols );

				cols = new Vector();
				cols.addElement( c.getName() + ": Version" );
				cols.addElement( "" + c.getVersion() );
				rows.addElement( cols );
			}
		}
		return rows;
	}
	
	/**
	 * getRequestData returns a Vector containing HTTP request information. 
	 * The output of this method is used as input 
	 * to the makeTableEntry method.
	 */
	private Vector getRequestData( HttpServletRequest req ) {
		Vector rows = new Vector();
	
		Vector cols = new Vector();
		cols.addElement( "Request method" );
		cols.addElement( req.getMethod() );
		rows.addElement( cols );
	
		cols = new Vector();
		cols.addElement( "Request protocol" );
		cols.addElement( req.getProtocol() );
		rows.addElement( cols );
	
		cols = new Vector();
		cols.addElement( "Request URI" );
		cols.addElement( req.getRequestURI() );
		rows.addElement( cols );

        cols = new Vector();
		cols.addElement( "Context path" );
		cols.addElement( req.getContextPath() );
		rows.addElement( cols );
	
		cols = new Vector();
		cols.addElement( "Servlet path" );
		cols.addElement( req.getServletPath() );
		rows.addElement( cols );
	
		cols = new Vector();
		cols.addElement( "Path info" );
		cols.addElement( req.getPathInfo() );
		rows.addElement( cols );
	
		cols = new Vector();
		cols.addElement( "Path translated" );
		cols.addElement( req.getPathTranslated() );
		rows.addElement( cols );
	
		cols = new Vector();
		cols.addElement( "Query string" );
		cols.addElement( req.getQueryString() );
		rows.addElement( cols );
	
		cols = new Vector();
		cols.addElement( "Content length" );
		cols.addElement( "" + req.getContentLength() );
		rows.addElement( cols );
	
		cols = new Vector();
		cols.addElement( "Content type" );
		cols.addElement( req.getContentType() );
		rows.addElement( cols );
	
		cols = new Vector();
		cols.addElement( "Server name" );
		cols.addElement( req.getServerName() );
		rows.addElement( cols );
	
		cols = new Vector();
		cols.addElement( "Server port" );
		cols.addElement( "" + req.getServerPort() );
		rows.addElement( cols );
	
		cols = new Vector();
		cols.addElement( "Remote user" );
		cols.addElement( req.getRemoteUser() );
		rows.addElement( cols );
	
		cols = new Vector();
		cols.addElement( "Remote address" );
		cols.addElement( req.getRemoteAddr() );
		rows.addElement( cols );
	
		cols = new Vector();
		cols.addElement( "Remote host" );
		cols.addElement( req.getRemoteHost() );
		rows.addElement( cols );
	
		cols = new Vector();
		cols.addElement( "Authorization scheme" );
		cols.addElement( req.getAuthType() );
		rows.addElement( cols );
		
		cols = new Vector();
		cols.addElement( "Character Encoding" );
		cols.addElement( req.getCharacterEncoding() );
		rows.addElement( cols );
		
		/*
		cols = new Vector();
		cols.addElement( "Context Path" );
		cols.addElement( req.getContextPath() );
		rows.addElement( cols );
		*/
		
		cols = new Vector();
		cols.addElement( "Locale" );
		cols.addElement( "" + req.getLocale() );
		rows.addElement( cols );
		
		/*
		cols = new Vector();
		cols.addElement( "User Principal" );
		cols.addElement( "" + req.getUserPrincipal() );
		rows.addElement( cols );
		*/
		
		cols = new Vector();
		cols.addElement( "Is Secure" );
		cols.addElement( "" + req.isSecure() );
		rows.addElement( cols );
		
		return rows;
	}		
}