From ff331e16303b512ea6ef31834d89aefe4b071163 Mon Sep 17 00:00:00 2001 From: Vincent Guillet Date: Wed, 3 Dec 2025 12:28:22 +0100 Subject: [PATCH] Refactor PrestashopProxyController to streamline API endpoint handling and improve code readability --- .../prestashop/PrestashopProxyController.java | 141 ++++++------------ 1 file changed, 48 insertions(+), 93 deletions(-) diff --git a/api/src/main/java/fr/gameovergne/api/controller/prestashop/PrestashopProxyController.java b/api/src/main/java/fr/gameovergne/api/controller/prestashop/PrestashopProxyController.java index 98c6f52..e594280 100644 --- a/api/src/main/java/fr/gameovergne/api/controller/prestashop/PrestashopProxyController.java +++ b/api/src/main/java/fr/gameovergne/api/controller/prestashop/PrestashopProxyController.java @@ -2,139 +2,94 @@ package fr.gameovergne.api.controller.prestashop; import fr.gameovergne.api.service.prestashop.PrestashopClient; import jakarta.servlet.http.HttpServletRequest; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.util.AntPathMatcher; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; -import org.springframework.web.servlet.HandlerMapping; - -import java.net.URLDecoder; -import java.nio.charset.StandardCharsets; @RestController -@RequestMapping("/api/ps") +@RequestMapping("/api") // important : ton context-path est /gameovergne-api, donc ici on reste sur /api +@RequiredArgsConstructor +@Slf4j public class PrestashopProxyController { - Logger log = LoggerFactory.getLogger(PrestashopProxyController.class); - private final PrestashopClient prestashopClient; - public PrestashopProxyController(PrestashopClient prestashopClient) { - this.prestashopClient = prestashopClient; - } + // ---------- GET générique /api/ps/** ---------- - // --- Helpers communs --- - - private String extractPath(HttpServletRequest request) { - String fullPath = (String) request.getAttribute( - HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE - ); - String bestMatchPattern = (String) request.getAttribute( - HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE - ); - - String relativePath = new AntPathMatcher() - .extractPathWithinPattern(bestMatchPattern, fullPath); - - // On renvoie toujours avec un "/" devant - return "/" + relativePath; - } - - private String extractDecodedQuery(HttpServletRequest request) { - String rawQuery = request.getQueryString(); - if (rawQuery != null) { - rawQuery = URLDecoder.decode(rawQuery, StandardCharsets.UTF_8); - } - return rawQuery; - } - - // ---------- GET ---------- - @GetMapping("/**") + @GetMapping(path = "/ps/**", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity proxyGet(HttpServletRequest request) { - String path = extractPath(request); - String rawQuery = extractDecodedQuery(request); + String requestUri = request.getRequestURI(); // ex: /gameovergne-api/api/ps/categories + String path = requestUri.substring(request.getContextPath().length() + "/api".length()); // -> /ps/categories + String rawQuery = request.getQueryString(); - ResponseEntity prestaResponse = - prestashopClient.getWithRawQuery(path, rawQuery); - - return ResponseEntity - .status(prestaResponse.getStatusCode()) - .contentType(MediaType.APPLICATION_JSON) - .body(prestaResponse.getBody()); + return prestashopClient.getWithRawQuery(path, rawQuery); } - // ---------- POST ---------- - @PostMapping("/**") + // ---------- POST générique XML /api/ps/** (catégories, produits, etc.) ---------- + + @PostMapping( + path = "/ps/**", + consumes = MediaType.APPLICATION_XML_VALUE, + produces = MediaType.APPLICATION_XML_VALUE + ) public ResponseEntity proxyPost(HttpServletRequest request, @RequestBody String xmlBody) { - String path = extractPath(request); - String rawQuery = extractDecodedQuery(request); + + String requestUri = request.getRequestURI(); + String path = requestUri.substring(request.getContextPath().length() + "/api".length()); // ex: /ps/categories + String rawQuery = request.getQueryString(); log.info("XML envoyé à Presta:\n{}", xmlBody); - ResponseEntity prestaResponse = - prestashopClient.postWithRawQuery(path, rawQuery, xmlBody); - - return ResponseEntity - .status(prestaResponse.getStatusCode()) - .contentType(MediaType.APPLICATION_XML) - .body(prestaResponse.getBody()); + return prestashopClient.postWithRawQuery(path, rawQuery, xmlBody); } - // ---------- PUT ---------- - @PutMapping("/**") + // ---------- PUT générique XML /api/ps/** ---------- + + @PutMapping( + path = "/ps/**", + consumes = MediaType.APPLICATION_XML_VALUE, + produces = MediaType.APPLICATION_XML_VALUE + ) public ResponseEntity proxyPut(HttpServletRequest request, @RequestBody String xmlBody) { - String path = extractPath(request); - String rawQuery = extractDecodedQuery(request); - log.info("XML envoyé à Presta:\n{}", xmlBody); + String requestUri = request.getRequestURI(); + String path = requestUri.substring(request.getContextPath().length() + "/api".length()); + String rawQuery = request.getQueryString(); - ResponseEntity prestaResponse = - prestashopClient.putWithRawQuery(path, rawQuery, xmlBody); + log.info("XML envoyé (proxy PUT):\n{}", xmlBody); - return ResponseEntity - .status(prestaResponse.getStatusCode()) - .contentType(MediaType.APPLICATION_XML) - .body(prestaResponse.getBody()); + return prestashopClient.putWithRawQuery(path, rawQuery, xmlBody); } - // ---------- DELETE ---------- - @DeleteMapping("/**") + // ---------- DELETE générique /api/ps/** ---------- + + @DeleteMapping(path = "/ps/**", produces = MediaType.APPLICATION_XML_VALUE) public ResponseEntity proxyDelete(HttpServletRequest request) { - String path = extractPath(request); - String rawQuery = extractDecodedQuery(request); - ResponseEntity prestaResponse = - prestashopClient.deleteWithRawQuery(path, rawQuery); + String requestUri = request.getRequestURI(); + String path = requestUri.substring(request.getContextPath().length() + "/api".length()); + String rawQuery = request.getQueryString(); - return ResponseEntity - .status(prestaResponse.getStatusCode()) - .contentType(MediaType.APPLICATION_XML) - .body(prestaResponse.getBody()); + return prestashopClient.deleteWithRawQuery(path, rawQuery); } + // ---------- UPLOAD IMAGE PRODUIT /api/ps/images/products/{productId} ---------- - /** - * Upload d'une image produit : - * Front → (multipart/form-data) → /api/ps/images/products/{productId} - * Backend → (bytes) → https://shop.gameovergne.fr/api/images/products/{productId} - */ @PostMapping( - path = "/api/ps/images/products/{productId}", + path = "/ps/images/products/{productId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_XML_VALUE ) - public ResponseEntity uploadProductImage( - jakarta.servlet.http.HttpServletRequest request, - @PathVariable("productId") String productId, - @RequestPart("image") MultipartFile imageFile - ) { - String rawQuery = request.getQueryString(); // si jamais tu ajoutes des options côté Presta + public ResponseEntity uploadProductImage(HttpServletRequest request, + @PathVariable("productId") String productId, + @RequestPart("image") MultipartFile imageFile) { + String rawQuery = request.getQueryString(); // si un jour tu veux passer des options log.info("[Proxy] Upload image produit {} (size={} bytes, ct={})", productId, imageFile.getSize(), imageFile.getContentType());