001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: BasePO.java,v 1.1 2006-09-11 12:32:06 sinisa Exp $
022: */
023:
024: package discRack.presentation;
025:
026: import com.lutris.appserver.server.httpPresentation.*;
027: import com.lutris.appserver.server.Enhydra; //import com.lutris.xml.xmlc.*;
028: //import com.lutris.xml.xmlc.html.*;
029: import com.lutris.logging.*;
030: import com.lutris.util.KeywordValueException;
031: import com.lutris.appserver.server.user.User;
032:
033: import org.enhydra.xml.xmlc.XMLObject;
034: import org.w3c.dom.*;
035: import org.w3c.dom.html.HTMLElement;
036:
037: import java.lang.reflect.*;
038: import java.util.*;
039: import java.lang.Throwable;
040:
041: import discRack.spec.*;
042: import discRack.DiscRack;
043:
044: /**
045: * This is the parent Presentaion object. All presentation objects
046: * should extend this class.
047: *
048: * The run method looks for an event parameter and then calls
049: * handle<EventName>. If the "event" Parameter is not defined then
050: * the handleDefault() method is called in your child class.
051: *
052: *
053: */
054: public abstract class BasePO implements HttpPresentation {
055: protected static final String USER_KEY = "DiscRackPerson";
056: protected static String LOGIN_PAGE = "personMgmt/Login.po";
057: protected static String DISC_CATALOG_PAGE = "discMgmt/DiscCatalog.po";
058: private static String EVENT = "event";
059: private static String STANDARD_METHOD_PREFIX = "handle";
060:
061: /**
062: * This is the procedure that is called if there is no "event"
063: * HTTP parameter found. It must be overriden by the subclass to
064: * do default processing or error checking/handling.
065: *
066: * @return String The String representation of the HTML or (other format)
067: * of the document to be displayed. This method would need to be changed
068: * if you wanted to return binary data as well. It returns a String
069: * for simplicity right now.
070: */
071: public abstract XMLObject handleDefault()
072: throws HttpPresentationException;
073:
074: /**
075: * This method should be implemented in the subclass so that it returns
076: * true if this particular request requires the user to be logged
077: * in, otherwise false.
078: */
079: protected abstract boolean loggedInUserRequired();
080:
081: /**
082: * Saved input and output context, and session data
083: */
084: protected HttpPresentationComms myComms = null;
085: protected DiscRackSessionData mySessionData = null;
086:
087: /**
088: * Reference to the person logged in to the session
089: */
090: protected Person myPerson = null;
091:
092: /**
093: * Gets HttpPresentation object
094: *
095: * @return The saved comms objects
096: * to whichever subclass needs it
097: */
098: public HttpPresentationComms getComms() {
099: return this .myComms;
100: }
101:
102: /**
103: * Gets the session data
104: *
105: * @return session data
106: */
107: public DiscRackSessionData getSessionData() {
108: return this .mySessionData;
109: }
110:
111: /**
112: * Sets the user into the session
113: *
114: * @param thePerson the person to be set in the session
115: * @exception DiscRackPresentationException
116: */
117: public void setUser(Person thePerson)
118: throws DiscRackPresentationException {
119: this .getSessionData().setUser(thePerson);
120: }
121:
122: /**
123: * Gets the user from the session
124: *
125: * @return the person object in the session
126: */
127: public Person getUser() {
128: return this .getSessionData().getUser();
129: }
130:
131: /**
132: * Method to remove the current user from the session
133: */
134: public void removeUserFromSession() {
135: this .getSessionData().removeUser();
136: }
137:
138: /**
139: * This implements the run method in HttpPresentation.
140: *
141: * @param comms HttpPresentationComms
142: * @exception Exception
143: */
144: public void run(HttpPresentationComms comms) throws Exception {
145: // Initialize new or get the existing session data
146: initSessionData(comms);
147: // Check if the user needs to be logged in for this request.
148: if (this .loggedInUserRequired()) {
149: checkForUserLogin();
150:
151: }
152: // Handle the incoming event request
153: handleEvent(comms);
154: }
155:
156: /**
157: * Method to get or create the AgSessionData object from the user session
158: * This object is saved in the EbrokerPresentation object
159: *
160: * @param comms HttpPresentationComms
161: * @exception Exception
162: */
163: protected void initSessionData(HttpPresentationComms comms)
164: throws DiscRackPresentationException {
165: this .myComms = comms;
166:
167: try {
168: Object obj = comms.sessionData
169: .get(DiscRackSessionData.SESSION_KEY);
170: // If we found the session data, save it in a private data member
171: if (null != obj) {
172: this .mySessionData = (DiscRackSessionData) obj;
173: } else {
174: // If no session data was found, create a new session data instance
175: this .mySessionData = new DiscRackSessionData();
176: comms.sessionData.set(DiscRackSessionData.SESSION_KEY,
177: this .mySessionData);
178: }
179: } catch (KeywordValueException ex) {
180: writeDebugMsg("Problem getting session data from session: "
181: + ex.getMessage());
182: }
183: }
184:
185: /**
186: * Checks the session data for a User, if not there then redirects to the login page
187: */
188: protected void checkForUserLogin()
189: throws ClientPageRedirectException,
190: DiscRackPresentationException {
191:
192: try {
193: Person user = getUser();
194:
195: if (null == user) {
196: //We need to allow discRack_pres to be functional , so if the requested url is /discRack_pres/.. user dont need to be logged
197: String uri = myComms.request.getRequestURI();
198: boolean is = uri.startsWith("/discRack_pres");
199:
200: if (!is) {
201:
202: writeDebugMsg("USER NOT FOUND IN SESSION");
203: //send to LoginPage if a logged in user is required.
204: String requestedPO = myComms.request
205: .getRequestURI();
206: this .writeDebugMsg("PO: " + requestedPO);
207: // Call the subclass's implemented method
208: writeDebugMsg("REDIRECTING TO LOGIN PAGE");
209: throw new ClientPageRedirectException(
210: getComms().request.getApplicationPath()
211: + LOGIN_PAGE);
212: }
213: } else {
214: writeDebugMsg("USER ALREADY LOGGED IN WITH A SESSION");
215: }
216:
217: } catch (Exception ex) {
218: throw new DiscRackPresentationException(
219: "Trouble checking for user login status", ex);
220: }
221: }
222:
223: /**
224: * Method to call the proper method for the incoming event
225: *
226: * @param comms HttpPresentationComms
227: * @exception Exception
228: */
229: public void handleEvent(HttpPresentationComms comms)
230: throws Exception {
231: String event = comms.request.getParameter(EVENT);
232:
233: XMLObject returnHTML = null;
234:
235: if (event == null || event.length() == 0) {
236: returnHTML = handleDefault();
237: } else {
238: returnHTML = getPageContentForEvent(event);
239: }
240: //comms.response.setEncoding("UTF8");
241: comms.response.writeDOM(returnHTML);
242: }
243:
244: /**
245: * If an event parameter is defined then this invokes the method that
246: * handles that event.
247: *
248: * @param event the incoming event name
249: * @exception Exception
250: */
251: public XMLObject getPageContentForEvent(String event)
252: throws Exception {
253: try {
254: Method method = this .getClass().getMethod(
255: toMethodName(event), null);
256: XMLObject thePage = (XMLObject) method.invoke(this , null);
257: return thePage;
258:
259: } catch (InvocationTargetException ex) {
260: // Rethrow the originating exception if as it should be propagated as is
261: // It could be a page redirect exception, etc.
262: if (ex.getTargetException() instanceof Exception) {
263: throw (Exception) ex.getTargetException();
264: } else if (ex.getTargetException() instanceof Error) {
265: throw (Error) ex.getTargetException();
266: } else {
267: throw ex;
268: }
269: } catch (NoSuchMethodException ex) {
270: //The method to handle the event does not exist.
271: throw new DiscRackPresentationException(
272: "NO EVENT HANDLER FOUND FOR EVENT: " + event, ex);
273: } catch (IllegalAccessException ex) {
274: //The method to handle the event does not exist.
275: throw new DiscRackPresentationException(
276: "ILLEGAL ACCESS TO EVENT HANDLER (is it public?): "
277: + event, ex);
278: }
279: }
280:
281: /**
282: * This sets the first letter of the event parameter value in order
283: * to adhere to Java method naming conventions.
284: *
285: * @param event the incoming name of the event
286: * @return String the properly capitalized name
287: */
288: private String toMethodName(String event) {
289: StringBuffer methodName = new StringBuffer(
290: STANDARD_METHOD_PREFIX);
291: methodName.append(Character.toUpperCase(event.charAt(0)));
292:
293: if (event.length() > 1) {
294: methodName.append(event.substring(1));
295: }
296:
297: return methodName.toString();
298: }
299:
300: /**
301: * Returns the application object associated with the
302: * current request.
303: *
304: * @return the application object.
305: */
306: public DiscRack getApplication() {
307: return (DiscRack) Enhydra.getApplication();
308: }
309:
310: /**
311: * Method to write a debugging message to the debug log
312: * channel when the DEBUG flag is turned on
313: *
314: * @param msg The message to write to the DEBUG log channel
315: */
316: public static void writeDebugMsg(String msg) {
317: Enhydra.getLogChannel().write(Logger.DEBUG, msg);
318: }
319: }
|