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.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<String> usernames) throws JSONException
@SuppressWarnings("unchecked")
protected JSONArray getUserArray(List<String> 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));
}
}

View File

@@ -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<String> users = new ArrayList<String>(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;

View File

@@ -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);

View File

@@ -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));

View File

@@ -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);

View File

@@ -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));

View File

@@ -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<String> users = new ArrayList<String>(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;

View File

@@ -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));

View File

@@ -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);
}

View File

@@ -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<String> users = new ArrayList<String>(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;