package org.gbif.imgcache;

import java.io.IOException;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.io.ByteStreams;
import com.google.common.net.HttpHeaders;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Singleton
public class ImageCacheServlet extends HttpServlet {

  private final Logger LOG = LoggerFactory.getLogger(ImageCacheServlet.class);
  private static final long serialVersionUID = 8681716273998041332L;
  private final ImageCacheService cache;
  private static final String SIZE_PARAM = "size";
  private static final String URL_PARAM = "url";


  @Inject
  public ImageCacheServlet(ImageCacheService cache) {
    this.cache = cache;
  }

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    final ImageSize size = ImageSize.fromString(req.getParameter(SIZE_PARAM));

    try {
      URL url = new URL(req.getParameter(URL_PARAM));
      try {
        CachedImage img = cache.get(url, size);
        resp.setHeader(HttpHeaders.CONTENT_TYPE, img.getMimeType());
        resp.setHeader(HttpHeaders.CACHE_CONTROL, "public, max-age=604800");
        ByteStreams.copy(img.openStream(), resp.getOutputStream());
      } catch (IOException e) {
        String errMsg = String.format("No image found for url %s ", url);
        LOG.warn(errMsg);
        LOG.debug(errMsg, e);
        resp.sendError(HttpServletResponse.SC_NOT_FOUND, errMsg);
      } catch (NullPointerException e) {
        String errMsg = String.format("Likely unsupported format, or not an image %s ", url);
        LOG.warn(errMsg);
        LOG.debug(errMsg, e);
        resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
      } finally {
        resp.flushBuffer();
      }

    } catch (Exception e) {
      LOG.error("Invalid image url requested", e);
      resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Please provide a valid image url parameter");
    }
  }
}

@@ -41,32 +41,71 @@
 io.IOException;%0A
+import java.net.MalformedURLException;%0A
 import java.net.
@@ -1153,23 +1153,108 @@
     
-try %7B%0A      URL
+URL url = null;%0A    try %7B%0A      LOG.debug(%22URL parameter is %7B%7D%22, req.getParameter(URL_PARAM));%0A     
  url
@@ -1298,22 +1298,9 @@
 ));%0A
-      try %7B%0A  
+%0A
     
@@ -1339,34 +1339,32 @@
 l, size);%0A      
-  
 resp.setHeader(H
@@ -1410,26 +1410,24 @@
 e());%0A      
-  
 resp.setHead
@@ -1477,12 +1477,14 @@
 age=
+3153
 60
-48
 00%22)
@@ -1491,18 +1491,16 @@
 ;%0A      
-  
 ByteStre
@@ -1549,24 +1549,280 @@
 tream());%0A  
+  %7D catch (MalformedURLException e) %7B%0A      String errMsg = String.format(%22Invalid image URL requested %25s%22, req.getParameter(URL_PARAM));%0A      LOG.warn(errMsg);%0A      LOG.debug(errMsg, e);%0A      resp.sendError(HttpServletResponse.SC_BAD_REQUEST, errMsg);%0A
     %7D catch 
@@ -1831,34 +1831,32 @@
 OException e) %7B%0A
-  
       String err
@@ -1901,17 +1901,16 @@
 r url %25s
- 
 %22, url);
@@ -1908,34 +1908,32 @@
 s%22, url);%0A      
-  
 LOG.warn(errMsg)
@@ -1926,34 +1926,32 @@
 G.warn(errMsg);%0A
-  
       LOG.debug(
@@ -1954,34 +1954,32 @@
 bug(errMsg, e);%0A
-  
       resp.sendE
@@ -2030,18 +2030,16 @@
 g);%0A    
-  
 %7D catch 
@@ -2061,26 +2061,24 @@
 eption e) %7B%0A
-  
       String
@@ -2147,17 +2147,16 @@
 image %25s
- 
 %22, url);
@@ -2158,26 +2158,24 @@
 url);%0A      
-  
 LOG.warn(err
@@ -2176,26 +2176,24 @@
 rn(errMsg);%0A
-  
       LOG.de
@@ -2204,26 +2204,24 @@
 errMsg, e);%0A
-  
       resp.s
@@ -2286,239 +2286,252 @@
     
-  %7D finally %7B%0A        resp.flushBuffer();%0A      %7D%0A%0A    %7D catch (Exception e) %7B%0A      LOG.error(%22Invalid image url requested%22, e);%0A      resp.sendError(HttpServletResponse.SC_BAD_REQUEST, %22Please provide a valid image url parameter%22
+%7D catch (Exception e) %7B%0A      String errMsg = String.format(%22Unknown error processing URL %25s%22, url);%0A      LOG.error(errMsg, e);%0A      resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, errMsg);%0A    %7D finally %7B%0A      resp.flushBuffer(
 );%0A 

Handle incorrect URLs seperately to other exceptions.