package com.dev.swms.server.web.servlet;

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

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

import org.apache.commons.lang.StringUtils;

public class OverVeiwImageServlet extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * Constructor of the object.
	 */
	public OverVeiwImageServlet() {
		super();
	}
	
	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String imageSourcePath = request.getParameter("imageSourcePath");
		
		if(StringUtils.isEmpty(imageSourcePath))
			return;
		
		imageSourcePath = new String(imageSourcePath.getBytes("ISO-8859-1"),"UTF-8");
		
		if(imageSourcePath.indexOf(".pdf")>0){
			response.setContentType("application/pdf ");
		}else{
			response.setContentType("image/gif");
		}
		ServletOutputStream out = response.getOutputStream();
		
		BufferedInputStream bufIn = new BufferedInputStream(
										new FileInputStream(
											new File(imageSourcePath)));
		int len;
		while((len = bufIn.read())!= -1)
			out.write(len);
	
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}
}
