Class ContextUtils

java.lang.Object
com.webmethods.caf.faces.context.ContextUtils

public class ContextUtils extends Object
Utility functions for dealing with context/user information. Functions include logging, recording messages to the FacesContext, retrieving resource bundle strings and loading resources from the classpath.
  • Field Details

    • FACES_RESOURCES

      public static final String FACES_RESOURCES
      See Also:
    • DEFAULT_CURRENCY_FORMAT_EXPRESSION

      public static final String DEFAULT_CURRENCY_FORMAT_EXPRESSION
      See Also:
    • DEFAULT_NUMBER_FORMAT_EXPRESSION

      public static final String DEFAULT_NUMBER_FORMAT_EXPRESSION
      See Also:
    • DEFAULT_DATE_FORMAT_EXPRESSION

      public static final String DEFAULT_DATE_FORMAT_EXPRESSION
      See Also:
    • DEFAULT_TIME_FORMAT_EXPRESSION

      public static final String DEFAULT_TIME_FORMAT_EXPRESSION
      See Also:
    • DEFAULT_TIME_ZONE_EXPRESSION

      public static final String DEFAULT_TIME_ZONE_EXPRESSION
      See Also:
    • ICONTEXT_KEY

      protected static final String ICONTEXT_KEY
    • RE_FORMATTED_MESSAGE_ELEMENT

      protected static final Pattern RE_FORMATTED_MESSAGE_ELEMENT
    • RE_DATE_TIME_ORDER_ELEMENT

      protected static final Pattern RE_DATE_TIME_ORDER_ELEMENT
    • ATTR_APP_VERSION

      protected static final String ATTR_APP_VERSION
    • RE_BUILD_TIMESTAMP

      protected static final Pattern RE_BUILD_TIMESTAMP
    • RE_BUILD_VERSION

      protected static final Pattern RE_BUILD_VERSION
    • LAST_MODIFIED_VERSION_FORMAT

      protected static final DateFormat LAST_MODIFIED_VERSION_FORMAT
  • Constructor Details

    • ContextUtils

      public ContextUtils()
  • Method Details

    • localizeMessage

      public static String localizeMessage(String key)
      Returns the message from the default faces resource bundle with the specified key.
      Parameters:
      key - the resource bundle key.
    • localizeMessage

      public static String localizeMessage(String key, Object[] args)
      Returns the formatted message from the default faces resource bundle with the specified key
      Parameters:
      key - Resource bundle key as String
      args - MessageFormat formatted string arguments as an Object[], or null.
      Returns:
      text from the resource bundle after string arguments have been applied
    • localizeMessage

      public static String localizeMessage(String key, Object[] args, boolean prependKey)
      Returns the message from the default faces resource bundle with the specified key.
      Parameters:
      key - Resource bundle key as String
      args - MessageFormat formatted string arguments as an Object[], or null.
      prependKey - True to prepend the key to formatted message (for an error message).
      Returns:
      text from the resource bundle after string arguments have been applied
    • localizeMessage

      public static String localizeMessage(Class<?> bundleClass, String key)
      Returns the message from the specified resource bundle with the specified key.
      Parameters:
      bundleClass - Resource bundle class, or null for default faces resource bundle class.
      key - Resource bundle key as String
      Returns:
      the text from the resource bundle
    • localizeMessage

      public static String localizeMessage(FacesContext ctx, Class<?> bundleClass, String key)
      Returns the message from the specified resource bundle with the specified key.
      Parameters:
      ctx - Current context, or null for current tls instance.
      bundleClass - Resource bundle class, or null for default faces resource bundle class.
      key - Resource bundle key as String
      Returns:
      text from the resource bundle
    • localizeMessage

      public static String localizeMessage(FacesContext ctx, Class<?> bundleClass, String key, Object[] args)
      Returns the message from the specified resource bundle with the specified key, formatted with the specified MessageFormat arguments.
      Parameters:
      ctx - Current context, or null for current tls instance.
      bundleClass - Resource bundle class, or null for default faces resource bundle class.
      key - Resource bundle key as String
      args - MessageFormat formatted string arguments as an Object[], or null.
      Returns:
      the text from the resource bundle after string arguments have been applied
    • localizeMessage

      public static String localizeMessage(FacesContext ctx, Class<?> bundleClass, String key, Object[] args, boolean prependKey)
      Returns the message from the specified resource bundle with the specified key, formatted with the specified MessageFormat arguments.
      Parameters:
      ctx - Current context, or null for current tls instance.
      bundleClass - Resource bundle class, or null for default faces resource bundle class.
      key - Resource bundle key as String
      args - MessageFormat formatted string arguments as an Object[], or null.
      prependKey - True to prepend key to formatted message (for an error message).
      Returns:
      the text from the resource bundle after string arguments have been applied
    • localizeMessage

      public static String localizeMessage(FacesContext ctx, String bundleName, String key, Object[] args)
      Returns the message from the specified resource bundle with the specified key, formatted with the specified MessageFormat arguments.
      Parameters:
      ctx - Current context, or null for current tls instance.
      bundleName - Resource bundle name, or null for default faces resource bundle class.
      key - Resource bundle key as String
      args - MessageFormat formatted string arguments as an Object[], or null.
      Returns:
      the text from the resource bundle after string arguments have been applied
    • localizeMessage

      public static String localizeMessage(FacesContext ctx, String bundleName, String key, Object[] args, boolean prependKey) throws MissingResourceException
      Returns the message from the specified resource bundle with the specified key, formatted with the specified MessageFormat arguments.
      Parameters:
      ctx - Current context, or null for current tls instance.
      bundleName - Resource bundle name, or null for default faces resource bundle class.
      key - Resource bundle key as String
      args - MessageFormat formatted string arguments as an Object[], or null.
      prependKey - True to prepend key to formatted message (for an error message).
      Returns:
      the text from the resource bundle after string arguments have been applied
      Throws:
      MissingResourceException - if bundle could not be found OR if resource could not be found in the bundle.
    • localizeMessage

      public static String localizeMessage(FacesContext ctx, String bundleName, String key, Object[] args, boolean prependKey, boolean catchMissingResourceException)
      Returns the message from the specified resource bundle with the specified key, formatted with the specified MessageFormat arguments.
      Parameters:
      ctx - Current context, or null for current tls instance.
      bundleName - Resource bundle name, or null for default faces resource bundle class.
      key - Resource bundle key as String
      args - MessageFormat formatted string arguments as an Object[], or null.
      prependKey - True to prepend key to formatted message (for an error message).
      catchMissingResourceException - true to catch any MissingResourceExceptions
      Returns:
      the text from the resource bundle after string arguments have been applied
    • formatMessage

      public static String formatMessage(FacesContext ctx, String message, Object[] args)
      Formats specified message with the specified arguments using MessageFormat. Uses current user's locale, plus user's preferred default date/time/number patterns.
      Parameters:
      ctx - Current faces context.
      message - Message to format (ie "Distance is {0,number} km.").
      args - Arguments with which to format message.
      Returns:
      Formatted message. Never null.
    • _applyUserPrefsToMessagePattern

      protected static String _applyUserPrefsToMessagePattern(FacesContext ctx, String message, Object[] args)
      Applies user preferences for date/time/number formatting to message pattern.
      Parameters:
      ctx - Current faces context.
      message - Message to format (ie "Distance is {0,number} km.").
      args - (optional) Arguments with which to format message.
      Returns:
      Updated message pattern. Never null.
    • getResponseLocale

      public static Locale getResponseLocale(FacesContext ctx)
      Calculates the locale to use for the response.
      Parameters:
      ctx - Current context, or null for current thread instance.
      Returns:
      calculated Locale
    • getResponseIContext

      public static com.softwareag.g11n.util.iContext getResponseIContext(FacesContext ctx)
      Calculates the locale to use for the response.
      Parameters:
      ctx - Current context, or null for current thread instance.
      Returns:
      calculated Locale
    • findBundle

      public static ResourceBundle findBundle(FacesContext ctx, String name) throws MissingResourceException
      Finds a resource bundle by name using the current response locale
      Parameters:
      ctx - Current context, or null for current thread instance.
      name - Name of the resource bundle as String
      Throws:
      MissingResourceException - if bundle could not be found.
    • getResourceInputStream

      public static InputStream getResourceInputStream(String relPath)
      Returns an InputStream for a resource at the given path
      Parameters:
      relPath - Relative path to the resource as String
      Returns:
      InputStream containing the contents of the specified resource
    • error

      public static void error(FacesMessage.Severity severity, String message, String details)
    • error

      public static void error(String message, String details)
    • error

      public static void error(String message)
    • error

      public static void error(Throwable throwable)
    • error

      public static void error(String message, Throwable throwable)
    • getFacesContext

      public static FacesContext getFacesContext()
      Retrieve the current FacesContext
      Returns:
      the current FacesContext for this thread
    • getAppVersion

      public static String getAppVersion(FacesContext context)
      Calculate the application version of the current context.
      Parameters:
      context - Current faces context.
      Returns:
      Version string. Null or empty string to signal unknown.
    • initializeResponseWriter

      public static void initializeResponseWriter(FacesContext context) throws IOException
      Initialize the ResponseWriter for the servlet context if it has not already been created.
      Parameters:
      context - the context to initialize.
      Throws:
      IOException
    • isCafView

      public static boolean isCafView(FacesContext ctx)
      Helper to check if the faces context is for a CAF view
    • isCafView

      public static boolean isCafView(FacesContext ctx, String viewId)
      Helper to check if the faces context is for a CAF view
    • getServletContext

      @Deprecated public static ServletContext getServletContext(String webAppName) throws Exception
      Deprecated.
      use {@link RuntimeUtilOsgi.getServletContext(String webAppName)} instead.
      Gets the servlet context that corresponds to the supplied servlet context path
      Parameters:
      webAppName - the servlet context path to match
      Returns:
      the found servlet context or null if no match
      Throws:
      Exception
    • getWebAppClassLoader

      public static ClassLoader getWebAppClassLoader(ServletContext servletContext)
      Gets the classloader for the supplied servlet context
      Parameters:
      servletContext -
      Returns:
    • clearFacesContextDetails

      public static ContextUtils.FacesContextDetails clearFacesContextDetails()
      Clears out the current faces context and other related objects
      Returns:
      details of the previous faces context
    • restoreFacesContextDetails

      public static void restoreFacesContextDetails(ContextUtils.FacesContextDetails details)
      Restores the current faces context and other related objects
      Parameters:
      details - details of the previous faces context
    • setCurrentFacesContext

      public static void setCurrentFacesContext(FacesContext facesContext)
      Set the current faces context for this thread. NOTE: It is usually safer to use the clearFacesContextDetails() + restoreFacesContextDetails(FacesContextDetails) instead.
      Parameters:
      facesContext - the faces context for this thread