KickJava   Java API By Example, From Geeks To Geeks.

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


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): Niraj Tolia.
23  */

24
25 package edu.rice.rubbos.servlets;
26
27 import java.io.IOException JavaDoc;
28 import java.sql.Connection JavaDoc;
29 import java.sql.PreparedStatement JavaDoc;
30 import java.sql.ResultSet JavaDoc;
31
32 import javax.servlet.ServletException JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35
36 public class Author extends RubbosHttpServlet
37 {
38
39   public int getPoolSize()
40   {
41     return Config.BrowseCategoriesPoolSize;
42   }
43
44   private void closeConnection(PreparedStatement JavaDoc stmt, Connection JavaDoc conn)
45   {
46     try
47     {
48       if (stmt != null)
49         stmt.close(); // close statement
50
}
51     catch (Exception JavaDoc ignore)
52     {
53     }
54
55     try
56     {
57       if (conn != null)
58           releaseConnection(conn);
59     }
60     catch (Exception JavaDoc ignore)
61     {
62     }
63
64   }
65
66   /** Build the html page for the response */
67   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
68       throws IOException JavaDoc, ServletException JavaDoc
69   {
70     ServletPrinter sp = null;
71     Connection JavaDoc conn = null;
72     PreparedStatement JavaDoc stmt = null;
73
74     sp = new ServletPrinter(response, "Author");
75
76     conn = getConnection();
77
78     // int storyId =
79
// (Integer.valueOf(request.getParameter("storyId"))).intValue();
80

81     /*
82      * if (storyId == 0) { sp.printHTML( " <h3> You must provide a story
83      * identifier ! <br></h3> "); return; }
84      */

85
86     String JavaDoc nickname, password;
87     int userId = 0, access = 0;
88     ResultSet JavaDoc rs = null;
89
90     nickname = request.getParameter("nickname");
91     password = request.getParameter("password");
92
93     if (nickname == null)
94     {
95       sp.printHTML("Author: You must provide a nick name!<br>");
96       closeConnection(stmt, conn);
97       return;
98     }
99
100     if (password == null)
101     {
102       sp.printHTML("Author: You must provide a password!<br>");
103       closeConnection(stmt, conn);
104       return;
105     }
106
107     if ((nickname != null) && (password != null))
108     {
109
110       try
111       {
112         stmt = conn
113             .prepareStatement("SELECT id,access FROM users WHERE nickname=\""
114                 + nickname + "\" AND password=\"" + password + "\"");
115         rs = stmt.executeQuery();
116       }
117       catch (Exception JavaDoc e)
118       {
119         sp.printHTML(" Failed to execute Query for Author: " + e);
120         closeConnection(stmt, conn);
121         return;
122       }
123       try
124       {
125         if (rs.first())
126         {
127           userId = rs.getInt("id");
128           access = rs.getInt("access");
129         }
130       }
131       catch (Exception JavaDoc e)
132       {
133         sp.printHTML("Exception verifying author: " + e + "<br>");
134         closeConnection(stmt, conn);
135         /* To make sure there is no double free for the connection */
136         conn = null;
137         stmt = null;
138       }
139     }
140
141     closeConnection(stmt, conn);
142
143     if ((userId == 0) || (access == 0))
144     {
145       sp.printHTMLheader("RUBBoS: Author page");
146       sp
147           .printHTML("<p><center><h2>Sorry, but this feature is only accessible by users with an author access.</h2></center><p>\n");
148     }
149     else
150     {
151       sp.printHTMLheader("RUBBoS: Author page");
152       sp
153           .printHTML("<p><center><h2>Which administrative task do you want to do ?</h2></center>\n"
154               + "<p><p><a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.ReviewStories?authorId= \""
155               + userId + "\"\">Review submitted stories</a><br>\n");
156     }
157     sp.printHTMLfooter();
158
159   }
160
161   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
162       throws IOException JavaDoc, ServletException JavaDoc
163   {
164     doGet(request, response);
165   }
166
167 }
168
Popular Tags