001: /**
002: * Title: OpenUSS - Open Source University Support System
003: * Description: BaseWapPO Presentation Object
004: * Copyright: Copyright (c) B. Lofi Dewanto
005: * Company: University of Muenster
006: * @author B. Lofi Dewanto
007: * @version 1.0
008: */package org.openuss.presentation.enhydra.framework;
009:
010: import com.lutris.appserver.server.*;
011: import com.lutris.appserver.server.httpPresentation.*;
012: import com.lutris.appserver.server.user.User;
013:
014: import com.lutris.logging.*;
015:
016: import com.lutris.util.KeywordValueException;
017:
018: //import com.lutris.xml.xmlc.*;
019: //import com.lutris.xml.xmlc.html.*;
020:
021: import java.lang.Throwable;
022: import java.lang.reflect.*;
023:
024: import java.util.*;
025:
026: import org.w3c.dom.*;
027: import org.w3c.dom.html.HTMLElement;
028:
029: /**
030: * The base presentation object for WAP Enhydra.
031: *
032: * @author B. Lofi Dewanto
033: * @version 1.0
034: */
035: abstract public class BaseWapPO {
036: /**
037: * An event must be called "event".
038: */
039: private static String EVENT = "event";
040:
041: /**
042: * Every methods have to use the handle in the
043: * front of the method name, e.g.: handleLogin().
044: */
045: private static String STANDARD_METHOD_PREFIX = "handle";
046:
047: /**
048: * Saved HTML input and output context.
049: */
050: protected HttpPresentationComms myComms = null;
051:
052: /**
053: * This is the procedure that is called if there is no "event"
054: * HTTP parameter found. It must be overriden by the subclass to
055: * do default processing or error checking/handling.
056: *
057: * @return String The String representation of the HTML or (other format)
058: * of the document to be displayed. This method would need to be changed
059: * if you wanted to return binary data as well. It returns a String
060: * for simplicity right now.
061: */
062: abstract public String handleDefault()
063: throws HttpPresentationException;
064:
065: /**
066: * This method should be implemented in the subclass so that it returns
067: * true if this particular request requires the user to be logged
068: * in, otherwise false.
069: */
070: abstract protected boolean loggedInUserRequired();
071:
072: /**
073: * This method should be implemented in the subclass, so that it returns
074: * true if this particular request requires the user to apply first
075: * for the access. For public means everybody can access this,
076: * otherwise he or she needs to be activated first.
077: */
078: abstract protected boolean isForPublicAccess()
079: throws BasePOException;
080:
081: /**
082: * This method should be implemented in the subclass to change
083: * the behaviour of the access list. Access list contents the current
084: * student and the current enrollment. If the the current enrollment
085: * is not for a public access, the student has to have an access list (accepted)
086: * to get into this enrollment.
087: */
088: abstract protected void checkForAccessList()
089: throws ClientPageRedirectException, BasePOException;
090:
091: /**
092: * @return The saved comms objects
093: * to whichever subclass needs it
094: */
095: public HttpPresentationComms getComms() {
096: return this .myComms;
097: }
098:
099: /**
100: * Method to call the proper method for the incoming event
101: */
102: public void handleEvent(HttpPresentationComms comms)
103: throws Exception {
104: String event = comms.request.getParameter(EVENT);
105: String returnWML = null;
106:
107: if ((event == null) || (event.length() == 0)) {
108: returnWML = handleDefault();
109: } else {
110: returnWML = getPageContentForEvent(event);
111: }
112:
113: HttpPresentationResponse response = comms.response;
114: response.setContentType("text/vnd.wap.wml");
115:
116: HttpPresentationOutputStream out = response.getOutputStream();
117: out.println(returnWML);
118: response.flush();
119: }
120:
121: /**
122: * If an event parameter is defined then this invokes the method that
123: * handles that event.
124: */
125: public String getPageContentForEvent(String event) throws Exception {
126: try {
127: // Get the methodname
128: Method method = this .getClass().getMethod(
129: toMethodName(event), null);
130:
131: // Execute the run method
132: String thePage = (String) method.invoke(this , null);
133:
134: return thePage;
135: } catch (InvocationTargetException ex) {
136: // Rethrow the originating exception if as it should be propagated as is
137: // It could be a page redirect exception, etc.
138: if (ex.getTargetException() instanceof Exception) {
139: throw (Exception) ex.getTargetException();
140: } else if (ex.getTargetException() instanceof Error) {
141: throw (Error) ex.getTargetException();
142: } else {
143: throw ex;
144: }
145: } catch (NoSuchMethodException ex) {
146: // The method to handle the event does not exist.
147: throw new BasePOException(
148: "No event handler found for event: " + event, ex);
149: } catch (IllegalAccessException ex) {
150: // The method to handle the event does not exist.
151: throw new BasePOException(
152: "Illegal access to event handler (is it public?): "
153: + event, ex);
154: }
155: }
156:
157: /**
158: * This sets the first letter of the event parameter value in order
159: * to adhere to Java method naming conventions.
160: *
161: * @param String event the incoming name of the event
162: * @return String the properly capitalized name
163: */
164: private String toMethodName(String event) {
165: StringBuffer methodName = new StringBuffer(
166: STANDARD_METHOD_PREFIX);
167: methodName.append(Character.toUpperCase(event.charAt(0)));
168:
169: if (event.length() > 1) {
170: methodName.append(event.substring(1));
171: }
172:
173: return methodName.toString();
174: }
175:
176: /**
177: * Returns the application object associated with the
178: * current request.
179: *
180: * @return the ebroker application object.
181: */
182: public StandardApplication getApplication() {
183: return (StandardApplication) Enhydra.getApplication();
184: }
185:
186: /**
187: * Method to write a debugging message to the debug log
188: * channel when the DEBUG flag is turned on
189: *
190: * @param msg The message to write to the DEBUG log channel
191: */
192: public static void writeDebugMsg(String msg) {
193: Enhydra.getLogChannel().write(Logger.DEBUG, msg);
194: }
195: }
|