From 6a98ee59ebf804e86d2e1319c38ba19d0f1cae1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=BC?= Date: Fri, 2 Sep 2011 19:01:38 +0000 Subject: [PATCH] Switched subscription service from org.json to org.json.simple git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@30210 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../AbstractSubscriptionServiceWebScript.java | 54 +++++++++---------- .../SubscriptionServiceFollowPost.java | 27 +++++----- .../SubscriptionServiceFollowersCountGet.java | 7 ++- .../SubscriptionServiceFollowersGet.java | 7 ++- .../SubscriptionServiceFollowingCountGet.java | 7 ++- .../SubscriptionServiceFollowingGet.java | 7 ++- .../SubscriptionServiceFollowsPost.java | 33 ++++++------ .../SubscriptionServicePrivateListGet.java | 7 +-- .../SubscriptionServicePrivateListPut.java | 16 +++--- .../SubscriptionServiceUnfollowPost.java | 27 +++++----- 10 files changed, 91 insertions(+), 101 deletions(-) diff --git a/source/java/org/alfresco/repo/web/scripts/subscriptions/AbstractSubscriptionServiceWebScript.java b/source/java/org/alfresco/repo/web/scripts/subscriptions/AbstractSubscriptionServiceWebScript.java index 7a58f9c9a3..916b01a4d9 100644 --- a/source/java/org/alfresco/repo/web/scripts/subscriptions/AbstractSubscriptionServiceWebScript.java +++ b/source/java/org/alfresco/repo/web/scripts/subscriptions/AbstractSubscriptionServiceWebScript.java @@ -34,9 +34,9 @@ import org.alfresco.service.cmr.subscriptions.PrivateSubscriptionListException; import org.alfresco.service.cmr.subscriptions.SubscriptionService; import org.alfresco.service.cmr.subscriptions.SubscriptionsDisabledException; import org.alfresco.util.ISO8601DateFormat; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.parser.ParseException; import org.springframework.extensions.webscripts.AbstractWebScript; import org.springframework.extensions.webscripts.WebScriptException; import org.springframework.extensions.webscripts.WebScriptRequest; @@ -79,43 +79,43 @@ public abstract class AbstractSubscriptionServiceWebScript extends AbstractWebSc if (obj instanceof JSONObject || obj instanceof JSONArray) { res.setContentEncoding(Charset.defaultCharset().displayName()); - + Writer writer = res.getWriter(); if (obj instanceof JSONObject) { - ((JSONObject) obj).write(writer); - } - else + ((JSONObject) obj).writeJSONString(writer); + } else { - ((JSONArray) obj).write(writer); + ((JSONArray) obj).writeJSONString(writer); } writer.flush(); - } - else + } else { res.setStatus(204); } - } - catch (SubscriptionsDisabledException sde) + } catch (SubscriptionsDisabledException sde) { throw new WebScriptException(404, "Subscription service is disabled!", sde); - } - catch (NoSuchPersonException nspe) + } catch (NoSuchPersonException nspe) { throw new WebScriptException(404, "Unknown user '" + nspe.getUserName() + "'!", nspe); - } - catch (PrivateSubscriptionListException psle) + } catch (PrivateSubscriptionListException psle) { throw new WebScriptException(403, "Subscription list is private!", psle); - } - catch (JSONException je) + } catch (ParseException pe) { - throw new WebScriptException(500, "Unable to parse or serialize JSON!", je); + throw new WebScriptException(400, "Unable to parse JSON!", pe); + } catch (ClassCastException cce) + { + throw new WebScriptException(400, "Unable to parse JSON!", cce); + } catch (IOException ioe) + { + throw new WebScriptException(500, "Unable to serialize JSON!", ioe); } } public abstract Object executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException, - JSONException; + ParseException; protected int parseNumber(String name, String number, int def) { @@ -125,13 +125,11 @@ public abstract class AbstractSubscriptionServiceWebScript extends AbstractWebSc { return Integer.parseInt(number); - } - catch (NumberFormatException e) + } catch (NumberFormatException e) { throw new WebScriptException(400, name + " is not a number!", e); } - } - else + } else { return def; } @@ -148,7 +146,8 @@ public abstract class AbstractSubscriptionServiceWebScript extends AbstractWebSc return result; } - protected JSONObject getUserDetails(String username) throws JSONException + @SuppressWarnings("unchecked") + protected JSONObject getUserDetails(String username) { NodeRef node = personService.getPerson(username); @@ -176,7 +175,8 @@ public abstract class AbstractSubscriptionServiceWebScript extends AbstractWebSc return result; } - protected JSONArray getUserArray(List usernames) throws JSONException + @SuppressWarnings("unchecked") + protected JSONArray getUserArray(List usernames) { JSONArray result = new JSONArray(); @@ -184,7 +184,7 @@ public abstract class AbstractSubscriptionServiceWebScript extends AbstractWebSc { for (String username : usernames) { - result.put(getUserDetails(username)); + result.add(getUserDetails(username)); } } diff --git a/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceFollowPost.java b/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceFollowPost.java index b0dc413143..4018c11886 100644 --- a/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceFollowPost.java +++ b/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceFollowPost.java @@ -19,31 +19,28 @@ package org.alfresco.repo.web.scripts.subscriptions; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; +import org.json.simple.parser.ParseException; import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.WebScriptResponse; public class SubscriptionServiceFollowPost extends AbstractSubscriptionServiceWebScript { public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException, - JSONException + ParseException { - JSONArray jsonUsers = new JSONArray(req.getContent().getContent()); + JSONArray jsonUsers = (JSONArray) JSONValue.parseWithException(req.getContent().getContent()); - List users = new ArrayList(jsonUsers.length()); - for (int i = 0; i < jsonUsers.length(); i++) + for (Object o : jsonUsers) { - users.add(jsonUsers.getString(i)); - } - - for (String user : users) - { - subscriptionService.follow(userId, user); + String user = (o == null ? null : o.toString()); + if (user != null) + { + subscriptionService.follow(userId, user); + } } return null; diff --git a/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceFollowersCountGet.java b/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceFollowersCountGet.java index 4b53e6a50e..87e19e6c43 100644 --- a/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceFollowersCountGet.java +++ b/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceFollowersCountGet.java @@ -20,15 +20,14 @@ package org.alfresco.repo.web.scripts.subscriptions; import java.io.IOException; -import org.json.JSONException; -import org.json.JSONObject; +import org.json.simple.JSONObject; import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.WebScriptResponse; public class SubscriptionServiceFollowersCountGet extends AbstractSubscriptionServiceWebScript { - public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException, - JSONException + @SuppressWarnings("unchecked") + public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException { int count = subscriptionService.getFollowersCount(userId); diff --git a/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceFollowersGet.java b/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceFollowersGet.java index 758790ae6f..88f63769cf 100644 --- a/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceFollowersGet.java +++ b/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceFollowersGet.java @@ -21,15 +21,14 @@ package org.alfresco.repo.web.scripts.subscriptions; import java.io.IOException; import org.alfresco.service.cmr.subscriptions.PagingFollowingResults; -import org.json.JSONException; -import org.json.JSONObject; +import org.json.simple.JSONObject; import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.WebScriptResponse; public class SubscriptionServiceFollowersGet extends AbstractSubscriptionServiceWebScript { - public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException, - JSONException + @SuppressWarnings("unchecked") + public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException { PagingFollowingResults result = subscriptionService.getFollowers(userId, createPagingRequest(req)); diff --git a/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceFollowingCountGet.java b/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceFollowingCountGet.java index ad766e5de3..9aa769673d 100644 --- a/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceFollowingCountGet.java +++ b/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceFollowingCountGet.java @@ -20,15 +20,14 @@ package org.alfresco.repo.web.scripts.subscriptions; import java.io.IOException; -import org.json.JSONException; -import org.json.JSONObject; +import org.json.simple.JSONObject; import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.WebScriptResponse; public class SubscriptionServiceFollowingCountGet extends AbstractSubscriptionServiceWebScript { - public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException, - JSONException + @SuppressWarnings("unchecked") + public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException { int count = subscriptionService.getFollowingCount(userId); diff --git a/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceFollowingGet.java b/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceFollowingGet.java index e88beecfdd..24d1fb7044 100644 --- a/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceFollowingGet.java +++ b/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceFollowingGet.java @@ -21,15 +21,14 @@ package org.alfresco.repo.web.scripts.subscriptions; import java.io.IOException; import org.alfresco.service.cmr.subscriptions.PagingFollowingResults; -import org.json.JSONException; -import org.json.JSONObject; +import org.json.simple.JSONObject; import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.WebScriptResponse; public class SubscriptionServiceFollowingGet extends AbstractSubscriptionServiceWebScript { - public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException, - JSONException + @SuppressWarnings("unchecked") + public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException { PagingFollowingResults result = subscriptionService.getFollowing(userId, createPagingRequest(req)); diff --git a/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceFollowsPost.java b/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceFollowsPost.java index 699f4dbbc2..d0fbb146fd 100644 --- a/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceFollowsPost.java +++ b/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceFollowsPost.java @@ -19,34 +19,33 @@ package org.alfresco.repo.web.scripts.subscriptions; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; +import org.json.simple.parser.ParseException; import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.WebScriptResponse; public class SubscriptionServiceFollowsPost extends AbstractSubscriptionServiceWebScript { + @SuppressWarnings("unchecked") public JSONArray executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException, - JSONException + ParseException { - JSONArray jsonUsers = new JSONArray(req.getContent().getContent()); - - List users = new ArrayList(jsonUsers.length()); - for (int i = 0; i < jsonUsers.length(); i++) - { - users.add(jsonUsers.getString(i)); - } + JSONArray jsonUsers = (JSONArray) JSONValue.parseWithException(req.getContent().getContent()); JSONArray result = new JSONArray(); - for (String user : users) + + for (Object o : jsonUsers) { - JSONObject item = new JSONObject(); - item.put(user, subscriptionService.follows(userId, user)); - result.put(item); + String user = (o == null ? null : o.toString()); + if (user != null) + { + JSONObject item = new JSONObject(); + item.put(user, subscriptionService.follows(userId, user)); + result.add(item); + } } return result; diff --git a/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServicePrivateListGet.java b/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServicePrivateListGet.java index 821eb0f927..7998357ccd 100644 --- a/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServicePrivateListGet.java +++ b/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServicePrivateListGet.java @@ -20,15 +20,16 @@ package org.alfresco.repo.web.scripts.subscriptions; import java.io.IOException; -import org.json.JSONException; -import org.json.JSONObject; +import org.json.simple.JSONObject; +import org.json.simple.parser.ParseException; import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.WebScriptResponse; public class SubscriptionServicePrivateListGet extends AbstractSubscriptionServiceWebScript { + @SuppressWarnings("unchecked") public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException, - JSONException + ParseException { JSONObject obj = new JSONObject(); obj.put("private", subscriptionService.isSubscriptionListPrivate(userId)); diff --git a/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServicePrivateListPut.java b/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServicePrivateListPut.java index ad52cc772a..af481a406e 100644 --- a/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServicePrivateListPut.java +++ b/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServicePrivateListPut.java @@ -20,27 +20,27 @@ package org.alfresco.repo.web.scripts.subscriptions; import java.io.IOException; -import org.json.JSONException; -import org.json.JSONObject; +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; +import org.json.simple.parser.ParseException; import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.WebScriptResponse; public class SubscriptionServicePrivateListPut extends SubscriptionServicePrivateListGet { public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException, - JSONException + ParseException { - JSONObject obj = new JSONObject(req.getContent().getContent()); + JSONObject obj = (JSONObject) JSONValue.parseWithException(req.getContent().getContent()); - String setPrivate = obj.getString("private"); + Object setPrivate = obj.get("private"); if (setPrivate != null) { - if (setPrivate.equalsIgnoreCase("true")) + if (setPrivate.toString().equalsIgnoreCase("true")) { subscriptionService.setSubscriptionListPrivate(userId, true); - } - else if (setPrivate.equalsIgnoreCase("false")) + } else if (setPrivate.toString().equalsIgnoreCase("false")) { subscriptionService.setSubscriptionListPrivate(userId, false); } diff --git a/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceUnfollowPost.java b/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceUnfollowPost.java index 4e979cbed1..179453a27e 100644 --- a/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceUnfollowPost.java +++ b/source/java/org/alfresco/repo/web/scripts/subscriptions/SubscriptionServiceUnfollowPost.java @@ -19,31 +19,28 @@ package org.alfresco.repo.web.scripts.subscriptions; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; +import org.json.simple.parser.ParseException; import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.WebScriptResponse; public class SubscriptionServiceUnfollowPost extends AbstractSubscriptionServiceWebScript { public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException, - JSONException + ParseException { - JSONArray jsonUsers = new JSONArray(req.getContent().getContent()); + JSONArray jsonUsers = (JSONArray) JSONValue.parseWithException(req.getContent().getContent()); - List users = new ArrayList(jsonUsers.length()); - for (int i = 0; i < jsonUsers.length(); i++) + for (Object o : jsonUsers) { - users.add(jsonUsers.getString(i)); - } - - for (String user : users) - { - subscriptionService.unfollow(userId, user); + String user = (o == null ? null : o.toString()); + if (user != null) + { + subscriptionService.unfollow(userId, user); + } } return null;