Skip to content

Commit 078fbb7

Browse files
authored
Merge pull request #16 from mendrika261/sprint13
sprint13: capability to return Json with ModelView
2 parents 152c091 + fe70071 commit 078fbb7

17 files changed

Lines changed: 123 additions & 46 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package etu2024.framework.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Target({ElementType.METHOD, ElementType.TYPE})
9+
@Retention(RetentionPolicy.RUNTIME)
10+
public @interface Session {
11+
}

.framework/dev_files/etu2024/framework/core/ModelView.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class ModelView {
1111
String view;
1212
HashMap<String, Object> data = new HashMap<>();
1313
HashMap<String, Object> session = new HashMap<>();
14+
boolean isJson = false;
1415

1516
// Constructor
1617
public ModelView() {}
@@ -88,4 +89,12 @@ public void addSessionItem(String key, Object value) {
8889
public void removeSessionItem(String key) {
8990
getSession().put(key, null);
9091
}
92+
93+
public boolean isJson() {
94+
return isJson;
95+
}
96+
97+
public void setJson(boolean isJson) {
98+
this.isJson = isJson;
99+
}
91100
}

.framework/dev_files/etu2024/framework/servlet/FrontServlet.java

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package etu2024.framework.servlet;
22

3+
import com.google.gson.Gson;
4+
import etu2024.framework.annotation.Auth;
5+
import etu2024.framework.annotation.Session;
36
import etu2024.framework.annotation.Singleton;
47
import etu2024.framework.core.File;
58
import etu2024.framework.core.ModelView;
@@ -79,26 +82,39 @@ private void processRequest(HttpServletRequest request, HttpServletResponse resp
7982

8083
ModelView modelView = (ModelView) method.invoke(object, parameters.toArray());
8184

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");
95105
}
96106

97107
// Set the attributes from the modelView to the request
98108
for (String key : modelView.getData().keySet())
99109
request.setAttribute(key, modelView.getData().get(key));
100110

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
102118
if (modelView.getView() != null)
103119
request.getRequestDispatcher(modelView.getView()).forward(request, response);
104120

@@ -172,6 +188,13 @@ public Object constructObject(Class<?> objectClass) throws NoSuchMethodException
172188
return objectClass.getDeclaredConstructor().newInstance();
173189
}
174190

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+
175198
// Getters and setters
176199
public HashMap<String, Mapping> getMappingUrls() {
177200
return mappingUrls;

.framework/framework.jar

1.63 KB
Binary file not shown.

.framework/lib/gson-2.10.1.jar

277 KB
Binary file not shown.

WEB-INF/lib/framework.jar

-579 Bytes
Binary file not shown.

WEB-INF/web.xml

Lines changed: 0 additions & 28 deletions
This file was deleted.

compilation.log

Whitespace-only changes.

framework.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ if [[ "$1" == "-i" || "$1" == "--init" ]]; then
4141
# Move framework.jar to $PROJECT_JAVA_LIB
4242
cp .framework/framework.jar "$PROJECT_JAVA_LIB"
4343

44+
# Copy lib files from .framework
45+
cp -r .framework/lib/* "$PROJECT_JAVA_LIB"
46+
4447
# Project creation test
4548
error=0
4649

@@ -139,7 +142,7 @@ function compile {
139142
fi
140143
reminding_files=${#source_files[@]}
141144
done
142-
rm -rf compilation.temp
145+
rm -rf compilation.log
143146
}
144147

145148

framework/lib/gson-2.10.1.jar

277 KB
Binary file not shown.

0 commit comments

Comments
 (0)