Package com.groiss.util
Class ThreadContext
java.lang.Object
com.groiss.util.ThreadContext
This class is designed to hold thread specific information which can then be
requested anywhere in the code.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final short
session typesstatic final short
protected static final org.slf4j.Logger
static final short
static final short
static final short
static final short
static final short
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
addThreadLocale
(ThreadLocal<?> local) static void
clean()
Clean the thread context.static <T> T
getAttribute
(Object key) Gets the value identified by the passed keyReturns an iterator over all keys which were set via methodor #switchAttribute(Object, Object)
static String
Get the thread sessionId.static ThreadLocal2<String>
Get the sessions.static short
Get the type of the Sessionstatic Locale
Get the thread locale.static <T extends Principal>
TGet the thread principal, the user who performs the action.static HttpServletRequest
Get the HttpServletRequest of this thread.static HttpServletResponse
Get the HttpServletResponse of this thread.static TimeZone
Get the thread TimeZonestatic boolean
Is the thread privileged.static void
removeAttribute
(Object key) Removes the key-value pair identified by the passed keystatic void
setAttribute
(Object key, Object value) Adds a key-value pair to the thread's context.static void
Set the thread privileged.static void
Set the session id of the thread.static void
setSessionType
(short s) Set the type of this thread.static void
Set the locale of the thread.static void
Set the principal of the thread.static void
Set the HttpServletRequest of the thread.static void
Set the HttpServletResponse of the thread.static void
Set the TimeZone of the thread.static SilentCloseable
switchAttribute
(Object key, Object value) Temporarily change an attribute of the thread.static SilentCloseable
switchThreadLocale
(Locale locale) Temporarily change the locale of the thread.static SilentCloseable
Change the principal of the thread.static SilentCloseable
Temporarily change the time zone of the thread.
-
Field Details
-
logger
protected static final org.slf4j.Logger logger -
HTTP_SESSION
public static final short HTTP_SESSIONsession types- See Also:
-
RMI_SESSION
public static final short RMI_SESSION- See Also:
-
INTERNAL_SESSION
public static final short INTERNAL_SESSION- See Also:
-
WEBDAV_SESSION
public static final short WEBDAV_SESSION- See Also:
-
WEBSERVICE_SESSION
public static final short WEBSERVICE_SESSION- See Also:
-
WOPI_SESSION
public static final short WOPI_SESSION- See Also:
-
RESTAPI_SESSION
public static final short RESTAPI_SESSION- See Also:
-
-
Method Details
-
setThreadLocale
Set the locale of the thread.- Parameters:
l
- a locale object
-
getThreadLocale
Get the thread locale. If the thread has no locale set, the method returns the default locale.- Returns:
- a Locale object
-
setThreadPrincipal
Set the principal of the thread.- Parameters:
p
- a Principal object
-
switchThreadPrincipal
Change the principal of the thread.- Parameters:
p
- a Principal object- Returns:
SilentCloseable
may be used in combination with a try-with-resources statement
Upon leaving the block, the principal is automatically re-set to the old value it had when entering the block.try(
SilentCloseable
closable =switchThreadPrincipal(Principal)
) { ... }
-
switchThreadLocale
Temporarily change the locale of the thread.- Parameters:
locale
- aLocale
object- Returns:
SilentCloseable
: should be used in combination with a try-with-resources statement
Upon leaving the block, the locale is automatically re-set to the old value it had when entering the block.try(
SilentCloseable
closable =switchThreadLocale(Locale)
) { ... }
-
switchThreadTimeZone
Temporarily change the time zone of the thread.- Parameters:
tz
- aTimeZone
object- Returns:
SilentCloseable
: should be used in combination with a try-with-resources statement
Upon leaving the block, the time zone is automatically re-set to the old value it had when entering the block.try(
SilentCloseable
closable =switchThreadTimeZone(TimeZone)
) { ... }
-
switchAttribute
Temporarily change an attribute of the thread. If the key is already contained in the context, the old value is replaced.- Parameters:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key- Returns:
SilentCloseable
: should be used in combination with a try-with-resources statement
Upon leaving the block, the attribute is automatically re-set to the old value it had when entering the block; if there was no such value, the attribute is removed.try(
SilentCloseable
closable =switchAttribute(Object,Object)
) { ... }
-
getThreadPrincipal
Get the thread principal, the user who performs the action.- Returns:
- a Principal object
-
setSessionId
Set the session id of the thread.- Parameters:
s
- a string object
-
getSessionId
Get the thread sessionId.- Returns:
- a String object
-
setThreadTimeZone
Set the TimeZone of the thread.- Parameters:
t
- a TimeZone object
-
getThreadTimeZone
Get the thread TimeZone- Returns:
- a TimeZone object
-
getSessions
Get the sessions.- Returns:
- a ThreadLocal2 object
-
setPrivileged
public static void setPrivileged()Set the thread privileged. -
isPrivileged
public static boolean isPrivileged()Is the thread privileged.- Returns:
- true if user of this thread is sysadm
-
setThreadRequest
Set the HttpServletRequest of the thread.- Parameters:
req
- a HttpServletRequest object
-
getThreadRequest
Get the HttpServletRequest of this thread.- Returns:
- a HttpServletRequest object
-
setThreadResponse
Set the HttpServletResponse of the thread.- Parameters:
req
- a HttpServletResponse object
-
getThreadResponse
Get the HttpServletResponse of this thread. Be careful when manipulating the response. Setting cookies, headers etc. can only be done on uncommitted responses. CallingServletResponse.getOutputStream()
orServletResponse.getWriter()
in a servletcontext where actually aPage
is returned, is not recommended- Returns:
- a HttpServletResponse object
-
getSessionType
public static short getSessionType()Get the type of the Session- Returns:
- the Session type.
-
setSessionType
public static void setSessionType(short s) Set the type of this thread.- Parameters:
s
- either HTTP_SESSION, RMI_SESSION or INTERNAL_SESSION
-
setAttribute
Adds a key-value pair to the thread's context. If the key is already contained, the old value is replaced.- Parameters:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key
-
getAttribute
Gets the value identified by the passed key- Parameters:
key
- key whose value should be retrieved- Returns:
- the value object
-
removeAttribute
Removes the key-value pair identified by the passed key- Parameters:
key
- key whose value should be removed
-
getAttributeKeys
Returns an iterator over all keys which were set via methodor #switchAttribute(Object, Object)
- Returns:
- iterator containing the key objects or null if no attributes were set
-
clean
public static void clean()Clean the thread context. -
addThreadLocale
-