diff --git a/SpringBootProject/pom.xml b/SpringBootProject/pom.xml
index 42c773a..9a56cb5 100644
--- a/SpringBootProject/pom.xml
+++ b/SpringBootProject/pom.xml
@@ -15,6 +15,7 @@
API SAE ScienceQuest
21
+ UTF-8
diff --git a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/ApplicationFilter.java b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/ApplicationFilter.java
index b5836c7..df7c8a3 100644
--- a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/ApplicationFilter.java
+++ b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/ApplicationFilter.java
@@ -16,9 +16,11 @@ import org.springframework.core.annotation.Order;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
+import org.springframework.web.util.ContentCachingResponseWrapper;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
@Component
@Order(1)
@@ -30,83 +32,41 @@ public class ApplicationFilter extends OncePerRequestFilter {
}
@Override
- protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
- throws ServletException, IOException {
+ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
// Intercept and modify the JSON response
- JsonInterceptingResponseWrapper responseWrapper = new JsonInterceptingResponseWrapper(response);
+ ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
filterChain.doFilter(request, responseWrapper);
- // Check if the response content type is JSON
if (responseWrapper.getContentType() != null && responseWrapper.getContentType().startsWith(MediaType.APPLICATION_JSON_VALUE)) {
try {
- // Parse the JSON response
- JsonNode root = objectMapper.readTree(responseWrapper.getContent());
+ // Récupération du contenu en byteArray
+ byte[] responseArray = responseWrapper.getContentAsByteArray();
- // Check if the _embedded node exists and if it contains the tupleBackedMapList node
+ // Parse JSON
+ JsonNode root = objectMapper.readTree(new String(responseArray, StandardCharsets.UTF_8));
+
+ // Vérification de l'existance du node _embedded && s'il contient un node enfant
JsonNode embeddedNode = root.get("_embedded");
- if (embeddedNode != null && embeddedNode.isObject()) {
- JsonNode tupleBackedMapListNode = embeddedNode.get("tupleBackedMapList");
- if (tupleBackedMapListNode != null && tupleBackedMapListNode.isArray()) {
- ArrayNode tupleBackedMapList = (ArrayNode) tupleBackedMapListNode;
+ if (embeddedNode != null && embeddedNode.isObject() && embeddedNode.size() == 1) {
+ JsonNode subNode = embeddedNode.elements().next(); // Récupération node enfant
+ if (subNode != null && subNode.isArray()) {
+ ArrayNode subNodeArr = (ArrayNode) subNode;
- // Remove the _embedded node
+ // Correction de la profondeur
((ObjectNode) root).remove("_embedded");
-
- // Set the tupleBackedMapList directly to the root
- ((ObjectNode) root).set("_embedded", tupleBackedMapList);
+ ((ObjectNode) root).set("_embedded", subNodeArr);
}
}
- // Convert the modified JSON back to string
+ // Retour du JSON dans l'objectMapper en String
String modifiedResponseBody = objectMapper.writeValueAsString(root);
- // Write the modified JSON back to the response
- response.getOutputStream().write(modifiedResponseBody.getBytes());
- } catch (IOException e) {
- // Handle exception
- e.printStackTrace();
- }
- }
- }
-
- private static class JsonInterceptingResponseWrapper extends HttpServletResponseWrapper {
- private ByteArrayOutputStream baos = new ByteArrayOutputStream();
-
- public JsonInterceptingResponseWrapper(HttpServletResponse response) {
- super(response);
- }
-
- @Override
- public ServletOutputStream getOutputStream() throws IOException {
- return new ServletOutputStreamWrapper(baos);
- }
-
- public String getContent() {
- return baos.toString();
- }
- }
-
- private static class ServletOutputStreamWrapper extends ServletOutputStream {
- private ByteArrayOutputStream baos;
-
- public ServletOutputStreamWrapper(ByteArrayOutputStream baos) {
- this.baos = baos;
- }
-
- @Override
- public void write(int b) throws IOException {
- baos.write(b);
- }
-
- @Override
- public boolean isReady() {
- return true;
- }
-
- @Override
- public void setWriteListener(WriteListener writeListener) {
- throw new UnsupportedOperationException("Not implemented");
+ // Ecriture du JSON dans la réponse
+ response.setCharacterEncoding("UTF-8");
+ response.setContentType(MediaType.APPLICATION_JSON_VALUE);
+ response.getOutputStream().write(modifiedResponseBody.getBytes(StandardCharsets.UTF_8));
+ } catch (IOException e) { System.err.println(e.getMessage()); }
}
}
diff --git a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/controllers/Controller.java b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/controllers/Controller.java
new file mode 100644
index 0000000..43e5454
--- /dev/null
+++ b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/controllers/Controller.java
@@ -0,0 +1,83 @@
+package fr.iut.sciencequest.sae.controllers;
+
+import org.springframework.data.domain.Page;
+import org.springframework.hateoas.CollectionModel;
+import org.springframework.hateoas.EntityModel;
+import org.springframework.hateoas.Link;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Optional;
+
+import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
+
+public class Controller {
+ protected final int PAGE_SIZE = 1;
+
+ protected EntityModel getSelfLinkEntityModel(T entity, String method, Object... args) {
+ Class>[] argTypes = new Class[args.length];
+ for (int i = 0; i < args.length; i++) {
+ argTypes[i] = args[i].getClass();
+ }
+
+ try {
+ Link selfLink = linkTo(this.getClass(), this.getClass().getMethod(method, argTypes), args).withSelfRel();
+ return EntityModel.of(entity, selfLink);
+ } catch (NoSuchMethodException e) {
+ System.err.println("Method doesn't exist");
+ return null;
+ }
+ }
+
+ protected CollectionModel getSelfLinkCollectionModel(Iterable entities, String method, Object... args) {
+ Class>[] argTypes = new Class[args.length];
+ for (int i = 0; i < args.length; i++) {
+ argTypes[i] = args[i].getClass();
+ }
+
+ try {
+ Link selfLink = linkTo(this.getClass(), this.getClass().getMethod(method, argTypes), args).withSelfRel();
+ return CollectionModel.of(entities, selfLink);
+ } catch (NoSuchMethodException e) {
+ System.err.println("Method doesn't exist");
+ return null;
+ }
+ }
+
+
+ protected CollectionModel> getPageableCollectionModel(Page pagedResult, Integer page, String method, Object... args) {
+ try {
+ List> entities = pagedResult.map(EntityModel::of).toList();
+
+ Class>[] argTypes = new Class[args.length+1];
+ for (int i = 0; i < args.length; i++) {
+ argTypes[i] = args[i].getClass();
+ }
+ argTypes[args.length] = Optional.class;
+
+ Method finalMethod = this.getClass().getMethod(method, argTypes);
+
+ List