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.GalleryBean;
import com.lichtfragmente.beans.ImageBean;
/**
* Servlet concerned with dispatching a gallery to the client.
* This servlet handles the dispatching of the gallery views to a
* calling client. The gallery to dispatch is determined by a ID
* number which is supplied with the URL.
* There a two things, which are special about this servlet. Firstly,
* it is one of the few parts of this webapp which is accessible also
* without login and secondly, it dispatches a different view, depending
* whether the client uses a computer or a mobile device.
**/
public class GalleryServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//if the user wants to view the enlarged image
if (request.getParameter("large")!=null) {
RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/gallery/large.jsp");
dispatcher.forward(request,response);
} else {
//user wants to see the gallery view
//fetch the gallery ID from the request
int galleryid=Integer.parseInt(request.getParameter("galleryid"));
//load gallery into bean
GalleryBean gallery=this.getGalleryById(galleryid);
if (gallery!=null) {
//add gallery to request
request.setAttribute("gallery",gallery);
} else{
throw new ServletException("Gallery with ID "+galleryid+" not found");
}
//determine user agent
String ua=request.getHeader("User-Agent").toLowerCase();
//user agents for mobile devices
if(ua.matches(".*(android.+mobile|blackberry|htc|iemobile|ip(hone|od)|maemo|nokia|opera m(ob|in)i|phone|symbian|windows (ce|phone)).*")) {
//dispatch mobile-optimized view to mobile user
RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/gallery/gallery_mobile.jsp");
dispatcher.forward(request,response);
} else {
//dispatch normal (js-heavy) view to desktop users
RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/gallery/gallery.jsp");
dispatcher.forward(request,response);
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//this servlet does not answer POST requests any differently
this.doGet(request,response);
}
/**
* Retrieve images for a gallery.
* This method retrieves all images as a list of beans which a contained
* in a gallery given by a ID
*
* @param galleryid The ID of the gallery one wants to view
* @return A bean containing gallery information and images
**/
private GalleryBean getGalleryById(int galleryid)
throws ServletException {
GalleryBean temp=null;
synchronized(this) {
try {
//select gallery details
PreparedStatement stmt=DBInterface.doQuery("SELECT g.id,g.name,g.bgcolor,g.fontcolor,u.name " +
"FROM gallery g " +
"INNER JOIN users u ON g.owner=u.id " +
"WHERE g.id=?");
stmt.setInt(1,galleryid);
ResultSet result=stmt.executeQuery();
//store information in bean
if (result.next()) {
temp=new GalleryBean();
temp.setId(result.getInt(1));
temp.setName(result.getString(2));
temp.setBgcolor(result.getString(3));
temp.setFontcolor(result.getString(4));
temp.getUserBean().setName(result.getString(5));
result.close();
stmt.close();
result=null;
stmt=null;
//get images which are shown in this gallery
stmt=DBInterface.doQuery("SELECT i.id,i.title " +
"FROM gallery_has_image ghi " +
"INNER JOIN image i ON ghi.imageid=i.id " +
"WHERE ghi.galleryid=? " +
"ORDER BY RANDOM()");
stmt.setInt(1,galleryid);
result=stmt.executeQuery();
//store image bean in bean for gallery
Vector<ImageBean> images=new Vector<ImageBean>();
while (result.next()) {
ImageBean img=new ImageBean();
img.setId(result.getInt(1));
img.setTitle(result.getString(2));
images.add(img);
}
result.close();
stmt.close();
temp.setImages(images);
}
DBInterface.close();
} catch (SQLException se) {
throw new ServletException("Could not retrieve gallery: "+se.getMessage());
}
}
//return gallery beam
return temp;
}
}