KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > rubbos > servlets > ServletPrinter


1 /**
2  * RUBBoS: Rice University Bulletin Board System.
3  * Copyright (C) 2001-2004 Rice University and French National Institute For
4  * Research In Computer Science And Control (INRIA).
5  * Contact: jmob@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): ______________________.
23  */

24
25 package edu.rice.rubbos.servlets;
26
27 import java.io.FileReader JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30 import java.sql.Connection JavaDoc;
31 import java.sql.PreparedStatement JavaDoc;
32 import java.sql.ResultSet JavaDoc;
33 import java.util.GregorianCalendar JavaDoc;
34
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36
37 /**
38  * In fact, this class is not a servlet itself but it provides output services
39  * to servlets to send back HTML files.
40  *
41  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet </a> and <a
42  * HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite </a>
43  * @version 1.0
44  */

45
46 public class ServletPrinter
47 {
48   private PrintWriter JavaDoc out;
49   private String JavaDoc servletName;
50   private GregorianCalendar JavaDoc startDate;
51
52   public ServletPrinter(HttpServletResponse JavaDoc toWebServer,
53       String JavaDoc callingServletName)
54   {
55     startDate = new GregorianCalendar JavaDoc();
56     toWebServer.setContentType("text/html");
57     try
58     {
59       out = toWebServer.getWriter();
60     }
61     catch (IOException JavaDoc ioe)
62     {
63       ioe.printStackTrace();
64     }
65     servletName = callingServletName;
66   }
67
68   void printFile(String JavaDoc filename)
69   {
70     FileReader JavaDoc fis = null;
71     try
72     {
73       fis = new FileReader JavaDoc(filename);
74       char[] data = new char[4 * 1024]; // 4K buffer
75
int bytesRead;
76       bytesRead = fis.read(data);
77       while (/* (bytesRead = fis.read(data)) */bytesRead != -1)
78       {
79         out.write(data, 0, bytesRead);
80         bytesRead = fis.read(data);
81       }
82     }
83     catch (Exception JavaDoc e)
84     {
85       out.println("Unable to read file (exception: " + e + ")<br>");
86     }
87     finally
88     {
89       if (fis != null)
90         try
91         {
92           fis.close();
93         }
94         catch (Exception JavaDoc ex)
95         {
96           out.println("Unable to close the file reader (exception: " + ex
97               + ")<br>");
98         }
99     }
100   }
101
102   void printHTMLheader(String JavaDoc title)
103   {
104     printFile(Config.HTMLFilesPath + "/header.html");
105     out.println("<title>" + title + "</title>");
106   }
107
108   void printHTMLfooter()
109   {
110     GregorianCalendar JavaDoc endDate = new GregorianCalendar JavaDoc();
111
112     out
113         .println("<br><hr>RUBBoS (C) Rice University/INRIA<br><i>Page generated by "
114             + servletName
115             + " in "
116             + TimeManagement.diffTime(startDate, endDate) + "</i><br>");
117     out.println("</body>");
118     out.println("</html>");
119   }
120
121   void printHTML(String JavaDoc msg)
122   {
123     out.println(msg);
124   }
125
126   void printHTMLHighlighted(String JavaDoc msg)
127   {
128     out.println("<TABLE width=\"100%\" bgcolor=\"#CCCCFF\">");
129     out
130         .println("<TR><TD align=\"center\" width=\"100%\"><FONT size=\"4\" color=\"#000000\"><B>"
131             + msg + "</B></FONT></TD></TR>");
132     out.println("</TABLE><p>");
133   }
134
135   public String JavaDoc authenticate(String JavaDoc nickname, String JavaDoc password, Connection JavaDoc conn)
136   {
137     try
138     {
139       PreparedStatement JavaDoc stmt = conn
140           .prepareStatement("SELECT id FROM users WHERE nickname=\"" + nickname
141               + "\" AND password=\"" + password + "\"");
142       ResultSet JavaDoc rs = stmt.executeQuery();
143       rs.first();
144       if (!rs.first())
145         return "0"; // 0 is the anonymous user
146
return rs.getString("id");
147
148     }
149     catch (Exception JavaDoc e)
150     {
151       return e + "Authenticate function error";
152
153     }
154   }
155
156   public String JavaDoc getUserName(int UserId, Connection JavaDoc conn) throws Exception JavaDoc
157   {
158     try
159     {
160       PreparedStatement JavaDoc stmt = conn
161           .prepareStatement("SELECT nickname FROM users WHERE id=?");
162       stmt.setInt(1, UserId);
163       ResultSet JavaDoc rs = stmt.executeQuery();
164       rs.first();
165       return rs.getString("nickname");
166     }
167     catch (Exception JavaDoc e)
168     {
169       throw e;
170     }
171
172   }
173
174 }
Popular Tags