package com.lichtfragmente.servlets;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.Vector;
import com.lichtfragmente.helpers.*;
import com.lichtfragmente.beans.ImageBean;
import com.lichtfragmente.beans.CommentBean;
/**
* Servlet managing the display of a certain image to the client.
* This servlet is concerned with fetching the data associated to a certain image
* as well as an image's comments and further information.
* Furthermore, it supplies functionality to view an enlarged version of a given
* image adding comments and, provided the required privileges, also the deletion
* of an image.
**/
public class ImageServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if (GlobalHelpers.isLoggedIn(request)) {
//get image id from URL
int imageid=Integer.parseInt(request.getParameter("image"));
if (request.getParameter("large")!=null) {
//if the user wants to see the enlrged version of the image
RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/includes/image/large.jsp");
dispatcher.forward(request,response);
} else if (request.getParameter("delete")!=null) {
//if the user wants to delete a given image
//get the id of the user that wants to carry out the operation
int userid=(Integer)request.getSession().getAttribute("userid");
//check if the user is either the owner of the image in question or he
//has a privilege level of 2 (Moderator) or more
if (GlobalHelpers.getUserLevel(userid)>=2 || this.getImageInformation(imageid).getUserBean().getId()==userid) {
//delete image
this.deleteImage(imageid);
//redirect to home page
response.sendRedirect("/lichtfragmente/home");
}
} else {
//user just wants to view an image and all associated content
ImageBean imginfo=this.getImageInformation(imageid);
//image not found
if (imginfo==null) {
throw new ServletException("Image with id "+imageid+" not found!");
}
request.setAttribute("image",imginfo);
//get comments
Vector<CommentBean> comments=this.getImageComments(imageid);
request.setAttribute("comments",comments);
//dispatch image view
RequestDispatcher dispatcher=request.getRequestDispatcher(GlobalHelpers.getIncludedUrl("image/image"));
dispatcher.forward(request,response);
}
} else {
response.sendRedirect("/lichtfragmente");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//check login
//this servlet recieves a POST request whenever one wants to post a comment
if (GlobalHelpers.isLoggedIn(request)) {
//get image id and user id
int imageid=Integer.parseInt(request.getParameter("image"));
int userid=(Integer)request.getSession().getAttribute("userid");
synchronized(this) {
try {
//insert comment
PreparedStatement stmt=DBInterface.doQuery("INSERT INTO comment(content,userid,imageid) VALUES (?,?,?)");
stmt.setString(1,request.getParameter("content"));
stmt.setInt(2,userid);
stmt.setInt(3,imageid);
stmt.executeUpdate();
stmt.close();
DBInterface.close();
//redirect to original image
response.sendRedirect("/lichtfragmente/image/"+imageid);
} catch (SQLException se) { response.getWriter().println("ERROR: "+se); }
}
} else {
response.sendRedirect("/lichtfragmente");
}
}
/**
* Method to delete a certain image.
* This method tries to delete an image corresponding to a certain ID.
* In order to carry this out, the associated comments are deleted first,
* then the image entry in the database, then it is deleted from galleries
* it may be contained in and lastly the image and thumbnail file are
* removed from the file system.
*
* @param imageid The ID of the image to delete
**/
private synchronized void deleteImage(int imageid)
throws ServletException {
try {
//delete comments
PreparedStatement stmt=DBInterface.doQuery("DELETE FROM comment WHERE imageid=?");
stmt.setInt(1,imageid);
stmt.executeUpdate();
stmt.close();
stmt=null;
//delete image from galleries
stmt=DBInterface.doQuery("DELETE FROM gallery_has_image WHERE imageid=?");
stmt.setInt(1,imageid);
stmt.executeUpdate();
stmt.close();
stmt=null;
//delete image entry itself
stmt=DBInterface.doQuery("DELETE FROM image WHERE id=?");
stmt.setInt(1,imageid);
stmt.executeUpdate();
stmt.close();
//get absolute path of project root
String realpath=this.getServletContext().getRealPath("/");
//delete file
File f=new File(realpath+"images/"+imageid+".jpg");
f.delete();
f=null;
//delete thumbnail
f=new File(realpath+"images/"+imageid+"_thumb.jpg");
f.delete();
} catch (SQLException sql) { throw new ServletException(sql); }
}
/**
* Method which retrieves all information associated to an image.
* This method is given an image's ID and tris to retrieve all
* associated information from the database, which is then returned
* wrapped into a bean.
*
* @param imageid The ID of the image to obtain information for
* @return All image information wrapped into a bean
**/
private ImageBean getImageInformation(int imageid) {
ImageBean temp=null;
synchronized(this) {
try {
//get image information
PreparedStatement stmt=DBInterface.doQuery("SELECT i.id,i.title,i.description,i.tags,i.date,u.id,u.name " +
"FROM image i " +
"INNER JOIN users u ON u.id=i.owner " +
"WHERE i.id=? LIMIT 1");
stmt.setInt(1,imageid);
ResultSet result=stmt.executeQuery();
//store information
if (result.next()) {
temp=new ImageBean();
temp.setId(result.getInt(1));
temp.setTitle(result.getString(2));
temp.setDescription(GlobalHelpers.nl2br(result.getString(3)));
temp.setTags(result.getString(4));
temp.setDate(result.getTimestamp(5));
temp.getUserBean().setId(result.getInt(6));
temp.getUserBean().setName(result.getString(7));
}
result.close();
stmt.close();
DBInterface.close();
} catch(SQLException sql) {}
}
return temp;
}
/**
* Get comments for a certain image.
* This method fetches all comments which are associated
* to a given image id.
* These comments are then returned wrapped into a list of
* beans.
*
* @param imageid The ID image the image we want to fetch the comments for
* @return All comments for the given image
**/
private Vector<CommentBean> getImageComments(int imageid) {
Vector<CommentBean> list=new Vector<CommentBean>();
synchronized(this) {
try {
//fetch comments for image id
PreparedStatement stmt=DBInterface.doQuery("SELECT c.content,c.date,u.name,u.id " +
"FROM comment c " +
"INNER JOIN users u ON u.id=c.userid " +
"WHERE c.imageid=? ORDER BY c.date DESC");
stmt.setInt(1,imageid);
ResultSet result=stmt.executeQuery();
while (result.next()) {
CommentBean temp=new CommentBean();
temp.setContent(GlobalHelpers.nl2br(result.getString(1)));
temp.setDate(result.getTimestamp(2));
temp.getUserBean().setName(result.getString(3));
temp.setUserid(result.getInt(4));
list.add(temp);
}
result.close();
stmt.close();
DBInterface.close();
} catch (SQLException se) {}
}
return list;
}
}