package siprp.images.servlet;

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 siprp.database.cayenne.objects.Image;
import siprp.database.cayenne.providers.ImageDAO;

public final class ImagesServlet extends HttpServlet
{
	private static final long serialVersionUID = 999506147931075031L;
	
	private static final String PARAMETER_ID = "id";

	public void doGet( HttpServletRequest request, HttpServletResponse response ) throws IOException, ServletException
	{
		response.setContentType( "image/png" );
		ServletOutputStream out = response.getOutputStream();
		String param = request.getParameter( PARAMETER_ID );
		System.out.println(param);
		try
		{
			Integer id = new Integer(param);
			Image img = new ImageDAO().getImageForID( id );
			out.write( img.getImageData() );
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
