Skip to content

Commit dfb5449

Browse files
committed
✨ Implemented new method getRequestBuilder(path).
1 parent cca263d commit dfb5449

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

rest-services/jersey-client/src/main/java/com/_4point/aem/docservices/rest_services/client/jersey/JerseyRestClient.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.glassfish.jersey.media.multipart.MultiPartFeature;
1515

1616
import com._4point.aem.docservices.rest_services.client.RestClient;
17+
import com._4point.aem.docservices.rest_services.client.RestClient.GetRequest.Builder;
1718
import com._4point.aem.docservices.rest_services.client.helpers.AemConfig;
1819

1920
import jakarta.ws.rs.client.Client;
@@ -41,11 +42,10 @@ public class JerseyRestClient implements RestClient {
4142
* @param client Jersey Client object
4243
*/
4344
public JerseyRestClient(AemConfig aemConfig, String target, Supplier<String> correlationIdFn, Client client) {
44-
this.target = configureClient(client,aemConfig.user(), aemConfig.password())
45+
this(configureClient(client,aemConfig.user(), aemConfig.password())
4546
.target(aemConfig.url())
46-
.path(target)
47-
;
48-
this.correlationIdFn = correlationIdFn;
47+
.path(target),
48+
correlationIdFn);
4949
}
5050

5151
/**
@@ -58,6 +58,11 @@ public JerseyRestClient(AemConfig aemConfig, String target, Supplier<String> cor
5858
this(aemConfig, target, correlationIdFn, getClient());
5959
}
6060

61+
private JerseyRestClient(WebTarget target, Supplier<String> correlationIdFn) {
62+
this.target = target;
63+
this.correlationIdFn = correlationIdFn;
64+
}
65+
6166
private static Client configureClient(Client client, String username, String password) {
6267
return client.register(MultiPartFeature.class)
6368
.register(HttpAuthenticationFeature.basic(username, password));
@@ -225,7 +230,7 @@ public MultipartPayload build() {
225230
* Singleton Client-related code
226231
*
227232
*/
228-
// Safe way to lazily initialize singeleton. See https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom
233+
// Safe way to lazily initialize singleton. See https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom
229234
private static class ClientHolder {
230235
static final Client INSTANCE = ClientBuilder.newClient();
231236
}
@@ -249,6 +254,13 @@ public String target() {
249254
public GetRequest.Builder getRequestBuilder() {
250255
return new JerseyGetRequestBuilder();
251256
}
257+
258+
@Override
259+
public Builder getRequestBuilder(String additionalPath) {
260+
WebTarget updatedPath = target.path(additionalPath.startsWith("/") ? additionalPath : "/" + additionalPath);
261+
// Create a GetRequestBuilder for an updated JerseyRestClient path
262+
return new JerseyRestClient(updatedPath, correlationIdFn).new JerseyGetRequestBuilder();
263+
}
252264

253265
private final class JerseyGetRequestBuilder extends PayloadBuilder implements GetRequest.Builder {
254266

rest-services/jersey-client/src/test/java/com/_4point/aem/docservices/rest_services/client/jersey/JerseyRestClientTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
import org.junit.jupiter.api.BeforeEach;
1414
import org.junit.jupiter.api.DisplayName;
1515
import org.junit.jupiter.api.Test;
16+
import org.junit.jupiter.params.ParameterizedTest;
17+
import org.junit.jupiter.params.provider.ValueSource;
18+
1619
import com._4point.aem.docservices.rest_services.client.RestClient;
1720
import com._4point.aem.docservices.rest_services.client.RestClient.ContentType;
1821
import com._4point.aem.docservices.rest_services.client.RestClient.GetRequest;
@@ -451,6 +454,25 @@ void testGetFromServer_DocumentResponseWithHeader() throws Exception {
451454
);
452455
}
453456

457+
@DisplayName("GetFromServer with additional path parameter")
458+
@ParameterizedTest
459+
@ValueSource(strings = {"foo", "/foo"})
460+
void testGetFromServer_AdditionalPath(String additionalPath) throws Exception {
461+
// Given
462+
String expectedEndpoint = ENDPOINT + "/foo";
463+
stubFor(get(urlPathEqualTo(expectedEndpoint))
464+
.willReturn(okForContentType(ContentType.TEXT_HTML.contentType(), MOCK_PDF_BYTES)));
465+
466+
// When
467+
Response response = underTest.getRequestBuilder(additionalPath).build().getFromServer(ContentType.TEXT_HTML).orElseThrow();
468+
469+
// Then
470+
assertEquals(ContentType.TEXT_HTML, response.contentType());
471+
assertEquals(MOCK_PDF_BYTES, new String(response.data().readAllBytes()));
472+
assertTrue(response.retrieveHeader(SAMPLE_HEADER).isEmpty());
473+
verify(getRequestedFor(urlPathEqualTo(expectedEndpoint)));
474+
}
475+
454476
@DisplayName("When AEM returns 500 Internal Server error with no body, postToServer should throw RestClientException.")
455477
@Test
456478
void testGetFromServer_AemReturns500NoBody() throws Exception {

0 commit comments

Comments
 (0)