Skip to content

Commit

Permalink
Merge c41f077 into d8123e3
Browse files Browse the repository at this point in the history
  • Loading branch information
tomxor committed Jan 5, 2015
2 parents d8123e3 + c41f077 commit 0d12087
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 17 deletions.
52 changes: 38 additions & 14 deletions src/main/java/com/contentful/java/cda/ArrayParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.contentful.java.cda.model.ResourceWithMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -98,7 +99,7 @@ public ArrayParser(T source, ClientContext context) {
CDAResource item = entry.getValue();

if (item instanceof ResourceWithMap) {
resolveResourceLinks(item, assets, entries);
resolveResourceLinks((ResourceWithMap) item, assets, entries);
}
}

Expand Down Expand Up @@ -128,47 +129,70 @@ private void setLocalizedFields(ResourceWithMap res) {
}

/**
* Resolves any links contained in a {@code CDAEntry} object.
* Resolves any links contained in a {@code ResourceWithMap} object.
*
* @param resource entry to resolve
* @param res entry to resolve
* @param assets map of assets by ids
* @param entries map of entries by ids
*/
@SuppressWarnings("unchecked")
private void resolveResourceLinks(CDAResource resource,
HashMap<String, CDAResource> assets, HashMap<String, CDAResource> entries) {
ResourceWithMap res = (ResourceWithMap) resource;
private void resolveResourceLinks(ResourceWithMap res, HashMap<String, CDAResource> assets,
HashMap<String, CDAResource> entries) {
HashMap<String, Map> localizedFields = res.getLocalizedFieldsMap();

for (Map fields : localizedFields.values()) {
HashSet removeFromFields = new HashSet();

for (Object k : fields.keySet()) {
Object value = fields.get(k);

if (value instanceof Map) {
CDAResource match = getMatchForField((Map) value, assets, entries);

if (match != null) {
fields.put(k, match);
Map sys = (Map) ((Map) value).get("sys");
if (sys != null && containsLink(sys)) {
CDAResource match = getMatchForField((Map) value, assets, entries);
if (match != null) {
fields.put(k, match);
} else if (context.nullifyUnresolved) {
removeFromFields.add(k);
}
}
} else if (value instanceof List) {
List list = (List) value;
HashSet<Integer> removeFromList = new HashSet<Integer>();

for (int i = 0; i < list.size(); i++) {
Object item = list.get(i);

if (item instanceof Map) {
CDAResource match = getMatchForField((Map) item, assets, entries);

if (match != null) {
list.set(i, match);
Map sys = (Map) ((Map) item).get("sys");
if (sys != null && containsLink(sys)) {
CDAResource match = getMatchForField((Map) item, assets, entries);
if (match != null) {
list.set(i, match);
} else if (context.nullifyUnresolved) {
removeFromList.add(i);
}
}
}
}

for (int i = removeFromList.size() - 1; i >= 0; i--) {
list.remove(i);
}
}
}

if (removeFromFields.size() > 0) {
fields.keySet().removeAll(removeFromFields);
}
}
}

private boolean containsLink(Map map) {
String type = (String) map.get("type");
return CDAResourceType.Link.equals(CDAResourceType.valueOf(type));
}

/**
* Resolves field link.
*
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/com/contentful/java/cda/CDAClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private CDAClient(Builder builder) {

// Modules
ClientContext context = new ClientContext(service, callbackExecutor, spaceKey,
gson, spaceWrapper, classMap);
gson, spaceWrapper, classMap, builder.nullifyUnresolved);

this.moduleAssets = new ModuleAssets(context);
this.moduleContentTypes = new ModuleContentTypes(context);
Expand Down Expand Up @@ -266,10 +266,12 @@ public static class Builder {
Executor callbackExecutor;
Map<String, Class<?>> classMap;
boolean secure;
boolean nullifyUnresolved;

public Builder() {
// Defaults
this.secure = true;
this.nullifyUnresolved = false;
}

/**
Expand Down Expand Up @@ -427,6 +429,17 @@ public Builder preview() {
return setEndpoint(Constants.ENDPOINT_PREVIEW);
}

/**
* By default, unresolved links will point to a {@code Map} instance containing all the links
* details. This changes the default behaviour to remove any unresolved links.
*
* @return this {@code Builder} instance
*/
public Builder nullifyUnresolvedLinks() {
this.nullifyUnresolved = true;
return this;
}

/**
* Builds and returns a {@link CDAClient}
*
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/contentful/java/cda/ClientContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ final class ClientContext {
final Gson gson;
final SpaceWrapper spaceWrapper;
final Map<String, Class<?>> customTypesMap;
final boolean nullifyUnresolved;

public ClientContext(CDAService service, Executor callbackExecutor, String spaceId, Gson gson,
SpaceWrapper spaceWrapper, Map<String, Class<?>> customTypesMap) {
SpaceWrapper spaceWrapper, Map<String, Class<?>> customTypesMap, boolean nullifyUnresolved) {
this.service = service;
this.callbackExecutor = callbackExecutor;
this.spaceId = spaceId;
this.gson = gson;
this.spaceWrapper = spaceWrapper;
this.customTypesMap = customTypesMap;
this.nullifyUnresolved = nullifyUnresolved;
}
}
2 changes: 1 addition & 1 deletion src/test/kotlin/com/contentful/java/cda/BaseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ open class BaseTest {

fun getServerUrl(): String {
val url = server!!.getUrl("/")
return url.toString().substring(url.getProtocol().length + 3)
return url.toString().substring(url.getProtocol().length() + 3)
}
}
33 changes: 33 additions & 0 deletions src/test/kotlin/com/contentful/java/cda/EntryTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,44 @@ import com.contentful.java.cda.model.CDAEntry
import com.contentful.java.cda.model.CDAAsset
import com.contentful.java.cda.model.CDAArray
import kotlin.test.assertNotNull
import retrofit.RestAdapter
import kotlin.test.assertNull

/**
* Entry Tests.
*/
class EntryTests : BaseTest() {
test fun testFetchAllWithUnresolvedLink() {
enqueue("entry_fetch_all_unresolved_link.json")
val entry = client!!.entries().fetchAll().getItems()[0] as CDAEntry
assertTrue(entry.getFields().get("linked") is Map<*, *>)

val list = entry.getFields().get("linked_list") as List<*>
assertEquals(2, list.size())
assertTrue(list[0] is Map<*, *>)
}

test fun testNullifyUnresolvedLinks() {
val cli = CDAClient.Builder()
.setAccessToken("token")
.setSpaceKey("spaceid")
.setEndpoint(getServerUrl())
.setLogLevel(RestAdapter.LogLevel.FULL)
.nullifyUnresolvedLinks()
.noSSL()
.build()

enqueue("space_fetch_response.json")
enqueue("entry_fetch_all_unresolved_link.json")

val entry = cli!!.entries().fetchAll().getItems()[0] as CDAEntry
assertNull(entry.getFields().get("linked"))

val list = entry.getFields().get("linked_list") as List<*>
assertEquals(1, list.size())
assertTrue(list[0] is CDAEntry)
}

test fun testCustomClass() {
val cli = CDAClient.Builder()
.setAccessToken("token")
Expand Down
113 changes: 113 additions & 0 deletions src/test/resources/entry_fetch_all_unresolved_link.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
{
"sys": {
"type": "Array"
},
"total": 1,
"skip": 0,
"limit": 100,
"items": [
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "lsuyo9b7men3"
}
},
"type": "Entry",
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "5HYo9FeMfY6o2Aum4coQug"
}
},
"id": "3HGy9WXD2USmQsW4Q80ooM",
"revision": 2,
"createdAt": "2014-12-29T16:01:11.114Z",
"updatedAt": "2014-12-29T16:03:38.007Z",
"locale": "en-US"
},
"fields": {
"f1": "ff",
"mid1": "sdfsdf",
"linked": {
"sys": {
"type": "Link",
"linkType": "Entry",
"id": "3zYq1TBEysU0oQMmMeComi"
}
},
"linked_list": [
{
"sys": {
"type": "Link",
"linkType": "Entry",
"id": "3zYq1TBEysU0oQMmMeComi"
}
},
{
"sys": {
"type": "Link",
"linkType": "Entry",
"id": "doge"
}
}
]
}
}
],
"includes": {
"Entry": [
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "cfexampleapi"
}
},
"type": "Entry",
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "dog"
}
},
"id": "doge",
"revision": 2,
"createdAt": "2013-11-06T09:45:27.475Z",
"updatedAt": "2013-11-18T09:13:37.808Z",
"locale": "en-US"
},
"fields": {
"name": "Doge",
"image": {
"sys": {
"type": "Link",
"linkType": "Asset",
"id": "1x0xpXu4pSGS4OukSyWGUK"
}
},
"description": "such json\nwow"
}
}
]
},
"errors": [
{
"sys": {
"type": "error",
"id": "notResolvable"
},
"details": {
"type": "Link",
"linkType": "Entry",
"id": "3zYq1TBEysU0oQMmMeComi"
}
}
]
}

0 comments on commit 0d12087

Please sign in to comment.