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.wiki;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
@@ -34,14 +35,15 @@ import org.alfresco.service.cmr.wiki.WikiPageInfo;
import org.alfresco.service.cmr.wiki.WikiService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
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
@@ -91,11 +93,11 @@ public abstract class AbstractWikiWebScript 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;
}
@@ -150,13 +152,9 @@ public abstract class AbstractWikiWebScript 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)
@@ -167,15 +165,18 @@ public abstract class AbstractWikiWebScript extends DeclarativeWebScript
try
{
JSONObject activity = new JSONObject();
activity.put("title", wikiPage.getTitle());
activity.put("page", page + "?title=" + wikiPage.getSystemName());
StringWriter activityJson = new StringWriter();
JSONWriter activity = new JSONWriter(activityJson);
activity.startObject();
activity.writeValue("title", wikiPage.getTitle());
activity.writeValue("page", page + "?title=" + wikiPage.getSystemName());
activity.endObject();
activityService.postActivity(
"org.alfresco.wiki.page-" + event,
site.getShortName(),
WIKI_SERVICE_ACTIVITY_APP_NAME,
activity.toString()
activityJson.toString()
);
}
catch(Exception e)
@@ -251,17 +252,18 @@ public abstract class AbstractWikiWebScript 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());
}
}
@@ -274,22 +276,18 @@ public abstract class AbstractWikiWebScript extends DeclarativeWebScript
}
if(siteName == null && json != null)
{
try
if(json.containsKey("siteid"))
{
if(json.has("siteid"))
{
siteName = json.getString("siteid");
}
else 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("siteId"))
{
siteName = (String)json.get("siteId");
}
else if(json.containsKey("site"))
{
siteName = (String)json.get("site");
}
catch(JSONException e) {}
}
if(siteName == null)
{

View File

@@ -23,7 +23,7 @@ import java.util.Map;
import org.alfresco.service.cmr.site.SiteInfo;
import org.alfresco.service.cmr.wiki.WikiPageInfo;
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

@@ -31,7 +31,7 @@ import org.alfresco.repo.wiki.WikiServiceImpl;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.site.SiteInfo;
import org.alfresco.service.cmr.wiki.WikiPageInfo;
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

@@ -31,7 +31,7 @@ import org.alfresco.repo.wiki.WikiServiceImpl;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.site.SiteInfo;
import org.alfresco.service.cmr.wiki.WikiPageInfo;
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

@@ -26,7 +26,7 @@ import org.alfresco.service.cmr.model.FileExistsException;
import org.alfresco.service.cmr.site.SiteInfo;
import org.alfresco.service.cmr.wiki.WikiPageInfo;
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;
@@ -69,16 +69,8 @@ public class WikiPageMovePost extends AbstractWikiWebScript
// Grab the new Title
String newTitle;
try
{
// The "name" in the JSON is actually the title!
newTitle = json.getString("name");
}
catch(JSONException e)
{
throw new WebScriptException("Invalid JSON: " + e.getMessage());
}
// The "name" in the JSON is actually the title!
String newTitle = (String)json.get("name");
// Have the page re-named, if possible

View File

@@ -30,9 +30,8 @@ import org.alfresco.service.cmr.version.Version;
import org.alfresco.service.cmr.version.VersionService;
import org.alfresco.service.cmr.wiki.WikiPageInfo;
import org.alfresco.service.namespace.QName;
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;
@@ -60,56 +59,47 @@ public class WikiPagePut extends AbstractWikiWebScript
Map<String, Object> model = new HashMap<String, Object>();
// Grab the details of the change
List<String> tags = null;
String currentVersion = null;
boolean forceSave;
String contents;
// Fetch the contents
String contents = (String)json.get("pagecontent");
// Fetch the title, used only when creating
String title;
try
if(json.containsKey("title"))
{
// Fetch the contents
contents = json.getString("pagecontent");
// Fetch the title, used only when creating
if(json.has("title"))
title = (String)json.get("title");
}
else
{
title = pageName;
}
// Fetch the versioning details
boolean forceSave = json.containsKey("forceSave");
String currentVersion = null;
if(json.containsKey("currentVersion"))
{
currentVersion = (String)json.get("currentVersion");
}
// Fetch the tags, if given
List<String> tags = null;
if(json.containsKey("tags"))
{
tags = new ArrayList<String>();
if(json.get("tags").equals(""))
{
title = json.getString("title");
// Empty list given as a string, eg "tags":""
}
else
{
title = pageName;
}
// Fetch the versioning details
forceSave = json.has("forceSave");
if(json.has("currentVersion"))
{
currentVersion = json.getString("currentVersion");
}
// Fetch the tags, if given
if(json.has("tags"))
{
tags = new ArrayList<String>();
if(json.get("tags").equals(""))
// Array of tags
JSONArray tagsA = (JSONArray)json.get("tags");
for(int i=0; i<tagsA.size(); i++)
{
// Empty list given as a string, eg "tags":""
}
else
{
// Array of tags
JSONArray tagsA = json.getJSONArray("tags");
for(int i=0; i<tagsA.length(); i++)
{
tags.add(tagsA.getString(i));
}
tags.add((String)tagsA.get(i));
}
}
}
catch(JSONException e)
{
throw new WebScriptException("Invalid JSON: " + e.getMessage());
}
// Are we creating or editing?
WikiPageInfo page = wikiService.getWikiPage(site.getShortName(), pageName);

View File

@@ -33,7 +33,7 @@ import org.alfresco.service.cmr.version.VersionDoesNotExistException;
import org.alfresco.service.cmr.version.VersionHistory;
import org.alfresco.service.cmr.version.VersionService;
import org.alfresco.service.cmr.wiki.WikiPageInfo;
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;