Skip to content

Commit

Permalink
Merge 3873923 into c8b616f
Browse files Browse the repository at this point in the history
  • Loading branch information
tomxor committed Nov 24, 2014
2 parents c8b616f + 3873923 commit dabc56d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 40 deletions.
7 changes: 0 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
<!-- Dependencies -->
<retrofit.version>1.8.0</retrofit.version>
<rxjava.version>1.0.0</rxjava.version>
<httpcomp.version>4.3.6</httpcomp.version>

<!-- Test Dependencies -->
<commonsio.version>2.4</commonsio.version>
Expand Down Expand Up @@ -74,12 +73,6 @@
<version>${rxjava.version}</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpcomp.version}</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@

import com.contentful.java.model.ArrayResource;
import com.contentful.java.model.CDAArray;
import com.contentful.java.model.CDASyncedSpace;
import com.google.gson.Gson;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;

/**
Expand All @@ -47,6 +49,17 @@ class ArrayResourceTypeAdapter implements JsonDeserializer<ArrayResource> {
} catch (Exception e) {
throw new JsonParseException(e);
}
} else if (CDASyncedSpace.class.equals(type)) {
CDASyncedSpace syncedSpace = (CDASyncedSpace) result;

String nextSyncUrl = syncedSpace.getNextSyncUrl();
if (nextSyncUrl != null) {
try {
syncedSpace.setSyncToken(Utils.getQueryParamFromUrl(nextSyncUrl, "sync_token"));
} catch (UnsupportedEncodingException e) {
throw new JsonParseException("Unable to retrieve sync token.");
}
}
}

return result;
Expand Down
33 changes: 19 additions & 14 deletions src/main/java/com/contentful/java/api/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@

import com.contentful.java.model.CDAArray;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Properties;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URIBuilder;

/**
* SDK utilities
Expand Down Expand Up @@ -134,22 +134,27 @@ static String getFromProperties(String field) throws IOException {
return properties.getProperty(field);
}

static String getQueryParamFromUrl(String url, String param) {
String result = null;
static String getQueryParamFromUrl(String url, String param) throws UnsupportedEncodingException {
URI uri = URI.create(url);
String query = uri.getQuery();

try {
URIBuilder builder = new URIBuilder(url);
for (NameValuePair pair : builder.getQueryParams()) {
if (pair.getName().equalsIgnoreCase(param)) {
result = pair.getValue();
break;
}
if (query == null) {
return null;
}

String[] pairs = query.split("&");
for (String pair : pairs) {
String[] split = pair.split("=");
if (split.length != 2) {
continue;
}

if (param.equalsIgnoreCase(split[0])) {
return URLDecoder.decode(split[1], "UTF-8");
}
} catch (URISyntaxException e) {
e.printStackTrace();
}

return result;
return null;
}
}

29 changes: 10 additions & 19 deletions src/main/java/com/contentful/java/model/CDASyncedSpace.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.contentful.java.model;

import java.net.URI;
import java.util.ArrayList;

/**
Expand All @@ -27,6 +26,7 @@ public class CDASyncedSpace extends ArrayResource {
private ArrayList<CDAResource> items;
private String nextSyncUrl;
private String nextPageUrl;
private String syncToken;

public ArrayList<CDAResource> getItems() {
return items;
Expand All @@ -50,24 +50,15 @@ public String getNextPageUrl() {
* Returns the sync token from this Space's {@code nextSyncUrl} value.
*/
public String getSyncToken() {
if (nextSyncUrl == null) {
return null;
}

URI uri = URI.create(nextSyncUrl);

String query = uri.getQuery();

if (query == null) {
return null;
}

String[] split = query.split("=");

if (split.length < 2) {
return null;
}
return syncToken;
}

return split[1];
/**
* Sets the sync token for this synced Space.
*
* @param syncToken String representing the sync token
*/
public void setSyncToken(String syncToken) {
this.syncToken = syncToken;
}
}

0 comments on commit dabc56d

Please sign in to comment.