|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object magoffin.matt.ma2.support.AbstractMediaHandler
public abstract class AbstractMediaHandler
Base class for MediaHandler
implementations.
This class is a good starting point for MediaHandler
implementations to extend. It provides many useful methods common to
most implementations.
The handleMetadata(MediaRequest, Resource, MediaItem)
method
makes use of SmetaMediaMetadata
to extract metadata from media
resources, so is able to handle any file type supported by the
application's configuration of
sMeta.
The configurable properties of this class are:
DomainObjectFactory
to use
for creating new domain objects.MediaBiz
to use.UserBiz
for getting user locale
information from.MediaHandler
. In many cases there is a one-to-one
mapping of MediaHandler
instances to file types they
support, and thus will only need to return a single file extension
at all times. This property can be configured with this
file extension.preferredFileExtension
is configurable in this
class, this MIME type property allows implementations to return
a single MIME type for every file handled by each instance of
this class.SmetaMediaMetadata
instance created in the
getMediaMetadataInstance(MediaRequest, Resource, MediaItem)
method. A Spring BeanWrapper
is used on the newly
created objects, so this provides a way to initialize properties
on those objects after they are created.MediaSize
instances which should not
have a watermark applied, if a user has a watermark configured. This
defaults to all thumbnail sizes.
Field Summary | |
---|---|
protected org.apache.log4j.Logger |
log
A class Logger. |
static java.lang.String |
METADATA_PARAMETER_KEY
An MediaRequest parameter key for a cached MediaMetadata object. |
Constructor Summary | |
---|---|
AbstractMediaHandler(java.lang.String mime)
Construct with MIME type. |
Method Summary | |
---|---|
protected void |
applyEffects(MediaItem item,
MediaRequest request,
MediaResponse response)
Apply the effects for a request. |
protected java.util.List<Metadata> |
createMetadataList(java.util.Map<java.lang.String,java.lang.String> map)
Create a new List of Metadata instances from a Map. |
protected void |
defaultHandleRequestOriginal(MediaItem item,
org.springframework.core.io.Resource itemResource,
MediaResponse response)
Handle the original media data, without altering. |
DomainObjectFactory |
getDomainObjectFactory()
|
MediaEffect |
getEffect(java.lang.String key,
java.util.Map<java.lang.String,?> effectParameters)
Get a MediaEffect instance for a specific key. |
java.lang.String |
getFileExtension(MediaItem item,
MediaRequest request)
Get the file extension. |
MediaBiz |
getMediaBiz()
|
protected MediaMetadata |
getMediaMetadataInstance(MediaRequest request,
org.springframework.core.io.Resource mediaResource,
MediaItem item)
Get a MediaMetadata instance for the given resource. |
java.lang.String |
getMime()
|
java.util.Set<MediaSize> |
getNoWatermarkSizes()
|
java.lang.String |
getPreferredFileExtension()
|
protected java.util.List<MediaEffect> |
getRequestEffects(MediaItem item,
MediaRequest request)
Get a list of effects to apply to a request. |
java.util.Map<java.lang.String,java.lang.Object> |
getSmetaPropertyMap()
|
UserBiz |
getUserBiz()
|
protected MediaMetadata |
handleMetadata(MediaRequest request,
org.springframework.core.io.Resource mediaResource,
MediaItem item)
Extract metadata from a resource and replace item metadata with all extracted data. |
protected boolean |
needToAlter(MediaItem item,
MediaRequest request)
Test if the current request needs to alter the original media item in some way or not. |
protected boolean |
needToRotate(MediaItem item,
MediaRequest request)
Check if rotation needs to be performed for a given media item. |
void |
setDomainObjectFactory(DomainObjectFactory domainObjectFactory)
|
void |
setMediaBiz(MediaBiz mediaBiz)
|
void |
setMime(java.lang.String mime)
|
void |
setNoWatermarkSizes(java.util.Set<MediaSize> noWatermarkSizes)
|
void |
setPreferredFileExtension(java.lang.String preferredFileExtension)
|
void |
setSmetaPropertyMap(java.util.Map<java.lang.String,java.lang.Object> smetaPropertyMap)
|
void |
setUserBiz(UserBiz userBiz)
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface magoffin.matt.ma2.MediaHandler |
---|
createNewMediaItem, handleMediaRequest |
Field Detail |
---|
public static final java.lang.String METADATA_PARAMETER_KEY
MediaRequest
parameter key for a cached MediaMetadata
object.
protected final org.apache.log4j.Logger log
Constructor Detail |
---|
public AbstractMediaHandler(java.lang.String mime)
mime
- the MIME typeMethod Detail |
---|
protected java.util.List<Metadata> createMetadataList(java.util.Map<java.lang.String,java.lang.String> map)
This will return a list of newly-constructed Metadata
objects, using the keys of the map to call
Metadata.setKey(String)
and the corresponding map values
to call Metadata.setValue(String)
. This can be useful
for converting metadata properties returned by sMeta into
Metadata
objects for persisting on a MediaItem
(i.e. by adding the list to MediaItem.getMetadata()
.
map
- the map of properties to turn into Metadata
protected MediaMetadata handleMetadata(MediaRequest request, org.springframework.core.io.Resource mediaResource, MediaItem item)
This method provides a simple way to populate the Metadata
list of a newly created MediaItem
. It will generate the list
of Metadata
and then add all of them into the List returned
by MediaItem.getMetadata()
.
This method first calls getMediaMetadataInstance(MediaRequest, Resource, MediaItem)
to create a new instance of MediaMetadata
and then calls
MediaMetadata.setMediaResource(Resource)
.
If MediaMetadata.getCreationDate()
returns a non-null value
then a new Calendar
instance will be created from it and used
to set the creationDate
property of the MediaItem.
If the MediaMetadata is an instance of EmbeddedImageMetadata
then EmbeddedImageMetadata.getEmbeddedImage()
will be
called, and the width/height of the returned image will be used to
set the width/height properties of the MediaItem provided.
request
- the reqeust (may be null)mediaResource
- the media resource to extract the metadata fromitem
- the item to replace the extracted metadata in
protected MediaMetadata getMediaMetadataInstance(MediaRequest request, org.springframework.core.io.Resource mediaResource, MediaItem item)
MediaMetadata
instance for the given resource.
This implementation returns a new SmetaMediaMetadata
instance.
Extending classes may want to change this behavior or set properties
onto the returned instance.
If the getSmetaPropertyMap()
is configured, then the
created instance will have all the properties defined in that Map
set onto it before being returned. This allows the instance to be
easily configured. The properties are set via a Spring
BeanWrapper
so should follow those naming conventions.
If the request
parameter is not null then
the SmetaMediaMetadata
instance will also be placed
into the parameter Map of the MediaRequest
, using the
key METADATA_PARAMETER_KEY
.
request
- the reqeust (may be null)mediaResource
- the resourceitem
- the item
public java.lang.String getFileExtension(MediaItem item, MediaRequest request)
This method simply returns getPreferredFileExtension()
.
Extending class may override this for request-specific handling.
getFileExtension
in interface MediaHandler
item
- the media item to processrequest
- the request
public MediaEffect getEffect(java.lang.String key, java.util.Map<java.lang.String,?> effectParameters)
MediaHandler
Implementaions must define the keys they support, and define the parameters that go with it.
getEffect
in interface MediaHandler
key
- the key of the desired effecteffectParameters
- a Map of parameters (optional)
protected java.util.List<MediaEffect> getRequestEffects(MediaItem item, MediaRequest request)
This method provides a way to create a list of standard effects useful for every request:
MediaRequest.getSize()
. If the requested width/height are
not the same as those returned by MediaItem.getWidth()
and
MediaItem.getHeight()
then this method will call
MediaHandler.getEffect(String, Map)
using the key
MediaEffect.KEY_SCALE
.MediaRequest.getParameters()
contains
the key MediaEffect.MEDIA_REQUEST_PARAM_ROTATE_DEGREES
,
then this method will call MediaHandler.getEffect(String, Map)
using the key MediaEffect.KEY_ROTATE
.Extending classes that wish to support additional effects can override this method, adding any additional effects to the returned list as desired.
item
- the item the effects are to be applied torequest
- the current request
protected void applyEffects(MediaItem item, MediaRequest request, MediaResponse response)
This method provides a default way to apply effects to a media request.
This method will call getRequestEffects(MediaItem, MediaRequest)
and the call MediaEffect.apply(MediaItem, MediaRequest, MediaResponse)
for each effect returned. Note the MediaHandler implementation must have
set up any required parameters needed by the effects prior to calling this
method.
item
- the item to apply the effects torequest
- the current requestresponse
- the current responseprotected boolean needToAlter(MediaItem item, MediaRequest request)
This method provides a way test if there are any alterations needed to satifsy the request, including:
MediaItem.getWidth()
or MediaItem.getHeight()
.MediaQuality.HIGHEST
.MediaRequest.getEffects()
has at least one effect
in it.needToRotate(MediaItem, MediaRequest)
returns
true.If any of these are found to be true then this method will also return true.
item
- the item being testedrequest
- the current request
protected boolean needToRotate(MediaItem item, MediaRequest request)
This method will return true if any of the following are found to be true:
MediaRequest.getParameters()
Map contains a key
MediaEffect.MEDIA_REQUEST_PARAM_ROTATE_DEGREES
.MediaRequest.getEffects()
contains an effect
where MediaEffect.getKey()
ends with
MediaEffect.KEY_ROTATE
.
item
- the itemrequest
- the request
protected void defaultHandleRequestOriginal(MediaItem item, org.springframework.core.io.Resource itemResource, MediaResponse response)
This method will call MediaResponse.setMimeType(String)
with the value of getMime()
. Then it will call
MediaResponse.setMediaLength(long)
with the value returned
by the itemResource
's File File.length()
.
It will also call MediaResponse.setModifiedDate(long)
with
the value returned by File.lastModified()
.
Finally, it will copy the file to the
MediaResponse.getOutputStream()
.
item
- the item to handleitemResource
- the item's Resourceresponse
- the responsepublic DomainObjectFactory getDomainObjectFactory()
public void setDomainObjectFactory(DomainObjectFactory domainObjectFactory)
domainObjectFactory
- The domainObjectFactory to set.public MediaBiz getMediaBiz()
public void setMediaBiz(MediaBiz mediaBiz)
mediaBiz
- The mediaBiz to set.public java.lang.String getPreferredFileExtension()
public void setPreferredFileExtension(java.lang.String preferredFileExtension)
preferredFileExtension
- The preferredFileExtension to set.public java.lang.String getMime()
public void setMime(java.lang.String mime)
mime
- the mime to setpublic java.util.Map<java.lang.String,java.lang.Object> getSmetaPropertyMap()
public void setSmetaPropertyMap(java.util.Map<java.lang.String,java.lang.Object> smetaPropertyMap)
smetaPropertyMap
- the smetaPropertyMap to setpublic UserBiz getUserBiz()
public void setUserBiz(UserBiz userBiz)
userBiz
- the userBiz to setpublic java.util.Set<MediaSize> getNoWatermarkSizes()
public void setNoWatermarkSizes(java.util.Set<MediaSize> noWatermarkSizes)
noWatermarkSizes
- the noWatermarkSizes to set
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |