OLTU-209 - remove org.json
git-svn-id: https://svn.apache.org/repos/asf/oltu/trunk@1813271 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/commons/json/pom.xml b/commons/json/pom.xml
index 355bae7..e57f6b7 100644
--- a/commons/json/pom.xml
+++ b/commons/json/pom.xml
@@ -33,9 +33,9 @@
<dependencies>
<dependency>
- <groupId>org.json</groupId>
- <artifactId>json</artifactId>
- <version>20160212</version>
+ <groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-json_1.1_spec</artifactId>
+ <version>1.0</version>
</dependency>
</dependencies>
diff --git a/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntityReader.java b/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntityReader.java
index c2531d2..2907068 100644
--- a/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntityReader.java
+++ b/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntityReader.java
@@ -18,8 +18,10 @@
import static java.lang.String.format;
-import org.json.JSONArray;
-import org.json.JSONTokener;
+import java.io.StringReader;
+import java.util.Map.Entry;
+
+import javax.json.*;
/**
* TODO
@@ -42,75 +44,83 @@
* @param jsonString
*/
public void read(String jsonString) {
- final JSONTokener x = new JSONTokener(jsonString);
- char c;
- String key;
+ if (jsonString == null) {
+ throw new IllegalArgumentException("Null string does not represent a valid JSON object");
+ }
- if (x.nextClean() != '{') {
- throw new IllegalArgumentException(format("String '%s' is not a valid JSON object representation, a JSON object text must begin with '{'",
+ StringReader reader = new StringReader(jsonString);
+ JsonReader jsonReader = Json.createReader(reader);
+ JsonStructure structure = jsonReader.read();
+
+ if (structure == null || structure instanceof JsonArray) {
+ throw new IllegalArgumentException(format("String '%s' is not a valid JSON object representation",
jsonString));
}
- for (;;) {
- c = x.nextClean();
- switch (c) {
- case 0:
- throw new IllegalArgumentException(format("String '%s' is not a valid JSON object representation, a JSON object text must end with '}'",
- jsonString));
- case '}':
- return;
- default:
- x.back();
- key = x.nextValue().toString();
- }
- /*
- * The key is followed by ':'. We will also tolerate '=' or '=>'.
- */
- c = x.nextClean();
- if (c == '=') {
- if (x.next() != '>') {
- x.back();
- }
- } else if (c != ':') {
- throw new IllegalArgumentException(format("String '%s' is not a valid JSON object representation, expected a ':' after the key '%s'",
- jsonString, key));
- }
- Object value = x.nextValue();
+ JsonObject object = (JsonObject) structure;
+ for (Entry<String, JsonValue> entry : object.entrySet()) {
+ String key = entry.getKey();
+ JsonValue jsonValue = entry.getValue();
// guard from null values
- if (value != null) {
- if (value instanceof JSONArray) { // only plain simple arrays in this version
- JSONArray array = (JSONArray) value;
- Object[] values = new Object[array.length()];
- for (int i = 0; i < array.length(); i++) {
- values[i] = array.get(i);
- }
- value = values;
- }
+ if (jsonValue != null) {
+ Object value = toJavaObject(jsonValue);
// if the concrete implementation is not able to handle the property, set the custom field
if (!handleProperty(key, value)) {
builder.setCustomField(key, value);
}
}
-
- /*
- * Pairs are separated by ','. We will also tolerate ';'.
- */
- switch (x.nextClean()) {
- case ';':
- case ',':
- if (x.nextClean() == '}') {
- return;
- }
- x.back();
- break;
- case '}':
- return;
- default:
- throw new IllegalArgumentException("Expected a ',' or '}'");
- }
}
+
+ jsonReader.close();
+ }
+
+ private static Object toJavaObject(JsonValue jsonValue) {
+ Object value = null;
+
+ switch (jsonValue.getValueType()) {
+ case ARRAY:
+ JsonArray array = (JsonArray) jsonValue;
+ Object[] values = new Object[array.size()];
+ for (int i = 0; i < array.size(); i++) {
+ JsonValue current = array.get(i);
+ values[i] = toJavaObject(current);
+ }
+ value = values;
+ break;
+
+ case FALSE:
+ value = false;
+ break;
+
+ case NULL:
+ value = null;
+ break;
+
+ case NUMBER:
+ JsonNumber jsonNumber = (JsonNumber) jsonValue;
+ value = jsonNumber.numberValue();
+ break;
+
+ case OBJECT:
+ // not supported in this version
+ break;
+
+ case STRING:
+ JsonString jsonString = (JsonString) jsonValue;
+ value = jsonString.getString();
+ break;
+
+ case TRUE:
+ value = true;
+ break;
+
+ default:
+ break;
+ }
+
+ return value;
}
protected abstract <T> boolean handleProperty(String key, T value);
diff --git a/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntityWriter.java b/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntityWriter.java
index 4d3fc5f..42e8ce4 100644
--- a/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntityWriter.java
+++ b/commons/json/src/main/java/org/apache/oltu/commons/json/CustomizableEntityWriter.java
@@ -16,16 +16,22 @@
*/
package org.apache.oltu.commons.json;
+import java.io.StringWriter;
+import java.math.BigDecimal;
+import java.math.BigInteger;
import java.util.Map.Entry;
-import org.json.JSONStringer;
+import javax.json.Json;
+import javax.json.stream.JsonGenerator;
public abstract class CustomizableEntityWriter<CE extends CustomizableEntity> {
- private final JSONStringer jsonWriter = new JSONStringer();
+ private final StringWriter stringWriter = new StringWriter();
+
+ private final JsonGenerator generator = Json.createGenerator(stringWriter);
public final String write(CE customizableEntity) {
- jsonWriter.object();
+ generator.writeStartObject();
handleProperties(customizableEntity);
@@ -33,15 +39,33 @@
set(customFields.getKey(), customFields.getValue());
}
- jsonWriter.endObject();
- return jsonWriter.toString();
+ generator.writeEnd().close();
+
+ return stringWriter.toString();
}
protected abstract void handleProperties(CE customizableEntity);
protected final <T> void set(String key, T value) {
- if (value != null) {
- jsonWriter.key(key).value(value);
+ if (key != null && value != null) {
+ if (value instanceof Boolean) {
+ generator.write(key, (Boolean) value);
+ } else if (value instanceof Double) {
+ generator.write(key, (Double) value);
+ } else if (value instanceof Integer) {
+ generator.write(key, (Integer) value);
+ } else if (value instanceof BigDecimal) {
+ generator.write(key, (BigDecimal) value);
+ } else if (value instanceof BigInteger) {
+ generator.write(key, (BigInteger) value);
+ } else if (value instanceof Long) {
+ generator.write(key, (Long) value);
+ } else if (value instanceof String) {
+ String string = (String) value;
+ if (!string.isEmpty()) {
+ generator.write(key, string);
+ }
+ }
}
}
@@ -50,15 +74,29 @@
return;
}
- jsonWriter.key(key).array();
+ generator.writeStartArray(key);
for (T item : value) {
if (item != null) {
- jsonWriter.value(item);
+ if (item instanceof Boolean) {
+ generator.write((Boolean) item);
+ } else if (item instanceof Double) {
+ generator.write((Double) item);
+ } else if (item instanceof Integer) {
+ generator.write((Integer) item);
+ } else if (item instanceof BigDecimal) {
+ generator.write((BigDecimal) item);
+ } else if (item instanceof BigInteger) {
+ generator.write((BigInteger) item);
+ } else if (item instanceof Long) {
+ generator.write((Long) item);
+ } else if (item instanceof String) {
+ generator.write((String) item);
+ }
}
}
- jsonWriter.endArray();
+ generator.writeEnd();
}
}
diff --git a/oauth-2.0/authzserver/pom.xml b/oauth-2.0/authzserver/pom.xml
index 2a18bde..9a98ebb 100644
--- a/oauth-2.0/authzserver/pom.xml
+++ b/oauth-2.0/authzserver/pom.xml
@@ -37,8 +37,8 @@
</dependency>
<dependency>
- <groupId>org.json</groupId>
- <artifactId>json</artifactId>
+ <groupId>org.apache.johnzon</groupId>
+ <artifactId>johnzon-core</artifactId>
</dependency>
<dependency>
diff --git a/oauth-2.0/authzserver/src/test/java/org/apache/oltu/oauth2/as/response/OAuthASResponseTest.java b/oauth-2.0/authzserver/src/test/java/org/apache/oltu/oauth2/as/response/OAuthASResponseTest.java
index f00465e..73acd58 100644
--- a/oauth-2.0/authzserver/src/test/java/org/apache/oltu/oauth2/as/response/OAuthASResponseTest.java
+++ b/oauth-2.0/authzserver/src/test/java/org/apache/oltu/oauth2/as/response/OAuthASResponseTest.java
@@ -24,6 +24,7 @@
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
+import static org.junit.Assert.assertEquals;
import javax.servlet.http.HttpServletRequest;
@@ -31,7 +32,6 @@
import org.apache.oltu.oauth2.common.error.OAuthError;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.message.OAuthResponse;
-import org.junit.Assert;
import org.junit.Test;
/**
@@ -53,8 +53,8 @@
String url = oAuthResponse.getLocationUri();
- Assert.assertEquals("http://www.example.com?testValue=value2&state=ok&code=code", url);
- Assert.assertEquals(200, oAuthResponse.getResponseStatus());
+ assertEquals("http://www.example.com?code=code&state=ok&testValue=value2", url);
+ assertEquals(200, oAuthResponse.getResponseStatus());
}
@@ -71,8 +71,8 @@
String url = oAuthResponse.getLocationUri();
- Assert.assertEquals("http://www.example.com?testValue=value2&state=ok&code=code", url);
- Assert.assertEquals(200, oAuthResponse.getResponseStatus());
+ assertEquals("http://www.example.com?code=code&state=ok&testValue=value2", url);
+ assertEquals(200, oAuthResponse.getResponseStatus());
}
@@ -90,8 +90,8 @@
.buildQueryMessage();
String url = oAuthResponse.getLocationUri();
- Assert.assertEquals("http://www.example.com#testValue=value2&state=ok&expires_in=400&token_type=bearer&access_token=access_111", url);
- Assert.assertEquals(200, oAuthResponse.getResponseStatus());
+ assertEquals("http://www.example.com#access_token=access_111&state=ok&token_type=bearer&expires_in=400&testValue=value2", url);
+ assertEquals(200, oAuthResponse.getResponseStatus());
}
@Test
@@ -102,8 +102,8 @@
.buildBodyMessage();
String body = oAuthResponse.getBody();
- Assert.assertEquals(
- "expires_in=200&token_type=bearer&refresh_token=refresh_token2&access_token=access_token",
+ assertEquals(
+ "access_token=access_token&refresh_token=refresh_token2&token_type=bearer&expires_in=200",
body);
}
@@ -116,8 +116,9 @@
.buildBodyMessage();
String body = oAuthResponse.getBody();
- Assert.assertEquals(
- "some_param=new_param&expires_in=200&token_type=bearer&refresh_token=refresh_token2&access_token=access_token",
+
+ assertEquals(
+ "access_token=access_token&refresh_token=refresh_token2&some_param=new_param&token_type=bearer&expires_in=200",
body);
}
@@ -132,17 +133,15 @@
.uri("http://www.example.com/error");
OAuthResponse oAuthResponse = OAuthResponse.errorResponse(400).error(ex).buildJSONMessage();
-
- Assert.assertEquals(
- "{\"error_uri\":\"http://www.example.com/error\",\"error\":\"access_denied\",\"error_description\":\"Access denied\"}",
+ assertEquals(
+ "{\"error_description\":\"Access denied\",\"error\":\"access_denied\",\"error_uri\":\"http://www.example.com/error\"}",
oAuthResponse.getBody());
oAuthResponse = OAuthResponse.errorResponse(500)
.location("http://www.example.com/redirect?param2=true").error(ex).buildQueryMessage();
- Assert.assertEquals(
- "http://www.example.com/redirect?param2=true&error_uri=http%3A%2F%2Fwww.example.com%2Ferror"
- + "&error=access_denied&error_description=Access+denied",
+ assertEquals(
+ "http://www.example.com/redirect?param2=true&error_description=Access+denied&error=access_denied&error_uri=http%3A%2F%2Fwww.example.com%2Ferror",
oAuthResponse.getLocationUri());
}
@@ -156,9 +155,9 @@
OAuthResponse oAuthResponse = OAuthResponse.errorResponse(500)
.location("http://www.example.com/redirect?param2=true").error(ex).buildQueryMessage();
- Assert.assertEquals(
- "http://www.example.com/redirect?param2=true&error_uri=http%3A%2F%2Fwww.example.com%2Ferror"
- + "&error=access_denied&error_description=Access+denied",
+
+ assertEquals(
+ "http://www.example.com/redirect?param2=true&error_description=Access+denied&error=access_denied&error_uri=http%3A%2F%2Fwww.example.com%2Ferror",
oAuthResponse.getLocationUri());
}
@@ -170,10 +169,10 @@
.buildHeaderMessage();
String header = oAuthResponse.getHeader(OAuth.HeaderType.WWW_AUTHENTICATE);
- Assert.assertEquals("Bearer state=\"state_ok\",code=\"oauth_code\"", header);
+ assertEquals("Bearer code=\"oauth_code\",state=\"state_ok\"", header);
header = oAuthResponse.getHeaders().get(OAuth.HeaderType.WWW_AUTHENTICATE);
- Assert.assertEquals("Bearer state=\"state_ok\",code=\"oauth_code\"", header);
+ assertEquals("Bearer code=\"oauth_code\",state=\"state_ok\"", header);
}
}
diff --git a/oauth-2.0/client/pom.xml b/oauth-2.0/client/pom.xml
index e372c3e..5f70af2 100644
--- a/oauth-2.0/client/pom.xml
+++ b/oauth-2.0/client/pom.xml
@@ -42,6 +42,10 @@
<version>2.4</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.johnzon</groupId>
+ <artifactId>johnzon-core</artifactId>
+ </dependency>
</dependencies>
<build>
diff --git a/oauth-2.0/client/src/test/java/org/apache/oltu/oauth2/client/response/OAuthClientResponseFactoryTest.java b/oauth-2.0/client/src/test/java/org/apache/oltu/oauth2/client/response/OAuthClientResponseFactoryTest.java
index 729ea69..a8c8a21 100644
--- a/oauth-2.0/client/src/test/java/org/apache/oltu/oauth2/client/response/OAuthClientResponseFactoryTest.java
+++ b/oauth-2.0/client/src/test/java/org/apache/oltu/oauth2/client/response/OAuthClientResponseFactoryTest.java
@@ -21,9 +21,10 @@
package org.apache.oltu.oauth2.client.response;
-import org.junit.Assert;
-import org.junit.Test;
+import static org.junit.Assert.assertNotNull;
+
import org.apache.oltu.oauth2.common.OAuth;
+import org.junit.Test;
/**
@@ -37,14 +38,14 @@
public void testCreateGitHubTokenResponse() throws Exception {
OAuthClientResponse gitHubTokenResponse = OAuthClientResponseFactory
.createGitHubTokenResponse("access_token=123", OAuth.ContentType.URL_ENCODED, 200);
- Assert.assertNotNull(gitHubTokenResponse);
+ assertNotNull(gitHubTokenResponse);
}
@Test
public void testCreateJSONTokenResponse() throws Exception {
OAuthClientResponse jsonTokenResponse = OAuthClientResponseFactory
- .createJSONTokenResponse("{'access_token':'123'}", OAuth.ContentType.JSON, 200);
- Assert.assertNotNull(jsonTokenResponse);
+ .createJSONTokenResponse("{\"access_token\":\"123\"}", OAuth.ContentType.JSON, 200);
+ assertNotNull(jsonTokenResponse);
}
@Test
diff --git a/oauth-2.0/common/pom.xml b/oauth-2.0/common/pom.xml
index 5a425f7..7243e78 100644
--- a/oauth-2.0/common/pom.xml
+++ b/oauth-2.0/common/pom.xml
@@ -32,14 +32,20 @@
<dependencies>
<dependency>
- <groupId>org.json</groupId>
- <artifactId>json</artifactId>
+ <groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-json_1.1_spec</artifactId>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
+
+ <!-- test -->
+ <dependency>
+ <groupId>org.apache.johnzon</groupId>
+ <artifactId>johnzon-core</artifactId>
+ </dependency>
</dependencies>
<build>
diff --git a/oauth-2.0/common/src/main/java/org/apache/oltu/oauth2/common/utils/JSONUtils.java b/oauth-2.0/common/src/main/java/org/apache/oltu/oauth2/common/utils/JSONUtils.java
index 90ac56a..607138c 100644
--- a/oauth-2.0/common/src/main/java/org/apache/oltu/oauth2/common/utils/JSONUtils.java
+++ b/oauth-2.0/common/src/main/java/org/apache/oltu/oauth2/common/utils/JSONUtils.java
@@ -23,12 +23,26 @@
import static java.lang.String.format;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.lang.reflect.Array;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
+import java.util.Map.Entry;
-import org.json.JSONArray;
-import org.json.JSONStringer;
-import org.json.JSONTokener;
+import javax.json.Json;
+import javax.json.JsonArray;
+import javax.json.JsonNumber;
+import javax.json.JsonObject;
+import javax.json.JsonReader;
+import javax.json.JsonString;
+import javax.json.JsonStructure;
+import javax.json.JsonValue;
+import javax.json.stream.JsonGenerator;
+import javax.json.stream.JsonGeneratorFactory;
/**
*
@@ -37,89 +51,157 @@
*/
public final class JSONUtils {
+ private static final JsonGeneratorFactory GENERATOR_FACTORY = Json.createGeneratorFactory(null);
+
public static String buildJSON(Map<String, Object> params) {
- final JSONStringer stringer = new JSONStringer();
- stringer.object();
+ final StringWriter stringWriter = new StringWriter();
+ final JsonGenerator generator = GENERATOR_FACTORY.createGenerator(stringWriter);
+
+ generator.writeStartObject();
for (Map.Entry<String, Object> param : params.entrySet()) {
- if (param.getKey() != null && !"".equals(param.getKey()) && param.getValue() != null && !""
- .equals(param.getValue())) {
- stringer.key(param.getKey()).value(param.getValue());
+ String key = param.getKey();
+ Object value = param.getValue();
+ if (key != null && value != null) {
+ if (value instanceof Boolean) {
+ generator.write(key, (Boolean) value);
+ } else if (value instanceof Double) {
+ generator.write(key, (Double) value);
+ } else if (value instanceof Integer) {
+ generator.write(key, (Integer) value);
+ } else if (value instanceof BigDecimal) {
+ generator.write(key, (BigDecimal) value);
+ } else if (value instanceof BigInteger) {
+ generator.write(key, (BigInteger) value);
+ } else if (value instanceof Long) {
+ generator.write(key, (Long) value);
+ } else if (value instanceof String) {
+ String string = (String) value;
+ if (!string.isEmpty()) {
+ generator.write(key, string);
+ }
+ } else if (value.getClass().isArray()) {
+ generator.writeStartArray(key);
+
+ for (int i = 0; i < Array.getLength(value); i++) {
+ witeItem(generator, Array.get(value, i));
+ }
+
+ generator.writeEnd();
+ } else if (value instanceof Collection) {
+ generator.writeStartArray(key);
+
+ Collection<?> collection = (Collection<?>) value;
+ for (Object item : collection) {
+ witeItem(generator, item);
+ }
+
+ generator.writeEnd();
+ }
}
}
- return stringer.endObject().toString();
+ generator.writeEnd().close();
+
+ return stringWriter.toString();
+ }
+
+ private static <T> void witeItem(JsonGenerator generator, T item) {
+ if (item != null) {
+ if (item instanceof Boolean) {
+ generator.write((Boolean) item);
+ } else if (item instanceof Double) {
+ generator.write((Double) item);
+ } else if (item instanceof Integer) {
+ generator.write((Integer) item);
+ } else if (item instanceof BigDecimal) {
+ generator.write((BigDecimal) item);
+ } else if (item instanceof BigInteger) {
+ generator.write((BigInteger) item);
+ } else if (item instanceof Long) {
+ generator.write((Long) item);
+ } else if (item instanceof String) {
+ generator.write((String) item);
+ }
+ }
}
public static Map<String, Object> parseJSON(String jsonBody) {
final Map<String, Object> params = new HashMap<String, Object>();
- final JSONTokener x = new JSONTokener(jsonBody);
- char c;
- String key;
+ StringReader reader = new StringReader(jsonBody);
+ JsonReader jsonReader = Json.createReader(reader);
+ JsonStructure structure = jsonReader.read();
- if (x.nextClean() != '{') {
- throw new IllegalArgumentException(format("String '%s' is not a valid JSON object representation, a JSON object text must begin with '{'",
+ if (structure == null || structure instanceof JsonArray) {
+ throw new IllegalArgumentException(format("String '%s' is not a valid JSON object representation",
jsonBody));
}
- for (;;) {
- c = x.nextClean();
- switch (c) {
- case 0:
- throw new IllegalArgumentException(format("String '%s' is not a valid JSON object representation, a JSON object text must end with '}'",
- jsonBody));
- case '}':
- return params;
- default:
- x.back();
- key = x.nextValue().toString();
- }
- /*
- * The key is followed by ':'. We will also tolerate '=' or '=>'.
- */
- c = x.nextClean();
- if (c == '=') {
- if (x.next() != '>') {
- x.back();
+ JsonObject object = (JsonObject) structure;
+ for (Entry<String, JsonValue> entry : object.entrySet()) {
+ String key = entry.getKey();
+ if (key != null && !key.isEmpty()) {
+ JsonValue jsonValue = entry.getValue();
+
+ // guard from null values
+ if (jsonValue != null) {
+ Object value = toJavaObject(jsonValue);
+
+ params.put(key, value);
}
- } else if (c != ':') {
- throw new IllegalArgumentException(format("String '%s' is not a valid JSON object representation, expected a ':' after the key '%s'",
- jsonBody, key));
- }
- Object value = x.nextValue();
-
- // guard from null values
- if (value != null) {
- if (value instanceof JSONArray) { // only plain simple arrays in this version
- JSONArray array = (JSONArray) value;
- Object[] values = new Object[array.length()];
- for (int i = 0; i < array.length(); i++) {
- values[i] = array.get(i);
- }
- value = values;
- }
-
- params.put(key, value);
- }
-
- /*
- * Pairs are separated by ','. We will also tolerate ';'.
- */
- switch (x.nextClean()) {
- case ';':
- case ',':
- if (x.nextClean() == '}') {
- return params;
- }
- x.back();
- break;
- case '}':
- return params;
- default:
- throw new IllegalArgumentException("Expected a ',' or '}'");
}
}
+
+ jsonReader.close();
+ return params;
+ }
+
+ private static Object toJavaObject(JsonValue jsonValue) {
+ Object value = null;
+
+ switch (jsonValue.getValueType()) {
+ case ARRAY:
+ JsonArray array = (JsonArray) jsonValue;
+ Object[] values = new Object[array.size()];
+ for (int i = 0; i < array.size(); i++) {
+ JsonValue current = array.get(i);
+ values[i] = toJavaObject(current);
+ }
+ value = values;
+ break;
+
+ case FALSE:
+ value = false;
+ break;
+
+ case NULL:
+ value = null;
+ break;
+
+ case NUMBER:
+ JsonNumber jsonNumber = (JsonNumber) jsonValue;
+ value = jsonNumber.numberValue();
+ break;
+
+ case OBJECT:
+ // not supported in this version
+ break;
+
+ case STRING:
+ JsonString jsonString = (JsonString) jsonValue;
+ value = jsonString.getString();
+ break;
+
+ case TRUE:
+ value = true;
+ break;
+
+ default:
+ break;
+ }
+
+ return value;
}
}
diff --git a/oauth-2.0/common/src/test/java/org/apache/oltu/oauth2/common/message/OAuthResponseTest.java b/oauth-2.0/common/src/test/java/org/apache/oltu/oauth2/common/message/OAuthResponseTest.java
index 0cfca23..2aa910a 100644
--- a/oauth-2.0/common/src/test/java/org/apache/oltu/oauth2/common/message/OAuthResponseTest.java
+++ b/oauth-2.0/common/src/test/java/org/apache/oltu/oauth2/common/message/OAuthResponseTest.java
@@ -45,7 +45,7 @@
String body = oAuthResponse.getBody();
assertEquals(
- "{\"error_uri\":\"http://example-uri\",\"error\":\"error\",\"param\":\"value\",\"realm\":\"album\",\"state\":\"ok\",\"error_description\":\"error_description\"}",
+ "{\"param\":\"value\",\"error_description\":\"error_description\",\"realm\":\"album\",\"state\":\"ok\",\"error\":\"error\",\"error_uri\":\"http://example-uri\"}",
body);
}
diff --git a/oauth-2.0/common/src/test/java/org/apache/oltu/oauth2/common/parameters/JSONBodyParametersApplierTest.java b/oauth-2.0/common/src/test/java/org/apache/oltu/oauth2/common/parameters/JSONBodyParametersApplierTest.java
index 8e5d56b..a657fd4 100644
--- a/oauth-2.0/common/src/test/java/org/apache/oltu/oauth2/common/parameters/JSONBodyParametersApplierTest.java
+++ b/oauth-2.0/common/src/test/java/org/apache/oltu/oauth2/common/parameters/JSONBodyParametersApplierTest.java
@@ -21,16 +21,16 @@
package org.apache.oltu.oauth2.common.parameters;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
import java.util.HashMap;
import java.util.Map;
import org.apache.oltu.oauth2.common.OAuth;
import org.apache.oltu.oauth2.common.message.OAuthMessage;
-import org.apache.oltu.oauth2.common.parameters.JSONBodyParametersApplier;
-import org.apache.oltu.oauth2.common.parameters.OAuthParametersApplier;
import org.apache.oltu.oauth2.common.utils.DummyOAuthMessage;
import org.apache.oltu.oauth2.common.utils.JSONUtils;
-import org.junit.Assert;
import org.junit.Test;
/**
@@ -61,14 +61,14 @@
String msgBody = message.getBody();
Map<String, Object> map = JSONUtils.parseJSON(msgBody);
- Assert.assertEquals(3600, map.get(OAuth.OAUTH_EXPIRES_IN));
- Assert.assertEquals("token_authz", map.get(OAuth.OAUTH_ACCESS_TOKEN));
- Assert.assertEquals("code_", map.get(OAuth.OAUTH_CODE));
- Assert.assertEquals("read", map.get(OAuth.OAUTH_SCOPE));
- Assert.assertEquals("state", map.get(OAuth.OAUTH_STATE));
- Assert.assertNull(map.get("empty_param"));
- Assert.assertNull(map.get("null_param"));
- Assert.assertNull(map.get(""));
- Assert.assertNull(map.get(null));
+ assertEquals(3600L, map.get(OAuth.OAUTH_EXPIRES_IN));
+ assertEquals("token_authz", map.get(OAuth.OAUTH_ACCESS_TOKEN));
+ assertEquals("code_", map.get(OAuth.OAUTH_CODE));
+ assertEquals("read", map.get(OAuth.OAUTH_SCOPE));
+ assertEquals("state", map.get(OAuth.OAUTH_STATE));
+ assertNull(map.get("empty_param"));
+ assertNull(map.get("null_param"));
+ assertNull(map.get(""));
+ assertNull(map.get(null));
}
}
diff --git a/oauth-2.0/common/src/test/java/org/apache/oltu/oauth2/common/parameters/WWWAuthHeaderParametersApplierTest.java b/oauth-2.0/common/src/test/java/org/apache/oltu/oauth2/common/parameters/WWWAuthHeaderParametersApplierTest.java
index c4f8512..b24abff 100644
--- a/oauth-2.0/common/src/test/java/org/apache/oltu/oauth2/common/parameters/WWWAuthHeaderParametersApplierTest.java
+++ b/oauth-2.0/common/src/test/java/org/apache/oltu/oauth2/common/parameters/WWWAuthHeaderParametersApplierTest.java
@@ -21,14 +21,14 @@
package org.apache.oltu.oauth2.common.parameters;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
import java.util.HashMap;
import java.util.Map;
import org.apache.oltu.oauth2.common.OAuth;
import org.apache.oltu.oauth2.common.message.OAuthResponse;
-import org.apache.oltu.oauth2.common.parameters.OAuthParametersApplier;
-import org.apache.oltu.oauth2.common.parameters.WWWAuthHeaderParametersApplier;
-import org.junit.Assert;
import org.junit.Test;
/**
@@ -54,14 +54,12 @@
OAuthParametersApplier applier = new WWWAuthHeaderParametersApplier();
res = (OAuthResponse)applier.applyOAuthParameters(res, params);
- Assert.assertNotNull(res);
+ assertNotNull(res);
String header = res.getHeader(OAuth.HeaderType.WWW_AUTHENTICATE);
- Assert.assertNotNull(header);
- Assert.assertEquals(OAuth.OAUTH_HEADER_NAME
- + " scope=\"s1 s2 s3\",error_uri=\"http://www.example.com/error\",error=\"invalid_token\"",
+ assertNotNull(header);
+ assertEquals(OAuth.OAUTH_HEADER_NAME
+ + " scope=\"s1 s2 s3\",error=\"invalid_token\",error_uri=\"http://www.example.com/error\"",
header);
-
-
}
}
diff --git a/oauth-2.0/common/src/test/java/org/apache/oltu/oauth2/common/utils/JSONUtilsTest.java b/oauth-2.0/common/src/test/java/org/apache/oltu/oauth2/common/utils/JSONUtilsTest.java
index 00d5372..2d8a4e2 100644
--- a/oauth-2.0/common/src/test/java/org/apache/oltu/oauth2/common/utils/JSONUtilsTest.java
+++ b/oauth-2.0/common/src/test/java/org/apache/oltu/oauth2/common/utils/JSONUtilsTest.java
@@ -21,12 +21,12 @@
package org.apache.oltu.oauth2.common.utils;
+import static org.junit.Assert.assertEquals;
+
import java.util.HashMap;
import java.util.Map;
import org.apache.oltu.oauth2.common.error.OAuthError;
-import org.junit.Assert;
-import org.junit.Ignore;
import org.junit.Test;
/**
@@ -37,26 +37,13 @@
public class JSONUtilsTest {
@Test
- @Ignore
- // TODO what are testing here?
public void testBuildJSON() throws Exception {
-
Map<String, Object> params = new HashMap<String, Object>();
params.put(OAuthError.OAUTH_ERROR, OAuthError.TokenResponse.INVALID_REQUEST);
String json = JSONUtils.buildJSON(params);
- /* JSONObject obj = new JSONObject(json);
-
- AbstractXMLStreamReader reader = new MappedXMLStreamReader(obj);
-
- Assert.assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
- Assert.assertEquals(OAuthError.OAUTH_ERROR, reader.getName().getLocalPart());
-
- Assert.assertEquals(OAuthError.TokenResponse.INVALID_REQUEST, reader.getText());
- Assert.assertEquals(XMLStreamReader.CHARACTERS, reader.next());
- Assert.assertEquals(XMLStreamReader.END_ELEMENT, reader.next());
- Assert.assertEquals(XMLStreamReader.END_DOCUMENT, reader.next()); */
+ assertEquals("{\"error\":\"invalid_request\"}", json);
}
@Test
@@ -67,8 +54,8 @@
String s = JSONUtils.buildJSON(jsonParams);
Map<String, Object> map = JSONUtils.parseJSON(s);
- Assert.assertEquals("John B. Smith", map.get("author"));
- Assert.assertEquals("2000", map.get("year"));
+ assertEquals("John B. Smith", map.get("author"));
+ assertEquals("2000", map.get("year"));
}
}
diff --git a/oauth-2.0/dynamicreg-server/pom.xml b/oauth-2.0/dynamicreg-server/pom.xml
index 3a7e912..ace28ed 100644
--- a/oauth-2.0/dynamicreg-server/pom.xml
+++ b/oauth-2.0/dynamicreg-server/pom.xml
@@ -49,8 +49,8 @@
</dependency>
<dependency>
- <groupId>org.json</groupId>
- <artifactId>json</artifactId>
+ <groupId>org.apache.johnzon</groupId>
+ <artifactId>johnzon-core</artifactId>
</dependency>
<dependency>
diff --git a/oauth-2.0/dynamicreg-server/src/main/java/org/apache/oltu/oauth2/ext/dynamicreg/server/request/JSONHttpServletRequestWrapper.java b/oauth-2.0/dynamicreg-server/src/main/java/org/apache/oltu/oauth2/ext/dynamicreg/server/request/JSONHttpServletRequestWrapper.java
index 3d364d4..2a6875e 100644
--- a/oauth-2.0/dynamicreg-server/src/main/java/org/apache/oltu/oauth2/ext/dynamicreg/server/request/JSONHttpServletRequestWrapper.java
+++ b/oauth-2.0/dynamicreg-server/src/main/java/org/apache/oltu/oauth2/ext/dynamicreg/server/request/JSONHttpServletRequestWrapper.java
@@ -22,11 +22,22 @@
import static java.lang.String.format;
+import java.io.StringReader;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
+import java.util.Map.Entry;
+import javax.json.Json;
+import javax.json.JsonArray;
+import javax.json.JsonNumber;
+import javax.json.JsonObject;
+import javax.json.JsonReader;
+import javax.json.JsonString;
+import javax.json.JsonStructure;
+import javax.json.JsonValue;
+import javax.json.JsonValue.ValueType;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
@@ -36,8 +47,6 @@
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthRuntimeException;
import org.apache.oltu.oauth2.common.utils.OAuthUtils;
-import org.json.JSONArray;
-import org.json.JSONTokener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -68,78 +77,84 @@
if (!bodyRead) {
String body = readJsonBody();
- final JSONTokener x = new JSONTokener(body);
- char c;
- String key;
+ StringReader reader = new StringReader(body);
+ JsonReader jsonReader = Json.createReader(reader);
+ JsonStructure structure = jsonReader.read();
- if (x.nextClean() != '{') {
- throw new OAuthRuntimeException(format("String '%s' is not a valid JSON object representation, a JSON object text must begin with '{'",
- body));
+ if (structure == null || structure instanceof JsonArray) {
+ throw new IllegalArgumentException(format("String '%s' is not a valid JSON object representation",
+ body));
}
- for (;;) {
- c = x.nextClean();
- switch (c) {
- case 0:
- throw new OAuthRuntimeException(format("String '%s' is not a valid JSON object representation, a JSON object text must end with '}'",
- body));
- case '}':
- return Collections.unmodifiableMap(parameters);
- default:
- x.back();
- key = x.nextValue().toString();
- }
- /*
- * The key is followed by ':'. We will also tolerate '=' or '=>'.
- */
- c = x.nextClean();
- if (c == '=') {
- if (x.next() != '>') {
- x.back();
- }
- } else if (c != ':') {
- throw new OAuthRuntimeException(format("String '%s' is not a valid JSON object representation, expected a ':' after the key '%s'",
- body, key));
- }
- Object value = x.nextValue();
+ JsonObject object = (JsonObject) structure;
+ for (Entry<String, JsonValue> entry : object.entrySet()) {
+ String key = entry.getKey();
+ if (key != null) {
+ JsonValue jsonValue = entry.getValue();
- // guard from null values
- if (value != null) {
- if (value instanceof JSONArray) { // only plain simple arrays in this version
- JSONArray array = (JSONArray) value;
- String[] values = new String[array.length()];
- for (int i = 0; i < array.length(); i++) {
- values[i] = String.valueOf(array.get(i));
+ // guard from null values
+ if (jsonValue != null) {
+ String[] values;
+
+ if (ValueType.ARRAY == jsonValue.getValueType()) {
+ JsonArray array = (JsonArray) jsonValue;
+ values = new String[array.size()];
+ for (int i = 0; i < array.size(); i++) {
+ JsonValue current = array.get(i);
+ values[i] = toJavaObject(current);
+ }
+ } else {
+ values = new String[]{ toJavaObject(jsonValue) };
}
- parameters.put(key, values);
- } else {
- parameters.put(key, new String[]{ String.valueOf(value) });
- }
- }
- /*
- * Pairs are separated by ','. We will also tolerate ';'.
- */
- switch (x.nextClean()) {
- case ';':
- case ',':
- if (x.nextClean() == '}') {
- return Collections.unmodifiableMap(parameters);
+ parameters.put(key, values);
}
- x.back();
- break;
- case '}':
- return Collections.unmodifiableMap(parameters);
- default:
- throw new OAuthRuntimeException(format("String '%s' is not a valid JSON object representation, Expected a ',' or '}",
- body));
}
}
+
+ jsonReader.close();
}
return Collections.unmodifiableMap(parameters);
}
+ private static String toJavaObject(JsonValue jsonValue) {
+ String value = null;
+
+ switch (jsonValue.getValueType()) {
+ case FALSE:
+ value = Boolean.FALSE.toString();
+ break;
+
+ case NULL:
+ value = null;
+ break;
+
+ case NUMBER:
+ JsonNumber jsonNumber = (JsonNumber) jsonValue;
+ value = jsonNumber.numberValue().toString();
+ break;
+
+ case OBJECT:
+ // not supported in this version
+ break;
+
+ case STRING:
+ JsonString jsonString = (JsonString) jsonValue;
+ value = jsonString.getString();
+ break;
+
+ case TRUE:
+ value = Boolean.TRUE.toString();
+ break;
+
+ default:
+ break;
+ }
+
+ return value;
+ }
+
public Enumeration<String> getParameterNames() {
return Collections.enumeration(getParameterMap().keySet());
}
diff --git a/oauth-2.0/dynamicreg-server/src/main/java/org/apache/oltu/oauth2/ext/dynamicreg/server/validator/PushPullValidator.java b/oauth-2.0/dynamicreg-server/src/main/java/org/apache/oltu/oauth2/ext/dynamicreg/server/validator/PushPullValidator.java
index 9430c25..d6ccaf6 100644
--- a/oauth-2.0/dynamicreg-server/src/main/java/org/apache/oltu/oauth2/ext/dynamicreg/server/validator/PushPullValidator.java
+++ b/oauth-2.0/dynamicreg-server/src/main/java/org/apache/oltu/oauth2/ext/dynamicreg/server/validator/PushPullValidator.java
@@ -78,7 +78,7 @@
}
if (log.isDebugEnabled()) {
- log.debug("OAuth dynamic client registration type is: {}", new String[] {requestType});
+ log.debug("OAuth dynamic client registration type is: {}", new Object[] {requestType});
}
}
}
diff --git a/oauth-2.0/dynamicreg-server/src/test/java/org/apache/oltu/oauth2/ext/dynamicreg/server/request/OAuthServerRegistrationRequestTest.java b/oauth-2.0/dynamicreg-server/src/test/java/org/apache/oltu/oauth2/ext/dynamicreg/server/request/OAuthServerRegistrationRequestTest.java
index b5cf8df..6103e24 100644
--- a/oauth-2.0/dynamicreg-server/src/test/java/org/apache/oltu/oauth2/ext/dynamicreg/server/request/OAuthServerRegistrationRequestTest.java
+++ b/oauth-2.0/dynamicreg-server/src/test/java/org/apache/oltu/oauth2/ext/dynamicreg/server/request/OAuthServerRegistrationRequestTest.java
@@ -20,12 +20,11 @@
*/
package org.apache.oltu.oauth2.ext.dynamicreg.server.request;
+import static org.junit.Assert.assertEquals;
+
import org.apache.oltu.oauth2.common.OAuth;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
-import org.apache.oltu.oauth2.ext.dynamicreg.server.request.JSONHttpServletRequestWrapper;
-import org.apache.oltu.oauth2.ext.dynamicreg.server.request.OAuthServerRegistrationRequest;
import org.apache.oltu.oauth2.utils.test.FileUtils;
-import org.junit.Assert;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
@@ -45,14 +44,13 @@
final JSONHttpServletRequestWrapper jsonWrapper = new JSONHttpServletRequestWrapper(request);
OAuthServerRegistrationRequest registrationRequest = new OAuthServerRegistrationRequest(jsonWrapper);
- Assert.assertEquals("Uploading and also editing capabilities!",
+ assertEquals("Uploading and also editing capabilities!",
registrationRequest.getClientDescription());
- Assert.assertEquals("http://onlinephotogallery.com/icon.png", registrationRequest.getClientIcon());
- Assert.assertEquals("Online Photo Gallery", registrationRequest.getClientName());
- Assert
- .assertEquals("https://onlinephotogallery.com/client_reg", registrationRequest.getRedirectURI());
- Assert.assertEquals("push", registrationRequest.getType());
- Assert.assertEquals("http://onlinephotogallery.com", registrationRequest.getClientUrl());
+ assertEquals("http://onlinephotogallery.com/icon.png", registrationRequest.getClientIcon());
+ assertEquals("Online Photo Gallery", registrationRequest.getClientName());
+ assertEquals("https://onlinephotogallery.com/client_reg", registrationRequest.getRedirectURI());
+ assertEquals("push", registrationRequest.getType());
+ assertEquals("http://onlinephotogallery.com", registrationRequest.getClientUrl());
}
@Test(expected = OAuthProblemException.class)
diff --git a/oauth-2.0/integration-tests/pom.xml b/oauth-2.0/integration-tests/pom.xml
index 2724e2e..1490adf 100644
--- a/oauth-2.0/integration-tests/pom.xml
+++ b/oauth-2.0/integration-tests/pom.xml
@@ -119,6 +119,11 @@
<version>${org.springframework.version}</version>
<scope>test</scope>
</dependency>
+
+ <dependency>
+ <groupId>org.apache.johnzon</groupId>
+ <artifactId>johnzon-core</artifactId>
+ </dependency>
</dependencies>
<build>
diff --git a/oauth-2.0/pom.xml b/oauth-2.0/pom.xml
index 3c379e4..06639bb 100644
--- a/oauth-2.0/pom.xml
+++ b/oauth-2.0/pom.xml
@@ -88,9 +88,16 @@
<dependencyManagement>
<dependencies>
<dependency>
- <groupId>org.json</groupId>
- <artifactId>json</artifactId>
- <version>20140107</version>
+ <groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-json_1.1_spec</artifactId>
+ <version>1.0</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.johnzon</groupId>
+ <artifactId>johnzon-core</artifactId>
+ <version>1.1.4</version>
+ <scope>test</scope>
</dependency>
<dependency>