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