Skip to content

Commit 71bb033

Browse files
author
mendrika261
committed
feat: test and README.md
1 parent ae7a7a8 commit 71bb033

27 files changed

Lines changed: 567 additions & 148 deletions

File tree

.framework/app.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
3+
<!--
4+
The app.xml file is used to configure the application.
5+
It is loaded by the framework and can be accessed via the Conf class.
6+
-->
7+
8+
<app>
9+
<config id="auth">
10+
<session name="profile" />
11+
<profiles>
12+
<profile name="AUTH_PROFILE_ADMIN" value="admin" />
13+
<profile name="AUTH_PROFILE_USER" value="user" />
14+
</profiles>
15+
</config>
16+
</app>

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class ModelView {
1212
HashMap<String, Object> data = new HashMap<>();
1313
HashMap<String, Object> session = new HashMap<>();
1414
boolean isJson = false;
15+
boolean isRedirect = false;
1516

1617
// Constructor
1718
public ModelView() {}
@@ -24,7 +25,10 @@ public ModelView(String view) {
2425
public boolean login(String profile) {
2526
User.profileExists(profile);
2627
addSessionItem(Conf.getAuthSessionName(), profile);
27-
return isLogged(profile);
28+
boolean login = isLogged(profile);
29+
if (login)
30+
redirect(Conf.getAuthRedirections().get("AUTH_REDIRECT_LOGIN"));
31+
return login;
2832
}
2933

3034
public boolean loginUser() {
@@ -45,21 +49,35 @@ public boolean loginAdmin() {
4549

4650
public boolean logout() {
4751
removeSessionItem(Conf.getAuthSessionName());
48-
return !getSession().containsKey(Conf.getAuthSessionName()) || getSession().get(Conf.getAuthSessionName()) == null;
52+
boolean logout = !getSession().containsKey(Conf.getAuthSessionName()) || getSession().get(Conf.getAuthSessionName()) == null;
53+
if (logout)
54+
redirect(Conf.getAuthRedirections().get("AUTH_REDIRECT_LOGOUT"));
55+
return logout;
4956
}
5057

5158
public boolean isLogged(String profile) {
5259
return getSession().containsKey(Conf.getAuthSessionName()) && getSession().get(Conf.getAuthSessionName()) != null &&
5360
getSession().get(Conf.getAuthSessionName()).equals(profile);
5461
}
5562

63+
public boolean isLogged() {
64+
return getSession().containsKey(Conf.getAuthSessionName()) && getSession().get(Conf.getAuthSessionName()) != null;
65+
}
66+
67+
public ModelView redirect(String url) {
68+
setView(url);
69+
setRedirect(true);
70+
return this;
71+
}
72+
5673
// Getters and setters
5774
public String getView() {
5875
return view;
5976
}
6077

6178
public void setView(String view) {
6279
this.view = view;
80+
setRedirect(false);
6381
}
6482

6583
public HashMap<String, Object> getData() {
@@ -97,4 +115,12 @@ public boolean isJson() {
97115
public void setJson(boolean isJson) {
98116
this.isJson = isJson;
99117
}
118+
119+
public boolean isRedirect() {
120+
return isRedirect;
121+
}
122+
123+
public void setRedirect(boolean isRedirect) {
124+
this.isRedirect = isRedirect;
125+
}
100126
}

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

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import jakarta.servlet.*;
1515
import jakarta.servlet.annotation.MultipartConfig;
1616
import jakarta.servlet.http.*;
17+
import org.eclipse.jdt.internal.compiler.codegen.ObjectCache;
1718

1819
import java.io.IOException;
1920
import 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");

.framework/dev_files/etu2024/framework/utility/Conf.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,22 @@ public static HashMap<String, String> getAuthProfiles() {
4949
throw new RuntimeException("Profiles for auth not found in app.xml");
5050
}
5151
}
52+
53+
public static HashMap<String, String> getAuthRedirections() {
54+
try {
55+
Element redirectionsElement = (Element) getDocument().getElementsByTagName("redirections").item(0);
56+
NodeList redirectionNodes = redirectionsElement.getElementsByTagName("redirect");
57+
58+
HashMap<String, String> redirectionsMap = new HashMap<>();
59+
for (int i = 0; i < redirectionNodes.getLength(); i++) {
60+
Element redirectionElement = (Element) redirectionNodes.item(i);
61+
String redirectionName = redirectionElement.getAttribute("name");
62+
String redirectionValue = redirectionElement.getAttribute("value");
63+
redirectionsMap.put(redirectionName, redirectionValue);
64+
}
65+
return redirectionsMap;
66+
} catch (Exception e) {
67+
throw new RuntimeException("Redirections for auth not found in app.xml");
68+
}
69+
}
5270
}

.framework/framework.jar

8 Bytes
Binary file not shown.

.framework/web.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,42 @@
44
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
55
version="5.0">
66

7+
<!-- This is the main servlet of the framework -->
78
<servlet>
89
<servlet-name>FrontServlet</servlet-name>
910
<servlet-class>etu2024.framework.servlet.FrontServlet</servlet-class>
1011
<init-param>
12+
<!-- The root package of the project must set here (with framework.sh generally)-->
1113
<param-name>PACKAGE_ROOT</param-name>
1214
<param-value>${PROJECT_JAVA_SRC}/</param-value>
1315
</init-param>
1416
</servlet>
1517

18+
<!--
19+
All the requests are redirected to the FrontServlet
20+
NOTE: For some version of Tomcat, you must add static files into the default servlet
21+
-->
1622
<servlet-mapping>
1723
<servlet-name>FrontServlet</servlet-name>
1824
<url-pattern/>
1925
<url-pattern>/</url-pattern>
2026
</servlet-mapping>
2127

28+
<!--
29+
Static files are served by the default servlet
30+
-->
2231
<servlet-mapping>
2332
<servlet-name>default</servlet-name>
2433
<url-pattern>*.html</url-pattern>
2534
<url-pattern>*.css</url-pattern>
35+
<url-pattern>*.js</url-pattern>
36+
<url-pattern>*.png</url-pattern>
37+
<url-pattern>*.jpg</url-pattern>
38+
<url-pattern>*.jpeg</url-pattern>
39+
<url-pattern>*.gif</url-pattern>
40+
<url-pattern>*.svg</url-pattern>
41+
<url-pattern>*.ico</url-pattern>
42+
<url-pattern>*.ttf</url-pattern>
2643
</servlet-mapping>
2744

2845
</web-app>

README.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<div align="center">
2+
3+
# Weby Framework ☕️
4+
A simple Java Framework for Web Applications 👨‍💻
5+
6+
</div>
7+
8+
## Features 🐣
9+
DONE
10+
1. [x] Singleton class
11+
2. [x] Session
12+
3. [x] Auth with many profiles
13+
4. [x] Return Json (via modelView)
14+
5. [x] Rest API (direct returning object)
15+
6. [x] File upload or input array
16+
7. [x] `app.xml` external configuration file
17+
8. [x] `framework.sh` manager and auto-reload
18+
19+
**TODO**
20+
10. [ ] Database integration
21+
11. [ ] Error reporting (404 error page...)
22+
12. [ ] Log system
23+
13. [ ] Security improvements
24+
14. [ ] Full Documentation <br>
25+
... and more
26+
27+
## Requirements 📋
28+
| Requirements | Version |
29+
|-----------------|-------------------|
30+
| JDK ☕️ | equal or upper 17 |
31+
| Tomcat 🐱 | equal or upper 10 |
32+
| Gson library 📚 | 2.10 (provided ✅) |
33+
34+
## Installation 🚀
35+
Get the last released version from the [releases page]()
36+
- Demo version contains examples of use and some test
37+
- Production version contains only the framework
38+
39+
### First configuration
40+
- Open `conf.env` file and set all: jdk, tomcat, information about your project like where the project will be created<br>
41+
If it is not in your file get it from [here]() and change
42+
- Then, give permissions for `framework.sh` the script that will help you to manage the framework
43+
```bash
44+
sudo chmod +x framework.sh
45+
```
46+
- Finally, `init` your repository for a weby project
47+
```bash
48+
./framework.sh --init
49+
```
50+
51+
### Running
52+
- To run your project you can use the script `framework.sh`, make `framework.sh --help` to see all options
53+
```bash
54+
./framework.sh --run
55+
```
56+
> The script will compile your project and run it in tomcat with `auto reload` feature enabled,
57+
for `*.xml` or any configurations changed you might sometimes need to restart tomcat ⚠️
58+
59+
## Usage 🧑‍🍳
60+
> Let's learn by examples, it is the best (and fastest) way to learn. ℹ️
61+
62+
### Overview
63+
#### This is a basic controller
64+
```java
65+
import etu2024.framework.annotation.Auth;
66+
import etu2024.framework.core.ModelView;
67+
...
68+
@Url(url = "/") // The url where the controller will be called
69+
public ModelView index() {
70+
ModelView modelView = new ModelView(); // A class to manage the view
71+
72+
modelView.setView("home.jsp"); // The page to render
73+
return modelView;
74+
}
75+
...
76+
```
77+
#### Send data to the view
78+
```java
79+
...
80+
// The class where all process will be done
81+
ModelView modelView = new ModelView();
82+
// Add a variable to the view
83+
modelView.addItem("name", "Weby");
84+
// The page to render
85+
modelView.setView("home.jsp");
86+
...
87+
```
88+
#### Get data from the view
89+
Attributes and parameters are filled automatically
90+
```java
91+
public class Customer()
92+
private String name;
93+
...
94+
public ModelView getCustomer(String id) {
95+
ModelView modelView = new ModelView();
96+
97+
// Access directly to the variable name and id
98+
99+
modelView.setView("home.jsp");
100+
return modelView;
101+
}
102+
...
103+
```
104+
#### Redirection
105+
```java
106+
...
107+
ModelView modelView = new ModelView();
108+
return modelView.redirect("/home");
109+
...
110+
```
111+
112+
#### See full documentation [here]() 📖
113+
Report bugs [here]() 🐛 and contribute [here]() 🤝
114+

app.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
<session name="profile" />
66
<profiles>
77
<profile name="AUTH_PROFILE_ADMIN" value="admin" />
8+
<profile name="AUTH_PROFILE_USER" value="user" />
89
</profiles>
10+
<redirections>
11+
<redirect name="AUTH_REDIRECT_LOGIN" value="/" />
12+
<redirect name="AUTH_REDIRECT_LOGOUT" value="/login" />
13+
</redirections>
914
</config>
1015
</app>

conf.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ TOMCAT_PORT=8080
2525
JAVAC=`which javac`
2626
JAVA=`which java`
2727

28-
# Color configuration
28+
# Color configuration (DO NOT CHANGE)
2929
COLOR_RED="\033[0;31m"
3030
COLOR_GREEN="\033[0;32m"
3131
COLOR_BLUE="\033[0;34m"

0 commit comments

Comments
 (0)