Source code: gsoft/xervlet/BaseDBXervlet.java
1 /*************************************************************************
2 Copyright (C) 2003 Steve Gee
3 stevesgee@cox.net
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *************************************************************************/
19
20 package gsoft.xervlet;
21
22 import java.sql.*;
23 import javax.servlet.http.*;
24 import java.io.*;
25 import java.util.*;
26
27 import gsoft.sql.*;
28
29
30 public abstract class BaseDBXervlet extends HttpServlet {
31
32 public String INIFILE;
33 public ServletSettings iniHash;
34 public Hashtable iHash;
35 HttpServletResponse response;
36 TagReader tReader;
37 PrintWriter out;
38 private long INVALIDATE_TIME = 1800000;
39 private boolean enableLogging = false;
40 private HttpSession session;
41 Statement stmt;
42 Connection conn;
43 ResultSet rs;
44 long then = 0;
45 long timer = 0;
46
47 protected long getInvalidateTime() {
48 return INVALIDATE_TIME;
49 }//end getInvalidateTime
50
51 public abstract String getIniFileName();
52
53 public void construct() throws Exception {
54 try {
55 INIFILE = getInitParameter("inifile");
56
57 if (INIFILE == null) {
58 INIFILE = getIniFileName();
59 }//end if
60
61 tReader = new TagReader(INIFILE);
62 iHash = tReader.getTags("execute");
63
64 /*************************************************************************/
65 /****************** Load the database connection *************/
66 /*************************************************************************/
67 String SERVER,PORT,DATABASE,USER,PASSWORD;
68 String className = tReader.getTagValue("serversettings", "CLASS");
69 Class conObject = Class.forName(className.trim());
70 ConnectionInterface connection = (ConnectionInterface) conObject.newInstance();
71 SERVER = tReader.getTagValue("serversettings", "SERVER");
72 PORT = tReader.getTagValue("serversettings", "PORT");
73 DATABASE = tReader.getTagValue("serversettings", "DATABASE");
74 USER = tReader.getTagValue("serversettings", "USER");
75 PASSWORD = tReader.getTagValue("serversettings", "PASSWORD");
76 conn = connection.getConn(SERVER, PORT, DATABASE, USER, PASSWORD);
77 stmt = conn.createStatement();
78 if (stmt == null)
79 out.println("Statement is NULL");
80 } catch (Exception e) {
81 throw new Exception("There has been an error initializing: " + e);
82 }//end try-catch
83 }//end construct
84
85
86 public void service(HttpServletRequest req, HttpServletResponse res)
87 throws IOException {
88 out = res.getWriter();
89 try {
90
91 String sch = (req.getScheme()).toLowerCase();
92 session = req.getSession(true);
93 boolean useSecurePage = false;
94 boolean validPageSecurity = false;
95 if (session != null) {
96 // long then = session.getLastAccessedTime();
97
98 long now = new java.util.Date().getTime();
99 if (session.isNew()) {
100 timer = 0;
101 then = 0;
102 iniHash = new ServletSettings(session);
103 this.construct();
104 iniHash.setTags(tReader);
105 iniHash.setResponse(res);
106 iniHash.setRequest(req);
107 iniHash.setProjectName(tReader.getTagValue("serversettings", "PROJECTNAME"));
108 session.setAttribute("INIHASH", iniHash);
109
110 iniHash.setConnection(conn);
111 iniHash.setStatement(stmt);
112
113 ResultSet configSet = stmt.executeQuery("select "
114 + " projectid,https,logging,logfile,invalidate,maxoutputsize,outputdir"
115 + " from xervlet where projectname = '" + iniHash.getProjectName() + "'");
116 configSet.next();
117 useSecurePage = new Boolean(configSet.getString("https")).booleanValue();
118 enableLogging = new Boolean(configSet.getString("logging")).booleanValue();
119 INVALIDATE_TIME = new Double(configSet.getInt("invalidate")).intValue();
120 iniHash.setProjectID(configSet.getString("projectid"));
121 iniHash.setFileSize(configSet.getInt("maxoutputsize"));
122 iniHash.setFileDir(configSet.getString("outputdir"));
123
124 String logFile = configSet.getString("logfile");
125 configSet.close();
126
127 /*********************** LOAD MULTIPLE CONNECTIONS ************/
128 String SQL = "select url,username,password,server,port,dbase,statement"
129 + " FROM connection where projectid = " + iniHash.getProjectID();
130 try {
131 ResultSet cSet = stmt.executeQuery(SQL);
132 while (cSet.next()) {
133 Class conObject = Class.forName(cSet.getString("url"));
134 ConnectionInterface connection = (ConnectionInterface) conObject.newInstance();
135 iniHash.putAltStatement(cSet.getString("statement"), (connection.getConn(cSet.getString("server"), cSet.getString("port"), cSet.getString("dbase"), cSet.getString("username"), cSet.getString("password"))).createStatement());
136 }//end while
137 cSet.close();
138 } catch (Exception e) {
139 throw new Exception(SQL + " An Error has occured providing mutliple connections" + e.toString());
140 }//end try-catch
141 /**************************************************************/
142
143 if (useSecurePage) {
144 validPageSecurity = sch.equals("https");
145 } else {
146 validPageSecurity = true;
147 }//end if--securepage
148
149 iniHash.setUseSecure(validPageSecurity);
150
151 /*************************************************************************/
152 /*************************** Initialize Logging **************************/
153 /*************************************************************************/
154 try {
155 if (enableLogging) {
156 LogManager logger = new LogManager(logFile);
157 iniHash.setLogManager(logger);
158 }//end if
159 } catch (Exception ezero) {
160 res.setContentType("text/html");
161 throw new Exception("<br>Error Initializing Logging: " + ezero.toString());
162 }//end try-catch
163 } else {
164 iniHash = (ServletSettings) session.getAttribute("INIHASH");
165 }//end session-is New
166
167 /********** LOAD SESSION DATA ************/
168 if (then != 0)
169 timer += (now - then);
170
171 // if (timer > getInvalidateTime()) {
172 if (false) {
173 res.setContentType("text/html");
174 out.println("<font color=red><center><h3>The allocated session time has expired and your session has been invalidated</h3></center></font>");
175 session.invalidate();
176 } else {
177 then = new java.util.Date().getTime();
178
179 if (iniHash.getUseSecure()) {
180 //check for encoding type and call the proper service.
181
182 //This code was added to support websphere
183 //which for some unknown reason set the contenttype to null
184
185 String mpREQ = req.getContentType();
186 if (mpREQ == null)
187 mpREQ = "USENORMALSERVICE";
188
189 if (mpREQ.toLowerCase().startsWith("multipart/form-data"))
190 multipartService(req, res);
191 else
192 normalService(req, res);
193 } else {
194 session.invalidate();
195 res.setContentType("text/html");
196 out.println("<font color=red><h3><center>Warning -- This page is no longer secure</h3>"
197 + "<b>The page you are accessing is no longer secured and now poses a possible secury problem<br>"
198 + "All data related to this session has been deleted</b><p>"
199 + "If you wish to proceed please return to the Core Data Login Screen</center>");
200 }
201 }// end session timer
202
203 } else {
204 if (out == null) {
205 out = res.getWriter();
206 }
207 res.setContentType("text/html");
208 out.println("<font color=red><h3><center>Warning -- Your session has been expired</h3>"
209 + "<b>There are a number of reasons that may cause this to happen"
210 + "<br>1) The session time limit has expired"
211 + "<br>2) The has been a network error<b></center>");
212 }//end if-else
213
214 } catch (Exception e) {
215 res.setContentType("text/html");
216 out.println("<h3><font color=red>There has been an error initializing BaseDBXervlet<br>" + e.toString());
217 }
218
219 out.close();
220 }
221
222 protected void multipartService(HttpServletRequest req, HttpServletResponse res) throws IOException, Exception {
223 MultipartRequest multi = new MultipartRequest(req, iniHash.getFileDir(),
224 iniHash.getFileSize());
225
226 response = res;
227 String className = "";
228 /*************************************************************************/
229 /***************** Load the Keys and Parameters ******************/
230 /*************************************************************************/
231 Enumeration values = multi.getParameterNames();
232 Hashtable params = new Hashtable();
233 String key = null;
234 while (values.hasMoreElements()) {
235 key = (String) values.nextElement();
236 params.put(key, multi.getParameter(key));
237 if (key.startsWith("comm")) {
238 String cname = key.substring(4);
239 try {
240 rs = (iniHash.getStatement()).executeQuery("select class from classfiles where name = '" + cname + "' and projectid = " + iniHash.getProjectID());
241 rs.next();
242 className = rs.getString("class").trim();
243 rs.close();
244 } catch (Exception EXCPUSEDBSOURCE) {
245 res.setContentType("text/html");
246 out.println("<font color=red><h3><center>An error has occured looking up the Class for key [ " + cname + "]</center></h3><br>"); //<br>This session has been expired</center></h3>");
247 }//end try-catch for boolean serversettings
248 }
249 }//end while
250
251 session.setAttribute("PARAMS", params);
252 iniHash.setMultipartRequest(multi);
253 /***********************************************************************/
254 //************* Load the class and send it off ******************
255 try {
256 Class c = Class.forName(className);
257 ServletCommand s = (ServletCommand) c.newInstance();
258 s.execute(req, res);
259 //out.println("<br>Base Servlet Completed");
260 } catch (Exception e) {
261 res.setContentType("text/html");
262 out.println("<p><b><font color=red>Error Loading Class File::BaseServlet::service::" + e + "</font></b>");
263 out.println("<br>Error occured Reading command: " + key);
264 }//end try-catch
265 }//end multipart service
266
267
268 protected void normalService(HttpServletRequest req, HttpServletResponse res) throws IOException {
269
270 //res.setContentType("text/html");
271 //out.println("<br>Loading");
272
273 response = res;
274 String className = "";
275 /*************************************************************************/
276 /***************** Load the Keys and Parameters ******************/
277 /*************************************************************************/
278 Enumeration values = req.getParameterNames();
279 Hashtable params = new Hashtable();
280 String key = null;
281 while (values.hasMoreElements()) {
282 key = (String) values.nextElement();
283 if (key.startsWith("comm")) {
284 String cname = key.substring(4);
285 try {
286 rs = (iniHash.getStatement()).executeQuery("select class from classfiles where name = '" + cname + "' and projectid = " + iniHash.getProjectID());
287 rs.next();
288 className = rs.getString("class").trim();
289 rs.close();
290 } catch (Exception EXCPUSEDBSOURCE) {
291 res.setContentType("text/html");
292 out.println("<font color=red><h3><center>An error has occured looking up the Class for key [ " + cname + "]</center></h3><br>"); //<br>This session has been expired</center></h3>");
293 }//end try-catch for boolean serversettings
294 } else {
295 params.put(key, req.getParameterValues(key));
296 }//end if
297 }//end while
298
299 session.setAttribute("PARAMS", params);
300
301 /***********************************************************************/
302 //************* Load the class and send it off ******************
303 try {
304 Class c = Class.forName(className);
305 ServletCommand s = (ServletCommand) c.newInstance();
306 s.execute(req, res);
307 //out.println("<br>Base Servlet Completed");
308 } catch (Exception e) {
309 res.setContentType("text/html");
310 out.println("<p><b><font color=red>Error Loading Class File::BaseServlet::service::" + e + "</font></b>");
311 out.println("<br>Error occured Reading command: " + key);
312 }//end try-catch
313
314 }//end service
315
316 }