1 23 24 package discRack.presentation; 25 26 import com.lutris.appserver.server.httpPresentation.*; 27 import com.lutris.appserver.server.Enhydra; 28 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 ; 41 42 import discRack.spec.*; 43 import discRack.DiscRack; 44 45 55 public abstract class BasePO implements HttpPresentation { 56 protected static final String USER_KEY = "DiscRackPerson"; 57 protected static String LOGIN_PAGE = "personMgmt/Login.po"; 58 protected static String DISC_CATALOG_PAGE = "discMgmt/DiscCatalog.po"; 59 private static String EVENT = "event"; 60 private static String STANDARD_METHOD_PREFIX = "handle"; 61 62 72 public abstract XMLObject handleDefault() throws HttpPresentationException; 73 74 79 protected abstract boolean loggedInUserRequired(); 80 81 84 protected HttpPresentationComms myComms = null; 85 protected DiscRackSessionData mySessionData = null; 86 87 90 protected Person myPerson = null; 91 92 98 public HttpPresentationComms getComms() { 99 return this.myComms; 100 } 101 102 107 public DiscRackSessionData getSessionData() { 108 return this.mySessionData; 109 } 110 111 117 public void setUser(Person thePerson) 118 throws DiscRackPresentationException { 119 this.getSessionData().setUser(thePerson); 120 } 121 122 127 public Person getUser() { 128 return this.getSessionData().getUser(); 129 } 130 131 134 public void removeUserFromSession() { 135 this.getSessionData().removeUser(); 136 } 137 138 144 public void run(HttpPresentationComms comms) 145 throws Exception { 146 initSessionData(comms); 148 if(this.loggedInUserRequired()) { 150 checkForUserLogin(); 151 152 } 153 handleEvent(comms); 155 } 156 157 164 protected void initSessionData(HttpPresentationComms comms) 165 throws DiscRackPresentationException { 166 this.myComms = comms; 167 168 try { 169 Object obj = comms.sessionData.get(DiscRackSessionData.SESSION_KEY); 170 if(null != obj) { 172 this.mySessionData = (DiscRackSessionData)obj; 173 } else { 174 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 187 protected void checkForUserLogin() 188 throws ClientPageRedirectException, DiscRackPresentationException { 189 190 191 192 try { 193 Person user = getUser(); 194 195 if (null == user) { 196 String 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 String requestedPO = myComms.request.getRequestURI(); 206 this.writeDebugMsg("PO: "+ requestedPO); 207 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 ex) { 218 throw new DiscRackPresentationException("Trouble checking for user login status", ex); 219 } 220 } 221 222 228 public void handleEvent(HttpPresentationComms comms) 229 throws Exception { 230 String 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.writeDOM(returnHTML); 242 } 243 244 251 public XMLObject getPageContentForEvent(String event) 252 throws Exception { 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 if (ex.getTargetException() instanceof Exception ) { 262 throw (Exception )ex.getTargetException(); 263 } else if (ex.getTargetException() instanceof Error ) { 264 throw (Error )ex.getTargetException(); 265 } else { 266 throw ex; 267 } 268 } catch(NoSuchMethodException ex) { 269 throw new DiscRackPresentationException("NO EVENT HANDLER FOUND FOR EVENT: " + 271 event, ex); 272 } catch(IllegalAccessException ex) { 273 throw new DiscRackPresentationException("ILLEGAL ACCESS TO EVENT HANDLER (is it public?): " + 275 event, ex); 276 } 277 } 278 279 286 private String toMethodName(String event) { 287 StringBuffer methodName = new StringBuffer (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 303 public DiscRack getApplication() { 304 return (DiscRack)Enhydra.getApplication(); 305 } 306 307 313 public static void writeDebugMsg(String msg) { 314 Enhydra.getLogChannel().write(Logger.DEBUG,msg); 315 } 316 } 317 | Popular Tags |