Skip to content

Commit 5d6d975

Browse files
authored
Merge pull request #23 from mendrika261/sprint15
Fix static files bugs
2 parents 29afd94 + 9745def commit 5d6d975

14 files changed

Lines changed: 46 additions & 61 deletions

File tree

.framework/app.xml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22

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-
83
<app>
94
<config id="auth">
105
<session name="profile" />
116
<profiles>
127
<profile name="AUTH_PROFILE_ADMIN" value="admin" />
138
<profile name="AUTH_PROFILE_USER" value="user" />
149
</profiles>
10+
<redirections>
11+
<redirect name="AUTH_REDIRECT_LOGIN" value="/" />
12+
<redirect name="AUTH_REDIRECT_LOGOUT" value="/login" />
13+
</redirections>
1514
</config>
1615
</app>

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package etu2024.framework.servlet;
22

33
import com.google.gson.Gson;
4-
import etu2024.framework.annotation.Auth;
54
import etu2024.framework.annotation.RestAPI;
65
import etu2024.framework.annotation.Session;
76
import etu2024.framework.annotation.Singleton;
@@ -14,14 +13,12 @@
1413
import jakarta.servlet.*;
1514
import jakarta.servlet.annotation.MultipartConfig;
1615
import jakarta.servlet.http.*;
17-
import org.eclipse.jdt.internal.compiler.codegen.ObjectCache;
1816

1917
import java.io.IOException;
2018
import java.lang.reflect.Field;
2119
import java.lang.reflect.InvocationTargetException;
2220
import java.lang.reflect.Method;
2321
import java.lang.reflect.Parameter;
24-
import java.net.http.HttpRequest;
2522
import java.util.*;
2623

2724
@MultipartConfig
@@ -35,6 +32,7 @@ public void init() throws ServletException {
3532
// Get the mapping urls from the package root set in the web.xml
3633
setMappingUrls(Mapping.getAnnotatedUrlMethod(getInitParameter("PACKAGE_ROOT")));
3734
setInstances(new HashMap<>());
35+
Conf.CONFIG_FILE = getInitParameter("CONFIG_FILE");
3836
}
3937

4038
@Override
@@ -53,6 +51,11 @@ private void processRequest(HttpServletRequest request, HttpServletResponse resp
5351
// Get the url from the request
5452
String request_url = request.getRequestURL().toString().split(request.getContextPath())[1];
5553

54+
if(request_url.contains(".")) {
55+
// send to default servlet
56+
getServletContext().getNamedDispatcher("default").forward(request, response);
57+
}
58+
5659
// Get the mapping from the url
5760
Mapping mapping = getMappingUrls().get(request_url);
5861

@@ -112,7 +115,7 @@ private void processRequest(HttpServletRequest request, HttpServletResponse resp
112115
// Check if client authorized to call the method (if the method is annotated with @Auth)
113116
Object profile = session.getAttribute(Conf.getAuthSessionName());
114117
if(!User.isAuthorized(method, profile)) {
115-
response.sendRedirect(getServletContext().getContextPath()+Conf.getAuthRedirections().get("AUTH_REDIRECT_LOGOUT"));
118+
response.sendRedirect(request.getContextPath()+Conf.getAuthRedirections().get("AUTH_REDIRECT_LOGOUT"));
116119
return;
117120
}
118121

@@ -128,9 +131,9 @@ private void processRequest(HttpServletRequest request, HttpServletResponse resp
128131

129132
// Forward the request if view is set in the modelView
130133
if (modelView.getView() != null) {
131-
if (modelView.isRedirect())
132-
response.sendRedirect(getServletContext().getContextPath()+modelView.getView());
133-
else
134+
if (modelView.isRedirect()) {
135+
response.sendRedirect(request.getContextPath() + modelView.getView());
136+
} else
134137
request.getRequestDispatcher(modelView.getView()).forward(request, response);
135138
}
136139

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static Document getDocument() {
1919
DocumentBuilder builder = factory.newDocumentBuilder();
2020
return builder.parse(CONFIG_FILE);
2121
} catch (Exception e) {
22-
throw new RuntimeException(e);
22+
throw new RuntimeException("Error while parsing app.xml config, verify path or syntax");
2323
}
2424
}
2525

.framework/framework.jar

131 Bytes
Binary file not shown.

.framework/web.xml

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,19 @@
1313
<param-name>PACKAGE_ROOT</param-name>
1414
<param-value>${PROJECT_JAVA_SRC}/</param-value>
1515
</init-param>
16+
<init-param>
17+
<!-- The config file of the project -->
18+
<param-name>CONFIG_FILE</param-name>
19+
<param-value>${PROJECT_CONFIG_FILE}</param-value>
20+
</init-param>
1621
</servlet>
1722

1823
<!--
1924
All the requests are redirected to the FrontServlet
20-
NOTE: For some version of Tomcat, you must add static files into the default servlet
2125
-->
2226
<servlet-mapping>
2327
<servlet-name>FrontServlet</servlet-name>
24-
<url-pattern/>
2528
<url-pattern>/</url-pattern>
2629
</servlet-mapping>
2730

28-
<!--
29-
Static files are served by the default servlet
30-
-->
31-
<servlet-mapping>
32-
<servlet-name>default</servlet-name>
33-
<url-pattern>*.html</url-pattern>
34-
<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>
43-
</servlet-mapping>
44-
4531
</web-app>

conf.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Project configuration
22
## DO NOT PUT SLASHES AT THE END OF THE PATHS
33
PROJECT_NAME=test-framework
4+
PROJECT_CONFIG_FILE=$PWD/app.xml
45
PROJECT_JAVA_SRC=$PWD/test-framework/src/main/java
56
PROJECT_JAVA_LIB=$PWD/test-framework/src/main/webapp/WEB-INF/lib
67
PROJECT_WEB_XML=$PWD/test-framework/src/main/webapp/WEB-INF/web.xml

framework.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ if [[ "$1" == "-i" || "$1" == "--init" ]]; then
3636

3737
# Create web.xml jakarta from ./.framework/web.xml and evaluate variables in the file
3838
export PROJECT_JAVA_SRC
39+
export PROJECT_CONFIG_FILE
3940
envsubst < .framework/web.xml > "$PROJECT_WEB_XML"
41+
envsubst < .framework/web.xml > "$PROJECT_CONFIG_FILE"
42+
43+
# copy app.xml
44+
cp .framework/app.xml "$PROJECT_CONFIG_FILE"
4045

4146
# Move framework.jar to $PROJECT_JAVA_LIB
4247
cp .framework/framework.jar "$PROJECT_JAVA_LIB"
@@ -249,7 +254,8 @@ if [[ "$1" == "-r" || "$1" == "--run" ]]; then
249254

250255
# Run tomcat with hot reload feature
251256
echo -e "${COLOR_BLUE}Running tomcat server... please wait!${COLOR_RESET}"
252-
"$TOMCAT_BIN"/catalina.sh start >> 'tomcat.log' 2>&1
257+
# "$TOMCAT_BIN"/catalina.sh start >> 'tomcat.log' 2>&1
258+
"$TOMCAT_BIN"/catalina.sh run
253259

254260
# Check if tomcat is running on port $TOMCAT_PORT
255261
response=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:"${TOMCAT_PORT}")
@@ -282,6 +288,7 @@ if [[ "$1" == "-r" || "$1" == "--run" ]]; then
282288

283289
# If any files have been modified
284290
if [ ${#changes} -ne 0 ]; then
291+
"$TOMCAT_BIN"/catalina.sh stop >> 'tomcat.log' 2>&1
285292
# Clear the output
286293
clear
287294
echo -ne "\033c"

framework/src/main/java/etu2024/framework/servlet/FrontServlet.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package etu2024.framework.servlet;
22

33
import com.google.gson.Gson;
4-
import etu2024.framework.annotation.Auth;
54
import etu2024.framework.annotation.RestAPI;
65
import etu2024.framework.annotation.Session;
76
import etu2024.framework.annotation.Singleton;
@@ -14,14 +13,12 @@
1413
import jakarta.servlet.*;
1514
import jakarta.servlet.annotation.MultipartConfig;
1615
import jakarta.servlet.http.*;
17-
import org.eclipse.jdt.internal.compiler.codegen.ObjectCache;
1816

1917
import java.io.IOException;
2018
import java.lang.reflect.Field;
2119
import java.lang.reflect.InvocationTargetException;
2220
import java.lang.reflect.Method;
2321
import java.lang.reflect.Parameter;
24-
import java.net.http.HttpRequest;
2522
import java.util.*;
2623

2724
@MultipartConfig
@@ -35,6 +32,7 @@ public void init() throws ServletException {
3532
// Get the mapping urls from the package root set in the web.xml
3633
setMappingUrls(Mapping.getAnnotatedUrlMethod(getInitParameter("PACKAGE_ROOT")));
3734
setInstances(new HashMap<>());
35+
Conf.CONFIG_FILE = getInitParameter("CONFIG_FILE");
3836
}
3937

4038
@Override
@@ -53,6 +51,11 @@ private void processRequest(HttpServletRequest request, HttpServletResponse resp
5351
// Get the url from the request
5452
String request_url = request.getRequestURL().toString().split(request.getContextPath())[1];
5553

54+
if(request_url.contains(".")) {
55+
// send to default servlet
56+
getServletContext().getNamedDispatcher("default").forward(request, response);
57+
}
58+
5659
// Get the mapping from the url
5760
Mapping mapping = getMappingUrls().get(request_url);
5861

@@ -112,7 +115,7 @@ private void processRequest(HttpServletRequest request, HttpServletResponse resp
112115
// Check if client authorized to call the method (if the method is annotated with @Auth)
113116
Object profile = session.getAttribute(Conf.getAuthSessionName());
114117
if(!User.isAuthorized(method, profile)) {
115-
response.sendRedirect(getServletContext().getContextPath()+Conf.getAuthRedirections().get("AUTH_REDIRECT_LOGOUT"));
118+
response.sendRedirect(request.getContextPath()+Conf.getAuthRedirections().get("AUTH_REDIRECT_LOGOUT"));
116119
return;
117120
}
118121

@@ -128,9 +131,9 @@ private void processRequest(HttpServletRequest request, HttpServletResponse resp
128131

129132
// Forward the request if view is set in the modelView
130133
if (modelView.getView() != null) {
131-
if (modelView.isRedirect())
132-
response.sendRedirect(getServletContext().getContextPath()+modelView.getView());
133-
else
134+
if (modelView.isRedirect()) {
135+
response.sendRedirect(request.getContextPath() + modelView.getView());
136+
} else
134137
request.getRequestDispatcher(modelView.getView()).forward(request, response);
135138
}
136139

framework/src/main/java/etu2024/framework/utility/Conf.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static Document getDocument() {
1919
DocumentBuilder builder = factory.newDocumentBuilder();
2020
return builder.parse(CONFIG_FILE);
2121
} catch (Exception e) {
22-
throw new RuntimeException(e);
22+
throw new RuntimeException("Error while parsing app.xml config, verify path or syntax");
2323
}
2424
}
2525

32 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)