commit 05381d82e869905121813c7d9ef9969f8332e6a7
Author: Pavel Fidranský <fidransky@outlook.com>
Date:   Mon Aug 27 22:25:50 2018 +0200

    minor servlet improvements

diff --git a/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/UploadFiles.java b/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/UploadFiles.java
index 8c121a1..7ca5534 100644
--- a/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/UploadFiles.java
+++ b/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/UploadFiles.java
@@ -24,24 +24,6 @@ public class UploadFiles extends BaseServlet {
 
     @Override
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-        doPost(request, response);
-    }
-
-    @Override
-    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
-        if (ServletFileUpload.isMultipartContent(request)) {
-            String jsonGraph = new FileManager("","").loadFile(request);
-
-            if(Strings.isNullOrEmpty(jsonGraph)){
-                request.setAttribute("errorMessage", "<strong>Unsupported file</strong><br/>");
-            } else {
-                request.getSession().setAttribute("json_graph", jsonGraph);
-                response.sendRedirect(getServletContext().getInitParameter("HOME_URL") + "graph");
-                return;
-            }
-        }
-
         DB db = new DB(getServletContext());
         Diagram diagram = new Diagram(db);
 
@@ -60,4 +42,17 @@ public class UploadFiles extends BaseServlet {
         RequestDispatcher rd = getServletContext().getRequestDispatcher("/uploadFiles.jsp");
         rd.forward(request, response);
     }
+
+    @Override
+    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+        String jsonGraph = new FileManager("","").loadFile(request);
+
+        if (Strings.isNullOrEmpty(jsonGraph)) {
+            request.setAttribute("errorMessage", "<strong>Unsupported file</strong><br/>");
+            doGet(request, response);
+        } else {
+            request.getSession().setAttribute("json_graph", jsonGraph);
+            response.sendRedirect(getServletContext().getInitParameter("HOME_URL") + "graph");
+        }
+    }
 }
diff --git a/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/api/LoadGraphData.java b/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/api/LoadGraphData.java
index c908bf2..59a8c52 100644
--- a/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/api/LoadGraphData.java
+++ b/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/api/LoadGraphData.java
@@ -13,7 +13,6 @@ import cz.zcu.kiv.offscreen.user.DB;
 import cz.zcu.kiv.offscreen.user.Diagram;
 import net.sf.json.JSONObject;
 
-import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.File;
@@ -30,13 +29,11 @@ public class LoadGraphData extends BaseServlet {
      * graph is returned as JSON in response body.
      */
     @Override
-    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
-
+    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
         response.setContentType("application/json");
         response.setCharacterEncoding("UTF-8");
 
         if (request.getSession().getAttribute("demo_id") == null) {
-
             String diagram_id = request.getParameter("diagramId");
 
             if (diagram_id == null) {
@@ -66,26 +63,32 @@ public class LoadGraphData extends BaseServlet {
 
             return json.toString();
         }
-        return "";
+
+        return "";  // TODO: ugly fallback
     }
 
     /**
      * Return json of diagram which is taken from database. Permissions of user to this diagram is checked.
      */
     private String getDiagramById(HttpServletRequest request, int diagramId){
-
         DB db = new DB(getServletContext());
         Diagram diagram = new Diagram(db, diagramId);
 
-        if(!diagram.isPublic()){
-            // Diagram is not public
+        if (diagram.isPublic()) {
+            return diagram.getJsonDiagram();
+        }
+
+        // diagram is not public
+        if (isLoggedIn(request)) {
+            int loggedUserId = getUserId(request);
 
-            if (!isLoggedIn(request) || diagram.getUserId() != getUserId(request)) {
-                return ""; // User is not logged in or is not owner of diagram
+            if (diagram.getUserId() == loggedUserId) {
+                return diagram.getJsonDiagram();
             }
         }
 
-        return diagram.getJsonDiagram();
+        // User is not logged in or is not owner of diagram
+        return "";  // TODO: ugly fallback
     }
 
     /**
diff --git a/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/api/Register.java b/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/api/Register.java
index 64b9ce6..46dabc0 100644
--- a/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/api/Register.java
+++ b/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/api/Register.java
@@ -59,8 +59,6 @@ public class Register extends BaseServlet {
     		errors.put("user_password", "Passwords must be at least 5 characters long.");
     	}
 
-    	JSONObject json = new JSONObject();
-    	
     	if (errors.isEmpty()) {
     		Map<String, String> userMap = new HashMap<>();
     		userMap.put("name", name);
@@ -74,13 +72,14 @@ public class Register extends BaseServlet {
 			response.setStatus(HttpServletResponse.SC_CREATED);
 
     	} else {
-    		json.put("error", errors);
-			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
-    	}
+			JSONObject json = new JSONObject();
+			json.put("error", new JSONObject(errors));
 
-		response.setContentType("application/json");
-		response.getWriter().write(json.toString());
-		response.getWriter().flush();
+			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
+			response.setContentType("application/json");
+			response.getWriter().write(json.toString());
+			response.getWriter().flush();
+		}
     }
     
     /**
diff --git a/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/api/RemoveDiagram.java b/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/api/RemoveDiagram.java
index 63053b9..652a9a2 100644
--- a/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/api/RemoveDiagram.java
+++ b/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/api/RemoveDiagram.java
@@ -12,14 +12,13 @@ public class RemoveDiagram extends BaseServlet {
 
     @Override
     protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws IOException {
-
         if (!isLoggedIn(request)) {
-            response.sendError(response.SC_UNAUTHORIZED);
+            response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
             return;
         }
 
-        if(request.getParameter("diagram_id") == null){
-            response.sendError(response.SC_BAD_REQUEST);
+        if (request.getParameter("diagram_id") == null) {
+            response.sendError(HttpServletResponse.SC_BAD_REQUEST);
             return;
         }
 
@@ -29,12 +28,13 @@ public class RemoveDiagram extends BaseServlet {
         DB db = new DB(getServletContext());
         Diagram diagram = new Diagram(db, diagramId);
 
-        if(diagram.getUserId() != loggedUserId){
-            response.sendError(response.SC_UNAUTHORIZED);
+        if (diagram.getUserId() != loggedUserId) {
+            response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
             return;
         }
 
         diagram.delete();
-        response.sendRedirect("/");
+
+        response.setStatus(HttpServletResponse.SC_NO_CONTENT);
     }
 }
diff --git a/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/api/SaveDiagram.java b/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/api/SaveDiagram.java
index 7fb4a11..be9d5ab 100644
--- a/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/api/SaveDiagram.java
+++ b/sources/src/main/java/cz/zcu/kiv/offscreen/servlets/api/SaveDiagram.java
@@ -16,10 +16,8 @@ public class SaveDiagram extends BaseServlet {
 
     @Override
     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
-
-        // user is not logged in
         if (!isLoggedIn(request)) {
-            response.sendError(response.SC_UNAUTHORIZED);
+            response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
             return;
         }
 
@@ -32,7 +30,7 @@ public class SaveDiagram extends BaseServlet {
 
         // input parameters are invalid
         if (Strings.isNullOrEmpty(name) || Strings.isNullOrEmpty(graphJson)) {
-            response.sendError(response.SC_BAD_REQUEST);
+            response.sendError(HttpServletResponse.SC_BAD_REQUEST);
             return;
         }
 
@@ -50,12 +48,11 @@ public class SaveDiagram extends BaseServlet {
 
             // user is not owner of the diagram
             if (loggedUserId != diagram.getUserId()) {
-                response.sendError(response.SC_UNAUTHORIZED);
+                response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
                 return;
             }
         }
 
-
         Map<String, String> diagramParams = new HashMap<>();
         diagramParams.put("name", name);
         diagramParams.put("public", isPublic);
