Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #2 from dadoonet/master

Don't encode/decode JSon String payloads
  • Loading branch information...
commit 20816a2dd9ee74fa8f06ee7df9052cfa420704e7 2 parents f8d6bdc + 5f8ede1
@nicoo authored
View
2  .gitignore
@@ -2,3 +2,5 @@
/.classpath
/.project
/target
+/.idea
+/*.iml
View
13 pom.xml
@@ -18,7 +18,7 @@
<dependency>
<groupId>com.ning</groupId>
<artifactId>async-http-client</artifactId>
- <version>1.6.5</version>
+ <version>1.7.8</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
@@ -26,9 +26,14 @@
<version>10.0.1</version>
</dependency>
<dependency>
- <groupId>org.codehaus.jackson</groupId>
- <artifactId>jackson-mapper-lgpl</artifactId>
- <version>1.9.1</version>
+ <groupId>com.fasterxml.jackson.core</groupId>
+ <artifactId>jackson-core</artifactId>
+ <version>2.1.1</version>
+ </dependency>
+ <dependency>
+ <groupId>com.fasterxml.jackson.core</groupId>
+ <artifactId>jackson-databind</artifactId>
+ <version>2.1.1</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
View
18 src/main/java/net/linkfluence/jspore/Method.java
@@ -9,18 +9,11 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.ning.http.client.RequestBuilder;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
+
+import java.util.*;
import java.util.Map.Entry;
-import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import org.jboss.netty.handler.codec.http.HttpResponseStatus;
/**
* Describe a route specification.
@@ -170,9 +163,8 @@ public Builder(String name, String path, String httpMethod) {
this.headers = new HashMap<String, String>();
}
- public Builder addExepectedStatus(int status) {
- HttpResponseStatus s = HttpResponseStatus.valueOf(status);
- expectedStatus.add(s.getCode());
+ public Builder addExpectedStatus(int status) {
+ expectedStatus.add(status);
return this;
}
@@ -202,7 +194,7 @@ public Builder addOptionalParams(Collection<String> params) {
public Builder addExpectedStatuses(Collection<Integer> statuses) {
for (int status : statuses) {
- addExepectedStatus(status);
+ addExpectedStatus(status);
}
return this;
}
View
36 src/main/java/net/linkfluence/jspore/SpecParser.java
@@ -2,20 +2,16 @@
*/
package net.linkfluence.jspore;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableSet;
+
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
+import java.util.*;
import java.util.Map.Entry;
-import java.util.Set;
-import org.codehaus.jackson.JsonNode;
-import org.codehaus.jackson.map.ObjectMapper;
/**
* Internal spec parser.
@@ -73,20 +69,20 @@ private Model parse(JsonNode root) throws InvalidSporeSpecException, MalformedUR
throw new InvalidSporeSpecException("Cannot parse input spec stream");
}
Model m = new Model();
- m.version = root.get(VERSION).getTextValue();
- m.baseUrl = new URL(root.get(BASE_URL).getTextValue());
- m.name = root.get(NAME).getTextValue();
+ m.version = root.get(VERSION).asText();
+ m.baseUrl = new URL(root.get(BASE_URL).asText());
+ m.name = root.get(NAME).asText();
// read all object properties to find method
JsonNode methods = root.get(METHODS);
- Iterator<Entry<String, JsonNode>> nodeIt = methods.getFields();
+ Iterator<Entry<String, JsonNode>> nodeIt = methods.fields();
while(nodeIt.hasNext()){
Entry<String, JsonNode> e = nodeIt.next();
String methodName = e.getKey();
JsonNode n = e.getValue();
- String httpMethod = n.get(HTTP_METHOD).getTextValue();
- String path = n.get(PATH).getTextValue();
+ String httpMethod = n.get(HTTP_METHOD).asText();
+ String path = n.get(PATH).asText();
Collection<String> requiredParams = readStringCollection(n.get(REQUIRED_PARAMS));
Collection<Integer> expectedStatuses = readIntegerCollection(n.get(EXPECTED_STATUS));
Collection<String> optionalParams = readStringCollection(n.get(OPTIONAL_PARAMS));
@@ -117,9 +113,9 @@ private Model parse(JsonNode root) throws InvalidSporeSpecException, MalformedUR
private Collection<String> readStringCollection(JsonNode jsonArray){
Collection<String> ret = new ArrayList<String>();
if(jsonArray != null){
- Iterator<JsonNode> it = jsonArray.getElements();
+ Iterator<JsonNode> it = jsonArray.elements();
while(it.hasNext()){
- ret.add(it.next().getTextValue());
+ ret.add(it.next().asText());
}
}
return ret;
@@ -128,9 +124,9 @@ private Model parse(JsonNode root) throws InvalidSporeSpecException, MalformedUR
private Collection<Integer> readIntegerCollection(JsonNode jsonArray){
Collection<Integer> ret = new ArrayList<Integer>();
if(jsonArray != null){
- Iterator<JsonNode> it = jsonArray.getElements();
+ Iterator<JsonNode> it = jsonArray.elements();
while(it.hasNext()){
- ret.add(it.next().getIntValue());
+ ret.add(it.next().asInt());
}
}
return ret;
@@ -139,10 +135,10 @@ private Model parse(JsonNode root) throws InvalidSporeSpecException, MalformedUR
private Map<String, String> readStringMap(JsonNode jsonMap) {
Map<String, String> map = new HashMap<String, String>();
if(jsonMap != null){
- Iterator<Entry<String, JsonNode>> mapIt = jsonMap.getFields();
+ Iterator<Entry<String, JsonNode>> mapIt = jsonMap.fields();
while(mapIt.hasNext()){
Entry<String, JsonNode> e = mapIt.next();
- map.put(e.getKey(), e.getValue().getTextValue());
+ map.put(e.getKey(), e.getValue().asText());
}
}
return map;
View
18 src/main/java/net/linkfluence/jspore/middleware/format/Json.java
@@ -2,13 +2,12 @@
*/
package net.linkfluence.jspore.middleware.format;
+import com.fasterxml.jackson.databind.ObjectMapper;
import com.ning.http.client.RequestBuilder;
import com.ning.http.client.Response;
import net.linkfluence.jspore.Method;
import net.linkfluence.jspore.SporeException;
import net.linkfluence.jspore.middleware.Middleware;
-import org.codehaus.jackson.JsonNode;
-import org.codehaus.jackson.map.ObjectMapper;
/**
* Format request/result payload as JSON.
@@ -26,7 +25,12 @@ public void sendRequest(RequestBuilder request, Object body, Method context) thr
String content = null;
try {
if(body != null){
- content = mapper.writeValueAsString(body);
+ // If we have already a JSon string, we don't have to encode it in JSon !
+ if (body instanceof String) {
+ content = (String) body;
+ } else {
+ content = mapper.writeValueAsString(body);
+ }
request.setBody(content);
}
} catch (Exception ex) {
@@ -37,12 +41,14 @@ public void sendRequest(RequestBuilder request, Object body, Method context) thr
@Override
public Object receiveRequest(Response response, Object body, Method context) throws SporeException {
+ Object tree;
try {
- JsonNode tree = mapper.readTree(body.toString());
- return next(response, tree, context);
+ tree = mapper.readTree(body.toString());
} catch (Exception ex){
- throw new SporeException(ex.getMessage() + "\ncannot deserialize response: " + body.toString());
+ // When failing, we can assume that the content is not a JSon document but a String
+ tree = body.toString();
}
+ return next(response, tree, context);
}
@Override
View
16 src/test/java/net/linkfluence/jspore/GitHubPrivateTest.java
@@ -2,21 +2,19 @@
*/
package net.linkfluence.jspore;
-import static org.junit.Assert.assertEquals;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.google.common.collect.ImmutableMap;
+import net.linkfluence.jspore.middleware.auth.Basic;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
-import net.linkfluence.jspore.middleware.auth.Basic;
-
-import org.codehaus.jackson.JsonNode;
-import org.junit.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.common.collect.ImmutableMap;
+import static org.junit.Assert.assertEquals;
/**
* set the appropriate value to USERNAME and PASS to get these
View
19 src/test/java/net/linkfluence/jspore/GitHubPublicTest.java
@@ -2,17 +2,20 @@
*/
package net.linkfluence.jspore;
-import java.util.Iterator;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
import com.google.common.collect.ImmutableMap;
+import org.junit.Test;
+
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
+import java.util.Iterator;
import java.util.Map;
-import org.codehaus.jackson.JsonNode;
-import org.codehaus.jackson.node.ArrayNode;
-import org.junit.Test;
-import static org.junit.Assert.*;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
/**
* Test the github spore spec.
@@ -70,7 +73,7 @@ public void test_get_repo_info() throws SporeException {
@Test
public void test_list_following() throws SporeException {
SporeResult<JsonNode> result = spore.call("list_following", publicParams);
- Iterator<JsonNode> users = result.body.getElements();
+ Iterator<JsonNode> users = result.body.elements();
boolean hasNgrunwald = false;
while (users.hasNext()) {
if (users.next().get("login").asText().equals("ngrunwald")) {
@@ -83,7 +86,7 @@ public void test_list_following() throws SporeException {
@Test
public void test_list_followers() throws SporeException {
SporeResult<JsonNode> result = spore.call("list_followers", publicParams);
- Iterator<JsonNode> users = result.body.getElements();
+ Iterator<JsonNode> users = result.body.elements();
boolean hasDocteurZ = false;
while (users.hasNext()) {
if (users.next().get("login").asText().equals("docteurZ")) {
@@ -96,7 +99,7 @@ public void test_list_followers() throws SporeException {
@Test
public void test_list_watched_repos() throws SporeException {
SporeResult<JsonNode> result = spore.call("list_watched_repos", publicParams);
- Iterator<JsonNode> watchedrepos = result.body.getElements();
+ Iterator<JsonNode> watchedrepos = result.body.elements();
boolean hasCljRome = false;
while (watchedrepos.hasNext()) {
if (watchedrepos.next().get("full_name").asText().equals("ngrunwald/clj-rome")) {
View
9 src/test/java/net/linkfluence/jspore/SpecParserTest.java
@@ -1,11 +1,14 @@
package net.linkfluence.jspore;
+import com.fasterxml.jackson.databind.JsonNode;
+import org.junit.Test;
+
import java.io.File;
import java.io.IOException;
import java.net.URL;
-import org.codehaus.jackson.JsonNode;
-import org.junit.Test;
-import static org.junit.Assert.* ;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
/**
*
Please sign in to comment.
Something went wrong with that request. Please try again.