package com.github.onsdigital.babbage.api.endpoint.content;

import com.github.davidcarboni.restolino.framework.Api;
import com.github.onsdigital.babbage.api.error.ErrorHandler;
import com.github.onsdigital.babbage.content.client.ContentClient;
import com.github.onsdigital.babbage.content.client.ContentResponse;
import com.github.onsdigital.babbage.response.BabbageContentBasedBinaryResponse;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.core.Context;
import java.io.IOException;

/**
 * Created by bren on 01/07/15.
 * <p/>
 * Serves resource files from content service with no content disposition
 */
@Api
public class Resource {

    @GET
    public void get(@Context HttpServletRequest request, @Context HttpServletResponse response) throws IOException {
        try {
            ContentResponse contentResponse = ContentClient.getInstance().getResource(request.getParameter("uri"));
            String contentDispositionHeader = "inline; ";
            contentDispositionHeader += contentResponse.getName() == null ? "" : "filename=\"" + contentResponse.getName() + "\"";
            response.setHeader("Content-Disposition", contentDispositionHeader);
            new BabbageContentBasedBinaryResponse(contentResponse, contentResponse.getDataStream(), contentResponse.getMimeType()).apply(request, response);
        } catch (Throwable t) {
            ErrorHandler.handle(request, response, t);
        }
    }
}

@@ -389,16 +389,81 @@
 ponse;%0A%0A
+import javax.imageio.IIOException;%0Aimport javax.imageio.ImageIO;%0A
 import j
@@ -619,23 +619,222 @@
 java
-.io.IOException
+x.ws.rs.ext.ExceptionMapper;%0Aimport java.awt.*;%0Aimport java.awt.image.BufferedImage;%0Aimport java.io.ByteArrayInputStream;%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream
 ;%0A%0A/
@@ -1124,16 +1124,129 @@
   try %7B%0A
+            String uri = request.getParameter(%22uri%22);%0A            String width = request.getParameter(%22width%22);%0A%0A
         
@@ -1327,35 +1327,11 @@
 rce(
-request.getParameter(%22
 uri
-%22)
 );%0A 
@@ -1599,16 +1599,17 @@
 eader);%0A
+%0A
         
@@ -1616,94 +1616,1530 @@
     
-new BabbageContentBasedBinaryResponse(contentResponse, contentResponse.getDataStream()
+InputStream contentResponseBody = contentResponse.getDataStream();%0A            BufferedImage image = null;%0A%0A            if (width != null) %7B%0A%0A                try %7B%0A                    Integer w = Integer.parseInt(width, 10);%0A%0A                    if (uri.toLowerCase().endsWith(%22.png%22) %7C%7C uri.toLowerCase().endsWith(%22.jpg%22) %7C%7C uri.toLowerCase().endsWith(%22.jpeg%22)) %7B%0A                        image = ImageIO.read(contentResponseBody);%0A                        Integer clampedWidth = Math.max(1, Math.min(image.getWidth(), w));%0A%0A                        Double w2 = (double)clampedWidth;%0A                        Double ratio = w2 / (double)image.getWidth();%0A                        Double h = (double)image.getHeight() * ratio;%0A%0A                        Image newImage = image.getScaledInstance(clampedWidth, h.intValue(), Image.SCALE_SMOOTH);%0A                        image = new BufferedImage(newImage.getWidth(null), newImage.getHeight(null), BufferedImage.TYPE_INT_RGB);%0A                        image.getGraphics().drawImage(newImage, 0, 0, null);%0A                    %7D%0A%0A                %7D catch (IOException e) %7B%0A                    e.printStackTrace();%0A                %7D%0A%0A            %7D%0A%0A%0A            if (image != null) %7B%0A                ByteArrayOutputStream os = new ByteArrayOutputStream();%0A                ImageIO.write(image, %22png%22, os);%0A                contentResponseBody = new ByteArrayInputStream(os.toByteArray());%0A            %7D%0A%0A            new BabbageContentBasedBinaryResponse(contentResponse, contentResponseBody
 , co
@@ -3296,10 +3296,11 @@
 %7D%0A    %7D%0A
+%0A
 %7D%0A

Add width parameter functionality to end point