1 21 22 package org.opensubsystems.core.www; 23 24 import java.io.IOException ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 import java.util.logging.Logger ; 28 29 import javax.servlet.RequestDispatcher ; 30 import javax.servlet.ServletConfig ; 31 import javax.servlet.ServletException ; 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpServletResponse ; 34 import javax.servlet.http.HttpSession ; 35 36 import org.opensubsystems.core.util.CallContext; 37 import org.opensubsystems.core.util.GlobalConstants; 38 import org.opensubsystems.core.util.Log; 39 import org.opensubsystems.core.util.MyTimer; 40 41 52 public class WebUIServlet extends WebSessionServlet 53 { 54 56 60 public static final String WEBUI_MESSAGEBOX_PAGE = "oss.webui.messagebox.page"; 61 62 66 public static final String WEBUI_MESSAGEBOX_STYLE_SHEET = "oss.webui.messagebox.css"; 67 68 73 75 80 public static final String ORIGINAL_REQUEST_INFO_REQUEST_PARAM = "originalrequest"; 81 82 87 public static final String DATA_ATTRIBUTE_REQUEST_PARAM = "databean"; 88 89 92 public static final String PAGE_TITLE_REQUEST_PARAM = "pagetitle"; 93 94 97 public static final String LOGGEDIN_REQUEST_PARAM = "loggedin"; 98 99 102 public static final String FORCE_MAXIMIZE_PARAM = "forcemaximize"; 103 104 109 public static final String FORM_NAME_REQUEST_PARAM = "FORM_NAME"; 110 111 116 public static final int FORM_UNKNOWN_ID = -1; 117 118 121 public static final int FORM_COUNT_WEBUI = 0; 122 123 125 128 private static Logger s_logger = Log.getInstance(WebUIServlet.class); 129 130 133 protected String m_strMessageStyleSheet; 134 135 143 private final Map m_mpUIPathCache = new HashMap (); 144 145 153 private final Map m_mpUIRendererCache = new HashMap (); 154 155 157 160 private static final long serialVersionUID = 322547463691937622L; 161 162 164 167 public void init( 168 ServletConfig scConfig 169 ) throws ServletException 170 { 171 super.init(scConfig); 172 173 cacheUIPath(scConfig, WEBUI_MESSAGEBOX_PAGE, 175 "Path to message box page is not set in property " 176 + WEBUI_MESSAGEBOX_PAGE); 177 182 m_strMessageStyleSheet = WebUtils.readProperty(scConfig, 183 WEBUI_MESSAGEBOX_STYLE_SHEET, 184 null, 185 true); 186 if ((m_strMessageStyleSheet == null) || (m_strMessageStyleSheet.length() == 0)) 187 { 188 s_logger.config(WEBUI_MESSAGEBOX_STYLE_SHEET + " = not defined"); 189 m_strMessageStyleSheet = null; 191 } 192 else 193 { 194 s_logger.config(WEBUI_MESSAGEBOX_STYLE_SHEET + " = " 195 + m_strMessageStyleSheet); 196 } 197 } 198 199 202 public String getServletInfo( 203 ) 204 { 205 return this.getClass().getName(); 206 } 207 208 210 213 protected void preservice( 214 HttpSession hsSession, 215 HttpServletRequest hsrqRequest, 216 HttpServletResponse hsrpResponse, 217 boolean bLoginVerified 218 ) throws ServletException , 219 IOException 220 { 221 super.preservice(hsSession, hsrqRequest, hsrpResponse, bLoginVerified); 222 223 setLoggedInFlag(hsrqRequest, hsrpResponse); 225 } 226 227 238 protected final void cacheUIPath( 239 ServletConfig scConfig, 240 String strUIID, 241 String strErrorMessage 242 ) throws ServletException 243 { 244 String strUIPath; 245 246 strUIPath = WebUtils.readProperty(scConfig, strUIID, null, false); 247 if ((strUIPath != null) && (strUIPath.length() > 0)) 248 { 249 m_mpUIPathCache.put(strUIID, strUIPath); 252 cacheDispatcher(strUIID, strUIPath); 253 } 254 else 255 { 256 throw new ServletException (strErrorMessage); 257 } 258 } 259 260 267 protected final void cacheDispatcher( 268 String strUIID, 269 String strUIPath 270 ) throws ServletException 271 { 272 if (isDispatcherCachingEnabled()) 273 { 274 RequestDispatcher rdCachedDispatcher; 276 277 rdCachedDispatcher = m_scServletContext.getRequestDispatcher(strUIPath); 278 if (rdCachedDispatcher == null) 279 { 280 throw new ServletException ("Cannot initialize user interface handler" 281 + " specified by property " 282 + strUIID + " = " + strUIPath); 283 } 284 else 285 { 286 m_mpUIRendererCache.put(strUIID, rdCachedDispatcher); 288 } 289 } 290 } 291 292 303 protected final void displayUI( 304 String strUIID, 305 HttpServletRequest hsrqRequest, 306 HttpServletResponse hsrpResponse 307 ) throws ServletException , 308 IOException 309 { 310 RequestDispatcher rdReturn; 313 String strUIPath; 314 MyTimer timer = new MyTimer(); 315 316 if (isDispatcherCachingEnabled()) 317 { 318 rdReturn = (RequestDispatcher )m_mpUIRendererCache.get(strUIID); 320 321 if (GlobalConstants.ERROR_CHECKING) 322 { 323 assert rdReturn != null 324 : "Request dispatcher for " + strUIID + " wasn't cached properly."; 325 } 326 } 327 else 328 { 329 strUIPath = (String )m_mpUIPathCache.get(strUIID); 330 331 if (GlobalConstants.ERROR_CHECKING) 332 { 333 assert strUIPath != null 334 : "Unknown UI rendered to retrieve " + strUIID; 335 } 336 337 rdReturn = m_scServletContext.getRequestDispatcher(strUIPath); 338 if (rdReturn == null) 339 { 340 throw new ServletException ("Cannot initialize user interface handler" 341 + " specified by property " 342 + strUIID + " = " + strUIPath); 343 } 344 } 345 346 hsrqRequest.setAttribute(MessageTag.MESSAGES_REQUEST_PARAM, 349 CallContext.getInstance().getMessages()); 350 351 if (hsrqRequest.getParameter(FORCE_MAXIMIZE_PARAM) != null) 352 { 353 hsrqRequest.setAttribute(FORCE_MAXIMIZE_PARAM, "true"); 355 } 356 357 rdReturn.forward(hsrqRequest, hsrpResponse); 358 timer.stop(); 359 s_logger.fine("Displaying of " + strUIID + " took " + timer.toString()); 360 } 361 362 364 373 protected void setLoggedInFlag( 374 HttpServletRequest hsrqRequest, 375 HttpServletResponse hsrpResponse 376 ) 377 { 378 HttpSession hsSession; 379 380 hsSession = hsrqRequest.getSession(false); 383 384 if (WebSessionUtils.isLoggedIn(hsSession)) 385 { 386 hsrqRequest.setAttribute(LOGGEDIN_REQUEST_PARAM, Boolean.TRUE); 387 } 388 else 389 { 390 hsrqRequest.setAttribute(LOGGEDIN_REQUEST_PARAM, Boolean.FALSE); 391 } 392 } 393 394 406 protected void messageBoxPage( 407 HttpServletRequest hsrqRequest, 408 HttpServletResponse hsrpResponse, 409 String strPageTitle, 410 String strMessage, 411 String strProceedURL, 412 Throwable cause 413 ) throws IOException , 414 ServletException 415 { 416 messageBoxPage(hsrqRequest, 417 hsrpResponse, 418 strPageTitle, 419 strMessage, 420 strProceedURL, 421 cause, 422 WEBUI_MESSAGEBOX_PAGE); 423 } 424 425 438 protected void messageBoxPage( 439 HttpServletRequest hsrqRequest, 440 HttpServletResponse hsrpResponse, 441 String strPageTitle, 442 String strMessage, 443 String strProceedURL, 444 Throwable cause, 445 String strUIPage 446 ) throws IOException , 447 ServletException 448 { 449 if (m_strMessageStyleSheet != null) 450 { 451 hsrqRequest.setAttribute(WEBUI_MESSAGEBOX_STYLE_SHEET, 452 m_strMessageStyleSheet); 453 } 454 455 hsrqRequest.setAttribute(ORIGINAL_REQUEST_INFO_REQUEST_PARAM, 458 WebUtils.debug(hsrqRequest)); 459 460 hsrqRequest.setAttribute(DATA_ATTRIBUTE_REQUEST_PARAM, 463 (strMessage == null) ? "Unknown error has occured." 464 : strMessage); 465 hsrqRequest.setAttribute(PAGE_TITLE_REQUEST_PARAM, 466 (strPageTitle == null) ? "Unknown error has occured." 467 : strPageTitle); 468 if (strProceedURL != null) 469 { 470 hsrqRequest.setAttribute(DATA_ATTRIBUTE_REQUEST_PARAM + "2", 471 strProceedURL); 472 } 473 else 474 { 475 hsrqRequest.setAttribute(DATA_ATTRIBUTE_REQUEST_PARAM + "2", 476 WebCommonConstants.DEFAULT_DIRECTORY_WEB_PAGE); 477 } 478 if (cause != null) 479 { 480 hsrqRequest.setAttribute(DATA_ATTRIBUTE_REQUEST_PARAM + "3", 481 cause); 482 } 483 displayUI(strUIPage, hsrqRequest, hsrpResponse); 484 } 485 486 488 496 protected int getFormToProcess( 497 HttpServletRequest hsrqRequest 498 ) 499 { 500 return FORM_UNKNOWN_ID; 501 } 502 } 503 | Popular Tags |