Add support for raw query handling in PrestashopClient and update response handling in PrestashopProxyController

This commit is contained in:
Vincent Guillet
2025-12-02 16:35:42 +01:00
parent 14a6f66742
commit bceedc8620
2 changed files with 45 additions and 12 deletions

View File

@@ -32,7 +32,6 @@ public class PrestashopProxyController {
String relativePath = new AntPathMatcher() String relativePath = new AntPathMatcher()
.extractPathWithinPattern(bestMatchPattern, fullPath); .extractPathWithinPattern(bestMatchPattern, fullPath);
// Path Presta : "/categories" ou "/manufacturers/3" etc.
String path = relativePath.isEmpty() ? "/" : "/" + relativePath; String path = relativePath.isEmpty() ? "/" : "/" + relativePath;
// Query string brute, déjà encodée (display=%5Bid,name,active%5D&output_format=JSON) // Query string brute, déjà encodée (display=%5Bid,name,active%5D&output_format=JSON)
@@ -40,15 +39,19 @@ public class PrestashopProxyController {
var prestaResponse = prestashopClient.getWithRawQuery(path, rawQuery); var prestaResponse = prestashopClient.getWithRawQuery(path, rawQuery);
// On propage le status + body, et on garde le Content-Type renvoyé par Presta // Deux options :
MediaType contentType = prestaResponse.getHeaders().getContentType(); // 1) Propager les headers dorigine (dont Content-Type) :
if (contentType == null) { /*
contentType = MediaType.APPLICATION_JSON;
}
return ResponseEntity return ResponseEntity
.status(prestaResponse.getStatusCode()) .status(prestaResponse.getStatusCode())
.contentType(contentType) .headers(prestaResponse.getHeaders())
.body(prestaResponse.getBody());
*/
// 2) Forcer JSON (si tu veux être sûr du Content-Type côté front) :
return ResponseEntity
.status(prestaResponse.getStatusCode())
.contentType(MediaType.APPLICATION_JSON)
.body(prestaResponse.getBody()); .body(prestaResponse.getBody());
} }
} }

View File

@@ -38,8 +38,8 @@ public class PrestashopClient {
} }
/** /**
* Construit lURL complète en encodant proprement les query params * Construit lURL complète en encodant proprement les query params.
* (y compris les crochets utilisés par PrestaShop : [id,name,...]). * Utilisé par les services "admin" (listSimple, listProducts, etc.).
*/ */
private String buildUri(String path, MultiValueMap<String, String> params) { private String buildUri(String path, MultiValueMap<String, String> params) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
@@ -68,7 +68,6 @@ public class PrestashopClient {
continue; continue;
} }
// encode la clé
String encodedKey = URLEncoder.encode(key, StandardCharsets.UTF_8); String encodedKey = URLEncoder.encode(key, StandardCharsets.UTF_8);
for (String rawValue : values) { for (String rawValue : values) {
@@ -79,7 +78,6 @@ public class PrestashopClient {
sb.append('&'); sb.append('&');
} }
// encode la valeur (crochets, %, espaces, etc.)
String encodedValue = rawValue == null String encodedValue = rawValue == null
? "" ? ""
: URLEncoder.encode(rawValue, StandardCharsets.UTF_8); : URLEncoder.encode(rawValue, StandardCharsets.UTF_8);
@@ -146,4 +144,36 @@ public class PrestashopClient {
.retrieve() .retrieve()
.toBodilessEntity(); .toBodilessEntity();
} }
/**
* Méthode spéciale pour le proxy brut :
* - on reçoit la query string déjà encodée (rawQuery : "display=%5Bid,name,active%5D&output_format=JSON")
* - on NE REENCODE PAS cette query
* - on renvoie un ResponseEntity<String> complet (status + headers + body)
*/
public ResponseEntity<String> getWithRawQuery(String path, String rawQuery) {
StringBuilder sb = new StringBuilder();
sb.append(baseUrl);
if (path != null && !path.isEmpty()) {
if (path.charAt(0) == '/') {
sb.append(path);
} else {
sb.append('/').append(path);
}
}
if (rawQuery != null && !rawQuery.isBlank()) {
sb.append('?').append(rawQuery); // déjà encodé
}
String uri = sb.toString();
log.info("[PrestaShop] RAW GET {}", uri);
return client.get()
.uri(uri)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.toEntity(String.class);
}
} }