Listing of Source cgi/CookieCGI.java
package se.entra.phantom.server.http;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;
import java.util.Vector;
import java.util.Hashtable;
import org.w3c.dom.Element;

import se.entra.phantom.common.Utilities;

/**
 * A sample CGI to demonstrate the use of cookies.
 */
public class CookieCGI implements CgiPrintInterface, IncludeCgiPrintInterface
{
  /**
   * Prints the current cookies passed to the server from the browser.
   *
   * <p>This method should print an HTML document to the <code>printWriter</code>.
   * The current directory of the CGI for the client agent is defined in the
   * <code>HttpSession</code> class instance.
   *
   * @throws  IOException  for IO errors.
   */
  @Override
  public void performAction(HttpSession session,HttpResource resource,Element element,PrintWriter out) throws IOException
    {
    Vector<HttpCookie> v=session.headerFields.getCookies();
    boolean found=false;
    if ( v!=null )
      {
      if ( v.size()>0 )
        {
        found=true;
        Enumeration<HttpCookie> e=v.elements();
        out.print("<ol>\r\n");
        while ( e.hasMoreElements() )
          {
          HttpCookie cookie=e.nextElement();
          out.print("<li>name = '"+InternetCharacterConversion.stringConvertToHTMLString(cookie.name)
                   +"', value = '"+InternetCharacterConversion.stringConvertToHTMLString(cookie.value)+"'");
          if ( cookie.path!=null )
            out.print(", path = '"+cookie.path+"'");
          if ( cookie.domain!=null )
            out.print(", domain = '"+cookie.domain+"'");
          out.print(", isVersion1 = "+cookie.isVersion1+"</li>\r\n");
          }
        out.print("</ol>\r\n");
        }
      }

    if ( !found )
      out.print("No cookies sent from your browser...<br>\r\n");
    }

  /**
   * Sends a cookie to the client depending on the information provided in the
   * "cgi_cookiesample.html" form.
   *
   * <p>This method should print a HTML document content using the
   * <code>PrintWriter</code> parameter.
   *
   * <p>The current directory of the CGI for the client agent is defined in the
   * <code>HttpSession</code> class instance.
   *
   * @throws  IOException  for IO errors.
   */
  @Override
  public void performAction(HttpSession session,PrintWriter out) throws IOException
    {
    out.print("<html>\r\n"
             +"<head>\r\n"
             +"  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\r\n"
             +"  <meta name=\"GENERATOR\" content=\""+Utilities.PROGRAM_NAME+" CookieCGI\">\r\n"
             +"  <title>"+Utilities.PROGRAM_NAME+" - Cookie Servlet/CGI</title>\r\n"
             +"</head>\r\n"
             +"<body><font size=2 face=\"Verdana, Arial, Helvetica, sans-serif\">\r\n"
             +"  <h1>General Information</h1>\r\n"
             +"      Server universal time: "+HttpDate.getCurrentUTC()+"\r\n"
             +"  <br>Server local time: "+(new Date())+"\r\n"
             +"  <br>Host name: "+session.hostName+"\r\n"
             +"  <br>Host address: "+session.hostAddress+"\r\n"
             +"  <br>Port: "+session.serverPort+"\r\n"
             +"  <h1>Cookie Information</h1>\r\n");

    Hashtable<String,String> ht=session.formData;
    if ( ht!=null )
      {
      String comment=ht.get("COMMENT");
      String name   =ht.get("NAME"   );
      String value  =ht.get("VALUE"  );
      String path   =ht.get("PATH"   );
      String domain =ht.get("DOMAIN" );
      String secure =ht.get("SECURE" );
      String date   =ht.get("DATE"   );
      String maxAge =ht.get("MAXAGE" );

      // Secure might be null if not checked.
      if ( secure==null )
        secure="0";

      // Check for validity.
      if ( comment==null
        || name   ==null || name.length()==0
        || value  ==null
        || path   ==null
        || domain ==null
        || date   ==null
        || maxAge ==null )
        {
        out.print("The info does not come from the <b>/samples/cgi_cookiesample.html</b> form or you have not filled in the <b>Name</b> and/or <b>Path</b> fields!<p>\r\n");
        }
      else
        {
        // Parse the date information.
        boolean isVersion1=true;
        Date dt=null;
        if ( date.length()>0 )
          {
          dt=HttpDate.getDate(date);
          if ( dt==null )
            date+=" -- <b>The Date format is invalid!</b>";
          else
            isVersion1=false;
          }

        // Parse the maximum age.
        int imaxAge=-1;
        if ( !isVersion1 )
          maxAge+=" -- Not used in non-version 1";
        else
        if ( maxAge.length()>0 )
          {
          try
            {
            imaxAge=Integer.parseInt(maxAge);
            }
          catch(Exception e)
            {
            maxAge+=" -- <b>The Max-Age is an invalid number!</b>";
            }
          }
        
        // Check secure cookie flag.
        boolean isSecure=(secure.equals("1"));

        // Display input data from form.
        out.print("<font size=3><pre>"
                 +"Name      : '"+InternetCharacterConversion.stringConvertToHTMLString(name   )+"'\r\n"
                 +"Value     : '"+InternetCharacterConversion.stringConvertToHTMLString(value  )+"'\r\n"
                 +"Comment   : '"+InternetCharacterConversion.stringConvertToHTMLString(comment)+"'\r\n"
                 +"Path      : '"+InternetCharacterConversion.stringConvertToHTMLString(path   )+"'\r\n"
                 +"Domain    : '"+InternetCharacterConversion.stringConvertToHTMLString(domain )+"'\r\n"
                 +"Secure    : '"+isSecure+"'\r\n"
                 +"Date      : '"+((dt!=null)?
                                 HttpDate.getCurrentUTC(dt):
                                 InternetCharacterConversion.stringConvertToHTMLString(date))+"'\r\n"
                 +"Max-Age   : '"+maxAge+"'\r\n"
                 +"isVersion1: '"+isVersion1
                 +"'</pre></font>\r\n");

        // Create the new cookie and add it...
        HttpCookie cookie=new HttpCookie(name,value,comment,path,domain,dt,imaxAge,isSecure,isVersion1);
        session.setCookie(cookie);
        }
      }
    else
      out.print("The info does not come from the <b>/samples/cgi_cookiesample.html</b> form!<p>\r\n");

    out.print("<p><b><a href=\"/samples/cgi_cookiesample.html\">Click here to return to the Cookie Test Form</a></b>\r\n"
             +"</body>\r\n"
             +"</hmtl>\r\n");
    }
}