package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modele.UserSession;
import com.command.FrontCommand;
/**
* Servlet implementation class FrontController point d'entrer de toute les
* requete
*/
public class FrontController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public FrontController() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
processRequest(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
processRequest(request, response);
}
protected void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String commandName = request.getParameter("command");
if (UserSession.getInstance().isTimeOut() || commandName == null || commandName.length() <= 0)
{
UserSession session = UserSession.getInstance();
session.authentifiedUser = null;
session.sessionID = "";
commandName="FrontController";
}
if (commandName.compareTo("Logout") == 0) {
UserSession session = UserSession.getInstance();
session.authentifiedUser = null;
session.sessionID = "";
commandName="FrontController";
}
// obtient le front command object correspondant.
FrontCommand frontCommand = null;
frontCommand = getCommand(commandName);
// si la commande existe
System.out.println("Time Recorded : " + commandName);
UserSession.getInstance().recordTime();
frontCommand.processRequest(request, response);
}
/**
* Cette m�thode instancie une nouvel Objet FrontCommand pour la Servlet.
*/
private FrontCommand getCommand(String commandName) {
FrontCommand frontCommand = null;
try {
if (getCommandClass(commandName) != null)
frontCommand = (FrontCommand) getCommandClass(commandName)
.newInstance();
} catch (Exception e) {
// TODO forward to error page
e.printStackTrace();
}
return frontCommand;
}
/**
* Obtenir la classe correspondant au nom de la command r�cu.
*/
private Class<?> getCommandClass(String commandName) {
Class<?> command = null;
try {
String commandClassName = "com.command." + commandName + "Command";
command = Class.forName(commandClassName);
} catch (ClassNotFoundException cnfe) {
// TODO forward to error page
} catch (Exception e) {
// TODO forward to error page
e.printStackTrace();
}
return command;
}
}