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
This commit is contained in:
Florian Mü
2011-09-02 19:01:38 +00:00
parent 28b7e033d9
commit 6a98ee59eb
10 changed files with 91 additions and 101 deletions

View File

@@ -34,9 +34,9 @@ import org.alfresco.service.cmr.subscriptions.PrivateSubscriptionListException;
import org.alfresco.service.cmr.subscriptions.SubscriptionService; import org.alfresco.service.cmr.subscriptions.SubscriptionService;
import org.alfresco.service.cmr.subscriptions.SubscriptionsDisabledException; import org.alfresco.service.cmr.subscriptions.SubscriptionsDisabledException;
import org.alfresco.util.ISO8601DateFormat; import org.alfresco.util.ISO8601DateFormat;
import org.json.JSONArray; import org.json.simple.JSONArray;
import org.json.JSONException; import org.json.simple.JSONObject;
import org.json.JSONObject; import org.json.simple.parser.ParseException;
import org.springframework.extensions.webscripts.AbstractWebScript; import org.springframework.extensions.webscripts.AbstractWebScript;
import org.springframework.extensions.webscripts.WebScriptException; import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.WebScriptRequest;
@@ -83,39 +83,39 @@ public abstract class AbstractSubscriptionServiceWebScript extends AbstractWebSc
Writer writer = res.getWriter(); Writer writer = res.getWriter();
if (obj instanceof JSONObject) if (obj instanceof JSONObject)
{ {
((JSONObject) obj).write(writer); ((JSONObject) obj).writeJSONString(writer);
} } else
else
{ {
((JSONArray) obj).write(writer); ((JSONArray) obj).writeJSONString(writer);
} }
writer.flush(); writer.flush();
} } else
else
{ {
res.setStatus(204); res.setStatus(204);
} }
} } catch (SubscriptionsDisabledException sde)
catch (SubscriptionsDisabledException sde)
{ {
throw new WebScriptException(404, "Subscription service is disabled!", 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); throw new WebScriptException(404, "Unknown user '" + nspe.getUserName() + "'!", nspe);
} } catch (PrivateSubscriptionListException psle)
catch (PrivateSubscriptionListException psle)
{ {
throw new WebScriptException(403, "Subscription list is private!", psle); throw new WebScriptException(403, "Subscription list is private!", psle);
} } catch (ParseException pe)
catch (JSONException je)
{ {
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, public abstract Object executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException,
JSONException; ParseException;
protected int parseNumber(String name, String number, int def) protected int parseNumber(String name, String number, int def)
{ {
@@ -125,13 +125,11 @@ public abstract class AbstractSubscriptionServiceWebScript extends AbstractWebSc
{ {
return Integer.parseInt(number); return Integer.parseInt(number);
} } catch (NumberFormatException e)
catch (NumberFormatException e)
{ {
throw new WebScriptException(400, name + " is not a number!", e); throw new WebScriptException(400, name + " is not a number!", e);
} }
} } else
else
{ {
return def; return def;
} }
@@ -148,7 +146,8 @@ public abstract class AbstractSubscriptionServiceWebScript extends AbstractWebSc
return result; return result;
} }
protected JSONObject getUserDetails(String username) throws JSONException @SuppressWarnings("unchecked")
protected JSONObject getUserDetails(String username)
{ {
NodeRef node = personService.getPerson(username); NodeRef node = personService.getPerson(username);
@@ -176,7 +175,8 @@ public abstract class AbstractSubscriptionServiceWebScript extends AbstractWebSc
return result; return result;
} }
protected JSONArray getUserArray(List<String> usernames) throws JSONException @SuppressWarnings("unchecked")
protected JSONArray getUserArray(List<String> usernames)
{ {
JSONArray result = new JSONArray(); JSONArray result = new JSONArray();
@@ -184,7 +184,7 @@ public abstract class AbstractSubscriptionServiceWebScript extends AbstractWebSc
{ {
for (String username : usernames) for (String username : usernames)
{ {
result.put(getUserDetails(username)); result.add(getUserDetails(username));
} }
} }

View File

@@ -19,32 +19,29 @@
package org.alfresco.repo.web.scripts.subscriptions; package org.alfresco.repo.web.scripts.subscriptions;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray; import org.json.simple.JSONArray;
import org.json.JSONException; import org.json.simple.JSONObject;
import org.json.JSONObject; import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;
import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse; import org.springframework.extensions.webscripts.WebScriptResponse;
public class SubscriptionServiceFollowPost extends AbstractSubscriptionServiceWebScript public class SubscriptionServiceFollowPost extends AbstractSubscriptionServiceWebScript
{ {
public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException, 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<String> users = new ArrayList<String>(jsonUsers.length()); for (Object o : jsonUsers)
for (int i = 0; i < jsonUsers.length(); i++)
{ {
users.add(jsonUsers.getString(i)); String user = (o == null ? null : o.toString());
} if (user != null)
for (String user : users)
{ {
subscriptionService.follow(userId, user); subscriptionService.follow(userId, user);
} }
}
return null; return null;
} }

View File

@@ -20,15 +20,14 @@ package org.alfresco.repo.web.scripts.subscriptions;
import java.io.IOException; import java.io.IOException;
import org.json.JSONException; import org.json.simple.JSONObject;
import org.json.JSONObject;
import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse; import org.springframework.extensions.webscripts.WebScriptResponse;
public class SubscriptionServiceFollowersCountGet extends AbstractSubscriptionServiceWebScript public class SubscriptionServiceFollowersCountGet extends AbstractSubscriptionServiceWebScript
{ {
public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException, @SuppressWarnings("unchecked")
JSONException public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException
{ {
int count = subscriptionService.getFollowersCount(userId); int count = subscriptionService.getFollowersCount(userId);

View File

@@ -21,15 +21,14 @@ package org.alfresco.repo.web.scripts.subscriptions;
import java.io.IOException; import java.io.IOException;
import org.alfresco.service.cmr.subscriptions.PagingFollowingResults; import org.alfresco.service.cmr.subscriptions.PagingFollowingResults;
import org.json.JSONException; import org.json.simple.JSONObject;
import org.json.JSONObject;
import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse; import org.springframework.extensions.webscripts.WebScriptResponse;
public class SubscriptionServiceFollowersGet extends AbstractSubscriptionServiceWebScript public class SubscriptionServiceFollowersGet extends AbstractSubscriptionServiceWebScript
{ {
public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException, @SuppressWarnings("unchecked")
JSONException public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException
{ {
PagingFollowingResults result = subscriptionService.getFollowers(userId, createPagingRequest(req)); PagingFollowingResults result = subscriptionService.getFollowers(userId, createPagingRequest(req));

View File

@@ -20,15 +20,14 @@ package org.alfresco.repo.web.scripts.subscriptions;
import java.io.IOException; import java.io.IOException;
import org.json.JSONException; import org.json.simple.JSONObject;
import org.json.JSONObject;
import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse; import org.springframework.extensions.webscripts.WebScriptResponse;
public class SubscriptionServiceFollowingCountGet extends AbstractSubscriptionServiceWebScript public class SubscriptionServiceFollowingCountGet extends AbstractSubscriptionServiceWebScript
{ {
public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException, @SuppressWarnings("unchecked")
JSONException public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException
{ {
int count = subscriptionService.getFollowingCount(userId); int count = subscriptionService.getFollowingCount(userId);

View File

@@ -21,15 +21,14 @@ package org.alfresco.repo.web.scripts.subscriptions;
import java.io.IOException; import java.io.IOException;
import org.alfresco.service.cmr.subscriptions.PagingFollowingResults; import org.alfresco.service.cmr.subscriptions.PagingFollowingResults;
import org.json.JSONException; import org.json.simple.JSONObject;
import org.json.JSONObject;
import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse; import org.springframework.extensions.webscripts.WebScriptResponse;
public class SubscriptionServiceFollowingGet extends AbstractSubscriptionServiceWebScript public class SubscriptionServiceFollowingGet extends AbstractSubscriptionServiceWebScript
{ {
public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException, @SuppressWarnings("unchecked")
JSONException public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException
{ {
PagingFollowingResults result = subscriptionService.getFollowing(userId, createPagingRequest(req)); PagingFollowingResults result = subscriptionService.getFollowing(userId, createPagingRequest(req));

View File

@@ -19,34 +19,33 @@
package org.alfresco.repo.web.scripts.subscriptions; package org.alfresco.repo.web.scripts.subscriptions;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray; import org.json.simple.JSONArray;
import org.json.JSONException; import org.json.simple.JSONObject;
import org.json.JSONObject; import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;
import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse; import org.springframework.extensions.webscripts.WebScriptResponse;
public class SubscriptionServiceFollowsPost extends AbstractSubscriptionServiceWebScript public class SubscriptionServiceFollowsPost extends AbstractSubscriptionServiceWebScript
{ {
@SuppressWarnings("unchecked")
public JSONArray executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException, public JSONArray 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<String> users = new ArrayList<String>(jsonUsers.length());
for (int i = 0; i < jsonUsers.length(); i++)
{
users.add(jsonUsers.getString(i));
}
JSONArray result = new JSONArray(); JSONArray result = new JSONArray();
for (String user : users)
for (Object o : jsonUsers)
{
String user = (o == null ? null : o.toString());
if (user != null)
{ {
JSONObject item = new JSONObject(); JSONObject item = new JSONObject();
item.put(user, subscriptionService.follows(userId, user)); item.put(user, subscriptionService.follows(userId, user));
result.put(item); result.add(item);
}
} }
return result; return result;

View File

@@ -20,15 +20,16 @@ package org.alfresco.repo.web.scripts.subscriptions;
import java.io.IOException; import java.io.IOException;
import org.json.JSONException; import org.json.simple.JSONObject;
import org.json.JSONObject; import org.json.simple.parser.ParseException;
import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse; import org.springframework.extensions.webscripts.WebScriptResponse;
public class SubscriptionServicePrivateListGet extends AbstractSubscriptionServiceWebScript public class SubscriptionServicePrivateListGet extends AbstractSubscriptionServiceWebScript
{ {
@SuppressWarnings("unchecked")
public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException, public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException,
JSONException ParseException
{ {
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
obj.put("private", subscriptionService.isSubscriptionListPrivate(userId)); obj.put("private", subscriptionService.isSubscriptionListPrivate(userId));

View File

@@ -20,27 +20,27 @@ package org.alfresco.repo.web.scripts.subscriptions;
import java.io.IOException; import java.io.IOException;
import org.json.JSONException; import org.json.simple.JSONObject;
import org.json.JSONObject; import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;
import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse; import org.springframework.extensions.webscripts.WebScriptResponse;
public class SubscriptionServicePrivateListPut extends SubscriptionServicePrivateListGet public class SubscriptionServicePrivateListPut extends SubscriptionServicePrivateListGet
{ {
public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException, 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 != null)
{ {
if (setPrivate.equalsIgnoreCase("true")) if (setPrivate.toString().equalsIgnoreCase("true"))
{ {
subscriptionService.setSubscriptionListPrivate(userId, true); subscriptionService.setSubscriptionListPrivate(userId, true);
} } else if (setPrivate.toString().equalsIgnoreCase("false"))
else if (setPrivate.equalsIgnoreCase("false"))
{ {
subscriptionService.setSubscriptionListPrivate(userId, false); subscriptionService.setSubscriptionListPrivate(userId, false);
} }

View File

@@ -19,32 +19,29 @@
package org.alfresco.repo.web.scripts.subscriptions; package org.alfresco.repo.web.scripts.subscriptions;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray; import org.json.simple.JSONArray;
import org.json.JSONException; import org.json.simple.JSONObject;
import org.json.JSONObject; import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;
import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse; import org.springframework.extensions.webscripts.WebScriptResponse;
public class SubscriptionServiceUnfollowPost extends AbstractSubscriptionServiceWebScript public class SubscriptionServiceUnfollowPost extends AbstractSubscriptionServiceWebScript
{ {
public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException, 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<String> users = new ArrayList<String>(jsonUsers.length()); for (Object o : jsonUsers)
for (int i = 0; i < jsonUsers.length(); i++)
{ {
users.add(jsonUsers.getString(i)); String user = (o == null ? null : o.toString());
} if (user != null)
for (String user : users)
{ {
subscriptionService.unfollow(userId, user); subscriptionService.unfollow(userId, user);
} }
}
return null; return null;
} }