Modification commentaires + suppresssions quelques lignes

Springboot
Alix JEUDI--LEMOINE 1 year ago
parent fc555b98a9
commit 9a451618bd

@ -33,17 +33,15 @@ public class ApplicationFilter extends OncePerRequestFilter {
@NonNull @NonNull
protected void doFilterInternal(@Nullable HttpServletRequest request, @Nullable HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { protected void doFilterInternal(@Nullable HttpServletRequest request, @Nullable HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
// Intercept and modify the JSON response // Intercept and modify the JSON response
assert response != null;
ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response); ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
filterChain.doFilter(request, responseWrapper); filterChain.doFilter(request, responseWrapper);
if (responseWrapper.getContentType() != null && responseWrapper.getContentType().startsWith(MediaType.APPLICATION_JSON_VALUE)) { if (responseWrapper.getContentType() != null && responseWrapper.getContentType().startsWith(MediaType.APPLICATION_JSON_VALUE)) {
try { try {
// Récupération du contenu en byteArray
byte[] responseArray = responseWrapper.getContentAsByteArray();
// Parse JSON // Parse JSON
JsonNode root = objectMapper.readTree(new String(responseArray, StandardCharsets.UTF_8)); JsonNode root = objectMapper.readTree(new String(responseWrapper.getContentAsByteArray(), StandardCharsets.UTF_8));
// Vérification de l'existance du node _embedded && s'il contient un node enfant // Vérification de l'existance du node _embedded && s'il contient un node enfant
JsonNode embeddedNode = root.get("_embedded"); JsonNode embeddedNode = root.get("_embedded");
@ -65,7 +63,7 @@ public class ApplicationFilter extends OncePerRequestFilter {
response.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8");
response.setContentType(MediaType.APPLICATION_JSON_VALUE); response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.getOutputStream().write(modifiedResponseBody.getBytes(StandardCharsets.UTF_8)); response.getOutputStream().write(modifiedResponseBody.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) { System.err.println(e.getMessage()); } } catch (IOException e) { System.err.println("ApplicationFilter (_embedded) : " + e.getMessage()); }
} }
} }

Loading…
Cancel
Save