|
1 | 1 | package etu2024.framework.servlet; |
2 | 2 |
|
| 3 | +import com.google.gson.Gson; |
| 4 | +import etu2024.framework.annotation.Auth; |
| 5 | +import etu2024.framework.annotation.Session; |
3 | 6 | import etu2024.framework.annotation.Singleton; |
4 | 7 | import etu2024.framework.core.File; |
5 | 8 | import etu2024.framework.core.ModelView; |
@@ -79,26 +82,39 @@ private void processRequest(HttpServletRequest request, HttpServletResponse resp |
79 | 82 |
|
80 | 83 | ModelView modelView = (ModelView) method.invoke(object, parameters.toArray()); |
81 | 84 |
|
82 | | - // Set session attributes from the modelView to the request |
83 | | - for (String key: modelView.getSession().keySet()) { |
84 | | - // If the value is null, remove the attribute from the session |
85 | | - if(modelView.getSession().get(key) == null) |
86 | | - session.removeAttribute(key); |
87 | | - else |
88 | | - session.setAttribute(key, modelView.getSession().get(key)); |
89 | | - } |
90 | | - |
91 | | - // Set session from the request to the modelView |
92 | | - for (Enumeration<String> e = session.getAttributeNames(); e.hasMoreElements(); ) { |
93 | | - String key = e.nextElement(); |
94 | | - modelView.addSessionItem(key, session.getAttribute(key)); |
| 85 | + if(method.isAnnotationPresent(Session.class) || objectClass.isAnnotationPresent(Session.class) || |
| 86 | + method.isAnnotationPresent(Auth.class)) { |
| 87 | + // Set session attributes from the modelView to the request |
| 88 | + for (String key : modelView.getSession().keySet()) { |
| 89 | + // If the value is null, remove the attribute from the session |
| 90 | + if (modelView.getSession().get(key) == null) |
| 91 | + session.removeAttribute(key); |
| 92 | + else |
| 93 | + session.setAttribute(key, modelView.getSession().get(key)); |
| 94 | + } |
| 95 | + |
| 96 | + // Set session from the request to the modelView |
| 97 | + for (Enumeration<String> e = session.getAttributeNames(); e.hasMoreElements(); ) { |
| 98 | + String key = e.nextElement(); |
| 99 | + modelView.addSessionItem(key, session.getAttribute(key)); |
| 100 | + } |
| 101 | + } else { |
| 102 | + if(!modelView.getSession().isEmpty()) |
| 103 | + throw new RuntimeException("FRAMEWORK ERROR - You can't set or get session attributes if the method" + |
| 104 | + " or the class is not annotated with @Session"); |
95 | 105 | } |
96 | 106 |
|
97 | 107 | // Set the attributes from the modelView to the request |
98 | 108 | for (String key : modelView.getData().keySet()) |
99 | 109 | request.setAttribute(key, modelView.getData().get(key)); |
100 | 110 |
|
101 | | - // Forward the request to the view |
| 111 | + // Return JSON if isJson is true in the modelView |
| 112 | + if(modelView.isJson()) { |
| 113 | + printJson(modelView.getData(), response); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + // Forward the request if view is set in the modelView |
102 | 118 | if (modelView.getView() != null) |
103 | 119 | request.getRequestDispatcher(modelView.getView()).forward(request, response); |
104 | 120 |
|
@@ -172,6 +188,13 @@ public Object constructObject(Class<?> objectClass) throws NoSuchMethodException |
172 | 188 | return objectClass.getDeclaredConstructor().newInstance(); |
173 | 189 | } |
174 | 190 |
|
| 191 | + public void printJson(Object object, HttpServletResponse response) throws IOException { |
| 192 | + response.setContentType("application/json"); |
| 193 | + response.setCharacterEncoding("UTF-8"); |
| 194 | + Gson gson = new Gson(); |
| 195 | + gson.toJson(object, response.getWriter()); |
| 196 | + } |
| 197 | + |
175 | 198 | // Getters and setters |
176 | 199 | public HashMap<String, Mapping> getMappingUrls() { |
177 | 200 | return mappingUrls; |
|
0 commit comments