1414import jakarta .servlet .*;
1515import jakarta .servlet .annotation .MultipartConfig ;
1616import jakarta .servlet .http .*;
17+ import org .eclipse .jdt .internal .compiler .codegen .ObjectCache ;
1718
1819import java .io .IOException ;
1920import java .lang .reflect .Field ;
@@ -77,33 +78,29 @@ private void processRequest(HttpServletRequest request, HttpServletResponse resp
7778 HttpSession session = request .getSession ();
7879 Object profile = session .getAttribute (Conf .getAuthSessionName ());
7980 if (!User .isAuthorized (method , profile )) {
80- response .sendError ( 403 , "FRAMEWORK ERROR - You are not authorized to access this page" );
81+ response .sendRedirect ( getServletContext (). getContextPath ()+ Conf . getAuthRedirections (). get ( "AUTH_REDIRECT_LOGOUT" ) );
8182 return ;
8283 }
8384
85+ // @RestAPI function directly return JSON
8486 if (method .isAnnotationPresent (RestAPI .class )) {
8587 printJson (method .invoke (object , parameters .toArray ()), response );
8688 return ;
8789 }
8890
89- ModelView modelView = (ModelView ) method .invoke (object , parameters .toArray ());
91+ Object functionReturn = method .invoke (object , parameters .toArray ());
92+ if (functionReturn == null || functionReturn .getClass () != ModelView .class ) {
93+ response .sendError (500 , "FRAMEWORK ERROR - The method \" " + method .getName () + "\" in the class \" " +
94+ objectClass .getName () + "\" make errors, make sure it return a ModelView object or use @RestAPI" +
95+ " to return all types" );
96+ return ;
97+ }
98+ ModelView modelView = (ModelView ) functionReturn ;
9099
100+ // 4. Set the session attributes if the method or the class is annotated with @Session
91101 if (method .isAnnotationPresent (Session .class ) || objectClass .isAnnotationPresent (Session .class ) ||
92102 method .isAnnotationPresent (Auth .class )) {
93- // Set session attributes from the modelView to the request
94- for (String key : modelView .getSession ().keySet ()) {
95- // If the value is null, remove the attribute from the session
96- if (modelView .getSession ().get (key ) == null )
97- session .removeAttribute (key );
98- else
99- session .setAttribute (key , modelView .getSession ().get (key ));
100- }
101-
102- // Set session from the request to the modelView
103- for (Enumeration <String > e = session .getAttributeNames (); e .hasMoreElements (); ) {
104- String key = e .nextElement ();
105- modelView .addSessionItem (key , session .getAttribute (key ));
106- }
103+ setSessions (modelView , session );
107104 } else {
108105 if (!modelView .getSession ().isEmpty ())
109106 throw new RuntimeException ("FRAMEWORK ERROR - You can't set or get session attributes if the method" +
@@ -121,8 +118,12 @@ private void processRequest(HttpServletRequest request, HttpServletResponse resp
121118 }
122119
123120 // Forward the request if view is set in the modelView
124- if (modelView .getView () != null )
125- request .getRequestDispatcher (modelView .getView ()).forward (request , response );
121+ if (modelView .getView () != null ) {
122+ if (modelView .isRedirect ())
123+ response .sendRedirect (getServletContext ().getContextPath ()+modelView .getView ());
124+ else
125+ request .getRequestDispatcher (modelView .getView ()).forward (request , response );
126+ }
126127
127128 } catch (ClassNotFoundException | InvocationTargetException | IllegalAccessException | NoSuchMethodException |
128129 InstantiationException e ) {
@@ -194,6 +195,26 @@ public Object constructObject(Class<?> objectClass) throws NoSuchMethodException
194195 return objectClass .getDeclaredConstructor ().newInstance ();
195196 }
196197
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 )
204+ session .removeAttribute (key );
205+ else
206+ session .setAttribute (key , modelView .getSession ().get (key ));
207+ }
208+
209+ // Set session from the request to the modelView
210+ for (Enumeration <String > e = session .getAttributeNames (); e .hasMoreElements (); ) {
211+ String key = e .nextElement ();
212+ modelView .addSessionItem (key , session .getAttribute (key ));
213+ }
214+ }
215+
216+
217+
197218 public void printJson (Object object , HttpServletResponse response ) throws IOException {
198219 response .setContentType ("application/json" );
199220 response .setCharacterEncoding ("UTF-8" );
0 commit comments