com.day.cq.wcm.commons
Class AbstractImageServlet

java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by org.apache.sling.api.servlets.SlingSafeMethodsServlet
          extended by com.day.cq.wcm.commons.AbstractImageServlet
All Implemented Interfaces:
Serializable, Servlet, ServletConfig

public abstract class AbstractImageServlet
extends SlingSafeMethodsServlet

Servers as base for image servlets

See Also:
Serialized Form

Nested Class Summary
static class AbstractImageServlet.ImageContext
          Convenience class that holds useful stuff needed for image generation
 
Constructor Summary
AbstractImageServlet()
           
 
Method Summary
protected  boolean applyDiff(Layer layer, AbstractImageServlet.ImageContext c)
          Default behavior that applies diff information to the layer
protected  boolean checkModifiedSince(SlingHttpServletRequest req, SlingHttpServletResponse resp)
          Checks if the request contains a if-last-modified-since header and if the node has a jcr:lastModified property. if the properties were modified before the header a 304 is sent otherwise the response last modified header is set.
protected  ImageResource createImageResource(Resource resource)
          Creates an image resource based on the resource in the image context.
protected abstract  Layer createLayer(AbstractImageServlet.ImageContext c)
          Creates the image layer.
protected  void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
          Handles a GET request and created the desired image.
protected  double getImageQuality()
          Returns the image quality. default 1.0
protected  String getImageType()
          Returns the image type. default "image/png"
protected  String getImageType(String ext)
          Returns the image type for the given extension. currently there are only "png", "gif" and "jpg" supported, but an subclass can provide other mappings.
protected  boolean isModifiedDiff(AbstractImageServlet.ImageContext c)
          Calculates if the underlying image was modified in respect to the versioned diff.
protected  void writeLayer(SlingHttpServletRequest request, SlingHttpServletResponse response, AbstractImageServlet.ImageContext context, Layer layer)
          Writes the layer to the response.
protected  void writeLayer(SlingHttpServletRequest request, SlingHttpServletResponse response, AbstractImageServlet.ImageContext context, Layer layer, double quality)
          Writes the layer to the response at the specified quality.
 
Methods inherited from class org.apache.sling.api.servlets.SlingSafeMethodsServlet
doGeneric, doHead, doOptions, doTrace, getAllowedRequestMethods, getServletInfo, handleMethodNotImplemented, mayService, service, service
 
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, init, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractImageServlet

public AbstractImageServlet()
Method Detail

doGet

protected void doGet(SlingHttpServletRequest request,
                     SlingHttpServletResponse response)
              throws ServletException,
                     IOException
Handles a GET request and created the desired image. calls the following methods: If the requested extension does not map to a known image type via getImageType(String), a 404 is responded.

Overrides:
doGet in class SlingSafeMethodsServlet
Parameters:
request - The HTTP request
response - The HTTP response
Throws:
ServletException - Not thrown by this implementation.
IOException - If the error status cannot be reported back to the client.

writeLayer

protected void writeLayer(SlingHttpServletRequest request,
                          SlingHttpServletResponse response,
                          AbstractImageServlet.ImageContext context,
                          Layer layer)
                   throws IOException,
                          RepositoryException
Writes the layer to the response.

Parameters:
request - servlet request
response - servlet response
context - the context
layer - layer
Throws:
IOException - if an I/O error occurs.
RepositoryException - if an repository error occurs.

writeLayer

protected void writeLayer(SlingHttpServletRequest request,
                          SlingHttpServletResponse response,
                          AbstractImageServlet.ImageContext context,
                          Layer layer,
                          double quality)
                   throws IOException,
                          RepositoryException
Writes the layer to the response at the specified quality.

Parameters:
request - servlet request
response - servlet response
context - the context
layer - layer
quality - image quality
Throws:
IOException - if an I/O error occurs.
RepositoryException - if an repository error occurs.

getImageType

protected String getImageType()
Returns the image type. default "image/png"

Returns:
the image type.

getImageType

protected String getImageType(String ext)
Returns the image type for the given extension. currently there are only "png", "gif" and "jpg" supported, but an subclass can provide other mappings.

Parameters:
ext - the extension
Returns:
the image type or null.

getImageQuality

protected double getImageQuality()
Returns the image quality. default 1.0

Returns:
the image quality.

checkModifiedSince

protected boolean checkModifiedSince(SlingHttpServletRequest req,
                                     SlingHttpServletResponse resp)
Checks if the request contains a if-last-modified-since header and if the node has a jcr:lastModified property. if the properties were modified before the header a 304 is sent otherwise the response last modified header is set. If the give doesn't have the property, the parent node is searched.

Parameters:
req - the request
resp - the response
Returns:
true if the response was sent

createLayer

protected abstract Layer createLayer(AbstractImageServlet.ImageContext c)
                              throws RepositoryException,
                                     IOException
Creates the image layer.

Parameters:
c - the convenience context
Returns:
the layer
Throws:
RepositoryException - if an error occurs.
IOException - if an I/O error occurs

applyDiff

protected boolean applyDiff(Layer layer,
                            AbstractImageServlet.ImageContext c)
Default behavior that applies diff information to the layer

Parameters:
layer - the layer
c - the context
Returns:
true if the layer was modified.

createImageResource

protected ImageResource createImageResource(Resource resource)
Creates an image resource based on the resource in the image context. Subclasses can override this to create extended variants of image resources.

Parameters:
resource - the resource
Returns:
the image resource

isModifiedDiff

protected boolean isModifiedDiff(AbstractImageServlet.ImageContext c)
Calculates if the underlying image was modified in respect to the versioned diff.

Parameters:
c - the image context
Returns:
true if modified


Copyright © 2011-2013 Adobe Systems Incorporated. All Rights Reserved.