Skip to content

Commit

Permalink
Merge pull request #66 from contentful/no-links
Browse files Browse the repository at this point in the history
Support optional non link resolving mode
  • Loading branch information
tomxor committed May 15, 2015
2 parents 9c1babd + e2c2493 commit ce30e3d
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 57 deletions.
14 changes: 7 additions & 7 deletions src/main/java/com/contentful/java/cda/ArrayParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public ArrayParser(T source, ClientContext context) {
this.context = context;
}

@Override public T call() throws Exception {
public T call() throws Exception {
HashMap<String, CDAResource> assets = new HashMap<String, CDAResource>();
HashMap<String, CDAResource> entries = new HashMap<String, CDAResource>();
boolean sync = source instanceof CDASyncedSpace;
Expand Down Expand Up @@ -94,12 +94,12 @@ public ArrayParser(T source, ClientContext context) {
}
}

// Iterate through all entries and attempt to resolve contained links.
for (Map.Entry<String, CDAResource> entry : entries.entrySet()) {
CDAResource item = entry.getValue();

if (item instanceof ResourceWithMap) {
resolveResourceLinks((ResourceWithMap) item, assets, entries);
if (!context.skipLinks) {
// Iterate through all entries and attempt to resolve contained links.
for (CDAResource resource : entries.values()) {
if (resource instanceof ResourceWithMap) {
resolveResourceLinks((ResourceWithMap) resource, assets, entries);
}
}
}

Expand Down
45 changes: 37 additions & 8 deletions src/main/java/com/contentful/java/cda/CDAClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ public class CDAClient {
final Gson gson;
final SpaceWrapper spaceWrapper;
final Executor callbackExecutor;
final boolean skipLinks;

// Modules
final ModuleAssets moduleAssets;
final ModuleContentTypes moduleContentTypes;
final ModuleEntries moduleEntries;
final ModuleSpaces moduleSpaces;
final ModuleSync moduleSync;
final boolean nullifyUnresolved;

private CDAClient(Builder builder) {
if (builder.accessToken == null) {
Expand All @@ -82,18 +84,30 @@ private CDAClient(Builder builder) {
this.callbackExecutor = createCallbackExecutor(builder);
this.gson = createGson();
this.service = createRetrofitService(builder);
this.skipLinks = builder.skipLinks;
this.nullifyUnresolved = builder.nullifyUnresolved;

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

ClientContext context = createContext();
this.moduleAssets = new ModuleAssets(context);
this.moduleContentTypes = new ModuleContentTypes(context);
this.moduleEntries = new ModuleEntries(context);
this.moduleSpaces = new ModuleSpaces(context);
this.moduleSync = new ModuleSync(context);
}

private ClientContext createContext() {
return ClientContext.builder()
.setService(service)
.setCallbackExecutor(callbackExecutor)
.setSpaceId(spaceKey)
.setGson(gson)
.setSpaceWrapper(spaceWrapper)
.setCustomTypesMap(classMap)
.setSkipLinks(skipLinks)
.setNullifyUnresolved(nullifyUnresolved)
.build();
}

/**
* Returns the {@code CDASpace} object associated with this client. Note that a Space is attached
* to a client only <b>after</b> it's first request was successfully executed.
Expand Down Expand Up @@ -237,7 +251,7 @@ private void setEndPoint(Builder builder, RestAdapter.Builder restBuilder) {

private RequestInterceptor createInterceptor() {
return new RequestInterceptor() {
@Override public void intercept(RequestFacade requestFacade) {
public void intercept(RequestFacade requestFacade) {
if (accessToken != null && !accessToken.isEmpty()) {
requestFacade.addHeader(HTTP_HEADER_AUTH, String.format(HTTP_OAUTH_PATTERN, accessToken));
}
Expand All @@ -247,6 +261,11 @@ private RequestInterceptor createInterceptor() {
};
}

/** Creates a new {@link Builder} instance. */
public static Builder builder() {
return new Builder();
}

/**
* Client builder.
*
Expand All @@ -267,11 +286,11 @@ public static class Builder {
Map<String, Class<?>> classMap;
boolean secure;
boolean nullifyUnresolved;
boolean skipLinks;

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

/**
Expand Down Expand Up @@ -344,7 +363,7 @@ public Builder setClient(final Client client) {
}

return setClientProvider(new Client.Provider() {
@Override public Client get() {
public Client get() {
return client;
}
});
Expand Down Expand Up @@ -429,6 +448,16 @@ public Builder preview() {
return setEndpoint(Constants.ENDPOINT_PREVIEW);
}

/**
* Sets the behavior of this client to never resolve any links.
*
* @return this {@code Builder} instance
*/
public Builder skipLinks() {
this.skipLinks = true;
return this;
}

/**
* 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.
Expand Down
85 changes: 76 additions & 9 deletions src/main/java/com/contentful/java/cda/ClientContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,83 @@ final class ClientContext {
final Gson gson;
final SpaceWrapper spaceWrapper;
final Map<String, Class<?>> customTypesMap;
final boolean skipLinks;
final boolean nullifyUnresolved;

public ClientContext(CDAService service, Executor callbackExecutor, String spaceId, Gson gson,
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;
private ClientContext() {
throw new AssertionError();
}

public ClientContext(Builder builder) {
this.service = builder.service;
this.callbackExecutor = builder.callbackExecutor;
this.spaceId = builder.spaceId;
this.gson = builder.gson;
this.spaceWrapper = builder.spaceWrapper;
this.customTypesMap = builder.customTypesMap;
this.skipLinks = builder.skipLinks;
this.nullifyUnresolved = builder.nullifyUnresolved;
}

static Builder builder() {
return new Builder();
}

static class Builder {
private CDAService service;
private Executor callbackExecutor;
private String spaceId;
private Gson gson;
private SpaceWrapper spaceWrapper;
private Map<String, Class<?>> customTypesMap;
private boolean skipLinks;
private boolean nullifyUnresolved;

private Builder() {
}

public Builder setService(CDAService service) {
this.service = service;
return this;
}

public Builder setCallbackExecutor(Executor callbackExecutor) {
this.callbackExecutor = callbackExecutor;
return this;
}

public Builder setSpaceId(String spaceId) {
this.spaceId = spaceId;
return this;
}

public Builder setGson(Gson gson) {
this.gson = gson;
return this;
}

public Builder setSpaceWrapper(SpaceWrapper spaceWrapper) {
this.spaceWrapper = spaceWrapper;
return this;
}

public Builder setCustomTypesMap(Map<String, Class<?>> customTypesMap) {
this.customTypesMap = customTypesMap;
return this;
}

public Builder setSkipLinks(boolean skipLinks) {
this.skipLinks = skipLinks;
return this;
}

public Builder setNullifyUnresolved(boolean nullifyUnresolved) {
this.nullifyUnresolved = nullifyUnresolved;
return this;
}

public ClientContext build() {
return new ClientContext(this);
}
}
}
2 changes: 1 addition & 1 deletion src/test/kotlin/com/contentful/java/cda/AndroidTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class AndroidTests : BaseTest() {
}
}

val androidClient = CDAClient.Builder()
val androidClient = CDAClient.builder()
.setSpaceKey("space")
.setAccessToken("token")
.setEndpoint(getIntent().getStringExtra("EXTRA_URL"))
Expand Down
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 @@ -39,7 +39,7 @@ open class BaseTest {
server!!.play()

// Client
client = CDAClient.Builder()
client = CDAClient.builder()
.setAccessToken("token")
.setSpaceKey("spaceid")
.setEndpoint(getServerUrl())
Expand Down
Loading

0 comments on commit ce30e3d

Please sign in to comment.