Enhance PrestashopProxyController and PrestashopClient to support POST, PUT, and DELETE methods with raw query handling
This commit is contained in:
@@ -1,13 +1,11 @@
|
|||||||
package fr.gameovergne.api.controller;
|
package fr.gameovergne.api.controller.prestashop;
|
||||||
|
|
||||||
import fr.gameovergne.api.service.prestashop.PrestashopClient;
|
import fr.gameovergne.api.service.prestashop.PrestashopClient;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.util.AntPathMatcher;
|
import org.springframework.util.AntPathMatcher;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
import org.springframework.web.servlet.HandlerMapping;
|
import org.springframework.web.servlet.HandlerMapping;
|
||||||
|
|
||||||
import java.net.URLDecoder;
|
import java.net.URLDecoder;
|
||||||
@@ -23,28 +21,90 @@ public class PrestashopProxyController {
|
|||||||
this.prestashopClient = prestashopClient;
|
this.prestashopClient = prestashopClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/**")
|
// --- Helpers communs ---
|
||||||
public ResponseEntity<String> proxyGet(HttpServletRequest request) {
|
|
||||||
|
private String extractPath(HttpServletRequest request) {
|
||||||
String fullPath = (String) request.getAttribute(
|
String fullPath = (String) request.getAttribute(
|
||||||
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
|
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE
|
||||||
|
);
|
||||||
String bestMatchPattern = (String) request.getAttribute(
|
String bestMatchPattern = (String) request.getAttribute(
|
||||||
HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
|
HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE
|
||||||
|
);
|
||||||
|
|
||||||
String relativePath = new AntPathMatcher()
|
String relativePath = new AntPathMatcher()
|
||||||
.extractPathWithinPattern(bestMatchPattern, fullPath);
|
.extractPathWithinPattern(bestMatchPattern, fullPath);
|
||||||
|
|
||||||
String rawQuery = request.getQueryString();
|
// On renvoie toujours avec un "/" devant
|
||||||
if (rawQuery != null) {
|
return "/" + relativePath;
|
||||||
// 🔥 Correction : on désencode les caractères URL avant de relayer à Presta
|
|
||||||
rawQuery = URLDecoder.decode(rawQuery, StandardCharsets.UTF_8);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String extractDecodedQuery(HttpServletRequest request) {
|
||||||
|
String rawQuery = request.getQueryString();
|
||||||
|
if (rawQuery != null) {
|
||||||
|
rawQuery = URLDecoder.decode(rawQuery, StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
return rawQuery;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- GET ----------
|
||||||
|
@GetMapping("/**")
|
||||||
|
public ResponseEntity<String> proxyGet(HttpServletRequest request) {
|
||||||
|
String path = extractPath(request);
|
||||||
|
String rawQuery = extractDecodedQuery(request);
|
||||||
|
|
||||||
ResponseEntity<String> prestaResponse =
|
ResponseEntity<String> prestaResponse =
|
||||||
prestashopClient.getWithRawQuery("/" + relativePath, rawQuery);
|
prestashopClient.getWithRawQuery(path, rawQuery);
|
||||||
|
|
||||||
return ResponseEntity
|
return ResponseEntity
|
||||||
.status(prestaResponse.getStatusCode())
|
.status(prestaResponse.getStatusCode())
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
.body(prestaResponse.getBody());
|
.body(prestaResponse.getBody());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------- POST ----------
|
||||||
|
@PostMapping("/**")
|
||||||
|
public ResponseEntity<String> proxyPost(HttpServletRequest request,
|
||||||
|
@RequestBody String xmlBody) {
|
||||||
|
String path = extractPath(request);
|
||||||
|
String rawQuery = extractDecodedQuery(request);
|
||||||
|
|
||||||
|
ResponseEntity<String> prestaResponse =
|
||||||
|
prestashopClient.postWithRawQuery(path, rawQuery, xmlBody);
|
||||||
|
|
||||||
|
return ResponseEntity
|
||||||
|
.status(prestaResponse.getStatusCode())
|
||||||
|
.contentType(MediaType.APPLICATION_XML)
|
||||||
|
.body(prestaResponse.getBody());
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- PUT ----------
|
||||||
|
@PutMapping("/**")
|
||||||
|
public ResponseEntity<String> proxyPut(HttpServletRequest request,
|
||||||
|
@RequestBody String xmlBody) {
|
||||||
|
String path = extractPath(request);
|
||||||
|
String rawQuery = extractDecodedQuery(request);
|
||||||
|
|
||||||
|
ResponseEntity<String> prestaResponse =
|
||||||
|
prestashopClient.putWithRawQuery(path, rawQuery, xmlBody);
|
||||||
|
|
||||||
|
return ResponseEntity
|
||||||
|
.status(prestaResponse.getStatusCode())
|
||||||
|
.contentType(MediaType.APPLICATION_XML)
|
||||||
|
.body(prestaResponse.getBody());
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- DELETE ----------
|
||||||
|
@DeleteMapping("/**")
|
||||||
|
public ResponseEntity<String> proxyDelete(HttpServletRequest request) {
|
||||||
|
String path = extractPath(request);
|
||||||
|
String rawQuery = extractDecodedQuery(request);
|
||||||
|
|
||||||
|
ResponseEntity<String> prestaResponse =
|
||||||
|
prestashopClient.deleteWithRawQuery(path, rawQuery);
|
||||||
|
|
||||||
|
return ResponseEntity
|
||||||
|
.status(prestaResponse.getStatusCode())
|
||||||
|
.contentType(MediaType.APPLICATION_XML)
|
||||||
|
.body(prestaResponse.getBody());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -99,12 +99,8 @@ public class PrestashopClient {
|
|||||||
.toBodilessEntity();
|
.toBodilessEntity();
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------- Méthode générique utilisée par le proxy /api/ps/** --------
|
// -------- Méthodes génériques utilisées par le proxy /api/ps/** --------
|
||||||
|
|
||||||
/**
|
|
||||||
* Proxy brut : on lui donne le path Presta (ex: "/categories") et la query string déjà encodée.
|
|
||||||
* On récupère un ResponseEntity<String> pour pouvoir propager le status code.
|
|
||||||
*/
|
|
||||||
public ResponseEntity<String> getWithRawQuery(String path, String rawQuery) {
|
public ResponseEntity<String> getWithRawQuery(String path, String rawQuery) {
|
||||||
String uri = baseUrl + path;
|
String uri = baseUrl + path;
|
||||||
if (rawQuery != null && !rawQuery.isBlank()) {
|
if (rawQuery != null && !rawQuery.isBlank()) {
|
||||||
@@ -119,4 +115,50 @@ public class PrestashopClient {
|
|||||||
.retrieve()
|
.retrieve()
|
||||||
.toEntity(String.class);
|
.toEntity(String.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ResponseEntity<String> postWithRawQuery(String path, String rawQuery, String xmlBody) {
|
||||||
|
String uri = baseUrl + path;
|
||||||
|
if (rawQuery != null && !rawQuery.isBlank()) {
|
||||||
|
uri = uri + "?" + rawQuery;
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("[PrestaShop] POST (proxy) {}", uri);
|
||||||
|
|
||||||
|
return client.post()
|
||||||
|
.uri(uri)
|
||||||
|
.contentType(MediaType.APPLICATION_XML)
|
||||||
|
.body(xmlBody)
|
||||||
|
.retrieve()
|
||||||
|
.toEntity(String.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ResponseEntity<String> putWithRawQuery(String path, String rawQuery, String xmlBody) {
|
||||||
|
String uri = baseUrl + path;
|
||||||
|
if (rawQuery != null && !rawQuery.isBlank()) {
|
||||||
|
uri = uri + "?" + rawQuery;
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("[PrestaShop] PUT (proxy) {}", uri);
|
||||||
|
|
||||||
|
return client.put()
|
||||||
|
.uri(uri)
|
||||||
|
.contentType(MediaType.APPLICATION_XML)
|
||||||
|
.body(xmlBody)
|
||||||
|
.retrieve()
|
||||||
|
.toEntity(String.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ResponseEntity<String> deleteWithRawQuery(String path, String rawQuery) {
|
||||||
|
String uri = baseUrl + path;
|
||||||
|
if (rawQuery != null && !rawQuery.isBlank()) {
|
||||||
|
uri = uri + "?" + rawQuery;
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("[PrestaShop] DELETE (proxy) {}", uri);
|
||||||
|
|
||||||
|
return client.delete()
|
||||||
|
.uri(uri)
|
||||||
|
.retrieve()
|
||||||
|
.toEntity(String.class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user