KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openuss > presentation > enhydra > framework > BaseWapPO


1 /**
2  * Title: OpenUSS - Open Source University Support System
3  * Description: BaseWapPO Presentation Object
4  * Copyright: Copyright (c) B. Lofi Dewanto
5  * Company: University of Muenster
6  * @author B. Lofi Dewanto
7  * @version 1.0
8  */

9 package org.openuss.presentation.enhydra.framework;
10
11 import com.lutris.appserver.server.*;
12 import com.lutris.appserver.server.httpPresentation.*;
13 import com.lutris.appserver.server.user.User;
14
15 import com.lutris.logging.*;
16
17 import com.lutris.util.KeywordValueException;
18
19 //import com.lutris.xml.xmlc.*;
20
//import com.lutris.xml.xmlc.html.*;
21

22 import java.lang.Throwable JavaDoc;
23 import java.lang.reflect.*;
24
25 import java.util.*;
26
27 import org.w3c.dom.*;
28 import org.w3c.dom.html.HTMLElement;
29
30
31 /**
32  * The base presentation object for WAP Enhydra.
33  *
34  * @author B. Lofi Dewanto
35  * @version 1.0
36  */

37 abstract public class BaseWapPO {
38     /**
39      * An event must be called "event".
40      */

41     private static String JavaDoc EVENT = "event";
42
43     /**
44      * Every methods have to use the handle in the
45      * front of the method name, e.g.: handleLogin().
46      */

47     private static String JavaDoc STANDARD_METHOD_PREFIX = "handle";
48
49     /**
50      * Saved HTML input and output context.
51      */

52     protected HttpPresentationComms myComms = null;
53
54     /**
55      * This is the procedure that is called if there is no "event"
56      * HTTP parameter found. It must be overriden by the subclass to
57      * do default processing or error checking/handling.
58      *
59      * @return String The String representation of the HTML or (other format)
60      * of the document to be displayed. This method would need to be changed
61      * if you wanted to return binary data as well. It returns a String
62      * for simplicity right now.
63      */

64     abstract public String JavaDoc handleDefault() throws HttpPresentationException;
65
66     /**
67      * This method should be implemented in the subclass so that it returns
68      * true if this particular request requires the user to be logged
69      * in, otherwise false.
70      */

71     abstract protected boolean loggedInUserRequired();
72
73     /**
74      * This method should be implemented in the subclass, so that it returns
75      * true if this particular request requires the user to apply first
76      * for the access. For public means everybody can access this,
77      * otherwise he or she needs to be activated first.
78      */

79     abstract protected boolean isForPublicAccess() throws BasePOException;
80
81     /**
82      * This method should be implemented in the subclass to change
83      * the behaviour of the access list. Access list contents the current
84      * student and the current enrollment. If the the current enrollment
85      * is not for a public access, the student has to have an access list (accepted)
86      * to get into this enrollment.
87      */

88     abstract protected void checkForAccessList()
89                                         throws ClientPageRedirectException,
90                                                BasePOException;
91
92     /**
93      * @return The saved comms objects
94      * to whichever subclass needs it
95      */

96     public HttpPresentationComms getComms() {
97         return this.myComms;
98     }
99
100     /**
101      * Method to call the proper method for the incoming event
102      */

103     public void handleEvent(HttpPresentationComms comms)
104                      throws Exception JavaDoc {
105         String JavaDoc event = comms.request.getParameter(EVENT);
106         String JavaDoc returnWML = null;
107
108         if ((event == null) || (event.length() == 0)) {
109             returnWML = handleDefault();
110         } else {
111             returnWML = getPageContentForEvent(event);
112         }
113
114         HttpPresentationResponse response = comms.response;
115         response.setContentType("text/vnd.wap.wml");
116
117         HttpPresentationOutputStream out = response.getOutputStream();
118         out.println(returnWML);
119         response.flush();
120     }
121
122     /**
123      * If an event parameter is defined then this invokes the method that
124      * handles that event.
125      */

126     public String JavaDoc getPageContentForEvent(String JavaDoc event)
127                                   throws Exception JavaDoc {
128         try {
129             // Get the methodname
130
Method method = this.getClass()
131                                 .getMethod(toMethodName(event), null);
132
133             // Execute the run method
134
String JavaDoc thePage = (String JavaDoc) method.invoke(this, null);
135
136             return thePage;
137         } catch (InvocationTargetException ex) {
138             // Rethrow the originating exception if as it should be propagated as is
139
// It could be a page redirect exception, etc.
140
if (ex.getTargetException() instanceof Exception JavaDoc) {
141                 throw (Exception JavaDoc) ex.getTargetException();
142             } else if (ex.getTargetException() instanceof Error JavaDoc) {
143                 throw (Error JavaDoc) ex.getTargetException();
144             } else {
145                 throw ex;
146             }
147         } catch (NoSuchMethodException JavaDoc ex) {
148             // The method to handle the event does not exist.
149
throw new BasePOException("No event handler found for event: " +
150                                       event, ex);
151         } catch (IllegalAccessException JavaDoc ex) {
152             // The method to handle the event does not exist.
153
throw new BasePOException(
154                     "Illegal access to event handler (is it public?): " +
155                     event, ex);
156         }
157     }
158
159     /**
160      * This sets the first letter of the event parameter value in order
161      * to adhere to Java method naming conventions.
162      *
163      * @param String event the incoming name of the event
164      * @return String the properly capitalized name
165      */

166     private String JavaDoc toMethodName(String JavaDoc event) {
167         StringBuffer JavaDoc methodName = new StringBuffer JavaDoc(STANDARD_METHOD_PREFIX);
168         methodName.append(Character.toUpperCase(event.charAt(0)));
169
170         if (event.length() > 1) {
171             methodName.append(event.substring(1));
172         }
173
174         return methodName.toString();
175     }
176
177     /**
178      * Returns the application object associated with the
179      * current request.
180      *
181      * @return the ebroker application object.
182      */

183     public StandardApplication getApplication() {
184         return (StandardApplication) Enhydra.getApplication();
185     }
186
187     /**
188      * Method to write a debugging message to the debug log
189      * channel when the DEBUG flag is turned on
190      *
191      * @param msg The message to write to the DEBUG log channel
192      */

193     public static void writeDebugMsg(String JavaDoc msg) {
194         Enhydra.getLogChannel().write(Logger.DEBUG, msg);
195     }
196 }
Popular Tags