KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > transactionsDiscRack > 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.1 2004/08/16 10:48:52 slobodan Exp $
22  */

23
24 package transactionsDiscRack.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 transactionsDiscRack.spec.*;
43 import transactionsDiscRack.*;
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 TransactionsDiscRackSessionData 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 TransactionsDiscRackSessionData 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 TransactionsDiscRackPresentationException {
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         // 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 TransactionsDiscRackPresentationException {
165         this.myComms = comms;
166
167         try {
168             Object JavaDoc obj = comms.sessionData.get(TransactionsDiscRackSessionData.SESSION_KEY);
169             // If we found the session data, save it in a private data member
170
if(null != obj) {
171                 this.mySessionData = (TransactionsDiscRackSessionData)obj;
172             } else {
173                 // If no session data was found, create a new session data instance
174
this.mySessionData = new TransactionsDiscRackSessionData();
175                 comms.sessionData.set(TransactionsDiscRackSessionData.SESSION_KEY, this.mySessionData);
176             }
177         } catch(KeywordValueException ex) {
178             writeDebugMsg("Problem getting session data from session: " +
179                           ex.getMessage());
180         }
181     }
182
183     /**
184      * Checks the session data for a User, if not there then redirects to the login page
185      */

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

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

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

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

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

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