Package com.webmethods.caf.faces.context
Class ContextUtils
java.lang.Object
com.webmethods.caf.faces.context.ContextUtils
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Stores information about FacesContext that can be used to switch back to a previous FacesContext -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final String
static final String
static final String
static final String
static final String
static final String
static final String
protected static final String
protected static final DateFormat
protected static final Pattern
protected static final Pattern
protected static final Pattern
protected static final Pattern
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected static String
_applyUserPrefsToMessagePattern
(FacesContext ctx, String message, Object[] args) Applies user preferences for date/time/number formatting to message pattern.Clears out the current faces context and other related objectsstatic void
static void
static void
static void
static void
error
(FacesMessage.Severity severity, String message, String details) static ResourceBundle
findBundle
(FacesContext ctx, String name) Finds a resource bundle by name using the current response localestatic String
formatMessage
(FacesContext ctx, String message, Object[] args) Formats specified message with the specified arguments usingMessageFormat
.static String
getAppVersion
(FacesContext context) Calculate the application version of the current context.static FacesContext
Retrieve the current FacesContextstatic InputStream
getResourceInputStream
(String relPath) Returns an InputStream for a resource at the given pathstatic com.softwareag.g11n.util.iContext
Calculates the locale to use for the response.static Locale
Calculates the locale to use for the response.static ServletContext
getServletContext
(String webAppName) Deprecated.use {@link RuntimeUtilOsgi.getServletContext(String webAppName)} instead.static ClassLoader
getWebAppClassLoader
(ServletContext servletContext) Gets the classloader for the supplied servlet contextstatic void
initializeResponseWriter
(FacesContext context) Initialize the ResponseWriter for the servlet context if it has not already been created.static boolean
isCafView
(FacesContext ctx) Helper to check if the faces context is for a CAF viewstatic boolean
isCafView
(FacesContext ctx, String viewId) Helper to check if the faces context is for a CAF viewstatic String
localizeMessage
(Class<?> bundleClass, String key) Returns the message from the specified resource bundle with the specified key.static String
localizeMessage
(String key) Returns the message from the default faces resource bundle with the specified key.static String
localizeMessage
(String key, Object[] args) Returns the formatted message from the default faces resource bundle with the specified keystatic String
localizeMessage
(String key, Object[] args, boolean prependKey) Returns the message from the default faces resource bundle with the specified key.static String
localizeMessage
(FacesContext ctx, Class<?> bundleClass, String key) Returns the message from the specified resource bundle with the specified key.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 specifiedMessageFormat
arguments.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 specifiedMessageFormat
arguments.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 specifiedMessageFormat
arguments.static String
localizeMessage
(FacesContext ctx, String bundleName, String key, Object[] args, boolean prependKey) Returns the message from the specified resource bundle with the specified key, formatted with the specifiedMessageFormat
arguments.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 specifiedMessageFormat
arguments.static void
Restores the current faces context and other related objectsstatic void
setCurrentFacesContext
(FacesContext facesContext) Set the current faces context for this thread.
-
Field Details
-
FACES_RESOURCES
- See Also:
-
DEFAULT_CURRENCY_FORMAT_EXPRESSION
- See Also:
-
DEFAULT_NUMBER_FORMAT_EXPRESSION
- See Also:
-
DEFAULT_DATE_FORMAT_EXPRESSION
- See Also:
-
DEFAULT_TIME_FORMAT_EXPRESSION
- See Also:
-
DEFAULT_TIME_ZONE_EXPRESSION
- See Also:
-
ICONTEXT_KEY
-
RE_FORMATTED_MESSAGE_ELEMENT
-
RE_DATE_TIME_ORDER_ELEMENT
-
ATTR_APP_VERSION
-
RE_BUILD_TIMESTAMP
-
RE_BUILD_VERSION
-
LAST_MODIFIED_VERSION_FORMAT
-
-
Constructor Details
-
ContextUtils
public ContextUtils()
-
-
Method Details
-
localizeMessage
Returns the message from the default faces resource bundle with the specified key.- Parameters:
key
- the resource bundle key.
-
localizeMessage
Returns the formatted message from the default faces resource bundle with the specified key- Parameters:
key
- Resource bundle key as Stringargs
-MessageFormat
formatted string arguments as an Object[], or null.- Returns:
- text from the resource bundle after string arguments have been applied
-
localizeMessage
Returns the message from the default faces resource bundle with the specified key.- Parameters:
key
- Resource bundle key as Stringargs
-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
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
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 specifiedMessageFormat
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 Stringargs
-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 specifiedMessageFormat
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 Stringargs
-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 specifiedMessageFormat
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 Stringargs
-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 specifiedMessageFormat
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 Stringargs
-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 specifiedMessageFormat
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 Stringargs
-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
Formats specified message with the specified arguments usingMessageFormat
. 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
Calculates the locale to use for the response.- Parameters:
ctx
- Current context, or null for current thread instance.- Returns:
- calculated Locale
-
getResponseIContext
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
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
-
error
-
error
-
error
-
error
-
getFacesContext
Retrieve the current FacesContext- Returns:
- the current FacesContext for this thread
-
getAppVersion
Calculate the application version of the current context.- Parameters:
context
- Current faces context.- Returns:
- Version string. Null or empty string to signal unknown.
-
initializeResponseWriter
Initialize the ResponseWriter for the servlet context if it has not already been created.- Parameters:
context
- the context to initialize.- Throws:
IOException
-
isCafView
Helper to check if the faces context is for a CAF view -
isCafView
Helper to check if the faces context is for a CAF view -
getServletContext
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
Gets the classloader for the supplied servlet context- Parameters:
servletContext
-- Returns:
-
clearFacesContextDetails
Clears out the current faces context and other related objects- Returns:
- details of the previous faces context
-
restoreFacesContextDetails
Restores the current faces context and other related objects- Parameters:
details
- details of the previous faces context
-
setCurrentFacesContext
Set the current faces context for this thread. NOTE: It is usually safer to use theclearFacesContextDetails()
+restoreFacesContextDetails(FacesContextDetails)
instead.- Parameters:
facesContext
- the faces context for this thread
-