/*
 * Servlet for controlling access to photos.
 * 
 * $Id: PhotoServlet.java,v 1.9 2003/04/14 01:47:59 davis Exp $
 * 
 * Copyright (C) 2001  Guy Davis
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.

 * You should have received a copy of the GNU Lesser Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  
 * 02111-1307, USA.
 */


package ca.guydavis.site;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;


/**
 *
 */
public class PhotoServlet extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException { 
        try{
            HttpSession session = request.getSession();      // session used to verify login
            OutputStream out = response.getOutputStream();
            BufferedOutputStream buf_out = new BufferedOutputStream(out);
            String id = request.getParameter("id");              // photo name should be specified
            String thumb = request.getParameter("thumb");              // photo name should be specified
            String field_name;

            if (id != null)  { 

                if (thumb != null)
                {
                    field_name = "thumb_data";
                }
                else
                {
                    field_name = "img_data";
                }

                String query = "SELECT access, date_shot, filename, " + field_name + " as the_data FROM photos WHERE id="+id;
                ResultSet rs = WebUtils.doSQL(query);

                if (!rs.first()) {
                    displayError(request, response);
                    return;
                }  

                // if it's marked personal, only logged in people should see it
                if ((rs.getString("access").equalsIgnoreCase("Personal")) && !WebUtils.isLoggedIn(session))) {
                    displayError(request, response);
                    return;
                }

                if (rs.getString("filename").toUpperCase().indexOf(".GIF") >= 0)
                    response.setContentType("image/gif");       
                else
                    response.setContentType("image/jpeg");      

                Blob img_blob = rs.getBlob("the_data");
                InputStream in = img_blob.getBinaryStream();
                int len = (new Long(img_blob.length())).intValue();
                response.setContentLength(len);    // Set the content length to speed display
                long modDate = rs.getDate("date_shot").getTime();
                response.setDateHeader("Last-Modified", modDate);
                while (in.available() > 0)
                    buf_out.write(in.read());
                buf_out.flush();
                buf_out.close();
            } else
                displayError(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            displayError(request, response);
        }
    }
    

    /** POST should do the same thing as GET request */
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException 
    {
        doGet(request, response);
    }
    
    /**
     * Displays a default error message contained within a JPEG
     */
    private void displayError(HttpServletRequest request, HttpServletResponse response) 
    {
        try{
            OutputStream out = response.getOutputStream();
            response.setContentType("image/gif");  
            String query = "SELECT * FROM photos WHERE id=0";  // id=0 is error image!
            ResultSet rs = WebUtils.doSQL(query);
            rs.first();  // move to only row returned
            Blob img_blob = rs.getBlob("img_data");
            InputStream in = img_blob.getBinaryStream();
            while (in.available() > 0) {
                out.write(in.read());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


