diff --git a/.gitignore b/.gitignore
new file mode 100644
index 00000000..9198dfe8
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,9 @@
+/StackExchange WS/nbproject/
+/StackExchange Client/nbproject/
+/Identity Service/nbproject/
+/Identity Service/build/
+/Identity Service/dist/
+/StackExchange WS/build/
+/StackExchange WS/dist/
+/StackExchange Client/build/
+/StackExchange Client/dist/
\ No newline at end of file
diff --git a/Identity Service/build.xml b/Identity Service/build.xml
new file mode 100644
index 00000000..a5d7ca3a
--- /dev/null
+++ b/Identity Service/build.xml
@@ -0,0 +1,71 @@
+
+
+
+
+
+
+
+
+
+GET
and POST
+ * 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("application/json");
+ try (PrintWriter out = response.getWriter()) {
+ String token = request.getParameter("auth");
+ String query = "SELECT * FROM token WHERE access_token = ?";
+ JSONObject obj = new JSONObject();
+ if (token != null) try (PreparedStatement statement = conn.prepareStatement(query)) {
+ statement.setString(1, token);
+
+ ResultSet result = statement.executeQuery();
+ if(result.next()) {
+ Date current_date = new Date();
+ DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ try {
+ Date expire_date = format.parse(result.getString("expire_date"));
+ if (current_date.after(expire_date)) {
+ obj.put("status", -1);
+ query = "DELETE FROM token WHERE access_token = ?";
+ try (PreparedStatement deleteStatement = conn.prepareStatement(query)) {
+ deleteStatement.setString(1, token);
+ deleteStatement.executeUpdate();
+ }
+ }
+ else {
+ obj.put("status", 1);
+ obj.put("id", result.getInt("user_id"));
+ }
+ }
+ catch ( SQLException | ParseException ex ){
+ System.err.println(ex.getMessage());
+ }
+ }
+ else {
+ obj.put("status", 0);
+ }
+ }
+ out.println(obj);
+ }
+ catch(SQLException ex) {
+ System.err.println(ex.getMessage());
+ }
+ }
+
+ // GET
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 POST
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";
+ }// GET
and POST
+ * 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, SQLException {
+ response.setContentType("application/json");
+ try (PrintWriter out = response.getWriter()) {
+ // get parameter from client
+ String email = request.getParameter("email");
+ String password = request.getParameter("password");
+ JSONObject object = new JSONObject();
+ if (email != null && password != null) {
+ // is user exists in database
+ String sql = "SELECT * FROM user WHERE email = ? AND password = MD5(?)";
+ try (PreparedStatement statement = conn.prepareStatement(sql)) {
+ statement.setString(1, email);
+ statement.setString(2, password);
+ ResultSet result = statement.executeQuery();
+ if (result.next()) {
+ int user_id = result.getInt("id");
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(new Date());
+ calendar.add(Calendar.DATE, 1);
+ Timestamp expire_date = new Timestamp(calendar.getTime().getTime());
+ String uuid = UUID.randomUUID().toString().replaceAll("-", "");
+ conn.setAutoCommit(false);
+
+ // only 1 token from 1 user is allowed
+ String deleteQuery = "DELETE from token WHERE user_id = ?";
+ String insertQuery = "INSERT INTO token (access_token, user_id, expire_date) VALUES (?, ?, ?)";
+ try (
+ PreparedStatement deleteStatement = conn.prepareStatement(deleteQuery);
+ PreparedStatement insertStatement = conn.prepareStatement(insertQuery)) {
+ deleteStatement.setInt(1, user_id);
+ insertStatement.setString(1, uuid);
+ insertStatement.setInt(2, user_id);
+ insertStatement.setTimestamp(3, expire_date);
+ deleteStatement.execute();
+ insertStatement.execute();
+ object.put("auth", uuid);
+ object.put("expire_date", expire_date.getTime());
+ conn.commit();
+ }
+ finally {
+ conn.setAutoCommit(true);
+ }
+ }
+ else {
+ object.put("error", "Invalid username or password");
+ }
+ }
+ }
+ out.println(object.toString());
+ }
+ catch(SQLException ex){
+ Logger.getLogger(ISLoginServlet.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ }
+
+ // GET
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 {
+ try {
+ processRequest(request, response);
+ } catch (SQLException ex) {
+ Logger.getLogger(ISLoginServlet.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ }
+
+ /**
+ * Handles the HTTP POST
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 {
+ try {
+ processRequest(request, response);
+ } catch (SQLException ex) {
+ Logger.getLogger(ISLoginServlet.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ }
+
+ /**
+ * Returns a short description of the servlet.
+ *
+ * @return a String containing servlet description
+ */
+ @Override
+ public String getServletInfo() {
+ return "Short description";
+ }//
GET
and POST
+ * 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 {
+
+ }
+
+ // GET
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 POST
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";
+ }// GET
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 {
+ request.getRequestDispatcher("WEB-INF/view/ask.jsp").forward(request, response);
+ }
+
+ /**
+ * Handles the HTTP POST
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 {
+ // check request topic and content
+ // reload this page if they are empty
+ String
+ topic = request.getParameter("topic"),
+ content = request.getParameter("content");
+ if (topic == null || content == null || topic.isEmpty() || content.isEmpty()) {
+ response.sendRedirect(request.getRequestURI());
+ return;
+ }
+
+ // check access token
+ // forward to login page if access token is not set
+ User user = (User) request.getAttribute("user");
+ if (user == null) {
+ response.sendRedirect(request.getContextPath() + "/login");
+ }
+ else {
+ StackExchange port = service.getStackExchangePort();
+ Question question = port.addQuestion(user.getId(), topic, content);
+ // redirect to question page if success
+ if (question != null) {
+ response.sendRedirect(request.getContextPath() + "/question?id=" + question.getId());
+ }
+ // if failed let the get method handles it
+ else {
+ request.setAttribute("error", "Failed to create question");
+ doGet(request, response);
+ }
+ }
+ }
+
+ /**
+ * Returns a short description of the servlet.
+ *
+ * @return a String containing servlet description
+ */
+ @Override
+ public String getServletInfo() {
+ return "Handle ask request";
+ }// GET
and POST
+ * 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");
+ }
+
+ // GET
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 {
+
+ StackExchange port = service.getStackExchangePort();
+ try{
+ int id = Integer.parseInt(request.getParameter("id"));
+ User user = (User) request.getAttribute("user");
+ Question question = port.getQuestion(id);
+ if(user != null && question != null){
+ // get id user from question
+ int idUser = question.getIdUser();
+ if(idUser == user.getId()){ // user is the owner of the question
+ // delete question
+ port.deleteQuestion(id);
+ }
+ }
+ }
+ catch(Exception ex){
+
+ }
+ response.sendRedirect(request.getContextPath());
+ }
+
+ /**
+ * Handles the HTTP POST
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";
+ }// GET
and POST
+ * 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 {
+
+ }
+
+ // GET
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 {
+ StackExchange port = service.getStackExchangePort();
+ int id = Integer.parseInt(request.getParameter("id"));
+ Question question = port.getQuestion(id);
+ if (question != null) {
+ User user = (User) request.getAttribute("user");
+ if (user != null && user.getId() == question.getIdUser()) {
+ request.setAttribute("question", question);
+ request.getRequestDispatcher("WEB-INF/view/ask.jsp").forward(request, response);
+ return;
+ }
+ }
+ response.sendRedirect(request.getContextPath());
+ }
+
+ /**
+ * Handles the HTTP POST
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 {
+
+ int id = Integer.parseInt(request.getParameter("id"));
+ String
+ topic = request.getParameter("topic"),
+ content = request.getParameter("content");
+ if (topic == null || content == null || topic.isEmpty() || content.isEmpty()) {
+ response.sendRedirect(request.getRequestURI());
+ return;
+ }
+
+ User user = (User) request.getAttribute("user");
+ if (user == null) {
+ response.sendRedirect(request.getContextPath() + "/login");
+ }
+ else {
+ StackExchange port = service.getStackExchangePort();
+ Question question = port.getQuestion(id);
+ if (user.getId() == question.getIdUser()) {
+ boolean flag = port.updateQuestion(id, topic, content);
+ // redirect to question page if success
+ if (flag) {
+ response.sendRedirect(request.getContextPath() + "/question?id=" + question.getId());
+ }
+ // if failed let the get method handles it
+ else {
+ request.setAttribute("error", "Failed to edit question");
+ doGet(request, response);
+ }
+ }
+ }
+ }
+
+ /**
+ * Returns a short description of the servlet.
+ *
+ * @return a String containing servlet description
+ */
+ @Override
+ public String getServletInfo() {
+ return "Short description";
+ }// GET
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 {
+ StackExchange port = service.getStackExchangePort();
+ String paramId = request.getParameter("id");
+ Question question = null;
+ if (paramId != null) {
+ try {
+ question = port.getQuestion(Integer.parseInt(paramId));
+ }
+ catch (NumberFormatException ex) {
+ }
+ }
+ if (question != null) {
+ User user = (User) request.getAttribute("user");
+ request.setAttribute("question", question);
+ request.setAttribute("asker", port.getUser(question.getIdUser()));
+ ListPOST
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 {
+ // get the content from parameter
+ String content = request.getParameter("content");
+ String paramId = request.getParameter("id");
+ boolean paramError = true;
+ int id = -1;
+ try {
+ if (content != null && paramId != null && !content.isEmpty() && !paramId.isEmpty()) {
+ paramError = false;
+ id = Integer.valueOf(paramId);
+ }
+ } catch (NumberFormatException ex) {
+
+ }
+ if (paramError) {
+ response.sendRedirect(request.getRequestURI());
+ return;
+ }
+
+ // get current signed in user
+ User user = (User) request.getAttribute("user");
+ if (user == null) {
+ // redirect to sign in page
+ response.sendRedirect(request.getContextPath() + "/signin");
+ return;
+ }
+
+ // add answer via web service
+ StackExchange port = service.getStackExchangePort();
+ Answer answer = port.addAnswer(id, user.getId(), content);
+ if (answer == null)
+ request.setAttribute("error", "Failed to post answer");
+
+ // get the page
+ doGet(request, response);
+ }
+
+ /**
+ * Returns a short description of the servlet.
+ *
+ * @return a String containing servlet description
+ */
+ @Override
+ public String getServletInfo() {
+ return "Handle question and answer request";
+ }
+
+}
diff --git a/StackExchange Client/src/java/controller/RegisterServlet.java b/StackExchange Client/src/java/controller/RegisterServlet.java
new file mode 100644
index 00000000..f6608340
--- /dev/null
+++ b/StackExchange Client/src/java/controller/RegisterServlet.java
@@ -0,0 +1,97 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package controller;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.xml.ws.WebServiceRef;
+import service.StackExchange;
+import service.StackExchange_Service;
+import service.User;
+
+/**
+ *
+ * @author Adz
+ */
+public class RegisterServlet extends HttpServlet {
+ @WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8081/StackExchange.wsdl")
+ private StackExchange_Service service;
+
+ /**
+ * Processes requests for both HTTP GET
and POST
+ * 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 {
+ }
+
+ // GET
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 {
+ User user = (User) request.getAttribute("user");
+ if(user != null) // user already login
+ response.sendRedirect(request.getContextPath());
+ else
+ request.getRequestDispatcher("WEB-INF/view/register.jsp").forward(request, response);
+ }
+
+ /**
+ * Handles the HTTP POST
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 {
+ StackExchange port = service.getStackExchangePort();
+ String
+ name = request.getParameter("name"),
+ email = request.getParameter("email"),
+ password = request.getParameter("password");
+
+ // get the result message
+ String message = port.addUser(name, email, password);
+
+ // message handling by get method
+ if(message.contains("Success")){ // SUCCESS
+ request.setAttribute("success", message);
+ }
+ else{
+ request.setAttribute("error", message);
+ }
+ doGet(request, response);
+ }
+
+ /**
+ * Returns a short description of the servlet.
+ *
+ * @return a String containing servlet description
+ */
+ @Override
+ public String getServletInfo() {
+ return "Short description";
+ }// GET
and POST
+ * 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 {
+ StackExchange port = service.getStackExchangePort();
+ ListGET
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 POST
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";
+ }// GET
and POST
+ * 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 {
+ }
+
+ // GET
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 {
+ StackExchange port = service.getStackExchangePort();
+
+ String key = request.getParameter("key");
+ ListPOST
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";
+ }// GET
and POST
+ * 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 {
+ }
+
+ // GET
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 {
+ User user = (User) request.getAttribute("user");
+ if (user != null) {
+ response.sendRedirect(request.getContextPath());
+ } else {
+ HttpSession session = request.getSession(false);
+ String error = null;
+ if (session != null && (error = (String) session.getAttribute("error")) != null) {
+ request.setAttribute("error", error);
+ session.removeAttribute("error");
+ }
+ request.getRequestDispatcher("WEB-INF/view/signin.jsp").forward(request, response);
+ }
+ }
+
+ /**
+ * Handles the HTTP POST
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 {
+
+ User user = (User) request.getAttribute("user");
+ if (user != null) { // user already login
+ response.sendRedirect(request.getContextPath());
+ return;
+ }
+ else { // user can login
+ String email = request.getParameter("email"),
+ password = request.getParameter("password");
+
+ JSONObject object = null;
+ if (email != null && password != null && (object = ISConnector.requestLogin(email, password)) != null) {
+ if (object.containsKey("auth")){
+ Cookie cookie = new Cookie("auth", (String)object.get("auth"));
+ cookie.setPath("/");
+ long age = -1;
+ if (object.containsKey("expire_date")) {
+ age = new Timestamp(new Date().getTime()).getTime()-(long)object.get("expire_date");
+ age /= 1000;
+ }
+ cookie.setMaxAge((int)age);
+ response.addCookie(cookie);
+ response.sendRedirect(request.getContextPath());
+ return;
+ } else if (object.containsKey("error")) {
+ request.setAttribute("error", (String)object.get("error"));
+ }
+ }
+ }
+ doGet(request, response);
+}
+
+ /**
+ * Returns a short description of the servlet.
+ *
+ * @return a String containing servlet description
+ */
+ @Override
+ public String getServletInfo() {
+ return "Short description";
+ }// GET
and POST
+ * 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 {
+ // remove auth cookie by set max age to 0
+ Cookie cookie = new Cookie("auth", null);
+ cookie.setMaxAge(0);
+ cookie.setPath("/");
+ response.addCookie(cookie);
+
+ // redirect to home page
+ response.sendRedirect(request.getContextPath());
+ }
+
+ // GET
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 POST
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 "Handle signout";
+ }// GET
and POST
+ * 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("application/json");
+ try (PrintWriter out = response.getWriter()) {
+ JSONObject jsonResponse = new JSONObject();
+ String questionId = request.getParameter("id");
+ String type = request.getParameter("type");
+ String action = request.getParameter("action");
+ User user = (User) request.getAttribute("user");
+ if (questionId != null && type != null && action != null && user != null) {
+ try {
+ int id = Integer.parseInt(questionId);
+ type = type.toLowerCase();
+ action = action.toLowerCase();
+ StackExchange port = service.getStackExchangePort();
+ boolean success = false;
+ if (type.equals("question")) {
+ if (action.equals("up"))
+ success = port.voteQuestionUp(user.getId(), id);
+ else if (action.equals("down"))
+ success = port.voteQuestionDown(user.getId(), id);
+ if (success)
+ jsonResponse.put("votes", port.getQuestion(id).getVotes());
+ }
+ else if (type.equals("answer")) {
+ if (action.equals("up"))
+ success = port.voteAnswerUp(user.getId(), id);
+ else if (action.equals("down"))
+ success = port.voteAnswerDown(user.getId(), id);
+ if (success)
+ jsonResponse.put("votes", port.getAnswer(id).getVotes());
+ }
+ }
+ catch (NumberFormatException ex) {
+ }
+ }
+ out.write(jsonResponse.toString());
+ }
+ }
+
+ // GET
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 POST
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 "Handles voting system";
+ }//