1 23 24 package barracudaDiscRack.presentation; 25 26 import com.lutris.appserver.server.httpPresentation.*; 27 import com.lutris.appserver.server.Enhydra; 28 import org.enhydra.xml.xmlc.*; 29 import org.enhydra.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 ; 41 42 import barracudaDiscRack.*; 43 import barracudaDiscRack.business.person.Person; 44 45 56 public abstract class BasePO implements HttpPresentation { 57 58 public static String DISC_CATALOG_PAGE = "discMgmt/DiscCatalog.po"; 59 public static String DISC_EVENT_CATALOG_PAGE = "../discMgmt/DiscCatalog.po"; 60 61 protected static final String USER_KEY = "DiscRackPerson"; 62 protected static String LOGIN_PAGE = "personMgmt/Login.po"; 63 private static String EVENT = "event"; 64 private static String STANDARD_METHOD_PREFIX = "handle"; 65 66 76 public abstract XMLObject handleDefault() throws HttpPresentationException; 77 78 83 protected abstract boolean loggedInUserRequired(); 84 85 88 protected HttpPresentationComms myComms = null; 89 protected DiscRackSessionData mySessionData = null; 90 91 94 protected Person myPerson = null; 95 96 102 public HttpPresentationComms getComms() { 103 return this.myComms; 104 } 105 106 111 public DiscRackSessionData getSessionData() { 112 return this.mySessionData; 113 } 114 115 121 public void setUser(Person thePerson) 122 throws DiscRackPresentationException { 123 this.getSessionData().setUser(thePerson); 124 } 125 126 131 public Person getUser() { 132 return this.getSessionData().getUser(); 133 } 134 135 138 public void removeUserFromSession() { 139 this.getSessionData().removeUser(); 140 } 141 142 148 public void run(HttpPresentationComms comms) 149 throws Exception { 150 initSessionData(comms); 152 if(this.loggedInUserRequired()) { 154 checkForUserLogin(); 155 } 156 handleEvent(comms); 158 } 159 160 167 protected void initSessionData(HttpPresentationComms comms) 168 throws DiscRackPresentationException { 169 this.myComms = comms; 170 171 try { 172 Object obj = comms.sessionData.get(DiscRackSessionData.SESSION_KEY); 173 if(null != obj) { 175 this.mySessionData = (DiscRackSessionData)obj; 176 } else { 177 this.mySessionData = new DiscRackSessionData(); 179 comms.sessionData.set(DiscRackSessionData.SESSION_KEY, this.mySessionData); 180 } 181 } catch(KeywordValueException ex) { 182 writeDebugMsg("Problem getting session data from session: " + 183 ex.getMessage()); 184 } 185 } 186 187 190 protected void checkForUserLogin() 191 throws ClientPageRedirectException, DiscRackPresentationException { 192 193 try { 194 Person user = getUser(); 195 196 if (null == user) { 197 writeDebugMsg("USER NOT FOUND IN SESSION"); 198 String requestedPO = myComms.request.getRequestURI(); 200 this.writeDebugMsg("PO: "+ requestedPO); 201 writeDebugMsg("REDIRECTING TO LOGIN PAGE"); 203 throw new ClientPageRedirectException(getComms().request.getApplicationPath()+LOGIN_PAGE); 204 } else { 205 writeDebugMsg("USER ALREADY LOGGED IN WITH A SESSION"); 206 } 207 } catch (Exception ex) { 208 throw new DiscRackPresentationException("Trouble checking for user login status", ex); 209 } 210 } 211 212 218 public void handleEvent(HttpPresentationComms comms) 219 throws Exception { 220 String event = comms.request.getParameter(EVENT); 221 222 XMLObject returnHTML = null; 223 224 if (event == null || event.length() == 0) { 225 returnHTML = handleDefault(); 226 } 227 else { 228 returnHTML = getPageContentForEvent(event); 229 } 230 231 comms.response.writeDOM(returnHTML); 232 } 233 234 241 public XMLObject getPageContentForEvent(String event) 242 throws Exception { 243 try { 244 Method method = this.getClass().getMethod(toMethodName(event), null); 245 XMLObject thePage = (XMLObject)method.invoke(this, null); 246 return thePage; 247 248 } catch(InvocationTargetException ex) { 249 if (ex.getTargetException() instanceof Exception ) { 252 throw (Exception )ex.getTargetException(); 253 } else if (ex.getTargetException() instanceof Error ) { 254 throw (Error )ex.getTargetException(); 255 } else { 256 throw ex; 257 } 258 } catch(NoSuchMethodException ex) { 259 throw new DiscRackPresentationException("NO EVENT HANDLER FOUND FOR EVENT: " + 261 event, ex); 262 } catch(IllegalAccessException ex) { 263 throw new DiscRackPresentationException("ILLEGAL ACCESS TO EVENT HANDLER (is it public?): " + 265 event, ex); 266 } 267 } 268 269 276 private String toMethodName(String event) { 277 StringBuffer methodName = new StringBuffer (STANDARD_METHOD_PREFIX); 278 methodName.append(Character.toUpperCase(event.charAt(0))); 279 280 if (event.length() > 1) { 281 methodName.append(event.substring(1)); 282 } 283 284 return methodName.toString(); 285 } 286 287 293 public BarracudaDiscRackEnhydra getApplication() { 294 return (BarracudaDiscRackEnhydra)Enhydra.getApplication(); 295 } 296 297 303 public static void writeDebugMsg(String msg) { 304 Enhydra.getLogChannel().write(Logger.DEBUG,msg); 305 } 306 } 307 | Popular Tags |