KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > barracudaDiscRack > presentation > BasePO


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: BasePO.java,v 1.4 2004/12/03 14:12:34 slobodan Exp $
22  */

23
24 package barracudaDiscRack.presentation;
25
26 import com.lutris.appserver.server.httpPresentation.*;
27 import com.lutris.appserver.server.Enhydra;
28 import org.enhydra.xml.xmlc.*;
29 import org.enhydra.xml.xmlc.html.*;
30 import com.lutris.logging.*;
31 import com.lutris.util.KeywordValueException;
32 import com.lutris.appserver.server.user.User;
33
34 import org.enhydra.xml.xmlc.XMLObject;
35 import org.w3c.dom.*;
36 import org.w3c.dom.html.HTMLElement;
37
38 import java.lang.reflect.*;
39 import java.util.*;
40 import java.lang.Throwable JavaDoc;
41
42 import barracudaDiscRack.*;
43 import barracudaDiscRack.business.person.Person;
44
45 /**
46  * This is the parent Presentaion object. All presentation objects
47  * should extend this class.
48  *
49  * The run method looks for an event parameter and then calls
50  * handle<EventName>. If the "event" Parameter is not defined then
51  * the handleDefault() method is called in your child class.
52  *
53  * @author
54  * @version
55  */

56 public abstract class BasePO implements HttpPresentation {
57
58         public static String JavaDoc DISC_CATALOG_PAGE = "discMgmt/DiscCatalog.po";
59         public static String JavaDoc DISC_EVENT_CATALOG_PAGE = "../discMgmt/DiscCatalog.po";
60
61     protected static final String JavaDoc USER_KEY = "DiscRackPerson";
62     protected static String JavaDoc LOGIN_PAGE = "personMgmt/Login.po";
63     private static String JavaDoc EVENT = "event";
64     private static String JavaDoc STANDARD_METHOD_PREFIX = "handle";
65
66     /**
67      * This is the procedure that is called if there is no "event"
68      * HTTP parameter found. It must be overriden by the subclass to
69      * do default processing or error checking/handling.
70      *
71      * @return String The String representation of the HTML or (other format)
72      * of the document to be displayed. This method would need to be changed
73      * if you wanted to return binary data as well. It returns a String
74      * for simplicity right now.
75      */

76     public abstract XMLObject handleDefault() throws HttpPresentationException;
77
78     /**
79      * This method should be implemented in the subclass so that it returns
80      * true if this particular request requires the user to be logged
81      * in, otherwise false.
82      */

83     protected abstract boolean loggedInUserRequired();
84
85     /**
86      * Saved input and output context, and session data
87      */

88     protected HttpPresentationComms myComms = null;
89     protected DiscRackSessionData mySessionData = null;
90
91     /**
92      * Reference to the person logged in to the session
93      */

94     protected Person myPerson = null;
95
96     /**
97      * Gets HttpPresentation object
98      *
99      * @return The saved comms objects
100      * to whichever subclass needs it
101      */

102     public HttpPresentationComms getComms() {
103         return this.myComms;
104     }
105
106     /**
107      * Gets the session data
108      *
109      * @return session data
110      */

111     public DiscRackSessionData getSessionData() {
112         return this.mySessionData;
113     }
114
115     /**
116      * Sets the user into the session
117      *
118      * @param thePerson, the person to be set in the session
119      * @exception DiscRackPresentationException
120      */

121     public void setUser(Person thePerson)
122         throws DiscRackPresentationException {
123         this.getSessionData().setUser(thePerson);
124     }
125
126     /**
127      * Gets the user from the session
128      *
129      * @return the person object in the session
130      */

131     public Person getUser() {
132         return this.getSessionData().getUser();
133     }
134
135     /**
136      * Method to remove the current user from the session
137      */

138     public void removeUserFromSession() {
139         this.getSessionData().removeUser();
140     }
141
142     /**
143      * This implements the run method in HttpPresentation.
144      *
145      * @param HttpPresentationComms
146      * @exception Exception
147      */

148     public void run(HttpPresentationComms comms)
149         throws Exception JavaDoc {
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 JavaDoc obj = comms.sessionData.get(DiscRackSessionData.SESSION_KEY);
173             // If we found the session data, save it in a private data member
174
if(null != obj) {
175                 this.mySessionData = (DiscRackSessionData)obj;
176             } else {
177                 // If no session data was found, create a new session data instance
178
this.mySessionData = new DiscRackSessionData();
179                 comms.sessionData.set(DiscRackSessionData.SESSION_KEY, this.mySessionData);
180             }
181         } catch(KeywordValueException ex) {
182             writeDebugMsg("Problem getting session data from session: " +
183                           ex.getMessage());
184         }
185     }
186
187     /**
188      * Checks the session data for a User, if not there then redirects to the login page
189      */

190     protected void checkForUserLogin()
191         throws ClientPageRedirectException, DiscRackPresentationException {
192
193         try {
194             Person user = getUser();
195
196             if (null == user) {
197                 writeDebugMsg("USER NOT FOUND IN SESSION");
198                 //send to LoginPage if a logged in user is required.
199
String JavaDoc requestedPO = myComms.request.getRequestURI();
200                 this.writeDebugMsg("PO: "+ requestedPO);
201                 // Call the subclass's implemented method
202
writeDebugMsg("REDIRECTING TO LOGIN PAGE");
203                 throw new ClientPageRedirectException(getComms().request.getApplicationPath()+LOGIN_PAGE);
204             } else {
205                 writeDebugMsg("USER ALREADY LOGGED IN WITH A SESSION");
206             }
207         } catch (Exception JavaDoc ex) {
208             throw new DiscRackPresentationException("Trouble checking for user login status", ex);
209         }
210     }
211
212     /**
213      * Method to call the proper method for the incoming event
214      *
215      * @param HttpPresentationComms
216      * @exception Exception
217      */

218     public void handleEvent(HttpPresentationComms comms)
219         throws Exception JavaDoc {
220         String JavaDoc event = comms.request.getParameter(EVENT);
221
222     XMLObject returnHTML = null;
223
224         if (event == null || event.length() == 0) {
225             returnHTML = handleDefault();
226         }
227         else {
228             returnHTML = getPageContentForEvent(event);
229         }
230
231         comms.response.writeDOM(returnHTML);
232     }
233
234     /**
235      * If an event parameter is defined then this invokes the method that
236      * handles that event.
237      *
238      * @param event, the incoming event name
239      * @exception Exception
240      */

241     public XMLObject getPageContentForEvent(String JavaDoc event)
242         throws Exception JavaDoc {
243         try {
244             Method method = this.getClass().getMethod(toMethodName(event), null);
245             XMLObject thePage = (XMLObject)method.invoke(this, null);
246             return thePage;
247
248         } catch(InvocationTargetException ex) {
249             // Rethrow the originating exception if as it should be propagated as is
250
// It could be a page redirect exception, etc.
251
if (ex.getTargetException() instanceof Exception JavaDoc) {
252                 throw (Exception JavaDoc)ex.getTargetException();
253             } else if (ex.getTargetException() instanceof Error JavaDoc) {
254                 throw (Error JavaDoc)ex.getTargetException();
255             } else {
256                 throw ex;
257             }
258         } catch(NoSuchMethodException JavaDoc ex) {
259             //The method to handle the event does not exist.
260
throw new DiscRackPresentationException("NO EVENT HANDLER FOUND FOR EVENT: " +
261                                                     event, ex);
262         } catch(IllegalAccessException JavaDoc ex) {
263             //The method to handle the event does not exist.
264
throw new DiscRackPresentationException("ILLEGAL ACCESS TO EVENT HANDLER (is it public?): " +
265                                                     event, ex);
266         }
267     }
268
269     /**
270      * This sets the first letter of the event parameter value in order
271      * to adhere to Java method naming conventions.
272      *
273      * @param String event the incoming name of the event
274      * @return String the properly capitalized name
275      */

276     private String JavaDoc toMethodName(String JavaDoc event) {
277         StringBuffer JavaDoc methodName = new StringBuffer JavaDoc(STANDARD_METHOD_PREFIX);
278         methodName.append(Character.toUpperCase(event.charAt(0)));
279
280         if (event.length() > 1) {
281             methodName.append(event.substring(1));
282         }
283
284         return methodName.toString();
285     }
286
287     /**
288      * Returns the application object associated with the
289      * current request.
290      *
291      * @return the application object.
292      */

293     public BarracudaDiscRackEnhydra getApplication() {
294         return (BarracudaDiscRackEnhydra)Enhydra.getApplication();
295     }
296
297     /**
298      * Method to write a debugging message to the debug log
299      * channel when the DEBUG flag is turned on
300      *
301      * @param msg The message to write to the DEBUG log channel
302      */

303     public static void writeDebugMsg(String JavaDoc msg) {
304         Enhydra.getLogChannel().write(Logger.DEBUG,msg);
305     }
306 }
307
Popular Tags