Eric J. Bowersox a900d9d51f added support for QID (quick ID) generation within pages, in preparation for
supporting a hitcounter system in the page footer
2001-10-23 21:42:29 +00:00

172 lines
6.0 KiB
Java

/*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
*
* Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
* WARRANTY OF ANY KIND, either express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
* The Original Code is the Venice Web Communities System.
*
* The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
* for Silverwrist Design Studios. Portions created by Eric J. Bowersox are
* Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved.
*
* Contributor(s):
*/
package com.silverwrist.venice.servlets.format;
import java.util.*;
import java.io.Writer;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import com.silverwrist.util.StringUtil;
public class ConfirmBox implements ContentRender, ColorSelectors
{
/*--------------------------------------------------------------------------------
* Static data members
*--------------------------------------------------------------------------------
*/
private static final long TIMEOUT = 60000; // 1 minute
private static Random rng = null;
/*--------------------------------------------------------------------------------
* Attributes
*--------------------------------------------------------------------------------
*/
private int conf_num;
private String title;
private String message;
private String confirm_url;
private String deny_url;
private Date created;
/*--------------------------------------------------------------------------------
* Constructor
*--------------------------------------------------------------------------------
*/
public ConfirmBox(HttpServletRequest request, String attr_name, String param_name, String title,
String message, String confirm_url, String deny_url)
{
if (rng==null)
{ // initialize the random number gnerator
Date now = new Date();
rng = new Random(now.getTime());
} // end if
// Fill in all the parameters except created.
this.conf_num = rng.nextInt(0x1000000);
this.title = title;
this.message = message;
this.confirm_url = confirm_url + "&" + param_name + "=" + this.conf_num;
this.deny_url = deny_url;
// Stash this object in the HTTP session.
HttpSession session = request.getSession(true);
session.setAttribute(attr_name,this);
// Now start the timer ticking...
this.created = new Date();
} // end constructor
/*--------------------------------------------------------------------------------
* Internal operations
*--------------------------------------------------------------------------------
*/
protected boolean doConfirm(int test_num)
{
Date now = new Date();
if ((now.getTime()-created.getTime())>TIMEOUT)
return false;
return (test_num==conf_num);
} // end doConfirm
/*--------------------------------------------------------------------------------
* Implementations from interface VeniceContent
*--------------------------------------------------------------------------------
*/
public String getPageTitle(RenderData rdat)
{
return title;
} // end getPageTitle
public String getPageQID()
{
return null;
} // end getPageQID
/*--------------------------------------------------------------------------------
* Implementations from interface ContentRender
*--------------------------------------------------------------------------------
*/
public void renderHere(Writer out, RenderData rdat) throws IOException
{
out.write("<P><TABLE ALIGN=CENTER WIDTH=\"70%\" BORDER=1 CELLPADDING=2 CELLSPACING=1>");
out.write("<TR VALIGN=MIDDLE><TD ALIGN=CENTER BGCOLOR=\"" + rdat.getStdColor(CONFIRM_TITLE_BACKGROUND)
+ "\">\n");
out.write(rdat.getStdFontTag(CONFIRM_TITLE_FOREGROUND,3) + StringUtil.encodeHTML(title) + "</FONT>\n");
out.write("</TD></TR><TR VALIGN=MIDDLE><TD ALIGN=CENTER>\n");
out.write(rdat.getStdFontTag(CONTENT_FOREGROUND,3) + "<P>" + StringUtil.encodeHTML(message) + "<P>\n");
out.write("<A HREF=\"" + rdat.getEncodedServletPath(confirm_url) + "\">");
out.write("<IMG SRC=\"" + rdat.getFullImagePath("bn_yes.gif")
+ "\" ALT=\"Yes\" WIDTH=80 HEIGHT=24 BORDER=0></A>&nbsp;&nbsp;\n");
out.write("<A HREF=\"" + rdat.getEncodedServletPath(deny_url) + "\">");
out.write("<IMG SRC=\"" + rdat.getFullImagePath("bn_no.gif")
+ "\" ALT=\"No\" WIDTH=80 HEIGHT=24 BORDER=0></A>\n");
out.write("</FONT></TD></TR></TABLE><P>\n");
} // end renderHere
/*--------------------------------------------------------------------------------
* External static operations
*--------------------------------------------------------------------------------
*/
public static boolean isConfirmed(HttpServletRequest request, String attr_name, String param_name)
{
// First, get the ConfirmBox out of the session data
HttpSession session = request.getSession(true);
Object obj = session.getAttribute(attr_name);
session.removeAttribute(attr_name); // we can only get it out once!
if ((obj==null) || !(obj instanceof ConfirmBox)) // this is not a valid ConfirmBox!
return false;
ConfirmBox cb = (ConfirmBox)obj;
// Now get the confirmation number out of the URL parameters.
String test_num_str = request.getParameter(param_name);
if (test_num_str==null)
return false; // no confirmation!
int test_num;
try
{ // convert to an integer
test_num = Integer.parseInt(test_num_str);
} // end try
catch (NumberFormatException nfe)
{ // if the confirmation number is bogus, we're h0sed
return false;
} // end catch
return cb.doConfirm(test_num);
} // end isConfirmed
} // end class ConfirmBox