Skip to content

Commit a62d315

Browse files
authored
Merge pull request #55 from Kentico/delivery-sdk-java-48
#48 When querying getItem or getItems with a strongly typed class, automatically add a query filter for system type
2 parents 4dc11d8 + e060701 commit a62d315

File tree

3 files changed

+137
-14
lines changed

3 files changed

+137
-14
lines changed

src/main/java/com/kenticocloud/delivery/DeliveryClient.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,13 @@
3838
import org.apache.http.client.methods.RequestBuilder;
3939
import org.apache.http.impl.client.HttpClients;
4040
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
41+
import org.apache.http.message.BasicNameValuePair;
4142
import org.slf4j.Logger;
4243
import org.slf4j.LoggerFactory;
4344

4445
import java.io.IOException;
4546
import java.io.InputStream;
46-
import java.util.ArrayList;
47-
import java.util.List;
48-
import java.util.Properties;
49-
import java.util.UUID;
47+
import java.util.*;
5048

5149
/**
5250
* Executes requests against the Kentico Cloud Delivery API.
@@ -201,6 +199,7 @@ public ContentItemsListingResponse getItems(List<NameValuePair> params) throws I
201199
}
202200

203201
public <T> List<T> getItems(Class<T> tClass, List<NameValuePair> params) throws IOException {
202+
addTypeParameterIfNecessary(tClass, params);
204203
ContentItemsListingResponse contentItemsListingResponse = getItems(params);
205204
return contentItemsListingResponse.castTo(tClass);
206205
}
@@ -258,6 +257,7 @@ public ContentItemResponse getItem(String contentItemCodename, List<NameValuePai
258257
}
259258

260259
public <T> T getItem(String contentItemCodename, Class<T> tClass, List<NameValuePair> params) throws IOException {
260+
addTypeParameterIfNecessary(tClass, params);
261261
ContentItemResponse contentItemResponse = getItem(contentItemCodename, params);
262262
return contentItemResponse.castTo(tClass);
263263
}
@@ -462,6 +462,18 @@ private void handleErrorIfNecessary(HttpResponse response) throws IOException {
462462
}
463463
}
464464

465+
private void addTypeParameterIfNecessary(Class tClass, List<NameValuePair> params) {
466+
Optional<NameValuePair> any = params.stream()
467+
.filter(nameValuePair -> nameValuePair.getName().equals("system.type"))
468+
.findAny();
469+
if (!any.isPresent()) {
470+
String contentType = stronglyTypedContentItemConverter.getContentType(tClass);
471+
if (contentType != null) {
472+
params.add(new BasicNameValuePair("system.type", contentType));
473+
}
474+
}
475+
}
476+
465477
private void reconfigureDeserializer() {
466478
objectMapper = new ObjectMapper();
467479

src/main/java/com/kenticocloud/delivery/StronglyTypedContentItemConverter.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ protected void registerType(Class<?> clazz) {
6363
logger.debug("Registered type for {}", clazz.getSimpleName());
6464
}
6565

66+
protected String getContentType(Class tClass) {
67+
if (classToContentTypeMapping.containsKey(tClass)) {
68+
return classToContentTypeMapping.get(tClass);
69+
}
70+
return null;
71+
}
72+
6673
protected void registerInlineContentItemsResolver(InlineContentItemsResolver resolver) {
6774
typeToInlineResolverMapping.put(resolver.getType(), resolver);
6875
}

src/test/java/com/kenticocloud/delivery/DeliveryClientTest.java

Lines changed: 114 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -582,11 +582,20 @@ public void testGetStronglyTypedItemByRegisteringType() throws Exception {
582582

583583
this.serverBootstrap.registerHandler(
584584
String.format("/%s/%s", projectId, "items/on_roasts"),
585-
(request, response, context) -> response.setEntity(
586-
new InputStreamEntity(
587-
this.getClass().getResourceAsStream("SampleContentItem.json")
588-
)
589-
));
585+
(request, response, context) -> {
586+
String uri = String.format("http://testserver%s", request.getRequestLine().getUri());
587+
588+
List<NameValuePair> nameValuePairs =
589+
URLEncodedUtils.parse(URI.create(uri), Charset.defaultCharset());
590+
Assert.assertFalse(nameValuePairs.stream()
591+
.anyMatch(nameValuePair -> nameValuePair.getName().equals("system.type")));
592+
593+
response.setEntity(
594+
new InputStreamEntity(
595+
this.getClass().getResourceAsStream("SampleContentItem.json")
596+
)
597+
);
598+
});
590599
HttpHost httpHost = this.start();
591600
DeliveryClient client = new DeliveryClient(projectId);
592601
client.registerType("article", ArticleItem.class);
@@ -602,6 +611,90 @@ public void testGetStronglyTypedItemByRegisteringType() throws Exception {
602611
Assert.assertTrue(itemObj instanceof ArticleItem);
603612
}
604613

614+
@Test
615+
public void testGetStronglyTypedItemAutomaticallyAddsSystemType() throws Exception {
616+
String projectId = "02a70003-e864-464e-b62c-e0ede97deb8c";
617+
618+
this.serverBootstrap.registerHandler(
619+
String.format("/%s/%s", projectId, "items/on_roasts"),
620+
(request, response, context) -> {
621+
String uri = String.format("http://testserver%s", request.getRequestLine().getUri());
622+
623+
List<NameValuePair> nameValuePairs =
624+
URLEncodedUtils.parse(URI.create(uri), Charset.defaultCharset());
625+
Assert.assertEquals(1, nameValuePairs.stream()
626+
.filter(nameValuePair -> nameValuePair.getName().equals("system.type"))
627+
.count());
628+
629+
Map<String, String> params = convertNameValuePairsToMap(nameValuePairs);
630+
631+
Assert.assertTrue(params.containsKey("system.type"));
632+
Assert.assertEquals("article", params.get("system.type"));
633+
634+
response.setEntity(
635+
new InputStreamEntity(
636+
this.getClass().getResourceAsStream("SampleContentItem.json")
637+
)
638+
);
639+
});
640+
HttpHost httpHost = this.start();
641+
DeliveryClient client = new DeliveryClient(projectId);
642+
client.registerType("article", ArticleItem.class);
643+
644+
//modify default baseurl to point to test server, this is private so using reflection
645+
String testServerUri = httpHost.toURI() + "/%s";
646+
Field deliveryOptionsField = client.getClass().getDeclaredField("deliveryOptions");
647+
deliveryOptionsField.setAccessible(true);
648+
((DeliveryOptions) deliveryOptionsField.get(client)).setProductionEndpoint(testServerUri);
649+
650+
ArticleItem itemObj = client.getItem("on_roasts", ArticleItem.class);
651+
Assert.assertNotNull(itemObj);
652+
}
653+
654+
@Test
655+
public void testGetStronglyTypedItemAutomaticallyDoesNotSentSystemTypeWhenAdded() throws Exception {
656+
String projectId = "02a70003-e864-464e-b62c-e0ede97deb8c";
657+
658+
this.serverBootstrap.registerHandler(
659+
String.format("/%s/%s", projectId, "items/on_roasts"),
660+
(request, response, context) -> {
661+
String uri = String.format("http://testserver%s", request.getRequestLine().getUri());
662+
663+
List<NameValuePair> nameValuePairs =
664+
URLEncodedUtils.parse(URI.create(uri), Charset.defaultCharset());
665+
Assert.assertEquals(1, nameValuePairs.stream()
666+
.filter(nameValuePair -> nameValuePair.getName().equals("system.type"))
667+
.count());
668+
Map<String, String> params = convertNameValuePairsToMap(nameValuePairs);
669+
670+
Assert.assertTrue(params.containsKey("system.type"));
671+
Assert.assertEquals("customVal", params.get("system.type"));
672+
673+
response.setEntity(
674+
new InputStreamEntity(
675+
this.getClass().getResourceAsStream("SampleContentItem.json")
676+
)
677+
);
678+
});
679+
HttpHost httpHost = this.start();
680+
DeliveryClient client = new DeliveryClient(projectId);
681+
client.registerType("article", ArticleItem.class);
682+
683+
//modify default baseurl to point to test server, this is private so using reflection
684+
String testServerUri = httpHost.toURI() + "/%s";
685+
Field deliveryOptionsField = client.getClass().getDeclaredField("deliveryOptions");
686+
deliveryOptionsField.setAccessible(true);
687+
((DeliveryOptions) deliveryOptionsField.get(client)).setProductionEndpoint(testServerUri);
688+
689+
ArticleItem itemObj = client.getItem(
690+
"on_roasts",
691+
ArticleItem.class,
692+
DeliveryParameterBuilder.params()
693+
.filterEquals("system.type", "customVal")
694+
.build());
695+
Assert.assertNotNull(itemObj);
696+
}
697+
605698
@Test
606699
public void testGetStronglyTypedItemByRegisteringMapping() throws Exception {
607700
String projectId = "02a70003-e864-464e-b62c-e0ede97deb8c";
@@ -752,11 +845,22 @@ public void testGetStronglyTypedItems() throws Exception {
752845

753846
this.serverBootstrap.registerHandler(
754847
String.format("/%s/%s", projectId, "items"),
755-
(request, response, context) -> response.setEntity(
756-
new InputStreamEntity(
757-
this.getClass().getResourceAsStream("SampleContentItemList.json")
758-
)
759-
));
848+
(request, response, context) -> {
849+
String uri = String.format("http://testserver%s", request.getRequestLine().getUri());
850+
851+
List<NameValuePair> nameValuePairs =
852+
URLEncodedUtils.parse(URI.create(uri), Charset.defaultCharset());
853+
Map<String, String> params = convertNameValuePairsToMap(nameValuePairs);
854+
855+
Assert.assertTrue(params.containsKey("system.type"));
856+
Assert.assertEquals("article", params.get("system.type"));
857+
858+
response.setEntity(
859+
new InputStreamEntity(
860+
this.getClass().getResourceAsStream("SampleContentItemList.json")
861+
)
862+
);
863+
});
760864
HttpHost httpHost = this.start();
761865
DeliveryClient client = new DeliveryClient(projectId);
762866

0 commit comments

Comments
 (0)