001: package com.salmonllc.examples;
002:
003: //The Salmon Open Framework for Internet Applications (SOFIA)
004: //Copyright (C) 1999 - 2002, Salmon LLC
005: //
006: //This program is free software; you can redistribute it and/or
007: //modify it under the terms of the GNU General Public License version 2
008: //as published by the Free Software Foundation;
009: //
010: //This program is distributed in the hope that it will be useful,
011: //but WITHOUT ANY WARRANTY; without even the implied warranty of
012: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: //GNU General Public License for more details.
014: //
015: //You should have received a copy of the GNU General Public License
016: //along with this program; if not, write to the Free Software
017: //Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: //
019: //For more information please visit http://www.salmonllc.com
020:
021: import com.salmonllc.examples.SessionManager;
022: import com.salmonllc.html.events.*;
023: import com.salmonllc.jsp.JspController;
024: import com.salmonllc.jsp.JspLink;
025: import com.salmonllc.util.MessageLog;
026: import com.salmonllc.sql.DBConnection;
027:
028: import javax.servlet.http.Cookie;
029:
030: /**
031: This controller is used as the ancestor for the other controllers in the example app.
032: Generally, every controller will extend from this one, but the examples all extend JspController in order to keep them encapsulated.
033: This controller has more functionality than is requied by the sample application so it can be copied and used as a starting point for your application's base controller.
034: It implements frequently used interfaces at the base level to avoid having to implement all the methods in each controller.
035: Also, it provides some basic functionality such as checking if the session or page is required and redirecting the user to the appropriate page.
036: */
037: public class BaseController extends JspController implements
038: SubmitListener, PageListener {
039:
040: public com.salmonllc.jsp.JspContainer _noCache;
041:
042: private boolean _redirected = true;
043: private boolean _checkSessionExpired = true;
044: private boolean _checkPageExpired = false;
045: private boolean _checkDB = true;
046:
047: /**
048: * This method tries to get the string parameter passed into this function from the URL. It then checks to see if that "name" parameter is of boolean type. The default is FALSE.
049: * @param name - name of the parameter that is being looked up
050: */
051:
052: public boolean getBooleanParameter(String name) {
053: String s = getParameter(name);
054: return s != null
055: && (s.equals("true") || s.equals("TRUE") || s
056: .equals("1"));
057: }
058:
059: /**
060: * Return the text of the request referer, if available, else null.
061: * @param key - name of the cookie
062: * @return java.lang.String
063: */
064: public Cookie getCookie(String key) {
065: Cookie ret = null;
066:
067: Cookie cookArr[] = getCurrentRequest().getCookies();
068: if (cookArr != null) {
069: for (int i = 0; i < cookArr.length; i++) {
070: if (cookArr[i].getName().equals(key)) {
071: ret = cookArr[i];
072: }
073: break;
074: }
075: }
076: return ret;
077: }
078:
079: /**
080: * This method tries to get the integer value from the parameter in the URL called passed into this method. If the value of the parameter is not an integer, a -1 value will be returned.
081: * @param name - name of the parameter that is being looked up
082: * @return int - returns the value of the parameter as an int, if the value can NOT be cast to an int -1 is returned
083: */
084:
085: public int getIntParameter(String name) {
086: String s = getParameter(name);
087: try {
088: return Integer.parseInt(s);
089: } catch (Exception e) {
090: return -1;
091: }
092: }
093:
094: /**
095: * Gets the SessionManager for this application.
096: */
097: public SessionManager getSessionManager() {
098: return new SessionManager(getSession(), getApplicationName());
099: }
100:
101: /**
102: * This method
103: * - creates a SessionManager a page Listener
104: * - adds a page Listener
105: * - adds a page Listener
106: * @throws Exception if anything goes wrong
107: */
108: public void initialize() throws Exception {
109: //make this controller listen for page events
110: addPageListener(this );
111: //check if there is a return to link in the page. If there is, set the url
112: JspLink l = (JspLink) getComponent("retToHome");
113: if (l != null)
114: l.setHref("HomePage.jsp");
115: }
116:
117: /**
118: * This method/event will get fired each time a page is requested by the browser before Html is generated and sent back.
119: * @param p PageEvent
120: * @throws Exception if anything goes wrong
121: */
122: public void pageRequested(PageEvent p) throws Exception {
123: checkPageRedirect();
124: if (hasPageRedirected())
125: p.setContinueProcessing(false);
126: }
127:
128: /**
129: * This method/event will get fired each time a page is requested by the browser after Html is generated and sent back.
130: * @param p PageEvent
131: * @throws Exception if anything goes wrong
132: */
133: public void pageRequestEnd(PageEvent p) throws Exception {
134:
135: }
136:
137: /**
138: * This method occurs each time a page is submitted before submit values are copied to components.
139: * @param p PageEvent
140: */
141: public void pageSubmitted(PageEvent p) {
142: checkPageRedirect();
143: if (hasPageRedirected())
144: p.setContinueProcessing(false);
145: }
146:
147: /**
148: * This method occurs each time a page is submitted after submit values are copied to components.
149: * @param p PageEvent
150: */
151: public void pageSubmitEnd(PageEvent p) {
152:
153: }
154:
155: /**
156: * This method is used to signal that a submit button has been pressed, and that the user has intended
157: * to submit the descendant class/page.
158: * @param e SubmitEvent
159: * @throws Exception if anything goes wrong
160: * @return true to continue processing events on the page or false to abort
161: */
162:
163: public boolean submitPerformed(SubmitEvent e) throws Exception {
164: return true;
165: }
166:
167: /**
168: * This method will check if the page is expired or the session is expired and redirect to the appropriate page
169: */
170: private void checkPageRedirect() {
171:
172: try {
173: _redirected = false;
174: if (_checkSessionExpired)
175: if (isSessionExpired()) {
176: _redirected = true;
177: gotoSiteMapPage(SiteMapConstants.SESSION_EXPIRED);
178: return;
179: }
180:
181: if (_checkPageExpired)
182: if (isExpired()) {
183: _redirected = true;
184: gotoSiteMapPage(SiteMapConstants.PAGE_EXPIRED);
185: return;
186: }
187:
188: if (_checkDB) {
189: DBConnection conn = null;
190: try {
191: conn = DBConnection
192: .getConnection(getApplicationName());
193: } catch (Exception e) {
194: _redirected = true;
195: gotoSiteMapPage(SiteMapConstants.DB_CONNECT_ERROR);
196: } finally {
197: if (conn != null)
198: conn.freeConnection();
199: }
200: }
201:
202: } catch (Exception e) {
203: MessageLog
204: .writeErrorMessage("checkPageRedirect()", e, this );
205: }
206: }
207:
208: /**
209: * Returns true if either the page or the session is expired and the page has been redirected to another page indicating that
210: */
211: public boolean hasPageRedirected() {
212: return _redirected;
213: }
214:
215: /**
216: * Tells the page whether or not it should check whether the page is expired on each request
217: */
218: public void setCheckPageExpired(boolean check) {
219: _checkPageExpired = check;
220: }
221:
222: /**
223: * Tells the page whether or not it should check whether the session is expired on each request
224: */
225: public void setCheckSessionExpired(boolean check) {
226: _checkSessionExpired = check;
227: }
228:
229: /**
230: * If you want to have the browser cache this page instead of reloading it each time call this method with a true argument.
231: */
232: public void setNoCache(boolean noCache) {
233: _noCache.setVisible(noCache);
234: }
235:
236: }
|