public class BasicImageRewriter extends Object implements ResponseRewriter
Security Note: Uses the Sanselan library to parse image content and metadata to avoid security issues in the ImageIO library. Uses ImageIO for output.
| Modifier and Type | Field and Description |
|---|---|
static String |
CONTENT_TYPE |
| Constructor and Description |
|---|
BasicImageRewriter(OptimizerConfig config) |
| Modifier and Type | Method and Description |
|---|---|
protected void |
applyOptimizer(HttpResponseBuilder response,
org.apache.sanselan.ImageFormat imageFormat,
JpegImageUtils.JpegImageParams jpegImageParams,
BufferedImage image,
OptimizerConfig config) |
protected void |
enforceUnreadableImageRestrictions(Uri uri,
HttpResponseBuilder response)
An image could not be read from the content.
|
protected boolean |
isImageUri(Uri uri)
Ensures that the URI points to an image, before continuing.
|
protected boolean |
isSupportedContent(HttpResponseBuilder response) |
protected boolean |
isSupportedImageResult(HttpResponseBuilder response,
Uri uri) |
protected BufferedImage |
readBmp(HttpResponseBuilder response) |
protected BufferedImage |
readGif(HttpResponseBuilder response) |
protected BufferedImage |
readImage(org.apache.sanselan.ImageFormat imageFormat,
HttpResponseBuilder response) |
protected BufferedImage |
readJpeg(HttpResponseBuilder response) |
protected BufferedImage |
readPng(HttpResponseBuilder response) |
BufferedImage |
resizeImage(BufferedImage image,
Integer requestedWidth,
Integer requestedHeight,
int extraWidth,
int extraHeight)
Two-step image resize.
|
void |
rewrite(HttpRequest request,
HttpResponseBuilder response,
Gadget gadget)
Rewrites the response.
|
void |
updateResponse(HttpResponseBuilder response,
BufferedImage image)
If the image is resized, the request needs to change so that the optimizer can
make sensible image size-related decisions down the pipeline.
|
public static final String CONTENT_TYPE
@Inject public BasicImageRewriter(OptimizerConfig config)
public void rewrite(HttpRequest request, HttpResponseBuilder response, Gadget gadget)
ResponseRewriterrewrite in interface ResponseRewriterrequest - The request that was made.response - The response generated as a result of the request.gadget - The gadget that made the request. This parameter may be null.public void updateResponse(HttpResponseBuilder response, BufferedImage image) throws IOException
response - the base response that will be modified with the resized imageimage - the resized image that needs to be substituted for the original image from
the responseIOExceptionpublic BufferedImage resizeImage(BufferedImage image, Integer requestedWidth, Integer requestedHeight, int extraWidth, int extraHeight)
The first step scales the image so that the smaller of the vertical and horizontal scaling ratios is satisfied. For square images the two ratios are equal and we leave it at that. For rectangular images, this leaves a part of the target image rectangle that is not covered, and we need to proceed to step 2.
The second step stretches the image along the dimension that came in short after the first step to fully cover the target image rectangle.
image - the image to resizerequestedWidth - the width in pixels of the requested resulting imagerequestedHeight - the height in pixels of the requested resulting imageextraWidth - the width (in pixels) to add on top of the original imageextraHeight - the height (in pixels) to add on top of the original imagerequestedWidth and requestedHeightprotected void applyOptimizer(HttpResponseBuilder response, org.apache.sanselan.ImageFormat imageFormat, JpegImageUtils.JpegImageParams jpegImageParams, BufferedImage image, OptimizerConfig config) throws IOException
IOExceptionprotected boolean isSupportedImageResult(HttpResponseBuilder response, Uri uri)
protected boolean isSupportedContent(HttpResponseBuilder response)
protected boolean isImageUri(Uri uri)
uri - the URI to checkprotected void enforceUnreadableImageRestrictions(Uri uri, HttpResponseBuilder response)
protected BufferedImage readImage(org.apache.sanselan.ImageFormat imageFormat, HttpResponseBuilder response) throws org.apache.sanselan.ImageReadException, IOException
org.apache.sanselan.ImageReadExceptionIOExceptionprotected BufferedImage readBmp(HttpResponseBuilder response) throws org.apache.sanselan.ImageReadException, IOException
org.apache.sanselan.ImageReadExceptionIOExceptionprotected BufferedImage readPng(HttpResponseBuilder response) throws org.apache.sanselan.ImageReadException, IOException
org.apache.sanselan.ImageReadExceptionIOExceptionprotected BufferedImage readGif(HttpResponseBuilder response) throws org.apache.sanselan.ImageReadException, IOException
org.apache.sanselan.ImageReadExceptionIOExceptionprotected BufferedImage readJpeg(HttpResponseBuilder response) throws org.apache.sanselan.ImageReadException, IOException
org.apache.sanselan.ImageReadExceptionIOExceptionCopyright © 2007-2014 The Apache Software Foundation. All Rights Reserved.