Advertisement
Guest User

Untitled

a guest
Nov 6th, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. package servlets;
  2.  
  3. import java.util.regex.*;
  4. import java.io.*;
  5. import javax.servlet.*;
  6. import javax.servlet.http.*;
  7. import command.*;
  8. import javax.persistence.EntityManager;
  9. import javax.persistence.EntityManagerFactory;
  10. import javax.persistence.Persistence;
  11. import javax.servlet.annotation.WebServlet;
  12.  
  13. @WebServlet(urlPatterns = {"/"})
  14. public class FrontController extends HttpServlet {
  15.  
  16. private static final long serialVersionUID = -2L;
  17. public EntityManager em;
  18.  
  19. @Override
  20. public void init(ServletConfig conf) throws ServletException {
  21. super.init(conf);
  22.  
  23. }
  24.  
  25. @Override
  26. public void doPost(HttpServletRequest req, HttpServletResponse res)
  27. throws ServletException, IOException {
  28.  
  29. try {
  30. ActionFlow flow;
  31. String operation;
  32. String role;
  33. Command cmd = null;
  34.  
  35. ServletContext sc = getServletContext();
  36. flow = new ActionFlow("Erreur", req.getContextPath() + "/erreur.html", true);
  37.  
  38. // Serv CSS request normaly
  39. Pattern p = Pattern.compile(".css");
  40. Matcher m = p.matcher(req.getRequestURI());
  41. if (m.find()) {
  42. RequestDispatcher rd = sc.getRequestDispatcher(req.getRequestURI());
  43. rd.forward(req, res);
  44. }
  45.  
  46. role = getRole(req.getRequestURI());
  47. operation = getOperation(req.getRequestURI());
  48.  
  49. // Retrieve the good CommandManager to handle request
  50. switch (role) {
  51. case "waiter":
  52. cmd = command.waiter.CommandManager.getCommand(operation);
  53. break;
  54. case "chef":
  55. cmd = command.chef.CommandManager.getCommand(operation);
  56. break;
  57. case "cashier":
  58. cmd = command.cashier.CommandManager.getCommand(operation);
  59. break;
  60.  
  61. }
  62.  
  63.  
  64.  
  65. if (cmd != null) {
  66. flow = cmd.actionPerform(req, sc);
  67. } else {
  68. flow = new ActionFlow("Erreur", req.getContextPath() + "/erreur.html", true);
  69. }
  70.  
  71. if (flow.isRedirect() == true) {
  72. res.sendRedirect(flow.getPath());
  73. } else {
  74.  
  75. RequestDispatcher rd = sc.getRequestDispatcher("/" + role + "/" + flow.getPath());
  76. rd.forward(req, res);
  77. } // doPost
  78. } catch (Exception e) {
  79. System.out.println(e.getMessage());
  80. }
  81. }
  82.  
  83. @Override
  84. public void doGet(HttpServletRequest req, HttpServletResponse res)
  85. throws ServletException,
  86. IOException {
  87. doPost(req, res);
  88. } // doGet
  89.  
  90. private String getOperation(String requestURI) {
  91. Pattern p = Pattern.compile(".*/(.*)");
  92. Matcher m = p.matcher(requestURI);
  93. if (m.find() && m.groupCount() == 1) {
  94. return m.group(1);
  95. } else {
  96. return null;
  97. }
  98. }
  99.  
  100. private String getRole(String requestURI) {
  101. Pattern p = Pattern.compile(".*/(.*)/.*");
  102. Matcher m = p.matcher(requestURI);
  103. if (m.find() && m.groupCount() == 1) {
  104. return m.group(1);
  105. } else {
  106. return null;
  107. }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement