1 19 20 package com.lutris.airsent.presentation; 21 22 import com.lutris.airsent.spec.address.*; 23 import com.lutris.airsent.spec.address.*; 24 25 import com.lutris.airsent.*; 26 27 import com.lutris.appserver.server.StandardAppUtil; 28 import org.enhydra.xml.xmlc.XMLObject; 29 import com.lutris.appserver.server.httpPresentation.*; 30 import com.lutris.appserver.server.session.*; 31 import com.lutris.appserver.server.Enhydra; 32 import com.lutris.logging.*; 35 import com.lutris.util.KeywordValueException; 36 import com.lutris.appserver.server.user.User; 37 import org.w3c.dom.*; 38 import org.w3c.dom.html.HTMLElement; 39 import java.lang.reflect.*; 40 import java.util.*; 41 42 43 44 55 public abstract class BasePO implements HttpPresentation { 56 private static String EVENT = "event"; 57 private static String STANDARD_METHOD_PREFIX = "handle"; 58 59 69 public abstract XMLObject handleDefault() 70 throws HttpPresentationException; 71 72 76 abstract protected int getRequiredAuthLevel(); 77 78 81 protected HttpPresentationComms myComms = null; 82 protected AirSentSessionData mySessionData = null; 83 84 90 public HttpPresentationComms getComms() { 91 return this.myComms; 92 } 93 94 99 public AirSentSessionData getSessionData() { 100 return this.mySessionData; 101 } 102 103 109 public void run(HttpPresentationComms comms) throws Exception { 110 111 rerouteForContent(comms); 113 114 initSessionData(comms); 116 117 checkAuthLevel(); 119 120 try { 121 122 handleEvent(comms); 124 } catch (Exception e) { 125 System.err.println("EXCEPTION: " + e); 126 throw new Exception ("Exception in run " + e); 127 } 128 } 129 130 140 protected void rerouteForContent(HttpPresentationComms comms) 141 throws AirSentPresentationException { 142 DeviceUtils.rerouteForContent(comms); 143 } 144 145 152 protected void initSessionData(HttpPresentationComms comms) 153 throws AirSentPresentationException { 154 155 this.myComms = comms; 156 try { 157 Object obj = comms.sessionData.get(AirSentSessionData.SESSION_KEY); 158 159 if (obj != null) { 161 this.mySessionData = (AirSentSessionData) obj; 162 } else { 163 this.mySessionData = new AirSentSessionData(); 165 comms.sessionData.set(AirSentSessionData.SESSION_KEY, this.mySessionData); 166 } 167 } catch (KeywordValueException ex) { 168 throw new AirSentPresentationException("Trouble initializing user", ex); 169 } 170 } 171 172 178 protected int getCurrentAuthLevel() 179 throws ClientPageRedirectException, AirSentPresentationException { 180 int accessLevel = 0; 181 182 try { 183 accessLevel = getSessionData().getUserAuth(); 184 } catch (Exception ex) { 185 throw new AirSentPresentationException("Trouble getting current authorization level", 186 ex); 187 } 188 189 return accessLevel; 190 } 191 192 202 protected void checkAuthLevel() 203 throws ClientPageRedirectException, AirSentPresentationException { 204 int currentAuth = getCurrentAuthLevel(); 205 206 try { 207 209 String uri = myComms.request.getRequestURI(); 210 boolean is= uri.startsWith("/AirSent_pres"); 211 212 if(!is){ 213 214 if (currentAuth < getRequiredAuthLevel()) { 215 if (currentAuth > AirSentConstants.CUSTOMER_USER) { 216 throw new ClientPageRedirectException(AirSentConstants.ADMIN_LOGIN_PAGE); 217 } 218 219 throw new ClientPageRedirectException("/" + AirSentConstants.HTML_PAGE); 220 } 221 222 } 223 } catch (Exception ex) { 224 throw new AirSentPresentationException("Trouble checking for user login status", 225 ex); 226 } 227 } 228 229 235 public void handleEvent(HttpPresentationComms comms) throws Exception { 236 String event = comms.request.getParameter(EVENT); 237 XMLObject returnDoc = null; 238 try { 239 if (event == null || event.length() == 0) { 240 returnDoc = handleDefault(); 241 } else { 242 returnDoc = getPage(event); 243 } 244 comms.response.writeDOM(returnDoc); 245 } catch (Exception e) { 246 throw new Exception ("Exception writing dom:" + e); 247 } 248 } 249 250 261 public XMLObject handleLogout() throws AirSentPresentationException { 262 try { 263 mySessionData = null; 264 SessionManager sessionManager = myComms.session.getSessionManager(); 265 sessionManager.deleteSession(myComms.session); 266 throw new ClientPageRedirectException(AirSentConstants.HTML_PAGE); 267 } catch (Exception e) { 268 throw new AirSentPresentationException("Trouble logging out user", 269 e); 270 } 271 } 272 273 280 public XMLObject getPage(String event) throws Exception { 281 try { 282 Method method = this.getClass().getMethod(toMethodName(event), 283 null); 284 XMLObject thePage = (XMLObject) method.invoke(this, null); 285 286 return thePage; 287 } catch (InvocationTargetException ex) { 288 289 if (ex.getTargetException() instanceof Exception ) { 292 throw (Exception ) ex.getTargetException(); 293 } else if (ex.getTargetException() instanceof Error ) { 294 throw (Error ) ex.getTargetException(); 295 } else { 296 throw ex; 297 } 298 } catch (NoSuchMethodException ex) { 299 300 throw new AirSentPresentationException("NO EVENT HANDLER FOUND FOR EVENT: " 302 + event, ex); 303 } catch (IllegalAccessException ex) { 304 305 throw new AirSentPresentationException("ILLEGAL ACCESS TO EVENT HANDLER (is it public?): " 307 + event, ex); 308 } 309 } 310 311 316 protected String getURIPrefix() throws AirSentPresentationException { 317 try { 318 String scheme = myComms.request.getScheme(); 319 String serverName = myComms.request.getServerName(); 320 int serverPort = myComms.request.getServerPort(); 321 String applicationPath = myComms.request.getApplicationPath(); 322 323 if (serverPort == 80) { 325 return scheme + "://" + serverName + applicationPath; 326 } else { 327 return scheme + "://" + serverName + ":" + serverPort 328 + applicationPath; 329 } 330 } catch (Exception ex) { 331 throw new AirSentPresentationException("Can't get Server Root Name " 332 + "Config file.", ex); 333 } 334 } 335 336 343 private String toMethodName(String event) { 344 StringBuffer methodName = new StringBuffer (STANDARD_METHOD_PREFIX); 345 346 methodName.append(Character.toUpperCase(event.charAt(0))); 347 348 if (event.length() > 1) { 349 methodName.append(event.substring(1)); 350 } 351 352 return methodName.toString(); 353 } 354 355 361 public AirSent getApplication() { 362 return (AirSent) Enhydra.getApplication(); 363 } 364 365 371 public static void writeDebugMsg(String msg) { 372 Enhydra.getLogChannel().write(Logger.DEBUG, msg); 373 } 374 375 376 380 protected static boolean isNullField(String field) { 381 if (field == null) { 382 return true; 383 } 384 385 if (field.trim().equals("")) { 386 return true; 387 } 388 389 return false; 390 } 391 392 393 397 protected static boolean checkField(String field, int size) { 398 if (field == null || field.equals("")) { 399 return false; 400 } 401 if (field.length() <= size) { 402 return true; 403 } 404 return false; 405 } 406 407 408 411 public static void dumpHeaders(HttpPresentationComms comms) { 412 System.out.println("DUMP HEADERS:" ); 413 try { 414 HttpPresentationRequest req = comms.request; 415 Enumeration e = req.getHeaderNames(); 416 while (e.hasMoreElements()) { 417 String k = (String ) e.nextElement(); 418 String p = req.getHeader(k); 419 System.out.println(" NAME: " + k + "; VALUE: " + p); 420 } 421 } catch (Exception ex) { 422 System.err.println("ERROR: " + ex); 423 } 424 } 425 426 429 public static void dump(HttpPresentationComms comms) { 430 System.out.println("DUMP PARAMETERS:" ); 431 try { 432 HttpPresentationRequest req = comms.request; 433 Enumeration e = req.getParameterNames(); 434 while (e.hasMoreElements()) { 435 String k = (String ) e.nextElement(); 436 String p = req.getParameter(k); 437 System.out.println(" NAME: " + k + "; VALUE: " + p); 438 } 439 } catch (Exception ex) { 440 System.err.println("ERROR: " + ex); 441 } 442 } 443 } 444 445 446 447 448
| Popular Tags
|