Skip to content

Commit

Permalink
fix/29-encoding Ampersand not encoded when contained in a search crit…
Browse files Browse the repository at this point in the history
…eria value (#30)
  • Loading branch information
antoinebesnardof authored Mar 13, 2024
1 parent 7392d61 commit 53e6f43
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
3 changes: 1 addition & 2 deletions querydsl-postgrest-resttemplate-adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ public class PostgrestConfiguration {
public PostgrestClient podstgrestClient() {
String serviceUrl = "http://localhost:9000";
RestTemplate restTemplate = new RestTemplate();
restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory(serviceUrl));
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(HttpClients.createDefault()));
return PostgrestRestTemplate.of(webclient);
return PostgrestRestTemplate.of(restTemplate, serviceUrl);
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import fr.ouestfrance.querydsl.postgrest.model.RangeResponse;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.reflect.TypeUtils;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
Expand All @@ -17,9 +15,10 @@
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.HashMap;
import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import static fr.ouestfrance.querydsl.postgrest.ParametrizedTypeUtils.listRef;
Expand All @@ -36,23 +35,19 @@ public class PostgrestRestTemplate implements PostgrestClient {
*/
private final RestTemplate restTemplate;

/**
* Postgrest restTemplate adapter
*
* @param restTemplate restTemplate
* @return PostgrestRestTemplate implementation
*/
public static PostgrestRestTemplate of(RestTemplate restTemplate) {
return new PostgrestRestTemplate(restTemplate);
private final String baseUrl;

public static PostgrestRestTemplate of(RestTemplate restTemplate, String baseUrl) {
return new PostgrestRestTemplate(restTemplate, baseUrl);
}

@Override
public <T> RangeResponse<T> search(String resource, Map<String, List<String>> params,
Map<String, List<String>> headers, Class<T> clazz) {
ResponseEntity<List<T>> response = restTemplate.exchange(restTemplate.getUriTemplateHandler()
.expand(UriComponentsBuilder.fromPath(resource)
.queryParams(toMultiMap(params)).build().toString(), new HashMap<>()), HttpMethod.GET,
ResponseEntity<List<T>> response = restTemplate.exchange(
getUri(resource, params), HttpMethod.GET,
new HttpEntity<>(null, toHeaders(headers)), listRef(clazz));

// Retrieve result headers
return Optional.of(response)
.map(HttpEntity::getBody)
Expand All @@ -65,30 +60,31 @@ public <T> RangeResponse<T> search(String resource, Map<String, List<String>> pa

@Override
public <T> BulkResponse<T> post(String resource, List<Object> value, Map<String, List<String>> headers, Class<T> clazz) {
ResponseEntity<List<T>> response = restTemplate.exchange(resource, HttpMethod.POST, new HttpEntity<>(value, toHeaders(headers)), listRef(clazz));
ResponseEntity<List<T>> response = restTemplate.exchange(getUri(resource), HttpMethod.POST, new HttpEntity<>(value, toHeaders(headers)), listRef(clazz));
return toBulkResponse(response);
}


@Override
public <T> BulkResponse<T> patch(String resource, Map<String, List<String>> params, Object value, Map<String, List<String>> headers, Class<T> clazz) {
ResponseEntity<List<T>> response = restTemplate.exchange(restTemplate.getUriTemplateHandler()
.expand(UriComponentsBuilder.fromPath(resource).queryParams(toMultiMap(params)).build().toString(), new HashMap<>()),
ResponseEntity<List<T>> response = restTemplate.exchange(
getUri(resource, params),
HttpMethod.PATCH, new HttpEntity<>(value, toHeaders(headers)), listRef(clazz));
return toBulkResponse(response);
}

@Override
public <T> BulkResponse<T> delete(String resource, Map<String, List<String>> params, Map<String, List<String>> headers, Class<T> clazz) {
ResponseEntity<List<T>> response = restTemplate.exchange(restTemplate.getUriTemplateHandler().expand(UriComponentsBuilder.fromPath(resource)
.queryParams(toMultiMap(params)).build().toString(), new HashMap<>()), HttpMethod.DELETE, new HttpEntity<>(null, toHeaders(headers)), listRef(clazz));
ResponseEntity<List<T>> response = restTemplate.exchange(
getUri(resource, params),
HttpMethod.DELETE, new HttpEntity<>(null, toHeaders(headers)), listRef(clazz));
return toBulkResponse(response);
}

@Override
public List<CountItem> count(String resource, Map<String, List<String>> map) {
return restTemplate.exchange(restTemplate.getUriTemplateHandler().expand(UriComponentsBuilder.fromPath(resource)
.queryParams(toMultiMap(map)).build().toString(), new HashMap<>()), HttpMethod.GET, new HttpEntity<>(null, new HttpHeaders()), listRef(CountItem.class)).getBody();
return restTemplate.exchange(
getUri(resource, map), HttpMethod.GET, new HttpEntity<>(null, new HttpHeaders()), listRef(CountItem.class)).getBody();
}


Expand All @@ -99,5 +95,17 @@ private static MultiValueMap<String, String> toMultiMap(Map<String, List<String>
private static HttpHeaders toHeaders(Map<String, List<String>> headers) {
return new HttpHeaders(toMultiMap(headers));
}
private URI getUri(String resource) {
return getUri(resource, null);
}

private URI getUri(String resource, Map<String, List<String>> params) {
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(baseUrl).path(resource);
if (Objects.nonNull(params)) {
uriBuilder = uriBuilder.queryParams(toMultiMap(params));
}
return uriBuilder.build().encode().toUri();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ class PostgrestRestTemplateRepositoryTest {
void beforeEach(MockServerClient client) {
client.reset();
RestTemplate restTemplate = new RestTemplate();
restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory("http://localhost:8007"));
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(HttpClients.createDefault()));
repository = new PostRepository(PostgrestRestTemplate.of(restTemplate));
repository = new PostRepository(PostgrestRestTemplate.of(restTemplate, "http://localhost:8007"));
}

@Test
Expand Down

0 comments on commit 53e6f43

Please sign in to comment.