/**
 * @license
 * Copyright 2018 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package foam.nanos.servlet;

import foam.util.SafetyUtil;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.text.StringEscapeUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ImageServlet
  extends HttpServlet
{
  protected static final int                 BUFFER_SIZE = 4096;
  protected static final String              DEFAULT_EXT = "application/octet-stream";
  protected static final Map<String, String> EXTS = new HashMap<String, String>();

  static {
    EXTS.put("js",    "application/javascript");
    EXTS.put("json",  "application/json");
    EXTS.put("class", "application/java-vm");
    EXTS.put("xml",   "application/xml");

    EXTS.put("gif",   "image/gif");
    EXTS.put("png",   "image/png");
    EXTS.put("svg",   "image/svg+xml");

    EXTS.put("java",  "text/x-java-source");
    EXTS.put("csv",   "text/csv");
    EXTS.put("txt",   "text/plain");
    EXTS.put("html",  "text/html");
  }

  @Override
  protected void service(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException
  {
    // get path
    String cwd = System.getProperty("user.dir");
    String path = getServletConfig().getInitParameter("path");
    String reqPath = req.getRequestURI().replaceFirst("/?images/?", "/");

    // get file
    File src = new File(cwd + "/" + path + reqPath);
    if ( src.isFile() && src.canRead() && src.getCanonicalPath().startsWith(new File(path).getCanonicalPath()) ) {
      String ext = EXTS.get(FilenameUtils.getExtension(src.getName()));
      try ( BufferedInputStream is = new BufferedInputStream(new FileInputStream(src)) ) {
        resp.setContentType(!SafetyUtil.isEmpty(ext) ? ext : DEFAULT_EXT);
        resp.setHeader("Content-Disposition", "filename=\"" + StringEscapeUtils.escapeHtml4(src.getName()) + "\"");
        resp.setContentLengthLong(src.length());

        int read = 0;
        byte[] buffer = new byte[BUFFER_SIZE];
        while ( (read = is.read(buffer, 0, BUFFER_SIZE)) != -1 ) {
          resp.getOutputStream().write(buffer, 0, read);
        }
      }
      return;
    }
  }
}

@@ -219,16 +219,54 @@
 eUtils;%0A
+import org.apache.commons.io.IOUtils;%0A
 import o
@@ -1601,21 +1601,24 @@
   String
+%5B%5D
  path
+s
  = getSe
@@ -1653,16 +1653,28 @@
 er(%22path
+s%22).split(%22:
 %22);%0A    
@@ -1755,17 +1755,84 @@
  // 
-get file%0A
+enumerate each file path%0A    for ( int i = 0 ; i %3C paths.length ; i++ ) %7B%0A  
     
@@ -1867,16 +1867,20 @@
 %22 + path
+s%5Bi%5D
  + reqPa
@@ -1884,16 +1884,18 @@
 qPath);%0A
+  
     if (
@@ -1975,16 +1975,20 @@
 ile(path
+s%5Bi%5D
 ).getCan
@@ -2001,24 +2001,26 @@
 Path()) ) %7B%0A
+  
       String
@@ -2085,16 +2085,18 @@
 ;%0A      
+ 
+ 
 try ( Bu
@@ -2172,32 +2172,34 @@
 c)) ) %7B%0A        
+  
 resp.setContentT
@@ -2249,16 +2249,18 @@
 T_EXT);%0A
+  
         
@@ -2367,32 +2367,34 @@
  %22%5C%22%22);%0A        
+  
 resp.setContentL
@@ -2431,230 +2431,137 @@
     
-int read = 0;%0A        byte%5B%5D buffer = new byte%5BBUFFER_SIZE%5D;%0A        while ( (read = is.read(buffer, 0, BUFFER_SIZE)) != -1 ) %7B%0A          resp.getOutputStream().write(buffer, 0, read);%0A        %7D%0A      %7D%0A      return;%0A    %7D
+  IOUtils.copy(is, resp.getOutputStream());%0A          return;%0A        %7D%0A      %7D%0A    %7D%0A    %0A    resp.sendError(resp.SC_NOT_FOUND);
 %0A  %7D

Update ImageServlet to enumerate a colon separated list of paths.