Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package servlets;
- import java.util.regex.*;
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- import command.*;
- import javax.persistence.EntityManager;
- import javax.persistence.EntityManagerFactory;
- import javax.persistence.Persistence;
- import javax.servlet.annotation.WebServlet;
- @WebServlet(urlPatterns = {"/"})
- public class FrontController extends HttpServlet {
- private static final long serialVersionUID = -2L;
- public EntityManager em;
- @Override
- public void init(ServletConfig conf) throws ServletException {
- super.init(conf);
- }
- @Override
- public void doPost(HttpServletRequest req, HttpServletResponse res)
- throws ServletException, IOException {
- try {
- ActionFlow flow;
- String operation;
- String role;
- Command cmd = null;
- ServletContext sc = getServletContext();
- flow = new ActionFlow("Erreur", req.getContextPath() + "/erreur.html", true);
- // Serv CSS request normaly
- Pattern p = Pattern.compile(".css");
- Matcher m = p.matcher(req.getRequestURI());
- if (m.find()) {
- RequestDispatcher rd = sc.getRequestDispatcher(req.getRequestURI());
- rd.forward(req, res);
- }
- role = getRole(req.getRequestURI());
- operation = getOperation(req.getRequestURI());
- // Retrieve the good CommandManager to handle request
- switch (role) {
- case "waiter":
- cmd = command.waiter.CommandManager.getCommand(operation);
- break;
- case "chef":
- cmd = command.chef.CommandManager.getCommand(operation);
- break;
- case "cashier":
- cmd = command.cashier.CommandManager.getCommand(operation);
- break;
- }
- if (cmd != null) {
- flow = cmd.actionPerform(req, sc);
- } else {
- flow = new ActionFlow("Erreur", req.getContextPath() + "/erreur.html", true);
- }
- if (flow.isRedirect() == true) {
- res.sendRedirect(flow.getPath());
- } else {
- RequestDispatcher rd = sc.getRequestDispatcher("/" + role + "/" + flow.getPath());
- rd.forward(req, res);
- } // doPost
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- }
- @Override
- public void doGet(HttpServletRequest req, HttpServletResponse res)
- throws ServletException,
- IOException {
- doPost(req, res);
- } // doGet
- private String getOperation(String requestURI) {
- Pattern p = Pattern.compile(".*/(.*)");
- Matcher m = p.matcher(requestURI);
- if (m.find() && m.groupCount() == 1) {
- return m.group(1);
- } else {
- return null;
- }
- }
- private String getRole(String requestURI) {
- Pattern p = Pattern.compile(".*/(.*)/.*");
- Matcher m = p.matcher(requestURI);
- if (m.find() && m.groupCount() == 1) {
- return m.group(1);
- } else {
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement