@@ -31,46 +31,53 @@ public class Handle404Controller implements ErrorController {
31
31
private PageRepository pageRepository ;
32
32
33
33
/**
34
- * Handles every route different from the ones registered in the HomeController.
34
+ * Handles every route different from the ones registered.
35
+ * This is the way of not throwing a 404 error.
35
36
*
36
37
* @param model
37
38
* @param request
38
39
* @return
39
40
*/
40
41
@ RequestMapping ("/error" )
41
- public String handleError (Model model , HttpServletRequest request ,
42
- Authentication authentication ,
42
+ public String handleError (Model model , HttpServletRequest request , Authentication authentication ,
43
43
@ RequestParam (value = "msg" , defaultValue = "" ) String msg ) {
44
44
model .addAttribute ("active" , authentication .getName ());
45
45
46
46
// Gets the URI that triggered the 404 error (to avoid /error)
47
47
String originalUri = request .getAttribute (RequestDispatcher .FORWARD_REQUEST_URI ).toString ();
48
+ // Fetches the user pages names (sorted by alphabetical order)
48
49
String parsedUri = originalUri .replace ("/" , "" );
50
+
49
51
model .addAttribute ("url" , parsedUri );
50
52
51
53
Page page = pageRepository .findByName (parsedUri );
52
54
55
+ // The page is already found by a user
53
56
if (page != null ) {
54
57
String ownerUsername = page .getOwner ().getUsername ();
55
58
Timestamp datetime = page .getDatetime ();
59
+ // Parses the page timestamp into a more readable format
56
60
String stringDatetime = TimeStampTools .timeStampToString (datetime );
57
61
58
62
model .addAttribute ("datetime" , stringDatetime );
59
63
64
+ // Page found the current user
60
65
if (ownerUsername .equals (authentication .getName ())) {
61
66
model .addAttribute ("username" , "you" );
62
67
model .addAttribute ("userLink" , authentication .getName ());
63
68
} else {
69
+ // Page found another user than the current one
64
70
model .addAttribute ("username" , ownerUsername );
65
71
model .addAttribute ("userLink" , ownerUsername );
66
72
}
67
73
74
+ // Fetches the page comments (sorted by DESC timestamps)
68
75
List <Comment > comments = page .getComments ()
69
76
.stream ()
70
77
.parallel ()
71
78
.sorted ((c1 , c2 ) -> - c1 .getDatetime ().compareTo (c2 .getDatetime ()))
72
79
.collect (Collectors .toList ());
73
-
80
+
74
81
model .addAttribute ("comments" , comments );
75
82
76
83
// Message - from the submission of a comment - to display in a toast
@@ -80,6 +87,7 @@ public String handleError(Model model, HttpServletRequest request,
80
87
return "already_found404" ;
81
88
}
82
89
90
+ // New page found ; added to the current user
83
91
User owner = userRepository .findByUsername (authentication .getName ());
84
92
85
93
page = new Page ();
@@ -92,6 +100,7 @@ public String handleError(Model model, HttpServletRequest request,
92
100
93
101
@ Override
94
102
public String getErrorPath () {
103
+ // Cannot omit this overriden method
95
104
return "getErrorPath" ;
96
105
}
97
106
}
0 commit comments