|
21 | 21 | import java.lang.reflect.InvocationTargetException; |
22 | 22 | import java.lang.reflect.Method; |
23 | 23 | import java.lang.reflect.Parameter; |
| 24 | +import java.net.http.HttpRequest; |
24 | 25 | import java.util.*; |
25 | 26 |
|
26 | 27 | @MultipartConfig |
@@ -73,38 +74,46 @@ private void processRequest(HttpServletRequest request, HttpServletResponse resp |
73 | 74 | Method method = Tools.getMethodByName(objectClass, mapping.getMethod()); |
74 | 75 | List<Object> parameters = setMethodParameters(method, request); |
75 | 76 |
|
76 | | - // 3. Call the method from the mapping |
77 | | - // Check if client authorized to call the method (if the method is annotated with @Auth) |
78 | | - HttpSession session = request.getSession(); |
79 | | - Object profile = session.getAttribute(Conf.getAuthSessionName()); |
80 | | - if(!User.isAuthorized(method, profile)) { |
81 | | - response.sendRedirect(getServletContext().getContextPath()+Conf.getAuthRedirections().get("AUTH_REDIRECT_LOGOUT")); |
82 | | - return; |
83 | | - } |
84 | 77 |
|
85 | 78 | // @RestAPI function directly return JSON |
86 | 79 | if(method.isAnnotationPresent(RestAPI.class)) { |
87 | 80 | printJson(method.invoke(object, parameters.toArray()), response); |
88 | 81 | return; |
89 | 82 | } |
90 | 83 |
|
| 84 | + // Check if the method return a ModelView object |
91 | 85 | Object functionReturn = method.invoke(object, parameters.toArray()); |
92 | 86 | if(functionReturn == null || functionReturn.getClass() != ModelView.class) { |
93 | 87 | response.sendError(500, "FRAMEWORK ERROR - The method \"" + method.getName() + "\" in the class \"" + |
94 | 88 | objectClass.getName() + "\" make errors, make sure it return a ModelView object or use @RestAPI" + |
95 | 89 | " to return all types"); |
96 | 90 | return; |
97 | 91 | } |
| 92 | + |
| 93 | + // Get the modelView object |
98 | 94 | ModelView modelView = (ModelView) functionReturn; |
99 | 95 |
|
100 | | - // 4. Set the session attributes if the method or the class is annotated with @Session |
101 | | - if(method.isAnnotationPresent(Session.class) || objectClass.isAnnotationPresent(Session.class) || |
102 | | - method.isAnnotationPresent(Auth.class)) { |
103 | | - setSessions(modelView, session); |
104 | | - } else { |
105 | | - if(!modelView.getSession().isEmpty()) |
106 | | - throw new RuntimeException("FRAMEWORK ERROR - You can't set or get session attributes if the method" + |
107 | | - " or the class is not annotated with @Session"); |
| 96 | + |
| 97 | + // Handle session |
| 98 | + HttpSession session = request.getSession(); |
| 99 | + |
| 100 | + // Set the session attributes from modelView to the request |
| 101 | + setSessionsToTheRequest(modelView, session); |
| 102 | + |
| 103 | + // Set the session attributes from request to controller if the method or the class is annotated with @Session |
| 104 | + if(method.isAnnotationPresent(Session.class) || objectClass.isAnnotationPresent(Session.class)) { |
| 105 | + setSessionsFromRequest(object, request, response); |
| 106 | + } |
| 107 | + |
| 108 | + // Re-get the modelView with the new session attributes |
| 109 | + modelView = (ModelView) method.invoke(object, parameters.toArray()); |
| 110 | + |
| 111 | + |
| 112 | + // Check if client authorized to call the method (if the method is annotated with @Auth) |
| 113 | + Object profile = session.getAttribute(Conf.getAuthSessionName()); |
| 114 | + if(!User.isAuthorized(method, profile)) { |
| 115 | + response.sendRedirect(getServletContext().getContextPath()+Conf.getAuthRedirections().get("AUTH_REDIRECT_LOGOUT")); |
| 116 | + return; |
108 | 117 | } |
109 | 118 |
|
110 | 119 | // Set the attributes from the modelView to the request |
@@ -195,21 +204,60 @@ public Object constructObject(Class<?> objectClass) throws NoSuchMethodException |
195 | 204 | return objectClass.getDeclaredConstructor().newInstance(); |
196 | 205 | } |
197 | 206 |
|
198 | | - // Set session attributes from the modelView to the session and vice versa |
199 | | - public void setSessions(ModelView modelView, HttpSession session) { |
200 | | - // Set session attributes from the modelView to the request |
201 | | - for (String key : modelView.getSession().keySet()) { |
202 | | - // If the value is null, remove the attribute from the session |
203 | | - if (modelView.getSession().get(key) == null) |
| 207 | + // Set session attributes from the modelView to the session |
| 208 | + public void setSessionsToTheRequest(ModelView modelView, HttpSession session) { |
| 209 | + /* Removing session */ |
| 210 | + // If invalidateSession is true, invalidate the session |
| 211 | + if(modelView.isInvalidateSession()) { |
| 212 | + // Iterate over the session attributes and remove them |
| 213 | + Enumeration<String> sessionAttributes = session.getAttributeNames(); |
| 214 | + while(sessionAttributes.hasMoreElements()) |
| 215 | + session.removeAttribute(sessionAttributes.nextElement()); |
| 216 | + } |
| 217 | + // If the removeSession list is not empty |
| 218 | + if(!modelView.getRemoveSessions().isEmpty()) { |
| 219 | + for(String key : modelView.getRemoveSessions()) |
204 | 220 | session.removeAttribute(key); |
205 | | - else |
206 | | - session.setAttribute(key, modelView.getSession().get(key)); |
207 | 221 | } |
208 | 222 |
|
209 | | - // Set session from the request to the modelView |
210 | | - for (Enumeration<String> e = session.getAttributeNames(); e.hasMoreElements(); ) { |
| 223 | + /* Adding session */ |
| 224 | + for (String key : modelView.getSession().keySet()) { |
| 225 | + session.setAttribute(key, modelView.getSession().get(key)); |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + // Set session from the request to the modelView |
| 230 | + public void setSessionsFromRequest(Object object, HttpServletRequest request, HttpServletResponse response) throws IOException { |
| 231 | + // Check if the session is new or invalidated |
| 232 | + HttpSession existingSession = request.getSession(false); |
| 233 | + if(existingSession == null || existingSession.isNew()) { |
| 234 | + return; |
| 235 | + } |
| 236 | + |
| 237 | + // Get the field where sessions attributes are stored |
| 238 | + Field field; |
| 239 | + try { |
| 240 | + field = object.getClass().getDeclaredField("sessions"); |
| 241 | + } catch (NoSuchFieldException ex) { |
| 242 | + throw new RuntimeException("FRAMEWORK ERROR - You can't get session attributes if the class `"+ object.getClass().getName() |
| 243 | + +"` does not have " + |
| 244 | + "`Hashmap<String, Object> sessions` as a field"); |
| 245 | + } |
| 246 | + // Set the field accessible |
| 247 | + field.setAccessible(true); |
| 248 | + |
| 249 | + // Iterate over the session attributes and add them to the HashMap |
| 250 | + HashMap<String, Object> sessions = new HashMap<>(); |
| 251 | + for (Enumeration<String> e = request.getSession().getAttributeNames(); e.hasMoreElements(); ) { |
211 | 252 | String key = e.nextElement(); |
212 | | - modelView.addSessionItem(key, session.getAttribute(key)); |
| 253 | + sessions.put(key, request.getSession().getAttribute(key)); |
| 254 | + } |
| 255 | + |
| 256 | + try { |
| 257 | + field.set(object, sessions); |
| 258 | + } catch (IllegalAccessException ex) { |
| 259 | + throw new RuntimeException("FRAMEWORK ERROR - Cannot have access to the field sessions, be sure that the " + |
| 260 | + "field is public"); |
213 | 261 | } |
214 | 262 | } |
215 | 263 |
|
|
0 commit comments