KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > app > Application


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.app;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Locale JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Properties JavaDoc;
23 import java.util.ResourceBundle JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 import javax.faces.context.FacesContext;
27 import javax.portlet.PortletContext;
28 import javax.portlet.PortletSession;
29 import javax.servlet.ServletContext JavaDoc;
30 import javax.servlet.ServletException JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33 import javax.servlet.http.HttpSession JavaDoc;
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 /**
48  * Utilities class
49  *
50  * @author gavinc
51  */

52 public class Application
53 {
54    private static final String JavaDoc LOCALE = "locale";
55    
56    public static final String JavaDoc BEAN_CONFIG_SERVICE = "webClientConfigService";
57    public static final String JavaDoc BEAN_DATA_DICTIONARY = "dataDictionary";
58    public static final String JavaDoc BEAN_IMPORTER_BOOTSTRAP = "spacesBootstrap";
59    
60    public static final String JavaDoc MESSAGE_BUNDLE = "alfresco.messages.webclient";
61    
62    private static boolean inPortalServer = false;
63    private static StoreRef repoStoreRef;
64    private static String JavaDoc rootPath;
65    private static String JavaDoc companyRootId;
66    private static String JavaDoc glossaryFolderName;
67    private static String JavaDoc spaceTemplatesFolderName;
68    private static String JavaDoc contentTemplatesFolderName;
69    private static String JavaDoc savedSearchesFolderName;
70    
71    /**
72     * Private constructor to prevent instantiation of this class
73     */

74    private Application()
75    {
76    }
77    
78    /**
79     * Sets whether this application is running inside a portal server
80     *
81     * @param inPortal true to indicate the application is running as a portlet
82     */

83    public static void setInPortalServer(boolean inPortal)
84    {
85       inPortalServer = inPortal;
86    }
87    
88    /**
89     * Determines whether the server is running in a portal
90     *
91     * @return true if we are running inside a portal server
92     */

93    public static boolean inPortalServer()
94    {
95       return inPortalServer;
96    }
97    
98    /**
99     * Handles errors thrown from servlets
100     *
101     * @param servletContext The servlet context
102     * @param request The HTTP request
103     * @param response The HTTP response
104     * @param error The exception
105     * @param logger The logger
106     */

107    public static void handleServletError(ServletContext JavaDoc servletContext, HttpServletRequest JavaDoc request,
108          HttpServletResponse JavaDoc response, Throwable JavaDoc error, Log logger, String JavaDoc returnPage)
109       throws IOException JavaDoc, ServletException JavaDoc
110    {
111       // get the error bean from the session and set the error that occurred.
112
HttpSession JavaDoc 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       // try and find the configured error page
123
boolean errorShown = false;
124       String JavaDoc 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 we could not show the error page for whatever reason, re-throw the error
149
if (!errorShown)
150       {
151          if (error instanceof IOException JavaDoc)
152          {
153             throw (IOException JavaDoc)error;
154          }
155          else if (error instanceof ServletException JavaDoc)
156          {
157             throw (ServletException JavaDoc)error;
158          }
159          else
160          {
161             throw new ServletException JavaDoc(error);
162          }
163       }
164    }
165    
166    /**
167     * Retrieves the configured error page for the application
168     *
169     * @param servletContext The servlet context
170     * @return The configured error page or null if the configuration is missing
171     */

172    public static String JavaDoc getErrorPage(ServletContext JavaDoc servletContext)
173    {
174       return getErrorPage(WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext));
175    }
176    
177    /**
178     * Retrieves the configured error page for the application
179     *
180     * @param portletContext The portlet context
181     * @return
182     */

183    public static String JavaDoc getErrorPage(PortletContext portletContext)
184    {
185       return getErrorPage((WebApplicationContext)portletContext.getAttribute(
186             WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
187    }
188    
189    /**
190     * Retrieves the configured login page for the application
191     *
192     * @param servletContext The servlet context
193     * @return The configured login page or null if the configuration is missing
194     */

195    public static String JavaDoc getLoginPage(ServletContext JavaDoc servletContext)
196    {
197       return getLoginPage(WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext));
198    }
199    
200    /**
201     * Retrieves the configured login page for the application
202     *
203     * @param portletContext The portlet context
204     * @return
205     */

206    public static String JavaDoc getLoginPage(PortletContext portletContext)
207    {
208       return getLoginPage((WebApplicationContext)portletContext.getAttribute(
209             WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
210    }
211    
212    /**
213     * @return Returns the User object representing the currently logged in user
214     */

215    public static User getCurrentUser(HttpSession JavaDoc session)
216    {
217       return (User)session.getAttribute(AuthenticationHelper.AUTHENTICATION_USER);
218    }
219    
220    /**
221     * @return Returns the User object representing the currently logged in user
222     */

223    public static User getCurrentUser(FacesContext context)
224    {
225       return (User)context.getExternalContext().getSessionMap().get(AuthenticationHelper.AUTHENTICATION_USER);
226    }
227    
228    /**
229     * @return Returns the repository store URL (retrieved from config service)
230     */

231    public static StoreRef getRepositoryStoreRef(ServletContext JavaDoc context)
232    {
233       return getRepositoryStoreRef(WebApplicationContextUtils.getRequiredWebApplicationContext(context));
234    }
235    
236    /**
237     * @return Returns the repository store URL (retrieved from config service)
238     */

239    public static StoreRef getRepositoryStoreRef(FacesContext context)
240    {
241       return getRepositoryStoreRef(FacesContextUtils.getRequiredWebApplicationContext(context));
242    }
243    
244    /**
245     * @return Returns id of the company root
246     */

247    public static String JavaDoc getCompanyRootId()
248    {
249       return companyRootId;
250    }
251    
252    /**
253     * Sets the company root id. This is setup by the ContextListener.
254     *
255     * @param id The company root id
256     */

257    public static void setCompanyRootId(String JavaDoc id)
258    {
259       companyRootId = id;
260    }
261    
262    /**
263     * @return Returns the root path for the application
264     */

265    public static String JavaDoc getRootPath(ServletContext JavaDoc context)
266    {
267       return getRootPath(WebApplicationContextUtils.getRequiredWebApplicationContext(context));
268    }
269    
270    /**
271     * @return Returns the root path for the application
272     */

273    public static String JavaDoc getRootPath(FacesContext context)
274    {
275       return getRootPath(FacesContextUtils.getRequiredWebApplicationContext(context));
276    }
277    
278    /**
279     * @return Returns the glossary folder name
280     */

281    public static String JavaDoc getGlossaryFolderName(ServletContext JavaDoc context)
282    {
283       return getGlossaryFolderName(WebApplicationContextUtils.getRequiredWebApplicationContext(context));
284    }
285    
286    /**
287     * @return Returns the glossary folder name
288     */

289    public static String JavaDoc getGlossaryFolderName(FacesContext context)
290    {
291       return getGlossaryFolderName(FacesContextUtils.getRequiredWebApplicationContext(context));
292    }
293    
294    /**
295     * @return Returns the Space templates folder name
296     */

297    public static String JavaDoc getSpaceTemplatesFolderName(ServletContext JavaDoc context)
298    {
299       return getSpaceTemplatesFolderName(WebApplicationContextUtils.getRequiredWebApplicationContext(context));
300    }
301    
302    /**
303     * @return Returns the Space templates folder name
304     */

305    public static String JavaDoc getSpaceTemplatesFolderName(FacesContext context)
306    {
307       return getSpaceTemplatesFolderName(FacesContextUtils.getRequiredWebApplicationContext(context));
308    }
309    
310    /**
311     * @return Returns the Content templates folder name
312     */

313    public static String JavaDoc getContentTemplatesFolderName(ServletContext JavaDoc context)
314    {
315       return getContentTemplatesFolderName(WebApplicationContextUtils.getRequiredWebApplicationContext(context));
316    }
317    
318    /**
319     * @return Returns the Content templates folder name
320     */

321    public static String JavaDoc getContentTemplatesFolderName(FacesContext context)
322    {
323       return getContentTemplatesFolderName(FacesContextUtils.getRequiredWebApplicationContext(context));
324    }
325    
326    /**
327     * @return Return the Saved Searches folder name
328     */

329    public static String JavaDoc getSavedSearchesFolderName(ServletContext JavaDoc context)
330    {
331       return getSavedSearchesFolderName(WebApplicationContextUtils.getRequiredWebApplicationContext(context));
332    }
333    
334    /**
335     * @return Return the Saved Searches folder name
336     */

337    public static String JavaDoc getSavedSearchesFolderName(FacesContext context)
338    {
339       return getSavedSearchesFolderName(FacesContextUtils.getRequiredWebApplicationContext(context));
340    }
341    
342    /**
343     * Set the language locale for the current user context
344     *
345     * @param context FacesContext for current user
346     * @param code The ISO locale code to set
347     */

348    public static void setLanguage(FacesContext context, String JavaDoc code)
349    {
350       Locale JavaDoc locale = parseLocale(code);
351       
352       // set locale for JSF framework usage
353
context.getViewRoot().setLocale(locale);
354       
355       // set locale for our framework usage
356
context.getExternalContext().getSessionMap().put(LOCALE, locale);
357       
358       // clear the current message bundle - so it's reloaded with new locale
359
context.getExternalContext().getSessionMap().remove(MESSAGE_BUNDLE);
360    }
361    
362    /**
363     * Set the language locale for the current user session
364     *
365     * @param session HttpSession for current user
366     * @param code The ISO locale code to set
367     */

368    public static void setLanguage(HttpSession JavaDoc session, String JavaDoc code)
369    {
370       Locale JavaDoc locale = parseLocale(code);
371       
372       session.putValue(LOCALE, locale);
373       session.removeAttribute(MESSAGE_BUNDLE);
374    }
375    
376    /**
377     * @param code Locale code (java format with underscores) to parse
378     * @return Locale object or default if unable to parse
379     */

380    private static Locale JavaDoc parseLocale(String JavaDoc code)
381    {
382       Locale JavaDoc locale = Locale.getDefault();
383       
384       StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(code, "_");
385       int tokens = t.countTokens();
386       if (tokens == 1)
387       {
388          locale = new Locale JavaDoc(code);
389       }
390       else if (tokens == 2)
391       {
392          locale = new Locale JavaDoc(t.nextToken(), t.nextToken());
393       }
394       else if (tokens == 3)
395       {
396          locale = new Locale JavaDoc(t.nextToken(), t.nextToken(), t.nextToken());
397       }
398       
399       return locale;
400    }
401    
402    /**
403     * Return the language Locale for the current user context
404     *
405     * @param context FacesContext for the current user
406     *
407     * @return Current language Locale set or the VM default if none set
408     */

409    public static Locale JavaDoc getLanguage(FacesContext context)
410    {
411       Locale JavaDoc locale = (Locale JavaDoc)context.getExternalContext().getSessionMap().get(LOCALE);
412       return locale != null ? locale : Locale.getDefault();
413    }
414    
415    /**
416     * Return the language Locale for the current user Session.
417     *
418     * @param session HttpSession for the current user
419     *
420     * @return Current language Locale set or the VM default if none set
421     */

422    public static Locale JavaDoc getLanguage(HttpSession JavaDoc session)
423    {
424       Locale JavaDoc locale = (Locale JavaDoc)session.getAttribute(LOCALE);
425       return locale != null ? locale : Locale.getDefault();
426    }
427    
428    /**
429     * Return the language Locale for the current user PortletSession.
430     *
431     * @param session PortletSession for the current user
432     *
433     * @return Current language Locale set or the VM default if none set
434     */

435    public static Locale JavaDoc getLanguage(PortletSession session)
436    {
437       Locale JavaDoc locale = (Locale JavaDoc)session.getAttribute(LOCALE);
438       return locale != null ? locale : Locale.getDefault();
439    }
440    
441    /**
442     * Get the specified I18N message string from the default message bundle for this user
443     *
444     * @param context FacesContext
445     * @param msg Message ID
446     *
447     * @return String from message bundle or $$msg$$ if not found
448     */

449    public static String JavaDoc getMessage(FacesContext context, String JavaDoc msg)
450    {
451       return getBundle(context).getString(msg);
452    }
453    
454    /**
455     * Get the specified I18N message string from the default message bundle for this user
456     *
457     * @param session HttpSession
458     * @param msg Message ID
459     *
460     * @return String from message bundle or $$msg$$ if not found
461     */

462    public static String JavaDoc getMessage(HttpSession JavaDoc session, String JavaDoc msg)
463    {
464       return getBundle(session).getString(msg);
465    }
466    
467    /**
468     * Get the specified the default message bundle for this user
469     *
470     * @param session HttpSession
471     *
472     * @return ResourceBundle for this user
473     */

474    public static ResourceBundle JavaDoc getBundle(HttpSession JavaDoc session)
475    {
476       ResourceBundle JavaDoc bundle = (ResourceBundle JavaDoc)session.getAttribute(MESSAGE_BUNDLE);
477       if (bundle == null)
478       {
479          // get Locale from language selected by each user on login
480
Locale JavaDoc locale = (Locale JavaDoc)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    /**
494     * Get the specified the default message bundle for this user
495     *
496     * @param context FacesContext
497     *
498     * @return ResourceBundle for this user
499     */

500    public static ResourceBundle JavaDoc getBundle(FacesContext context)
501    {
502       // get the resource bundle for the current locale
503
// we store the bundle in the users session
504
// this makes it easy to add a locale per user support later
505
Map JavaDoc session = context.getExternalContext().getSessionMap();
506       ResourceBundle JavaDoc bundle = (ResourceBundle JavaDoc)session.get(MESSAGE_BUNDLE);
507       if (bundle == null)
508       {
509          // get Locale from language selected by each user on login
510
Locale JavaDoc locale = (Locale JavaDoc)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    /**
524     * Helper to get the ConfigService instance
525     *
526     * @param context FacesContext
527     *
528     * @return ConfigService
529     */

530    public static ConfigService getConfigService(FacesContext context)
531    {
532       return (ConfigService)FacesContextUtils.getRequiredWebApplicationContext(context).getBean(
533             Application.BEAN_CONFIG_SERVICE);
534    }
535    
536    /**
537     * Helper to get the client config element from the config service
538     *
539     * @param context FacesContext
540     * @return The ClientConfigElement
541     */

542    public static ClientConfigElement getClientConfig(FacesContext context)
543    {
544       return (ClientConfigElement)getConfigService(context).getGlobalConfig().
545          getConfigElement(ClientConfigElement.CONFIG_ELEMENT_ID);
546    }
547    
548    /**
549     * Returns the repository store URL
550     *
551     * @param context The spring context
552     * @return The repository store URL to use
553     */

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    /**
566     * Returns the root path for the application
567     *
568     * @param context The spring context
569     * @return The application root path
570     */

571    private static String JavaDoc getRootPath(WebApplicationContext context)
572    {
573       if (rootPath == null)
574       {
575          ImporterBootstrap bootstrap = (ImporterBootstrap)context.getBean(BEAN_IMPORTER_BOOTSTRAP);
576          Properties JavaDoc configuration = bootstrap.getConfiguration();
577          rootPath = configuration.getProperty("spaces.company_home.childname");
578       }
579       
580       return rootPath;
581    }
582    
583    /**
584     * Returns the glossary folder name
585     *
586     * @param context The spring context
587     * @return The glossary folder name
588     */

589    private static String JavaDoc getGlossaryFolderName(WebApplicationContext context)
590    {
591       if (glossaryFolderName == null)
592       {
593          ImporterBootstrap bootstrap = (ImporterBootstrap)context.getBean(BEAN_IMPORTER_BOOTSTRAP);
594          Properties JavaDoc configuration = bootstrap.getConfiguration();
595          glossaryFolderName = configuration.getProperty("spaces.dictionary.childname");
596       }
597       
598       return glossaryFolderName;
599    }
600    
601    /**
602     * Returns the Space Templates folder name
603     *
604     * @param context The spring context
605     * @return The templates folder name
606     */

607    private static String JavaDoc getSpaceTemplatesFolderName(WebApplicationContext context)
608    {
609       if (spaceTemplatesFolderName == null)
610       {
611          ImporterBootstrap bootstrap = (ImporterBootstrap)context.getBean(BEAN_IMPORTER_BOOTSTRAP);
612          Properties JavaDoc configuration = bootstrap.getConfiguration();
613          spaceTemplatesFolderName = configuration.getProperty("spaces.templates.childname");
614       }
615       
616       return spaceTemplatesFolderName;
617    }
618    
619    /**
620     * Returns the Content Templates folder name
621     *
622     * @param context The spring context
623     * @return The templates folder name
624     */

625    private static String JavaDoc getContentTemplatesFolderName(WebApplicationContext context)
626    {
627       if (contentTemplatesFolderName == null)
628       {
629          ImporterBootstrap bootstrap = (ImporterBootstrap)context.getBean(BEAN_IMPORTER_BOOTSTRAP);
630          Properties JavaDoc configuration = bootstrap.getConfiguration();
631          contentTemplatesFolderName = configuration.getProperty("spaces.templates.content.childname");
632       }
633       
634       return contentTemplatesFolderName;
635    }
636    
637    /**
638     * Returns the Saved Searches folder name
639     *
640     * @param context The spring context
641     * @return The saved searches folder name
642     */

643    private static String JavaDoc getSavedSearchesFolderName(WebApplicationContext context)
644    {
645       /*
646        * This lookup is duplicated in a patch. If this logic changes, investigate the changes
647        * required for the patch(es).
648        */

649        
650       if (savedSearchesFolderName == null)
651       {
652          ImporterBootstrap bootstrap = (ImporterBootstrap)context.getBean(BEAN_IMPORTER_BOOTSTRAP);
653          Properties JavaDoc configuration = bootstrap.getConfiguration();
654          savedSearchesFolderName = configuration.getProperty("spaces.savedsearches.childname");
655       }
656       
657       return savedSearchesFolderName;
658    }
659    
660    /**
661     * Retrieves the configured error page for the application
662     *
663     * @param context The Spring contexr
664     * @return The configured error page or null if the configuration is missing
665     */

666    private static String JavaDoc getErrorPage(WebApplicationContext context)
667    {
668       String JavaDoc 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    /**
683     * Retrieves the configured login page for the application
684     *
685     * @param context The Spring contexr
686     * @return The configured login page or null if the configuration is missing
687     */

688    private static String JavaDoc getLoginPage(WebApplicationContext context)
689    {
690       String JavaDoc 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