View Javadoc

1   /* 
2    *  Copyright 2004 University of Hannover
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package olr.presentation;
18  
19  import java.lang.reflect.InvocationTargetException;
20  import java.lang.reflect.Method;
21  
22  import org.apache.log4j.Logger;
23  
24  import olr.Olr;
25  import olr.SessionData;
26  import olr.om.RdfUser;
27  
28  import com.lutris.appserver.server.Enhydra;
29  import com.lutris.appserver.server.httpPresentation.ClientPageRedirectException;
30  import com.lutris.appserver.server.httpPresentation.HttpPresentation;
31  import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
32  import com.lutris.appserver.server.httpPresentation.HttpPresentationException;
33  import com.lutris.util.KeywordValueException;
34  
35  /***
36   * @version $Id: ExtendedHttpPresentation.java,v 1.6 2004/08/03 08:33:53 roku Exp $
37   */
38  public abstract class ExtendedHttpPresentation implements HttpPresentation {
39      private static final String EVENT = "event";
40  
41      private static final String STANDARD_METHOD_PREFIX = "handle";
42  
43      private HttpPresentationComms myComms;
44  
45      /***
46       * Creates an instance.
47       */
48      public ExtendedHttpPresentation() {
49          myComms = null;
50      }
51  
52      /***
53       * Called upon page request.
54       * 
55       * @return Should return a string of (X)HTML 
56       * @throws HttpPresentationException
57       */
58      public abstract String handleDefault() throws HttpPresentationException;
59  
60      /***
61       * Called on subclass to determine if this Presenentation needs a valid logged in user to
62       * display.
63       * 
64       * @return
65       */
66      protected abstract boolean loggedInUserRequired();
67  
68      /***
69       * @return The comms object.
70       */
71      public HttpPresentationComms getComms() {
72          return myComms;
73      }
74  
75      /***
76       * @return This users SessionData
77       */
78      public SessionData getSessionData() {
79          return SessionData.getCurrentSession();
80      }
81  
82      /***
83       * @param user The current logged in user
84       * @throws HttpPresentationException
85       */
86      public void setUser(RdfUser user) throws HttpPresentationException {
87          getSessionData().setUser(user);
88      }
89  
90      /***
91       * @return The current logged in user
92       */
93      public RdfUser getUser() {
94          return getSessionData().getUser();
95      }
96  
97      /***
98       * Clears the current users session.
99       * 
100      * @see SessionData
101      */
102     public void clearSession() {
103         try {
104             SessionData.setCurrentSession(new SessionData());
105             getComms().sessionData.set("AdminSessionData", getSessionData());
106         } catch (KeywordValueException ex) {
107             writeDebugMsg("Problem getting session data from session: ".concat(String
108                     .valueOf(String.valueOf(ex.getMessage()))));
109         }
110     }
111 
112     /***
113      * @see com.lutris.appserver.server.httpPresentation.HttpPresentation#run(com.lutris.appserver.server.httpPresentation.HttpPresentationComms)
114      */
115     public void run(HttpPresentationComms comms) throws Exception {
116         initSessionData(comms);
117         if (loggedInUserRequired())
118             checkForUserLogin();
119         getSessionData().refreshLock(getComms().session.getHttpSession().getMaxInactiveInterval());
120         handleEvent(comms);
121     }
122 
123     /***
124      * Initializes session data based on comms
125      * 
126      * @param comms Parameter object
127      * @throws HttpPresentationException
128      */
129     protected void initSessionData(HttpPresentationComms comms) throws HttpPresentationException {
130         myComms = comms;
131         try {
132             Object obj = getComms().sessionData.get("AdminSessionData");
133             if (obj != null)
134                 SessionData.setCurrentSession((SessionData) obj);
135             else
136                 clearSession();
137         } catch (KeywordValueException ex) {
138             writeDebugMsg("Problem getting session data from session: ".concat(String
139                     .valueOf(String.valueOf(ex.getMessage()))));
140         }
141     }
142 
143     /***
144      * Checks if the user is already logged in.
145      * 
146      * @throws HttpPresentationException
147      * @throws ClientPageRedirectException If
148      */
149     protected void checkForUserLogin() throws HttpPresentationException,
150             ClientPageRedirectException {
151         try {
152             RdfUser user = getUser();
153             if (user == null) {
154                 writeDebugMsg("USER NOT FOUND IN SESSION");
155                 String requestedPO = getComms().request.getRequestURI();
156                 writeDebugMsg("PO: ".concat(String.valueOf(String.valueOf(requestedPO))));
157                 writeDebugMsg("REDIRECTING TO LOGGEDOUT PAGE");
158                 throw new ClientPageRedirectException("LoggedOut.po");
159             }
160             writeDebugMsg("USER ALREADY LOGGED IN WITH A SESSION");
161         } catch (Exception ex) {
162             throw new HttpPresentationException("Trouble checking for user login status", ex);
163         }
164     }
165 
166     /***
167      * @param comms
168      * @throws Exception
169      */
170     public void handleEvent(HttpPresentationComms comms) throws Exception {
171         String event = getComms().request.getParameter(EVENT);
172         String returnHTML = null;
173         if (event == null || event.length() == 0)
174             returnHTML = handleDefault();
175         else
176             returnHTML = getPageContentForEvent(event);
177         getComms().response.writeHTML(returnHTML);
178     }
179 
180     /***
181      * <p>
182      * Call a method to produce page content depending on the <em>event</em>. The method called
183      * is perfomed using <em>java reflection</em>. The name of the called method is
184      * <code>handel&lt;event&gt;()</code> where the first letter of the <em>event></em> is made
185      * uppercase.
186      * </p>
187      * <p>
188      * <strong>Example: </strong> event=login results in a method call <code>handleLogin()</code>
189      * </p>
190      * 
191      * @param event
192      * @return Normally a (X)HTML string should be returned
193      * @throws Exception
194      * @see #toMethodName(String)
195      */
196     public String getPageContentForEvent(String event) throws Exception {
197         try {
198             Method method = getClass().getMethod(toMethodName(event), null);
199             String thePage = (String) method.invoke(this, null);
200             String s = thePage;
201             return s;
202         } catch (InvocationTargetException ex) {
203             if (ex.getTargetException() instanceof Exception)
204                 throw (Exception) ex.getTargetException();
205             if (ex.getTargetException() instanceof Error)
206                 throw (Error) ex.getTargetException();
207             else
208                 throw ex;
209         } catch (NoSuchMethodException ex) {
210             throw new HttpPresentationException("NO EVENT HANDLER FOUND FOR EVENT: ".concat(String
211                     .valueOf(String.valueOf(event))), ex);
212         } catch (IllegalAccessException ex) {
213             throw new HttpPresentationException("ILLEGAL ACCESS TO EVENT HANDLER (is it public?): "
214                     .concat(String.valueOf(String.valueOf(event))), ex);
215         }
216     }
217 
218     private String toMethodName(String event) {
219         StringBuffer methodName = new StringBuffer(STANDARD_METHOD_PREFIX);
220         methodName.append(Character.toUpperCase(event.charAt(0)));
221         if (event.length() > 1)
222             methodName.append(event.substring(1));
223         return methodName.toString();
224     }
225 
226     /***
227      * 
228      * @return The Enhydra Application object
229      */
230     public Olr getApplication() {
231         return (Olr) Enhydra.getApplication();
232     }
233 
234     private static void writeDebugMsg(String msg) {
235         Logger.getLogger(ExtendedHttpPresentation.class).debug(msg);
236     }
237 }