package edu.ucsf.mousedatabase.servlets;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

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

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import edu.ucsf.mousedatabase.DBConnect;
import edu.ucsf.mousedatabase.Log;

/**
 * Servlet implementation class DownloadServlet
 */
public class DownloadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DownloadServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//String mouseID = request.getParameter("mouseID");
		//String fileName = request.getParameter("fileName");
		Log.Info("downloadServlet reached");

		Integer id = (Integer) Integer.parseInt(request.getParameter("mouseID"));
		Log.Info("id is " + id);
		//File file = null;
		//int BUFF_SIZE = 1024;
		//byte[] buffer = new byte[BUFF_SIZE];





		try {

			//String fileName = DBConnect.getFilePathByID(id);
			String fileName = request.getParameter("fileName");
			Log.Info("filename returned is " + fileName);

			response.setContentType("text");
			//response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() +"\"");
			response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName +"\"");
			//response.setContentLength((int) file.length()); see if it works with this surpressed
			//Log.Info("file length: " + file.length());
			OutputStream output = response.getOutputStream();
			FileInputStream input = new FileInputStream(fileName);
			IOUtils.copy(input, output);

			/*
			int len = input.read(buffer);
		    while (len != -1) {
		    	output.write(buffer, 0, content);
		    }
		    */
		    //output.flush();
		    output.close();
			input.close();
			
			Log.Info("end of download servlet");
			
			//FileUtils.copyFile(file, response.getOutputStream());
		} catch (Exception e) {
			Log.Info("exception in download servlet");
		}		
		//response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

@@ -2199,16 +2199,17 @@
 %09%09    //
+ 
 output.f
@@ -2220,18 +2220,16 @@
 ();%0A%09%09  
-  
 output.c
@@ -2383,16 +2383,24 @@
 eption e
+xception
 ) %7B%0A%09%09%09L
@@ -2402,20 +2402,21 @@
 %0A%09%09%09Log.
-Info
+Error
 (%22except
@@ -2435,24 +2435,35 @@
 oad servlet%22
+, exception
 );%0A%09%09%7D%09%09%0A%09%09/

Improve logs when downloading files throws an exception