Package org.ametys.plugins.core.ui.user
Class DefaultProfileImageProvider
- java.lang.Object
-
- org.apache.avalon.framework.logger.AbstractLogEnabled
-
- org.ametys.plugins.core.ui.user.SafeProfileImageProvider
-
- org.ametys.plugins.core.ui.user.DefaultProfileImageProvider
-
- All Implemented Interfaces:
ProfileImageProvider
,Component
,Contextualizable
,LogEnabled
,Serviceable
public class DefaultProfileImageProvider extends SafeProfileImageProvider implements Contextualizable, Component
Helper providing images that are used for user profiles
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
DefaultProfileImageProvider.ProfileImageSource
Profile image source enum-
Nested classes/interfaces inherited from interface org.ametys.plugins.core.ui.user.ProfileImageProvider
ProfileImageProvider.UserProfileImage
-
-
Field Summary
Fields Modifier and Type Field Description protected static String
__AVATAR_DIR_NAME
Name of the avatar directoryprotected static Map<String,String>
__avatarPaths
The map of paths to avatar images, keys are idprotected static String
__INITIALS_DIR_NAME
Name of the initials directoryprotected static List<String>
__initialsBgPaths
Ordered list of paths to available backgrounds for 'initials' imagesprivate Context
_context
protected JSONUtils
_jsonUtils
JSON Utilsprotected UploadManager
_uploadManager
Upload managerprotected UserPreferencesManager
_userPreferencesManager
User pref managerstatic String
USER_PROFILE_PREF_CONTEXT
The pref context for user profilestatic String
USERPREF_PROFILE_IMAGE
The profile image user pref id-
Fields inherited from class org.ametys.plugins.core.ui.user.SafeProfileImageProvider
__DEFAULT_FILE_NAME, __USER_PROFILES_DIR_PATH, _sourceResolver, _userManager
-
Fields inherited from interface org.ametys.plugins.core.ui.user.ProfileImageProvider
ROLE
-
-
Constructor Summary
Constructors Constructor Description DefaultProfileImageProvider()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected InputStream
_addImageBackground(UserIdentity user, InputStream is)
Add a background to an initials imageprotected Map<String,Object>
_extractBase64Parameters(Request request, UserIdentity user)
Extracts parameters for a local imageprotected Map<String,Object>
_extractLocalImageParameters(Request request, UserIdentity user)
Extracts parameters for a local imageprotected Map<String,Object>
_extractSourceParameters(UserIdentity user, DefaultProfileImageProvider.ProfileImageSource profileImageSource)
Provides the necessary parameters to retrieves the image from a given source.protected Map<String,Object>
_extractUploadParameters(Request request, UserIdentity user)
Extracts parameters for an uploaded imageprivate Integer
_getGravatarSize(Map<String,Object> sourceParams)
Extract the gravatar size from the source params if anyprotected Source
_getInitialsBackgroundSource(UserIdentity user)
Get the background image for the initials source.protected Source
_getInitialsImageSource(String initial)
Get the source of the initials imageprotected Map<String,String>
_getLocalImagePaths()
Get the map containing the relative path for each local image.protected Source
_getLocalImageSource(String localFileId)
Get the source of a local imageprivate Map<String,Object>
_getRawUserPrefImage(UserIdentity user)
Get the profile image user prefprivate void
_initializeInitialsBackgroundPaths()
Initializes the list of background paths for initials imagesprivate void
_initializeLocalImagePaths()
Initializes the map of local image pathsvoid
contextualize(Context context)
BufferedImage
cropUploadedImage(InputStream is)
Automatically crop the image to 64x64 pixels.ProfileImageProvider.UserProfileImage
getBase64Image(UserIdentity user, String data, String filename)
Get the image from a base 64 stringProfileImageProvider.UserProfileImage
getImage(UserIdentity user, String imageSource, int size, int maxSize)
Get the avatarProfileImageProvider.UserProfileImage
getImage(DefaultProfileImageProvider.ProfileImageSource source, UserIdentity user, Map<String,Object> sourceParams)
Get the image input streamProfileImageProvider.UserProfileImage
getInitialsImage(UserIdentity userIdentity)
Get the image with user initialsProfileImageProvider.UserProfileImage
getLocalImage(UserIdentity user, String localFileId)
Get the local imageList<String>
getLocalImageIds()
Get the list of local image identifiersDefaultProfileImageProvider.ProfileImageSource
getProfileImageSource(String imageSourceStr)
Get the profile image source given a source input stringProfileImageProvider.UserProfileImage
getUploadedImage(UserIdentity user, String uploadId)
Get the uploaded imageProfileImageProvider.UserProfileImage
getUserPrefImage(UserIdentity user, Map<String,Object> baseSourceParams)
Get the image from the user prefboolean
hasInitialsImage(UserIdentity userIdentity)
Test if the initials image is available for a given userboolean
hasLocalImage(String localFileId)
Test if the local image existsMap<String,Object>
hasUserPrefImage(UserIdentity user)
Test this user as a profile image set in its user prefvoid
service(ServiceManager smanager)
-
Methods inherited from class org.ametys.plugins.core.ui.user.SafeProfileImageProvider
getDefaultImage, getGravatarImage, hasGravatarImage
-
Methods inherited from class org.apache.avalon.framework.logger.AbstractLogEnabled
enableLogging, getLogger, setupLogger, setupLogger, setupLogger
-
-
-
-
Field Detail
-
USER_PROFILE_PREF_CONTEXT
public static final String USER_PROFILE_PREF_CONTEXT
The pref context for user profile- See Also:
- Constant Field Values
-
USERPREF_PROFILE_IMAGE
public static final String USERPREF_PROFILE_IMAGE
The profile image user pref id- See Also:
- Constant Field Values
-
__AVATAR_DIR_NAME
protected static final String __AVATAR_DIR_NAME
Name of the avatar directory- See Also:
- Constant Field Values
-
__INITIALS_DIR_NAME
protected static final String __INITIALS_DIR_NAME
Name of the initials directory- See Also:
- Constant Field Values
-
__avatarPaths
protected static Map<String,String> __avatarPaths
The map of paths to avatar images, keys are id
-
__initialsBgPaths
protected static List<String> __initialsBgPaths
Ordered list of paths to available backgrounds for 'initials' images
-
_uploadManager
protected UploadManager _uploadManager
Upload manager
-
_userPreferencesManager
protected UserPreferencesManager _userPreferencesManager
User pref manager
-
_jsonUtils
protected JSONUtils _jsonUtils
JSON Utils
-
-
Constructor Detail
-
DefaultProfileImageProvider
public DefaultProfileImageProvider()
-
-
Method Detail
-
service
public void service(ServiceManager smanager) throws ServiceException
- Specified by:
service
in interfaceServiceable
- Overrides:
service
in classSafeProfileImageProvider
- Throws:
ServiceException
-
contextualize
public void contextualize(Context context) throws ContextException
- Specified by:
contextualize
in interfaceContextualizable
- Throws:
ContextException
-
getImage
public ProfileImageProvider.UserProfileImage getImage(UserIdentity user, String imageSource, int size, int maxSize) throws ProcessingException
Description copied from interface:ProfileImageProvider
Get the avatar- Specified by:
getImage
in interfaceProfileImageProvider
- Overrides:
getImage
in classSafeProfileImageProvider
- Parameters:
user
- The userimageSource
- The image source. Can be null to get the default onesize
- The size in px. Can be 0.maxSize
- The maxSize in px. Can be 0.- Returns:
- The image
- Throws:
ProcessingException
- If an error occurred
-
getProfileImageSource
public DefaultProfileImageProvider.ProfileImageSource getProfileImageSource(String imageSourceStr)
Get the profile image source given a source input string- Parameters:
imageSourceStr
- The input string representing the source- Returns:
- The profile image source.
-
_extractSourceParameters
protected Map<String,Object> _extractSourceParameters(UserIdentity user, DefaultProfileImageProvider.ProfileImageSource profileImageSource) throws ResourceNotFoundException
Provides the necessary parameters to retrieves the image from a given source.- Parameters:
user
- The userprofileImageSource
- The image source type- Returns:
- A map of parameters
- Throws:
ResourceNotFoundException
- In case of a unhandled source type or if parameters could not be extracted
-
_extractUploadParameters
protected Map<String,Object> _extractUploadParameters(Request request, UserIdentity user)
Extracts parameters for an uploaded image- Parameters:
request
- The requestuser
- The user- Returns:
- A map containing the uploaded file id (key=id)
-
_extractLocalImageParameters
protected Map<String,Object> _extractLocalImageParameters(Request request, UserIdentity user)
Extracts parameters for a local image- Parameters:
request
- The requestuser
- The user- Returns:
- A map containing the local image id (key=id)
-
_extractBase64Parameters
protected Map<String,Object> _extractBase64Parameters(Request request, UserIdentity user)
Extracts parameters for a local image- Parameters:
request
- The requestuser
- The user- Returns:
- A map containing the local image id (key=id)
-
getImage
public ProfileImageProvider.UserProfileImage getImage(DefaultProfileImageProvider.ProfileImageSource source, UserIdentity user, Map<String,Object> sourceParams)
Get the image input stream- Parameters:
source
- The image source typeuser
- The usersourceParams
- The parameters used by the source- Returns:
- The UserProfileImage for the image or null if not found
-
getBase64Image
public ProfileImageProvider.UserProfileImage getBase64Image(UserIdentity user, String data, String filename)
Get the image from a base 64 string- Parameters:
user
- The userdata
- The base64 data representing the imagefilename
- The filename or null if not known- Returns:
- The UserProfileImage for the image or null if not set
-
_getGravatarSize
private Integer _getGravatarSize(Map<String,Object> sourceParams)
Extract the gravatar size from the source params if any- Parameters:
sourceParams
- The source params- Returns:
- The requested image size for gravatar or null if not provided
-
getUserPrefImage
public ProfileImageProvider.UserProfileImage getUserPrefImage(UserIdentity user, Map<String,Object> baseSourceParams)
Get the image from the user pref- Parameters:
user
- The userbaseSourceParams
- The base source params to be merge with the params stored in the user pref- Returns:
- The UserProfileImage for the image or null if not set
-
hasUserPrefImage
public Map<String,Object> hasUserPrefImage(UserIdentity user)
Test this user as a profile image set in its user pref- Parameters:
user
- The user- Returns:
- The map stored in the user pref
-
_getRawUserPrefImage
private Map<String,Object> _getRawUserPrefImage(UserIdentity user)
Get the profile image user pref- Parameters:
user
- The user- Returns:
- The map stored in the user pref
-
getUploadedImage
public ProfileImageProvider.UserProfileImage getUploadedImage(UserIdentity user, String uploadId)
Get the uploaded image- Parameters:
user
- The useruploadId
- The upload identifier- Returns:
- The UserProfileImage for the image or null if not found
-
cropUploadedImage
public BufferedImage cropUploadedImage(InputStream is) throws IOException
Automatically crop the image to 64x64 pixels.- Parameters:
is
- The input stream of the uploaded file- Returns:
- The base64 string
- Throws:
IOException
- If an exception occurs while manipulating streams
-
hasLocalImage
public boolean hasLocalImage(String localFileId)
Test if the local image exists- Parameters:
localFileId
- The local file identifier- Returns:
- True if the image exists
-
getLocalImage
public ProfileImageProvider.UserProfileImage getLocalImage(UserIdentity user, String localFileId)
Get the local image- Parameters:
user
- The userlocalFileId
- The local file identifier- Returns:
- The UserProfileImage for the image or null if not found
-
_getLocalImageSource
protected Source _getLocalImageSource(String localFileId) throws IOException
Get the source of a local image- Parameters:
localFileId
- The local file identifier- Returns:
- The source or null
- Throws:
IOException
- If an error occurs while resolving the source uri
-
getLocalImageIds
public List<String> getLocalImageIds()
Get the list of local image identifiers- Returns:
- Ordered list of identifiers
-
_getLocalImagePaths
protected Map<String,String> _getLocalImagePaths()
Get the map containing the relative path for each local image. Create the map if not existing yet.- Returns:
- Map where keys are ids and values are the relative paths
-
_initializeLocalImagePaths
private void _initializeLocalImagePaths()
Initializes the map of local image paths
-
hasInitialsImage
public boolean hasInitialsImage(UserIdentity userIdentity)
Test if the initials image is available for a given user- Parameters:
userIdentity
- The user- Returns:
- True if the image exists
-
getInitialsImage
public ProfileImageProvider.UserProfileImage getInitialsImage(UserIdentity userIdentity)
Get the image with user initials- Parameters:
userIdentity
- The user- Returns:
- The UserProfileImage for the image or null if not found
-
_getInitialsImageSource
protected Source _getInitialsImageSource(String initial) throws IOException
Get the source of the initials image- Parameters:
initial
- The initial- Returns:
- The source
- Throws:
IOException
- If an error occurs while resolving the source uri
-
_addImageBackground
protected InputStream _addImageBackground(UserIdentity user, InputStream is) throws IOException
Add a background to an initials image- Parameters:
user
- The user used to determine which background will be used (based on a hash representation of the login)is
- The inputstream of the image- Returns:
- The inputstream of the final image with the background
- Throws:
IOException
- If any sort of IO error occurs during the process
-
_getInitialsBackgroundSource
protected Source _getInitialsBackgroundSource(UserIdentity user) throws IOException
Get the background image for the initials source. The chosen background depend on the user login- Parameters:
user
- The user- Returns:
- The source
- Throws:
IOException
- If an error occurs while resolving the source uri
-
_initializeInitialsBackgroundPaths
private void _initializeInitialsBackgroundPaths()
Initializes the list of background paths for initials images
-
-