/* * File : RequestProcessor.java * Created : 23-jan-2003 10:31 * By : fbusquets * * JClic - Authoring and playing system for educational activities * * Copyright (C) 2000 - 2005 Francesc Busquets & Departament * d'Educacio de la Generalitat de Catalunya * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details (see the LICENSE file). */ package edu.xtec.servlet; import java.util.Vector; import java.util.Iterator; import java.util.StringTokenizer; import java.util.Date; import java.text.SimpleDateFormat; import java.util.Locale; import java.util.TimeZone; import java.io.InputStream; import java.util.HashMap; import java.util.Map; /** * * @author Francesc Busquets (fbusquets@xtec.net) */ public abstract class RequestProcessor { public static final String REDIRECT="REDIRECT", CONTENT_TYPE="CONTENT_TYPE", CONTENT_LENGTH="CONTENT_LENGTH", COOKIE="COOKIE", EXTRA="EXTRA", ERROR="ERROR"; public static final int HTTP_NOT_FOUND=404, HTTP_BAD_REQUEST=400, HTTP_UNAUTHORIZED=401; public static final String CHARSET="ISO-8859-1"; private java.util.HashMap params; private java.util.HashMap cookies; private java.util.HashMap headers; private InputStream inputStream; public int errCode; public String errMsg; private static boolean DIRECT_RESOURCES; protected static HashMap properties=new HashMap(); /** Creates a new instance of RequestProcessor */ public RequestProcessor() { params=new java.util.HashMap(); cookies=new java.util.HashMap(); headers=new java.util.HashMap(); inputStream=null; errCode=-1; errMsg=null; } public static void config(Map prop){ if(prop!=null) properties.putAll(prop); } public boolean init() throws Exception{ return true; } public boolean usesWriter(){ return true; } public boolean noCache(){ return true; } public void process(java.io.OutputStream out) throws Exception{ }; public void process(java.io.PrintWriter out) throws Exception{ startHead(out); head(out); endHead(out); startBody(out); body(out); endBody(out); }; public void header(Vector v){ if(cookies.size()>0){ Iterator it=cookies.keySet().iterator(); while(it.hasNext()){ String key=(String)it.next(); String value=(String)cookies.get(key); v.add(new String[]{COOKIE, key, value}); } } } public void end(){ } public boolean wantsInputStream(){ return false; } public InputStream getInputStream(){ return inputStream; } public void setInputStream(InputStream is){ inputStream=is; } public int getIntParam(String name, int defaultValue){ int result=defaultValue; String s=getParam(name); if(s!=null){ try{ result=Integer.parseInt(s); } catch(NumberFormatException ex){ } } return result; } public Date getDateParam(String name, Date defaultValue, boolean atMidnight){ Date result=defaultValue; String s=getParam(name); if(s!=null){ try{ result=edu.xtec.jclic.report.ReportUtils.strToDate(s, atMidnight); } catch(Exception ex){ } } return result; } public String getParamNotNull(String name){ String result=getParam(name, 0); if(result==null) result=""; return result.trim(); } public String getParam(String name){ return getParam(name, 0); } public Object[] getParams(String name){ return (Object[])params.get(name); } public String getParam(String name, int index){ String result=null; Object[] array=(Object[])params.get(name); if(index>=0 && array!=null && array.length>index) result=(String)array[index]; return result; } public void setParam(String name, String value){ params.put(name, new String[]{value}); } public void setParam(String name, String[] value){ params.put(name, value); } public void setParams(java.util.Map map){ //params.putAll(map); if(map!=null){ java.util.Set s=map.keySet(); Iterator it=s.iterator(); while (it.hasNext()){ Object o=it.next(); Object val=map.get(o); if(o!=null){ if (val instanceof String) setParam(o.toString(),(String)val); else if(val instanceof String[]) setParam(o.toString(),(String[])val); } } } } public String getCookie(String name){ return (String)cookies.get(name); } public void setCookie(String name, String value){ cookies.put(name, value); } public void setCookies(java.util.Map map){ cookies.putAll(map); } public void setHeaders(java.util.Map map){ headers.putAll(map); } public java.util.Vector getHeaders(String headerName){ return (java.util.Vector)headers.get(headerName.toLowerCase()); } public String getHeader(String headerName){ String result=null; java.util.Vector v=getHeaders(headerName); if(v!=null && !v.isEmpty()) result=(String)v.get(0); return result; } protected void startHead(java.io.PrintWriter out){ out.println(""); out.println("
"); out.print(""); } protected void head(java.io.PrintWriter out) throws Exception{ }; protected void endHead(java.io.PrintWriter out){ out.println(""); } protected void linkStyle(String fileScreen, String filePrint, java.io.PrintWriter out){ StringBuffer sb=new StringBuffer(); sb.append("\n"); sb.append(""); } else sb.append("\">"); out.println(sb.substring(0)); } protected void linkScript(String file, java.io.PrintWriter out){ out.print(""); } protected void writeScript(String text, java.io.PrintWriter out){ out.println(""); } protected void title(String prefix, String title, java.io.PrintWriter out){ StringBuffer sb=new StringBuffer(200); sb.append("