Add support for raw query handling in PrestashopClient and update response handling in PrestashopProxyController
This commit is contained in:
@@ -32,7 +32,6 @@ public class PrestashopProxyController {
|
||||
String relativePath = new AntPathMatcher()
|
||||
.extractPathWithinPattern(bestMatchPattern, fullPath);
|
||||
|
||||
// Path Presta : "/categories" ou "/manufacturers/3" etc.
|
||||
String path = relativePath.isEmpty() ? "/" : "/" + relativePath;
|
||||
|
||||
// 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);
|
||||
|
||||
// On propage le status + body, et on garde le Content-Type renvoyé par Presta
|
||||
MediaType contentType = prestaResponse.getHeaders().getContentType();
|
||||
if (contentType == null) {
|
||||
contentType = MediaType.APPLICATION_JSON;
|
||||
}
|
||||
|
||||
// Deux options :
|
||||
// 1) Propager les headers d’origine (dont Content-Type) :
|
||||
/*
|
||||
return ResponseEntity
|
||||
.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());
|
||||
}
|
||||
}
|
||||
@@ -38,8 +38,8 @@ public class PrestashopClient {
|
||||
}
|
||||
|
||||
/**
|
||||
* Construit l’URL complète en encodant proprement les query params
|
||||
* (y compris les crochets utilisés par PrestaShop : [id,name,...]).
|
||||
* Construit l’URL complète en encodant proprement les query params.
|
||||
* Utilisé par les services "admin" (listSimple, listProducts, etc.).
|
||||
*/
|
||||
private String buildUri(String path, MultiValueMap<String, String> params) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
@@ -68,7 +68,6 @@ public class PrestashopClient {
|
||||
continue;
|
||||
}
|
||||
|
||||
// encode la clé
|
||||
String encodedKey = URLEncoder.encode(key, StandardCharsets.UTF_8);
|
||||
|
||||
for (String rawValue : values) {
|
||||
@@ -79,7 +78,6 @@ public class PrestashopClient {
|
||||
sb.append('&');
|
||||
}
|
||||
|
||||
// encode la valeur (crochets, %, espaces, etc.)
|
||||
String encodedValue = rawValue == null
|
||||
? ""
|
||||
: URLEncoder.encode(rawValue, StandardCharsets.UTF_8);
|
||||
@@ -146,4 +144,36 @@ public class PrestashopClient {
|
||||
.retrieve()
|
||||
.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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user