/* * 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 . * * 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 Community System. * * The Initial Developer of the Original Code is Eric J. Bowersox , * 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.*; import java.text.DateFormat; import javax.servlet.*; import javax.servlet.http.*; import org.apache.log4j.*; import com.silverwrist.util.StringUtil; public class RenderData { /*-------------------------------------------------------------------------------- * Static data values *-------------------------------------------------------------------------------- */ private static final String ATTR_NAME = "com.silverwrist.venice.RenderData"; private static Category logger = Category.getInstance(RenderData.class.getName()); /*-------------------------------------------------------------------------------- * Attributes *-------------------------------------------------------------------------------- */ private RenderConfig rconf; private HttpServletRequest request; private HttpServletResponse response; private boolean can_gzip = false; private Locale my_locale; private TimeZone my_timezone; /*-------------------------------------------------------------------------------- * Constructor *-------------------------------------------------------------------------------- */ RenderData(RenderConfig rconf, HttpServletRequest request, HttpServletResponse response) { this.rconf = rconf; this.request = request; this.response = response; // determine whether this browser can accept GZIP-encoded content String encodings = request.getHeader("Accept-Encoding"); if ((encodings!=null) && (encodings.indexOf("gzip")>=0)) can_gzip = true; // TODO: replace with reading user's preferences my_locale = Locale.getDefault(); my_timezone = TimeZone.getDefault(); } // end constructor /*-------------------------------------------------------------------------------- * External static operations *-------------------------------------------------------------------------------- */ public static RenderData retrieve(ServletRequest request) { return (RenderData)(request.getAttribute(ATTR_NAME)); } // end retrieve /*-------------------------------------------------------------------------------- * External operations *-------------------------------------------------------------------------------- */ public void store(ServletRequest request) { request.setAttribute(ATTR_NAME,this); } // end store public boolean useHTMLComments() { return rconf.useHTMLComments(); } // end useHTMLComments public boolean canGZIPEncode() { return rconf.isGZIPAllowed() && can_gzip; } // end canGZIPEncode public String getSiteImageTag(int hspace, int vspace) { return rconf.getSiteImageTag(hspace,vspace); } // end getSiteImageTag public String getFullServletPath(String name) { return rconf.getFullServletPath(name); } // end getFullServletPath public String getEncodedServletPath(String name) { return response.encodeURL(rconf.getFullServletPath(name)); } // end getEncodedServletPath public String getFullImagePath(String name) { return rconf.getFullImagePath(name); } // end getFullImagePath public String getFormatJSPPath(String name) { return "/format/" + name; } // end getFullFormatJSPPath public String getStdFontTag(String color, int size) { return rconf.getStdFontTag(color,size); } // end getStdFontTag public String getTitleTag(String specific) { return rconf.getTitleTag(specific); } // end getTitleTag public String getRequiredBullet() { return rconf.getRequiredBullet(); } // end getRequiredBullet public void writeFooter(Writer out) throws IOException { rconf.writeFooter(out); } // end writeFooter public void writeContentHeader(Writer out, String primary, String secondary) throws IOException { rconf.writeContentHeader(out,primary,secondary); } // end writeContentHeader public void writeContentSelectorHeader(Writer out, String caption, List choices, List urls, int selected) throws IOException { int nchoice = choices.size(); if (urls.size()" + StringUtil.encodeHTML(caption) + "  " + rconf.getStdFontTag("#3333AA",3)); for (int i=0; i"); else out.write(""); out.write(StringUtil.encodeHTML((String)(choices.get(i)))); if (i==selected) out.write(""); else out.write(""); out.write(" "); } // end for out.write("]
\n"); } // end writeContentSelectorHeader public String formatDateForDisplay(Date date) { DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM,my_locale); fmt.setTimeZone(my_timezone); return fmt.format(date); } // end formatDateForDisplay public Calendar createCalendar() { return new GregorianCalendar(my_timezone,my_locale); } // end createCalendar public Calendar createCalendar(Date date) { Calendar rc = new GregorianCalendar(my_timezone,my_locale); rc.setTime(date); return rc; } // end createCalendar public void flushOutput() throws IOException { response.flushBuffer(); } // end flushOutput public String encodeURL(String url) { return response.encodeURL(url); } // end encodeURL public void storeBaseJSPData(BaseJSPData data) { data.store(request); } // end storeBaseJSPData public void storeJSPRender(JSPRender jr) { jr.store(request); } // end storeJSPRender public void forwardDispatch(RequestDispatcher dispatcher) throws IOException, ServletException { dispatcher.forward(request,response); } // end forwardDispatch public void includeDispatch(RequestDispatcher dispatcher) throws IOException, ServletException { dispatcher.include(request,response); } // end forwardDispatch public void redirectTo(String servlet) throws IOException { String url = response.encodeRedirectURL(rconf.getFullServletPath(servlet)); response.sendRedirect(url); } // end redirectTo public String getActivityString(Date date) { if (date==null) return "Never"; // safeguard Calendar c_last = createCalendar(date); Calendar c_now = createCalendar(new Date()); int delta_days = 0; while ( (c_last.get(Calendar.YEAR)!=c_now.get(Calendar.YEAR)) || (c_last.get(Calendar.DAY_OF_YEAR)!=c_now.get(Calendar.DAY_OF_YEAR))) { // advance until we're pointing at the same year and the same day of the year delta_days++; c_last.add(Calendar.DAY_OF_YEAR,1); } // end while switch (delta_days) { // now return a string based on the difference in days case 0: return "Today"; case 1: return "Yesterday"; default: return String.valueOf(delta_days) + " days ago"; } // end switch } // end getActivityString public void nullResponse() { response.setStatus(response.SC_NO_CONTENT); } // end nullResponse public void sendBinaryData(String type, String filename, int length, InputStream data) throws IOException { response.setContentType(type); response.setContentLength(length); if (filename!=null) // make sure we pass the filename along, too response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\";"); // Copy the contents of the "data" stream to the output. ServletOutputStream stm = response.getOutputStream(); byte[] buffer = new byte[4096]; int rd = data.read(buffer); while (rd>=0) { // simple read-write loop to shove data out the door if (rd>0) stm.write(buffer,0,rd); rd = data.read(buffer); } // end while } // end sendBinaryData } // end class RenderData