-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathBaseRouteController.java
More file actions
117 lines (95 loc) · 3.4 KB
/
BaseRouteController.java
File metadata and controls
117 lines (95 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package edu.uark.registerapp.controllers;
import java.util.Map;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import edu.uark.registerapp.commands.activeUsers.ValidateActiveUserCommand;
import edu.uark.registerapp.commands.exceptions.UnauthorizedException;
import edu.uark.registerapp.controllers.enums.ViewModelNames;
import edu.uark.registerapp.controllers.enums.QueryParameterMessages;
import edu.uark.registerapp.controllers.enums.QueryParameterNames;
import edu.uark.registerapp.controllers.enums.ViewNames;
import edu.uark.registerapp.models.entities.ActiveUserEntity;
import edu.uark.registerapp.models.enums.EmployeeClassification;
public abstract class BaseRouteController extends BaseController {
protected ModelAndView setErrorMessageFromQueryString(
ModelAndView modelAndView,
final Map<String, String> queryParameters
) {
if (!queryParameters.containsKey(QueryParameterNames.ERROR_CODE.getValue())) {
return modelAndView;
}
try {
modelAndView =
this.setErrorMessageFromQueryString(
modelAndView,
Integer.parseInt(
queryParameters.get(
QueryParameterNames.ERROR_CODE.getValue())));
} catch (final NumberFormatException e) { }
return modelAndView;
}
protected ModelAndView setErrorMessageFromQueryString(
final ModelAndView modelAndView,
final Optional<Integer> errorCode
) {
if (!errorCode.isPresent()) {
return modelAndView;
}
return this.setErrorMessageFromQueryString(modelAndView, errorCode.get());
}
protected Optional<ActiveUserEntity> getCurrentUser(
final HttpServletRequest request
) {
try {
return Optional.of(
this.validateActiveUserCommand
.setSessionKey(request.getSession().getId())
.execute());
} catch (final UnauthorizedException e) {
return Optional.ofNullable(null);
}
}
protected ModelAndView buildInvalidSessionResponse() {
return new ModelAndView(
REDIRECT_PREPEND.concat(
ViewNames.SIGN_IN.getRoute().concat(
this.buildInitialQueryParameter(
QueryParameterNames.ERROR_CODE.getValue(),
QueryParameterMessages.SESSION_NOT_ACTIVE.getKeyAsString()))));
}
protected boolean isElevatedUser(final ActiveUserEntity activeUserEntity) {
return EmployeeClassification.isElevatedUser(
activeUserEntity.getClassification());
}
protected ModelAndView buildNoPermissionsResponse() {
return this.buildNoPermissionsResponse(ViewNames.MAIN_MENU.getRoute());
}
protected ModelAndView buildNoPermissionsResponse(final String redirectRoute) {
return new ModelAndView(
REDIRECT_PREPEND.concat(
redirectRoute.concat(
this.buildInitialQueryParameter(
QueryParameterNames.ERROR_CODE.getValue(),
QueryParameterMessages.NO_PERMISSIONS_TO_VIEW.getKeyAsString()))));
}
protected static final String REDIRECT_PREPEND = "redirect:";
// Helper methods
private ModelAndView setErrorMessageFromQueryString(
final ModelAndView modelAndView,
final int errorCode
) {
final String errorMessage = QueryParameterMessages.mapMessage(errorCode);
if (!StringUtils.isBlank(errorMessage)) {
modelAndView.addObject(
ViewModelNames.ERROR_MESSAGE.getValue(),
errorMessage);
}
return modelAndView;
}
// Properties
@Autowired
private ValidateActiveUserCommand validateActiveUserCommand;
}