Search in sources :

Example 1 with CacheControl

use of com.yammer.dropwizard.jersey.caching.CacheControl in project photon-core by 1000Memories.

the class PhotoResource method getPhoto.

@GET
@Timed
@CacheControl(immutable = true)
public Response getPhoto(@PathParam("name") String name, @MatrixParam("w") Integer width, @MatrixParam("r") RotationParam rotateAngle, @MatrixParam("c") RectangleParam crop) throws Exception {
    InputStream resultStream;
    InputStream imageStream;
    try {
        imageStream = new BufferedInputStream(photoProvider.getPhotoInputStream(name));
    } catch (FileNotFoundException fnfe) {
        throw new WebApplicationException(Response.Status.NOT_FOUND);
    }
    String mimeType = URLConnection.guessContentTypeFromStream(imageStream);
    if (mimeType == null) {
        // Not implemented
        throw new WebApplicationException(501);
    }
    if (width != null || rotateAngle != null || crop != null) {
        BufferedImage image;
        TimerContext readContext = readTimer.time();
        try {
            image = ImageIO.read(imageStream);
        } finally {
            imageStream.close();
            readContext.stop();
        }
        if (crop != null) {
            image = com.thousandmemories.photon.core.Processor.crop(image, crop.get());
        }
        if (rotateAngle != null) {
            image = com.thousandmemories.photon.core.Processor.rotate(image, rotateAngle.get());
        }
        if (width != null) {
            image = com.thousandmemories.photon.core.Processor.fitToWidth(image, width);
        }
        Iterator<ImageWriter> i = ImageIO.getImageWritersByMIMEType(mimeType);
        if (!i.hasNext()) {
            mimeType = "image/jpeg";
            i = ImageIO.getImageWritersByMIMEType(mimeType);
        }
        ImageWriter writer = i.next();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        writer.setOutput(new MemoryCacheImageOutputStream(os));
        writer.write(image);
        image.flush();
        image = null;
        resultStream = new ByteArrayInputStream(os.toByteArray());
    } else {
        resultStream = imageStream;
    }
    return Response.ok(resultStream, mimeType).build();
}
Also used : MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) TimerContext(com.yammer.metrics.core.TimerContext) ImageWriter(javax.imageio.ImageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedImage(java.awt.image.BufferedImage) Timed(com.yammer.metrics.annotation.Timed) CacheControl(com.yammer.dropwizard.jersey.caching.CacheControl)

Aggregations

CacheControl (com.yammer.dropwizard.jersey.caching.CacheControl)1 Timed (com.yammer.metrics.annotation.Timed)1 TimerContext (com.yammer.metrics.core.TimerContext)1 BufferedImage (java.awt.image.BufferedImage)1 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 ImageWriter (javax.imageio.ImageWriter)1 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)1