KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > discRack > 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.2 2004/09/20 13:27:59 zoran Exp $
22  */

23
24 package discRack.presentation;
25
26 import com.lutris.appserver.server.httpPresentation.*;
27 import com.lutris.appserver.server.Enhydra;
28 //import com.lutris.xml.xmlc.*;
29
//import com.lutris.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 discRack.spec.*;
43 import discRack.DiscRack;
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  *
54  */

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

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

79     protected abstract boolean loggedInUserRequired();
80
81     /**
82      * Saved input and output context, and session data
83      */

84     protected HttpPresentationComms myComms = null;
85     protected DiscRackSessionData mySessionData = null;
86
87     /**
88      * Reference to the person logged in to the session
89      */

90     protected Person myPerson = null;
91
92     /**
93      * Gets HttpPresentation object
94      *
95      * @return The saved comms objects
96      * to whichever subclass needs it
97      */

98     public HttpPresentationComms getComms() {
99         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)
145         throws Exception JavaDoc {
146         // Initialize new or get the existing session data
147
initSessionData(comms);
148         // Check if the user needs to be logged in for this request.
149
if(this.loggedInUserRequired()) {
150                 checkForUserLogin();
151
152         }
153         // Handle the incoming event request
154
handleEvent(comms);
155     }
156
157     /**
158      * Method to get or create the AgSessionData object from the user session
159      * This object is saved in the EbrokerPresentation object
160      *
161      * @param comms HttpPresentationComms
162      * @exception Exception
163      */

164     protected void initSessionData(HttpPresentationComms comms)
165         throws DiscRackPresentationException {
166         this.myComms = comms;
167
168         try {
169             Object JavaDoc obj = comms.sessionData.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, this.mySessionData);
177             }
178         } catch(KeywordValueException ex) {
179             writeDebugMsg("Problem getting session data from session: " +
180                           ex.getMessage());
181         }
182     }
183
184     /**
185      * Checks the session data for a User, if not there then redirects to the login page
186      */

187     protected void checkForUserLogin()
188         throws ClientPageRedirectException, DiscRackPresentationException {
189
190
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 JavaDoc uri = myComms.request.getRequestURI();
198                      boolean is= uri.startsWith("/discRack_pres");
199
200                if(!is)
201                   {
202
203                 writeDebugMsg("USER NOT FOUND IN SESSION");
204                 //send to LoginPage if a logged in user is required.
205
String JavaDoc requestedPO = myComms.request.getRequestURI();
206                 this.writeDebugMsg("PO: "+ requestedPO);
207                 // Call the subclass's implemented method
208
writeDebugMsg("REDIRECTING TO LOGIN PAGE");
209                 throw new ClientPageRedirectException(getComms().request.getApplicationPath()+LOGIN_PAGE);
210                   }
211             } else {
212                 writeDebugMsg("USER ALREADY LOGGED IN WITH A SESSION");
213             }
214
215
216
217         } catch (Exception JavaDoc ex) {
218             throw new DiscRackPresentationException("Trouble checking for user login status", ex);
219         }
220     }
221
222     /**
223      * Method to call the proper method for the incoming event
224      *
225      * @param comms HttpPresentationComms
226      * @exception Exception
227      */

228     public void handleEvent(HttpPresentationComms comms)
229         throws Exception JavaDoc {
230         String JavaDoc event = comms.request.getParameter(EVENT);
231
232     XMLObject returnHTML = null;
233
234         if (event == null || event.length() == 0) {
235             returnHTML = handleDefault();
236         }
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 JavaDoc event)
252         throws Exception JavaDoc {
253         try {
254             Method method = this.getClass().getMethod(toMethodName(event), null);
255             XMLObject thePage = (XMLObject)method.invoke(this, null);
256             return thePage;
257
258         } catch(InvocationTargetException ex) {
259             // Rethrow the originating exception if as it should be propagated as is
260
// It could be a page redirect exception, etc.
261
if (ex.getTargetException() instanceof Exception JavaDoc) {
262                 throw (Exception JavaDoc)ex.getTargetException();
263             } else if (ex.getTargetException() instanceof Error JavaDoc) {
264                 throw (Error JavaDoc)ex.getTargetException();
265             } else {
266                 throw ex;
267             }
268         } catch(NoSuchMethodException JavaDoc ex) {
269             //The method to handle the event does not exist.
270
throw new DiscRackPresentationException("NO EVENT HANDLER FOUND FOR EVENT: " +
271                                                     event, ex);
272         } catch(IllegalAccessException JavaDoc ex) {
273             //The method to handle the event does not exist.
274
throw new DiscRackPresentationException("ILLEGAL ACCESS TO EVENT HANDLER (is it public?): " +
275                                                     event, ex);
276         }
277     }
278
279     /**
280      * This sets the first letter of the event parameter value in order
281      * to adhere to Java method naming conventions.
282      *
283      * @param event the incoming name of the event
284      * @return String the properly capitalized name
285      */

286     private String JavaDoc toMethodName(String JavaDoc event) {
287         StringBuffer JavaDoc methodName = new StringBuffer JavaDoc(STANDARD_METHOD_PREFIX);
288         methodName.append(Character.toUpperCase(event.charAt(0)));
289
290         if (event.length() > 1) {
291             methodName.append(event.substring(1));
292         }
293
294         return methodName.toString();
295     }
296
297     /**
298      * Returns the application object associated with the
299      * current request.
300      *
301      * @return the application object.
302      */

303     public DiscRack getApplication() {
304         return (DiscRack)Enhydra.getApplication();
305     }
306
307     /**
308      * Method to write a debugging message to the debug log
309      * channel when the DEBUG flag is turned on
310      *
311      * @param msg The message to write to the DEBUG log channel
312      */

313     public static void writeDebugMsg(String JavaDoc msg) {
314         Enhydra.getLogChannel().write(Logger.DEBUG,msg);
315     }
316 }
317
Popular Tags