diff --git a/querydsl-postgrest-resttemplate-adapter/README.md b/querydsl-postgrest-resttemplate-adapter/README.md index 8f8150e..8a8f3f2 100644 --- a/querydsl-postgrest-resttemplate-adapter/README.md +++ b/querydsl-postgrest-resttemplate-adapter/README.md @@ -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); } } ``` diff --git a/querydsl-postgrest-resttemplate-adapter/src/main/java/fr/ouestfrance/querydsl/postgrest/PostgrestRestTemplate.java b/querydsl-postgrest-resttemplate-adapter/src/main/java/fr/ouestfrance/querydsl/postgrest/PostgrestRestTemplate.java index 23273b0..0bcaa2c 100644 --- a/querydsl-postgrest-resttemplate-adapter/src/main/java/fr/ouestfrance/querydsl/postgrest/PostgrestRestTemplate.java +++ b/querydsl-postgrest-resttemplate-adapter/src/main/java/fr/ouestfrance/querydsl/postgrest/PostgrestRestTemplate.java @@ -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; @@ -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; @@ -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 RangeResponse search(String resource, Map> params, Map> headers, Class clazz) { - ResponseEntity> response = restTemplate.exchange(restTemplate.getUriTemplateHandler() - .expand(UriComponentsBuilder.fromPath(resource) - .queryParams(toMultiMap(params)).build().toString(), new HashMap<>()), HttpMethod.GET, + ResponseEntity> 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) @@ -65,30 +60,31 @@ public RangeResponse search(String resource, Map> pa @Override public BulkResponse post(String resource, List value, Map> headers, Class clazz) { - ResponseEntity> response = restTemplate.exchange(resource, HttpMethod.POST, new HttpEntity<>(value, toHeaders(headers)), listRef(clazz)); + ResponseEntity> response = restTemplate.exchange(getUri(resource), HttpMethod.POST, new HttpEntity<>(value, toHeaders(headers)), listRef(clazz)); return toBulkResponse(response); } @Override public BulkResponse patch(String resource, Map> params, Object value, Map> headers, Class clazz) { - ResponseEntity> response = restTemplate.exchange(restTemplate.getUriTemplateHandler() - .expand(UriComponentsBuilder.fromPath(resource).queryParams(toMultiMap(params)).build().toString(), new HashMap<>()), + ResponseEntity> response = restTemplate.exchange( + getUri(resource, params), HttpMethod.PATCH, new HttpEntity<>(value, toHeaders(headers)), listRef(clazz)); return toBulkResponse(response); } @Override public BulkResponse delete(String resource, Map> params, Map> headers, Class clazz) { - ResponseEntity> 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> response = restTemplate.exchange( + getUri(resource, params), + HttpMethod.DELETE, new HttpEntity<>(null, toHeaders(headers)), listRef(clazz)); return toBulkResponse(response); } @Override public List count(String resource, Map> 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(); } @@ -99,5 +95,17 @@ private static MultiValueMap toMultiMap(Map private static HttpHeaders toHeaders(Map> headers) { return new HttpHeaders(toMultiMap(headers)); } + private URI getUri(String resource) { + return getUri(resource, null); + } + + private URI getUri(String resource, Map> params) { + UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(baseUrl).path(resource); + if (Objects.nonNull(params)) { + uriBuilder = uriBuilder.queryParams(toMultiMap(params)); + } + return uriBuilder.build().encode().toUri(); + } + } diff --git a/querydsl-postgrest-resttemplate-adapter/src/test/java/fr/ouestfrance/querydsl/postgrest/PostgrestRestTemplateRepositoryTest.java b/querydsl-postgrest-resttemplate-adapter/src/test/java/fr/ouestfrance/querydsl/postgrest/PostgrestRestTemplateRepositoryTest.java index 05265d9..9d1ac00 100644 --- a/querydsl-postgrest-resttemplate-adapter/src/test/java/fr/ouestfrance/querydsl/postgrest/PostgrestRestTemplateRepositoryTest.java +++ b/querydsl-postgrest-resttemplate-adapter/src/test/java/fr/ouestfrance/querydsl/postgrest/PostgrestRestTemplateRepositoryTest.java @@ -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