ALF-10122 Switch the new Wiki, Links, Calendar and Discussions webscripts to using org.json.simple instead of org.json

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@30182 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Nick Burch
2011-09-02 11:48:02 +00:00
parent ca539d5d0d
commit efedb236b2
32 changed files with 262 additions and 343 deletions

View File

@@ -19,6 +19,7 @@
package org.alfresco.repo.web.scripts.links;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -36,15 +37,16 @@ import org.alfresco.service.cmr.site.SiteInfo;
import org.alfresco.service.cmr.site.SiteService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.DeclarativeWebScript;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.json.JSONWriter;
/**
* @author Nick Burch
@@ -92,25 +94,25 @@ public abstract class AbstractLinksWebScript extends DeclarativeWebScript
}
protected String getOrNull(JSONObject json, String key) throws JSONException
protected String getOrNull(JSONObject json, String key)
{
if(json.has(key))
if(json.containsKey(key))
{
return json.getString(key);
return (String)json.get(key);
}
return null;
}
protected List<String> getTags(JSONObject json) throws JSONException
protected List<String> getTags(JSONObject json)
{
List<String> tags = null;
if(json.has("tags"))
if(json.containsKey("tags"))
{
// Is it "tags":"" or "tags":[...] ?
if(json.get("tags") instanceof String)
{
// This is normally an empty string, skip
String tagsS = json.getString("tags");
String tagsS = (String)json.get("tags");
if("".equals(tagsS))
{
// No tags were given
@@ -127,10 +129,10 @@ public abstract class AbstractLinksWebScript extends DeclarativeWebScript
else
{
tags = new ArrayList<String>();
JSONArray jsTags = json.getJSONArray("tags");
for(int i=0; i<jsTags.length(); i++)
JSONArray jsTags = (JSONArray)json.get("tags");
for(int i=0; i<jsTags.size(); i++)
{
tags.add( jsTags.getString(i) );
tags.add( (String)jsTags.get(i) );
}
}
}
@@ -177,13 +179,9 @@ public abstract class AbstractLinksWebScript extends DeclarativeWebScript
String page = req.getParameter("page");
if(page == null && json != null)
{
if(json.has("page"))
if(json.containsKey("page"))
{
try
{
page = json.getString("page");
}
catch(JSONException e) {}
page = (String)json.get("page");
}
}
if(page == null)
@@ -194,15 +192,18 @@ public abstract class AbstractLinksWebScript extends DeclarativeWebScript
try
{
JSONObject activity = new JSONObject();
activity.put("title", link.getTitle());
activity.put("page", page + "?linkId=" + link.getSystemName());
StringWriter activityJson = new StringWriter();
JSONWriter activity = new JSONWriter(activityJson);
activity.startObject();
activity.writeValue("title", link.getTitle());
activity.writeValue("page", page + "?linkId=" + link.getSystemName());
activity.endObject();
activityService.postActivity(
"org.alfresco.links.link-" + event,
site.getShortName(),
LINKS_SERVICE_ACTIVITY_APP_NAME,
activity.toString()
activityJson.toString()
);
}
catch(Exception e)
@@ -272,17 +273,18 @@ public abstract class AbstractLinksWebScript extends DeclarativeWebScript
}
if(MimetypeMap.MIMETYPE_JSON.equals(contentType))
{
JSONParser parser = new JSONParser();
try
{
json = new JSONObject(new JSONTokener(req.getContent().getContent()));
json = (JSONObject)parser.parse(req.getContent().getContent());
}
catch(IOException io)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Invalid JSON: " + io.getMessage());
}
catch(JSONException je)
catch(ParseException pe)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Invalid JSON: " + je.getMessage());
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Invalid JSON: " + pe.getMessage());
}
}
@@ -295,18 +297,14 @@ public abstract class AbstractLinksWebScript extends DeclarativeWebScript
}
if(siteName == null && json != null)
{
try
if(json.containsKey("siteid"))
{
if(json.has("siteid"))
{
siteName = json.getString("siteid");
}
else if(json.has("site"))
{
siteName = json.getString("site");
}
siteName = (String)json.get("siteid");
}
else if(json.containsKey("site"))
{
siteName = (String)json.get("site");
}
catch(JSONException e) {}
}
if(siteName == null)
{

View File

@@ -24,7 +24,7 @@ import java.util.Map;
import org.alfresco.repo.security.permissions.AccessDeniedException;
import org.alfresco.service.cmr.links.LinkInfo;
import org.alfresco.service.cmr.site.SiteInfo;
import org.json.JSONObject;
import org.json.simple.JSONObject;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException;

View File

@@ -23,7 +23,7 @@ import java.util.Map;
import org.alfresco.service.cmr.links.LinkInfo;
import org.alfresco.service.cmr.site.SiteInfo;
import org.json.JSONObject;
import org.json.simple.JSONObject;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException;

View File

@@ -25,11 +25,9 @@ import java.util.Map;
import org.alfresco.repo.security.permissions.AccessDeniedException;
import org.alfresco.service.cmr.links.LinkInfo;
import org.alfresco.service.cmr.site.SiteInfo;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.JSONObject;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest;
/**
@@ -59,34 +57,27 @@ public class LinkPut extends AbstractLinksWebScript
// Get the new link details from the JSON
try
// Update the main properties
link.setTitle(getOrNull(json, "title"));
link.setDescription(getOrNull(json, "description"));
link.setURL(getOrNull(json, "url"));
// Handle internal / not internal
if(json.containsKey("internal"))
{
// Update the main properties
link.setTitle(getOrNull(json, "title"));
link.setDescription(getOrNull(json, "description"));
link.setURL(getOrNull(json, "url"));
// Handle internal / not internal
if(json.has("internal"))
{
link.setInternal(true);
}
else
{
link.setInternal(false);
}
// Do the tags
link.getTags().clear();
List<String> tags = getTags(json);
if(tags != null && tags.size() > 0)
{
link.getTags().addAll(tags);
}
link.setInternal(true);
}
catch(JSONException je)
else
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Invalid JSON: " + je.getMessage());
link.setInternal(false);
}
// Do the tags
link.getTags().clear();
List<String> tags = getTags(json);
if(tags != null && tags.size() > 0)
{
link.getTags().addAll(tags);
}

View File

@@ -26,12 +26,10 @@ import java.util.Map;
import org.alfresco.repo.security.permissions.AccessDeniedException;
import org.alfresco.service.cmr.links.LinkInfo;
import org.alfresco.service.cmr.site.SiteInfo;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest;
/**
@@ -53,26 +51,19 @@ public class LinksDeletePost extends AbstractLinksWebScript
// Get the requested nodes from the JSON
// Silently skips over any invalid ones specified
List<LinkInfo> links = new ArrayList<LinkInfo>();
try
if(json.containsKey("items"))
{
if(json.has("items"))
JSONArray items = (JSONArray)json.get("items");
for(int i=0; i<items.size(); i++)
{
JSONArray items = json.getJSONArray("items");
for(int i=0; i<items.length(); i++)
String name = (String)items.get(i);
LinkInfo link = linksService.getLink(site.getShortName(), name);
if(link != null)
{
String name = items.getString(i);
LinkInfo link = linksService.getLink(site.getShortName(), name);
if(link != null)
{
links.add(link);
}
links.add(link);
}
}
}
catch(JSONException je)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Invalid JSON: " + je.getMessage());
}
// Check we got at least one link, and bail if not

View File

@@ -31,7 +31,7 @@ import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.service.cmr.links.LinkInfo;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.site.SiteInfo;
import org.json.JSONObject;
import org.json.simple.JSONObject;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptRequest;

View File

@@ -25,11 +25,9 @@ import java.util.Map;
import org.alfresco.repo.security.permissions.AccessDeniedException;
import org.alfresco.service.cmr.links.LinkInfo;
import org.alfresco.service.cmr.site.SiteInfo;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.JSONObject;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest;
/**
@@ -51,23 +49,17 @@ public class LinksPost extends AbstractLinksWebScript
String url;
boolean internal;
List<String> tags;
try
{
// Fetch the main properties
title = getOrNull(json, "title");
description = getOrNull(json, "description");
url = getOrNull(json, "url");
// Handle internal / not internal
internal = json.has("internal");
// Do the tags
tags = getTags(json);
}
catch(JSONException je)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Invalid JSON: " + je.getMessage());
}
// Fetch the main properties
title = getOrNull(json, "title");
description = getOrNull(json, "description");
url = getOrNull(json, "url");
// Handle internal / not internal
internal = json.containsKey("internal");
// Do the tags
tags = getTags(json);
// Create the link