Novocode
Software & Networking
Previous Page Contents Next Page
 NetForge 1.0 Manual:   NFTestServlet.java 

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

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


/**
 * NetForge Test Servlet, similar to TestResponder.
 *
 * This source is in the public domain. Do with it whatever you want and
 * don't blame me if it doesn't work :-)
 *
 * @author Stefan Zeiger
 */

public final class NFTestServlet extends HttpServlet
{
  public void doGet(HttpServletRequest req, HttpServletResponse res)
         throws ServletException, IOException
  {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    out.print("<HTML><HEAD><TITLE>"+getServletInfo()+"</TITLE></HEAD>"+
              "<BODY BGCOLOR=\"#B2B2B2\"><H1>"+getServletInfo()+"</H1><HR>"+
              "<P>You requested to <I>"+req.getMethod()+"</I> URI \"<I>"+
              HttpUtils.getRequestURL(req).toString()+
              "</I>\" (local :\"<I>"+req.getPathInfo()+"</I>\").<P><HR><P>"+
              "Base name: <I>"+req.getServletPath()+"</I><P>"+
              "User: <I>"+
              ((req.getRemoteUser()==null)?"anonymous":req.getRemoteUser())+
              "</I>");

    // Request headers
    {
      out.print("<P><HR><P><H3>HTTP Request Properties:</H3>"+
                "<TABLE BORDER=1 WIDTH=100%>");
      Enumeration k = req.getHeaderNames();
      while(k.hasMoreElements())
      {
        String key = (String)(k.nextElement()), val = req.getHeader(key);
        out.print("<TR><TH>"+key+"</TH><TD ALIGN=CENTER>"+val+"</TD></TR>");
      }
      out.print("</TABLE>");
    }

    // Init parameters
    {
      Enumeration k = getServletConfig().getInitParameterNames();
      if((k != null) && (k.hasMoreElements()))
      {
        out.print("<P><HR><P><H3>Init Parameters:</H3>"+
                  "<TABLE BORDER=1 WIDTH=100%>");
        while(k.hasMoreElements())
        {
          String key = (String)(k.nextElement()), val = getInitParameter(key);
          out.print("<TR><TH>"+key+"</TH><TD ALIGN=CENTER>"+val+"</TD></TR>");
        }
        out.print("</TABLE>");
      }
    }

    // Servlet parameters
    {
      Enumeration k = req.getParameterNames();
      if((k != null) && (k.hasMoreElements()))
      {
        out.print("<P><HR><P><H3>Servlet Parameters:</H3>"+
                  "<TABLE BORDER=1 WIDTH=100%>");
        while(k.hasMoreElements())
        {
          String key = (String)(k.nextElement()), val = req.getParameter(key);
          out.print("<TR><TH>"+key+"</TH><TD ALIGN=CENTER>"+val+"</TD></TR>");
        }
        out.print("</TABLE>");
      }
    }

    // Security restrictions
    {
      SecurityManager man = System.getSecurityManager();
      if(man != null)
      {
        out.print("<P><HR><P><H3>Security Restrictions (Excerpt):</H3>"+
                  "<TABLE BORDER=1 WIDTH=100%>");
        String sec;

        try { man.checkPropertyAccess("java.version"); sec = "Allowed"; }
        catch(SecurityException e) { sec = "Forbidden"; }
        out.print("<TR><TH>Get system property \"java.version\""+
                  "</TH><TD ALIGN=CENTER>"+sec+"</TD></TR>");

        try { man.checkDelete("/tmp/foo"); sec = "Allowed"; }
        catch(SecurityException e) { sec = "Forbidden"; }
        out.print("<TR><TH>Delete file \"/tmp/foo\""+
                  "</TH><TD ALIGN=CENTER>"+sec+"</TD></TR>");

        try { man.checkExec("/bin/ls"); sec = "Allowed"; }
        catch(SecurityException e) { sec = "Forbidden"; }
        out.print("<TR><TH>Execute \"/bin/ls\""+
                  "</TH><TD ALIGN=CENTER>"+sec+"</TD></TR>");

        try { man.checkExit(0); sec = "Allowed"; }
        catch(SecurityException e) { sec = "Forbidden"; }
        out.print("<TR><TH>Exit virtual machine"+
                  "</TH><TD ALIGN=CENTER>"+sec+"</TD></TR>");

        try { man.checkCreateClassLoader(); sec = "Allowed"; }
        catch(SecurityException e) { sec = "Forbidden"; }
        out.print("<TR><TH>Create class loader"+
                  "</TH><TD ALIGN=CENTER>"+sec+"</TD></TR>");

        out.print("</TABLE>");
      }
    }

    out.print("</BODY></HTML>");
  }


  public String getServletInfo() { return "NetForge Test Servlet"; }
}


Last modified Feb/17/1999 by Stefan Zeiger