1 17 package org.alfresco.web.app; 18 19 import java.io.IOException ; 20 import java.util.Locale ; 21 import java.util.Map ; 22 import java.util.Properties ; 23 import java.util.ResourceBundle ; 24 import java.util.StringTokenizer ; 25 26 import javax.faces.context.FacesContext; 27 import javax.portlet.PortletContext; 28 import javax.portlet.PortletSession; 29 import javax.servlet.ServletContext ; 30 import javax.servlet.ServletException ; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 import javax.servlet.http.HttpSession ; 34 35 import org.alfresco.config.ConfigService; 36 import org.alfresco.repo.importer.ImporterBootstrap; 37 import org.alfresco.service.cmr.repository.StoreRef; 38 import org.alfresco.web.app.servlet.AuthenticationHelper; 39 import org.alfresco.web.bean.ErrorBean; 40 import org.alfresco.web.bean.repository.User; 41 import org.alfresco.web.config.ClientConfigElement; 42 import org.apache.commons.logging.Log; 43 import org.springframework.web.context.WebApplicationContext; 44 import org.springframework.web.context.support.WebApplicationContextUtils; 45 import org.springframework.web.jsf.FacesContextUtils; 46 47 52 public class Application 53 { 54 private static final String LOCALE = "locale"; 55 56 public static final String BEAN_CONFIG_SERVICE = "webClientConfigService"; 57 public static final String BEAN_DATA_DICTIONARY = "dataDictionary"; 58 public static final String BEAN_IMPORTER_BOOTSTRAP = "spacesBootstrap"; 59 60 public static final String MESSAGE_BUNDLE = "alfresco.messages.webclient"; 61 62 private static boolean inPortalServer = false; 63 private static StoreRef repoStoreRef; 64 private static String rootPath; 65 private static String companyRootId; 66 private static String glossaryFolderName; 67 private static String spaceTemplatesFolderName; 68 private static String contentTemplatesFolderName; 69 private static String savedSearchesFolderName; 70 71 74 private Application() 75 { 76 } 77 78 83 public static void setInPortalServer(boolean inPortal) 84 { 85 inPortalServer = inPortal; 86 } 87 88 93 public static boolean inPortalServer() 94 { 95 return inPortalServer; 96 } 97 98 107 public static void handleServletError(ServletContext servletContext, HttpServletRequest request, 108 HttpServletResponse response, Throwable error, Log logger, String returnPage) 109 throws IOException , ServletException 110 { 111 HttpSession session = request.getSession(); 113 ErrorBean errorBean = (ErrorBean)session.getAttribute(ErrorBean.ERROR_BEAN_NAME); 114 if (errorBean == null) 115 { 116 errorBean = new ErrorBean(); 117 session.setAttribute(ErrorBean.ERROR_BEAN_NAME, errorBean); 118 } 119 errorBean.setLastError(error); 120 errorBean.setReturnPage(returnPage); 121 122 boolean errorShown = false; 124 String errorPage = getErrorPage(servletContext); 125 126 if (errorPage != null) 127 { 128 if (logger.isDebugEnabled()) 129 logger.debug("An error has occurred, redirecting to error page: " + errorPage); 130 131 if (response.isCommitted() == false) 132 { 133 errorShown = true; 134 response.sendRedirect(request.getContextPath() + errorPage); 135 } 136 else 137 { 138 if (logger.isDebugEnabled()) 139 logger.debug("Response is already committed, re-throwing error"); 140 } 141 } 142 else 143 { 144 if (logger.isDebugEnabled()) 145 logger.debug("No error page defined, re-throwing error"); 146 } 147 148 if (!errorShown) 150 { 151 if (error instanceof IOException ) 152 { 153 throw (IOException )error; 154 } 155 else if (error instanceof ServletException ) 156 { 157 throw (ServletException )error; 158 } 159 else 160 { 161 throw new ServletException (error); 162 } 163 } 164 } 165 166 172 public static String getErrorPage(ServletContext servletContext) 173 { 174 return getErrorPage(WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)); 175 } 176 177 183 public static String getErrorPage(PortletContext portletContext) 184 { 185 return getErrorPage((WebApplicationContext)portletContext.getAttribute( 186 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)); 187 } 188 189 195 public static String getLoginPage(ServletContext servletContext) 196 { 197 return getLoginPage(WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)); 198 } 199 200 206 public static String getLoginPage(PortletContext portletContext) 207 { 208 return getLoginPage((WebApplicationContext)portletContext.getAttribute( 209 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)); 210 } 211 212 215 public static User getCurrentUser(HttpSession session) 216 { 217 return (User)session.getAttribute(AuthenticationHelper.AUTHENTICATION_USER); 218 } 219 220 223 public static User getCurrentUser(FacesContext context) 224 { 225 return (User)context.getExternalContext().getSessionMap().get(AuthenticationHelper.AUTHENTICATION_USER); 226 } 227 228 231 public static StoreRef getRepositoryStoreRef(ServletContext context) 232 { 233 return getRepositoryStoreRef(WebApplicationContextUtils.getRequiredWebApplicationContext(context)); 234 } 235 236 239 public static StoreRef getRepositoryStoreRef(FacesContext context) 240 { 241 return getRepositoryStoreRef(FacesContextUtils.getRequiredWebApplicationContext(context)); 242 } 243 244 247 public static String getCompanyRootId() 248 { 249 return companyRootId; 250 } 251 252 257 public static void setCompanyRootId(String id) 258 { 259 companyRootId = id; 260 } 261 262 265 public static String getRootPath(ServletContext context) 266 { 267 return getRootPath(WebApplicationContextUtils.getRequiredWebApplicationContext(context)); 268 } 269 270 273 public static String getRootPath(FacesContext context) 274 { 275 return getRootPath(FacesContextUtils.getRequiredWebApplicationContext(context)); 276 } 277 278 281 public static String getGlossaryFolderName(ServletContext context) 282 { 283 return getGlossaryFolderName(WebApplicationContextUtils.getRequiredWebApplicationContext(context)); 284 } 285 286 289 public static String getGlossaryFolderName(FacesContext context) 290 { 291 return getGlossaryFolderName(FacesContextUtils.getRequiredWebApplicationContext(context)); 292 } 293 294 297 public static String getSpaceTemplatesFolderName(ServletContext context) 298 { 299 return getSpaceTemplatesFolderName(WebApplicationContextUtils.getRequiredWebApplicationContext(context)); 300 } 301 302 305 public static String getSpaceTemplatesFolderName(FacesContext context) 306 { 307 return getSpaceTemplatesFolderName(FacesContextUtils.getRequiredWebApplicationContext(context)); 308 } 309 310 313 public static String getContentTemplatesFolderName(ServletContext context) 314 { 315 return getContentTemplatesFolderName(WebApplicationContextUtils.getRequiredWebApplicationContext(context)); 316 } 317 318 321 public static String getContentTemplatesFolderName(FacesContext context) 322 { 323 return getContentTemplatesFolderName(FacesContextUtils.getRequiredWebApplicationContext(context)); 324 } 325 326 329 public static String getSavedSearchesFolderName(ServletContext context) 330 { 331 return getSavedSearchesFolderName(WebApplicationContextUtils.getRequiredWebApplicationContext(context)); 332 } 333 334 337 public static String getSavedSearchesFolderName(FacesContext context) 338 { 339 return getSavedSearchesFolderName(FacesContextUtils.getRequiredWebApplicationContext(context)); 340 } 341 342 348 public static void setLanguage(FacesContext context, String code) 349 { 350 Locale locale = parseLocale(code); 351 352 context.getViewRoot().setLocale(locale); 354 355 context.getExternalContext().getSessionMap().put(LOCALE, locale); 357 358 context.getExternalContext().getSessionMap().remove(MESSAGE_BUNDLE); 360 } 361 362 368 public static void setLanguage(HttpSession session, String code) 369 { 370 Locale locale = parseLocale(code); 371 372 session.putValue(LOCALE, locale); 373 session.removeAttribute(MESSAGE_BUNDLE); 374 } 375 376 380 private static Locale parseLocale(String code) 381 { 382 Locale locale = Locale.getDefault(); 383 384 StringTokenizer t = new StringTokenizer (code, "_"); 385 int tokens = t.countTokens(); 386 if (tokens == 1) 387 { 388 locale = new Locale (code); 389 } 390 else if (tokens == 2) 391 { 392 locale = new Locale (t.nextToken(), t.nextToken()); 393 } 394 else if (tokens == 3) 395 { 396 locale = new Locale (t.nextToken(), t.nextToken(), t.nextToken()); 397 } 398 399 return locale; 400 } 401 402 409 public static Locale getLanguage(FacesContext context) 410 { 411 Locale locale = (Locale )context.getExternalContext().getSessionMap().get(LOCALE); 412 return locale != null ? locale : Locale.getDefault(); 413 } 414 415 422 public static Locale getLanguage(HttpSession session) 423 { 424 Locale locale = (Locale )session.getAttribute(LOCALE); 425 return locale != null ? locale : Locale.getDefault(); 426 } 427 428 435 public static Locale getLanguage(PortletSession session) 436 { 437 Locale locale = (Locale )session.getAttribute(LOCALE); 438 return locale != null ? locale : Locale.getDefault(); 439 } 440 441 449 public static String getMessage(FacesContext context, String msg) 450 { 451 return getBundle(context).getString(msg); 452 } 453 454 462 public static String getMessage(HttpSession session, String msg) 463 { 464 return getBundle(session).getString(msg); 465 } 466 467 474 public static ResourceBundle getBundle(HttpSession session) 475 { 476 ResourceBundle bundle = (ResourceBundle )session.getAttribute(MESSAGE_BUNDLE); 477 if (bundle == null) 478 { 479 Locale locale = (Locale )session.getAttribute(LOCALE); 481 if (locale == null) 482 { 483 locale = Locale.getDefault(); 484 } 485 bundle = ResourceBundleWrapper.getResourceBundle(MESSAGE_BUNDLE, locale); 486 487 session.setAttribute(MESSAGE_BUNDLE, bundle); 488 } 489 490 return bundle; 491 } 492 493 500 public static ResourceBundle getBundle(FacesContext context) 501 { 502 Map session = context.getExternalContext().getSessionMap(); 506 ResourceBundle bundle = (ResourceBundle )session.get(MESSAGE_BUNDLE); 507 if (bundle == null) 508 { 509 Locale locale = (Locale )session.get(LOCALE); 511 if (locale == null) 512 { 513 locale = Locale.getDefault(); 514 } 515 bundle = ResourceBundleWrapper.getResourceBundle(MESSAGE_BUNDLE, locale); 516 517 session.put(MESSAGE_BUNDLE, bundle); 518 } 519 520 return bundle; 521 } 522 523 530 public static ConfigService getConfigService(FacesContext context) 531 { 532 return (ConfigService)FacesContextUtils.getRequiredWebApplicationContext(context).getBean( 533 Application.BEAN_CONFIG_SERVICE); 534 } 535 536 542 public static ClientConfigElement getClientConfig(FacesContext context) 543 { 544 return (ClientConfigElement)getConfigService(context).getGlobalConfig(). 545 getConfigElement(ClientConfigElement.CONFIG_ELEMENT_ID); 546 } 547 548 554 private static StoreRef getRepositoryStoreRef(WebApplicationContext context) 555 { 556 if (repoStoreRef == null) 557 { 558 ImporterBootstrap bootstrap = (ImporterBootstrap)context.getBean(BEAN_IMPORTER_BOOTSTRAP); 559 repoStoreRef = bootstrap.getStoreRef(); 560 } 561 562 return repoStoreRef; 563 } 564 565 571 private static String getRootPath(WebApplicationContext context) 572 { 573 if (rootPath == null) 574 { 575 ImporterBootstrap bootstrap = (ImporterBootstrap)context.getBean(BEAN_IMPORTER_BOOTSTRAP); 576 Properties configuration = bootstrap.getConfiguration(); 577 rootPath = configuration.getProperty("spaces.company_home.childname"); 578 } 579 580 return rootPath; 581 } 582 583 589 private static String getGlossaryFolderName(WebApplicationContext context) 590 { 591 if (glossaryFolderName == null) 592 { 593 ImporterBootstrap bootstrap = (ImporterBootstrap)context.getBean(BEAN_IMPORTER_BOOTSTRAP); 594 Properties configuration = bootstrap.getConfiguration(); 595 glossaryFolderName = configuration.getProperty("spaces.dictionary.childname"); 596 } 597 598 return glossaryFolderName; 599 } 600 601 607 private static String getSpaceTemplatesFolderName(WebApplicationContext context) 608 { 609 if (spaceTemplatesFolderName == null) 610 { 611 ImporterBootstrap bootstrap = (ImporterBootstrap)context.getBean(BEAN_IMPORTER_BOOTSTRAP); 612 Properties configuration = bootstrap.getConfiguration(); 613 spaceTemplatesFolderName = configuration.getProperty("spaces.templates.childname"); 614 } 615 616 return spaceTemplatesFolderName; 617 } 618 619 625 private static String getContentTemplatesFolderName(WebApplicationContext context) 626 { 627 if (contentTemplatesFolderName == null) 628 { 629 ImporterBootstrap bootstrap = (ImporterBootstrap)context.getBean(BEAN_IMPORTER_BOOTSTRAP); 630 Properties configuration = bootstrap.getConfiguration(); 631 contentTemplatesFolderName = configuration.getProperty("spaces.templates.content.childname"); 632 } 633 634 return contentTemplatesFolderName; 635 } 636 637 643 private static String getSavedSearchesFolderName(WebApplicationContext context) 644 { 645 649 650 if (savedSearchesFolderName == null) 651 { 652 ImporterBootstrap bootstrap = (ImporterBootstrap)context.getBean(BEAN_IMPORTER_BOOTSTRAP); 653 Properties configuration = bootstrap.getConfiguration(); 654 savedSearchesFolderName = configuration.getProperty("spaces.savedsearches.childname"); 655 } 656 657 return savedSearchesFolderName; 658 } 659 660 666 private static String getErrorPage(WebApplicationContext context) 667 { 668 String errorPage = null; 669 670 ConfigService svc = (ConfigService)context.getBean(BEAN_CONFIG_SERVICE); 671 ClientConfigElement clientConfig = (ClientConfigElement)svc.getGlobalConfig(). 672 getConfigElement(ClientConfigElement.CONFIG_ELEMENT_ID); 673 674 if (clientConfig != null) 675 { 676 errorPage = clientConfig.getErrorPage(); 677 } 678 679 return errorPage; 680 } 681 682 688 private static String getLoginPage(WebApplicationContext context) 689 { 690 String loginPage = null; 691 692 ConfigService svc = (ConfigService)context.getBean(BEAN_CONFIG_SERVICE); 693 ClientConfigElement clientConfig = (ClientConfigElement)svc.getGlobalConfig(). 694 getConfigElement(ClientConfigElement.CONFIG_ELEMENT_ID); 695 696 if (clientConfig != null) 697 { 698 loginPage = clientConfig.getLoginPage(); 699 } 700 701 return loginPage; 702 } 703 } 704 | Popular Tags |