| @@ -0,0 +1,121 @@ | ||
| package servlets; | ||
|
|
||
| import database.chat.exceptions.ChatDoesNotExistException; | ||
| import database.chat.exceptions.UserNotInPartyException; | ||
| import database.www.exceptions.UserDoesNotExistException; | ||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.Logger; | ||
| import javax.servlet.ServletException; | ||
| import javax.servlet.annotation.WebServlet; | ||
| import javax.servlet.http.HttpServlet; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
| import model.Chat; | ||
| import model.Message; | ||
| import org.bson.types.ObjectId; | ||
| import org.json.JSONArray; | ||
| import org.json.JSONObject; | ||
| import servlets.tools.Helper; | ||
|
|
||
| @WebServlet(name = "Chats", urlPatterns = {"/chats/*"}) | ||
| public class Chats extends HttpServlet { | ||
|
|
||
| protected ObjectId getChatId(HttpServletRequest request) { | ||
| try { // rewrite all this bullshit | ||
| String [] path = request.getPathInfo().split("/"); | ||
|
|
||
| if (path.length == 2) { | ||
| return new ObjectId(path[1]); | ||
| } | ||
| } catch (NullPointerException e) {} | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. | ||
| * | ||
| * @param request servlet request | ||
| * @param response servlet response | ||
| * @throws ServletException if a servlet-specific error occurs | ||
| * @throws IOException if an I/O error occurs | ||
| */ | ||
| protected void processRequest(HttpServletRequest request, HttpServletResponse response) | ||
| throws ServletException, IOException { | ||
| // Logged user, sino redirect | ||
| } | ||
|
|
||
| /** | ||
| * Handles the HTTP <code>GET</code> method. | ||
| * | ||
| * @param request servlet request | ||
| * @param response servlet response | ||
| * @throws ServletException if a servlet-specific error occurs | ||
| * @throws IOException if an I/O error occurs | ||
| */ | ||
| @Override | ||
| protected void doGet(HttpServletRequest request, HttpServletResponse response) | ||
| throws ServletException, IOException { | ||
| processRequest(request, response); | ||
|
|
||
| if (Helper.isAjax(request)) { | ||
| try { | ||
| int userId = 1; // get userid of the session | ||
| // add getParameterers unread and focus | ||
|
|
||
| JSONArray JSONChats = new JSONArray(); | ||
|
|
||
| List<Chat> chats = Chat.retrieveChatsByUserPk(userId); | ||
| for (Chat chat : chats) { | ||
| JSONObject object = new JSONObject(); | ||
|
|
||
| String [] attrs = new String[2]; | ||
|
|
||
| attrs[0] = Integer.toString(chat.countUnreadMessages(userId)); | ||
| object.put("count", attrs[0]); | ||
|
|
||
| List<Message> messages = chat.getMessages(); | ||
| attrs[1] = messages.size() > 0 ? messages.get(0).getText() : ""; | ||
| object.put("last_msg", attrs[1]); | ||
|
|
||
| JSONObject JSONChat = new JSONObject().put(chat.toString(), object); | ||
| JSONChats.put(JSONChat); | ||
| } | ||
|
|
||
| response.setContentType("application/json;charset=UTF-8"); | ||
| JSONChats.write(response.getWriter()); | ||
| response.getWriter().close(); | ||
| } catch (UserDoesNotExistException | ChatDoesNotExistException | UserNotInPartyException ex) { | ||
| Logger.getLogger(Chats.class.getName()).log(Level.SEVERE, null, ex); | ||
| } | ||
| } else { | ||
| //ObjectId chatId = getChatId(request); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Handles the HTTP <code>POST</code> method. | ||
| * | ||
| * @param request servlet request | ||
| * @param response servlet response | ||
| * @throws ServletException if a servlet-specific error occurs | ||
| * @throws IOException if an I/O error occurs | ||
| */ | ||
| @Override | ||
| protected void doPost(HttpServletRequest request, HttpServletResponse response) | ||
| throws ServletException, IOException { | ||
| processRequest(request, response); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a short description of the servlet. | ||
| * | ||
| * @return a String containing servlet description | ||
| */ | ||
| @Override | ||
| public String getServletInfo() { | ||
| return "Short description"; | ||
| } | ||
| } |
| @@ -0,0 +1,37 @@ | ||
| package servlets; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.PrintWriter; | ||
| import javax.servlet.ServletException; | ||
| import javax.servlet.annotation.WebServlet; | ||
| import javax.servlet.http.HttpServlet; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
|
|
||
| @WebServlet(name = "Login", urlPatterns = {"/login"}) | ||
| public class Login extends HttpServlet { | ||
|
|
||
| /** | ||
| * Handles the HTTP <code>POST</code> method. | ||
| * | ||
| * @param request servlet request | ||
| * @param response servlet response | ||
| * @throws ServletException if a servlet-specific error occurs | ||
| * @throws IOException if an I/O error occurs | ||
| */ | ||
| @Override | ||
| protected void doPost(HttpServletRequest request, HttpServletResponse response) | ||
| throws ServletException, IOException { | ||
| response.getOutputStream().println("TU TE VOLS LOGUEAR MADAFAKA!!!"); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a short description of the servlet. | ||
| * | ||
| * @return a String containing servlet description | ||
| */ | ||
| @Override | ||
| public String getServletInfo() { | ||
| return "Short description"; | ||
| } | ||
| } |
| @@ -1,78 +1,78 @@ | ||
| package servlets; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.PrintWriter; | ||
| import javax.servlet.ServletException; | ||
| import javax.servlet.annotation.WebServlet; | ||
| import javax.servlet.http.HttpServlet; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
|
|
||
| @WebServlet(name = "Messages", urlPatterns = {"/chat/messages/*"}) // magradaria fer un canvi, pero me pareix que no podrem | ||
| public class Messages extends HttpServlet { | ||
|
|
||
| /** | ||
| * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. | ||
| * | ||
| * @param request servlet request | ||
| * @param response servlet response | ||
| * @throws ServletException if a servlet-specific error occurs | ||
| * @throws IOException if an I/O error occurs | ||
| */ | ||
| protected void processRequest(HttpServletRequest request, HttpServletResponse response) | ||
| throws ServletException, IOException { | ||
| response.setContentType("text/html;charset=UTF-8"); | ||
| try (PrintWriter out = response.getWriter()) { | ||
| /* TODO output your page here. You may use following sample code. */ | ||
| out.println("<!DOCTYPE html>"); | ||
| out.println("<html>"); | ||
| out.println("<head>"); | ||
| out.println("<title>Servlet Messages</title>"); | ||
| out.println("</head>"); | ||
| out.println("<body>"); | ||
| out.println("<h1>Servlet Messages at " + request.getContextPath() + "</h1>"); | ||
| out.println("</body>"); | ||
| out.println("</html>"); | ||
| } | ||
| } | ||
|
|
||
| // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> | ||
| /** | ||
| * Handles the HTTP <code>GET</code> method. | ||
| * | ||
| * @param request servlet request | ||
| * @param response servlet response | ||
| * @throws ServletException if a servlet-specific error occurs | ||
| * @throws IOException if an I/O error occurs | ||
| */ | ||
| @Override | ||
| protected void doGet(HttpServletRequest request, HttpServletResponse response) | ||
| throws ServletException, IOException { | ||
| processRequest(request, response); | ||
| } | ||
|
|
||
| /** | ||
| * Handles the HTTP <code>POST</code> method. | ||
| * | ||
| * @param request servlet request | ||
| * @param response servlet response | ||
| * @throws ServletException if a servlet-specific error occurs | ||
| * @throws IOException if an I/O error occurs | ||
| */ | ||
| @Override | ||
| protected void doPost(HttpServletRequest request, HttpServletResponse response) | ||
| throws ServletException, IOException { | ||
| processRequest(request, response); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a short description of the servlet. | ||
| * | ||
| * @return a String containing servlet description | ||
| */ | ||
| @Override | ||
| public String getServletInfo() { | ||
| return "Short description"; | ||
| }// </editor-fold> | ||
|
|
||
| } |
| @@ -0,0 +1,109 @@ | ||
| package servlets; | ||
|
|
||
| import java.io.IOException; | ||
| import javax.servlet.ServletException; | ||
| import javax.servlet.annotation.WebServlet; | ||
| import javax.servlet.http.HttpServlet; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
| import servlets.tools.Helper; | ||
|
|
||
| @WebServlet(name = "Users", urlPatterns = {"/admin/users/*", "/users"}) | ||
| public class Users extends HttpServlet { | ||
|
|
||
| /** | ||
| * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. | ||
| * | ||
| * @param request servlet request | ||
| * @param response servlet response | ||
| * @throws ServletException if a servlet-specific error occurs | ||
| * @throws IOException if an I/O error occurs | ||
| */ | ||
| protected void processRequest(HttpServletRequest request, HttpServletResponse response) | ||
| throws ServletException, IOException { | ||
| // Logged and administrator check, if not redirect to / | ||
| // ojo si es ajax nomes logged | ||
| //response.setContentType("text/html;charset=UTF-8"); | ||
| //response.getWriter().println("Dali Cebes"); | ||
| } | ||
|
|
||
| /** | ||
| * Handles the HTTP <code>GET</code> method. | ||
| * | ||
| * @param request servlet request | ||
| * @param response servlet response | ||
| * @throws ServletException if a servlet-specific error occurs | ||
| * @throws IOException if an I/O error occurs | ||
| */ | ||
| @Override | ||
| protected void doGet(HttpServletRequest request, HttpServletResponse response) | ||
| throws ServletException, IOException { | ||
| processRequest(request, response); | ||
|
|
||
|
|
||
|
|
||
| response.setContentType("text/html;charset=UTF-8"); | ||
|
|
||
| if (Helper.isAjax(request) && request.getRequestURI().equals("/users")) { | ||
| // cercador usuaris | ||
| String parameter = request.getParameter("q"); | ||
| response.getWriter().println("Parametre: " + parameter); | ||
| } else { | ||
| //llistat de usuaris per l'admin | ||
| response.getWriter().println("Super listado loco"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Handles the HTTP <code>POST</code> method. | ||
| * | ||
| * @param request servlet request | ||
| * @param response servlet response | ||
| * @throws ServletException if a servlet-specific error occurs | ||
| * @throws IOException if an I/O error occurs | ||
| */ | ||
| @Override | ||
| protected void doPost(HttpServletRequest request, HttpServletResponse response) | ||
| throws ServletException, IOException { | ||
| processRequest(request, response); | ||
| } | ||
|
|
||
| /** | ||
| * Handles the HTTP <code>PUT</code> method. | ||
| * | ||
| * @param request servlet request | ||
| * @param response servlet response | ||
| * @throws ServletException if a servlet-specific error occurs | ||
| * @throws IOException if an I/O error occurs | ||
| */ | ||
| @Override | ||
| protected void doPut(HttpServletRequest request, HttpServletResponse response) | ||
| throws ServletException, IOException { | ||
| processRequest(request, response); | ||
| } | ||
|
|
||
| /** | ||
| * Handles the HTTP <code>Delete</code> method. | ||
| * | ||
| * @param request servlet request | ||
| * @param response servlet response | ||
| * @throws ServletException if a servlet-specific error occurs | ||
| * @throws IOException if an I/O error occurs | ||
| */ | ||
| @Override | ||
| protected void doDelete(HttpServletRequest request, HttpServletResponse response) | ||
| throws ServletException, IOException { | ||
| processRequest(request, response); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a short description of the servlet. | ||
| * | ||
| * @return a String containing servlet description | ||
| */ | ||
| @Override | ||
| public String getServletInfo() { | ||
| return "Short description"; | ||
| }// </editor-fold> | ||
|
|
||
| } |
| @@ -0,0 +1,15 @@ | ||
| package servlets.tools; | ||
|
|
||
| import javax.servlet.http.HttpServletRequest; | ||
|
|
||
| public class Helper { | ||
|
|
||
| public static boolean isAjax(HttpServletRequest request) { | ||
| return "XMLHttpRequest".equals(request.getHeader("X-Requested-With")); | ||
| } | ||
|
|
||
| // TO DO | ||
| public static boolean isAdmin(HttpServletRequest request) { | ||
| return true; | ||
| } | ||
| } |
| @@ -0,0 +1,11 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd"> | ||
| <glassfish-web-app error-url=""> | ||
| <context-root>/</context-root> | ||
| <class-loader delegate="true"/> | ||
| <jsp-config> | ||
| <property name="keepgenerated" value="true"> | ||
| <description>Keep a copy of the generated servlet class' java code.</description> | ||
| </property> | ||
| </jsp-config> | ||
| </glassfish-web-app> |
| @@ -0,0 +1,17 @@ | ||
| maria: | ||
| image: mariadb:10.1 | ||
| environment: | ||
| - MYSQL_ROOT_PASSWORD=supersecret-password | ||
| - MYSQL_USER=interficies | ||
| - MYSQL_PASSWORD=living-on-the-edge | ||
| volumes: | ||
| - /opt/interficies/mysql:/var/lib/mysql | ||
| ports: | ||
| - 3306:3306 | ||
|
|
||
| mongo: | ||
| image: mongo:3.2 | ||
| volumes: | ||
| - /opt/interficies/mysql:/data/db | ||
| ports: | ||
| - 27017:27017 |