[VIVO-1320] Convert some org.json usages to Jackson

This commit is contained in:
Graham Triggs 2017-09-15 15:00:54 +01:00
parent 06a9360b43
commit 58cba471a8
16 changed files with 146 additions and 176 deletions

View file

@ -12,9 +12,10 @@ import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;
import org.apache.jena.ontology.OntModel; import org.apache.jena.ontology.OntModel;
import org.apache.jena.rdf.model.Model; import org.apache.jena.rdf.model.Model;
@ -139,7 +140,7 @@ public class MenuManagementEdit extends VitroHttpServlet {
private void sendReorderResponse(String errorMessage, HttpServletResponse resp) { private void sendReorderResponse(String errorMessage, HttpServletResponse resp) {
try{ try{
JSONObject rObj = new JSONObject(); ObjectNode rObj = JsonNodeFactory.instance.objectNode();
resp.setCharacterEncoding("UTF-8"); resp.setCharacterEncoding("UTF-8");
resp.setContentType("application/json;charset=UTF-8"); resp.setContentType("application/json;charset=UTF-8");

View file

@ -11,7 +11,6 @@ import org.apache.jena.rdf.model.RDFNode;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.IndividualTemplateModelBuilder; import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.IndividualTemplateModelBuilder;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import org.apache.jena.query.QuerySolution; import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet; import org.apache.jena.query.ResultSet;
@ -133,8 +132,6 @@ class IndividualResponseBuilder {
if (openSocialManager.isVisible()) { if (openSocialManager.isVisible()) {
body.put("bodyOnload", "my.init();"); body.put("bodyOnload", "my.init();");
} }
} catch (JSONException e) {
log.error("JSONException in doTemplate()", e);
} catch (IOException e) { } catch (IOException e) {
log.error("IOException in doTemplate()", e); log.error("IOException in doTemplate()", e);
} catch (SQLException e) { } catch (SQLException e) {

View file

@ -5,9 +5,10 @@ package edu.cornell.mannlib.vitro.webapp.controller.individuallist;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
import org.json.JSONException; import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONObject; import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder;
@ -26,19 +27,22 @@ public class IndividualJsonWrapper {
addJSONFields = add; addJSONFields = add;
} }
static JSONObject packageIndividualAsJson(VitroRequest vreq, Individual ind) static ObjectNode packageIndividualAsJson(VitroRequest vreq, Individual ind) {
throws JSONException {
// need an unfiltered dao to get firstnames and lastnames // need an unfiltered dao to get firstnames and lastnames
WebappDaoFactory fullWdf = vreq.getUnfilteredWebappDaoFactory(); WebappDaoFactory fullWdf = vreq.getUnfilteredWebappDaoFactory();
JSONObject jo = new JSONObject(); ObjectNode jo = JsonNodeFactory.instance.objectNode();
jo.put("URI", ind.getURI()); jo.put("URI", ind.getURI());
jo.put("label", ind.getRdfsLabel()); jo.put("label", ind.getRdfsLabel());
jo.put("name", ind.getName()); jo.put("name", ind.getName());
jo.put("thumbUrl", ind.getThumbUrl()); jo.put("thumbUrl", ind.getThumbUrl());
jo.put("imageUrl", ind.getImageUrl()); jo.put("imageUrl", ind.getImageUrl());
jo.put("profileUrl", UrlBuilder.getIndividualProfileUrl(ind, vreq)); jo.put("profileUrl", UrlBuilder.getIndividualProfileUrl(ind, vreq));
jo.put("mostSpecificTypes", getMostSpecificTypes(ind, fullWdf)); ArrayNode ao = JsonNodeFactory.instance.arrayNode();
for (String type : getMostSpecificTypes(ind, fullWdf)) {
ao.add(type);
}
jo.put("mostSpecificTypes", ao);
if (addJSONFields != null) { if (addJSONFields != null) {
addJSONFields.add(jo, vreq, ind); addJSONFields.add(jo, vreq, ind);
} }
@ -55,6 +59,6 @@ public class IndividualJsonWrapper {
} }
public interface AddJSONFields { public interface AddJSONFields {
public void add(JSONObject jo, VitroRequest vreq, Individual ind) throws JSONException; public void add(ObjectNode jo, VitroRequest vreq, Individual ind);
} }
} }

View file

@ -8,12 +8,13 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.VClass; import edu.cornell.mannlib.vitro.webapp.beans.VClass;
@ -34,9 +35,10 @@ public class IndividualListResultsUtils {
* Process results related to VClass or vclasses. Handles both single and * Process results related to VClass or vclasses. Handles both single and
* multiple vclasses being sent. * multiple vclasses being sent.
*/ */
public static JSONObject wrapIndividualListResultsInJson(IndividualListResults results, VitroRequest vreq, public static ObjectNode wrapIndividualListResultsInJson(IndividualListResults results, VitroRequest vreq,
boolean multipleVclasses) { boolean multipleVclasses) {
JSONObject rObj = new JSONObject();
ObjectNode rObj = JsonNodeFactory.instance.objectNode();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
dumpParametersFromRequest(vreq); dumpParametersFromRequest(vreq);
@ -91,47 +93,44 @@ public class IndividualListResultsUtils {
return list; return list;
} }
private static JSONObject packageVClassAsJson(VClass vclass) private static ObjectNode packageVClassAsJson(VClass vclass) {
throws JSONException { ObjectNode jvclass = JsonNodeFactory.instance.objectNode();
JSONObject jvclass = new JSONObject();
jvclass.put("URI", vclass.getURI()); jvclass.put("URI", vclass.getURI());
jvclass.put("name", vclass.getName()); jvclass.put("name", vclass.getName());
return jvclass; return jvclass;
} }
private static JSONArray packageLettersAsJson() throws JSONException, private static ArrayNode packageLettersAsJson() throws UnsupportedEncodingException {
UnsupportedEncodingException {
List<String> letters = Controllers.getLetters(); List<String> letters = Controllers.getLetters();
JSONArray jletters = new JSONArray(); ArrayNode jletters = JsonNodeFactory.instance.arrayNode();
for (String s : letters) { for (String s : letters) {
JSONObject jo = new JSONObject(); ObjectNode jo = JsonNodeFactory.instance.objectNode();
jo.put("text", s); jo.put("text", s);
jo.put("param", "alpha=" + URLEncoder.encode(s, "UTF-8")); jo.put("param", "alpha=" + URLEncoder.encode(s, "UTF-8"));
jletters.put(jo); jletters.add(jo);
} }
return jletters; return jletters;
} }
private static JSONArray packagePageRecordsAsJson(List<PageRecord> pages) private static ArrayNode packagePageRecordsAsJson(List<PageRecord> pages) {
throws JSONException { ArrayNode wpages = JsonNodeFactory.instance.arrayNode();
JSONArray wpages = new JSONArray();
for (PageRecord pr : pages) { for (PageRecord pr : pages) {
JSONObject p = new JSONObject(); ObjectNode p = JsonNodeFactory.instance.objectNode();
p.put("text", pr.text); p.put("text", pr.text);
p.put("param", pr.param); p.put("param", pr.param);
p.put("index", pr.index); p.put("index", pr.index);
wpages.put(p); wpages.add(p);
} }
return wpages; return wpages;
} }
private static JSONArray packageIndividualsAsJson(VitroRequest vreq, private static ArrayNode packageIndividualsAsJson(VitroRequest vreq,
List<Individual> inds) throws JSONException { List<Individual> inds) {
log.debug("Number of individuals returned from request: " + inds.size()); log.debug("Number of individuals returned from request: " + inds.size());
JSONArray jInds = new JSONArray(); ArrayNode jInds = JsonNodeFactory.instance.arrayNode();
for (Individual ind : inds) { for (Individual ind : inds) {
jInds.put(IndividualJsonWrapper.packageIndividualAsJson(vreq, ind)); jInds.add(IndividualJsonWrapper.packageIndividualAsJson(vreq, ind));
} }
return jInds; return jInds;
} }

View file

@ -6,9 +6,12 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;
import edu.cornell.mannlib.vitro.webapp.beans.VClass; import edu.cornell.mannlib.vitro.webapp.beans.VClass;
import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup; import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup;
@ -28,8 +31,8 @@ public class GetAllVClasses extends JsonObjectProducer {
} }
@Override @Override
protected JSONObject process() throws Exception { protected ObjectNode process() throws Exception {
JSONObject map = new JSONObject(); ObjectNode map = JsonNodeFactory.instance.objectNode();
//Get all VClassGroups //Get all VClassGroups
List<VClass> vclasses = new ArrayList<VClass>(); List<VClass> vclasses = new ArrayList<VClass>();
VClassGroupsForRequest vcgc = VClassGroupCache.getVClassGroups(vreq); VClassGroupsForRequest vcgc = VClassGroupCache.getVClassGroups(vreq);
@ -43,10 +46,10 @@ public class GetAllVClasses extends JsonObjectProducer {
//Sort vclass by name //Sort vclass by name
Collections.sort(vclasses); Collections.sort(vclasses);
ArrayList<JSONObject> classes = new ArrayList<JSONObject>(vclasses.size()); ArrayNode classes = JsonNodeFactory.instance.arrayNode();
for(VClass vc: vclasses) { for(VClass vc: vclasses) {
JSONObject vcObj = new JSONObject(); ObjectNode vcObj = JsonNodeFactory.instance.objectNode();
vcObj.put("name", vc.getName()); vcObj.put("name", vc.getName());
vcObj.put("URI", vc.getURI()); vcObj.put("URI", vc.getURI());
classes.add(vcObj); classes.add(vcObj);

View file

@ -6,12 +6,11 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.IndividualTemplateModelBuilder; import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.IndividualTemplateModelBuilder;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
@ -37,8 +36,8 @@ public class GetRandomSearchIndividualsByVClass extends GetSearchIndividualsByVC
* Search for individuals by VClass. * Search for individuals by VClass.
*/ */
@Override @Override
protected JSONObject process() throws Exception { protected ObjectNode process() throws Exception {
JSONObject rObj = null; ObjectNode rObj = null;
//This gets the first vclass value and sets that as display type. //This gets the first vclass value and sets that as display type.
List<String> vclassIds = super.getVclassIds(vreq); List<String> vclassIds = super.getVclassIds(vreq);
@ -57,13 +56,13 @@ public class GetRandomSearchIndividualsByVClass extends GetSearchIndividualsByVC
* Look through the return object. For each individual, render the short * Look through the return object. For each individual, render the short
* view and insert the resulting HTML into the object. * view and insert the resulting HTML into the object.
*/ */
private void addShortViewRenderings(JSONObject rObj) throws JSONException { private void addShortViewRenderings(ObjectNode rObj) {
JSONArray individuals = rObj.getJSONArray("individuals"); ArrayNode individuals = (ArrayNode) rObj.get("individuals");
String vclassName = rObj.getJSONObject("vclass").getString("name"); String vclassName = rObj.get("vclass").get("name").asText();
for (int i = 0; i < individuals.length(); i++) { for (int i = 0; i < individuals.size(); i++) {
JSONObject individual = individuals.getJSONObject(i); ObjectNode individual = (ObjectNode) individuals.get(i);
individual.put("shortViewHtml", individual.put("shortViewHtml",
renderShortView(individual.getString("URI"), vclassName)); renderShortView(individual.get("URI").asText(), vclassName));
} }
} }

View file

@ -6,12 +6,11 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.IndividualTemplateModelBuilder; import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.IndividualTemplateModelBuilder;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
@ -38,8 +37,8 @@ public class GetRenderedSearchIndividualsByVClass extends GetSearchIndividualsBy
* information are in the request parameters. * information are in the request parameters.
*/ */
@Override @Override
protected JSONObject process() throws Exception { protected ObjectNode process() throws Exception {
JSONObject rObj = null; ObjectNode rObj = null;
//This gets the first vclass value and sets that as display type //This gets the first vclass value and sets that as display type
List<String> vclassIds = super.getVclassIds(vreq); List<String> vclassIds = super.getVclassIds(vreq);
@ -65,13 +64,13 @@ public class GetRenderedSearchIndividualsByVClass extends GetSearchIndividualsBy
* Look through the return object. For each individual, render the short * Look through the return object. For each individual, render the short
* view and insert the resulting HTML into the object. * view and insert the resulting HTML into the object.
*/ */
private void addShortViewRenderings(JSONObject rObj) throws JSONException { private void addShortViewRenderings(ObjectNode rObj) {
JSONArray individuals = rObj.getJSONArray("individuals"); ArrayNode individuals = (ArrayNode) rObj.get("individuals");
String vclassName = rObj.getJSONObject("vclass").getString("name"); String vclassName = rObj.get("vclass").get("name").asText();
for (int i = 0; i < individuals.length(); i++) { for (int i = 0; i < individuals.size(); i++) {
JSONObject individual = individuals.getJSONObject(i); ObjectNode individual = (ObjectNode) individuals.get(i);
individual.put("shortViewHtml", individual.put("shortViewHtml",
renderShortView(individual.getString("URI"), vclassName)); renderShortView(individual.get("URI").asText(), vclassName));
} }
} }

View file

@ -2,9 +2,10 @@
package edu.cornell.mannlib.vitro.webapp.controller.json; package edu.cornell.mannlib.vitro.webapp.controller.json;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;
import edu.cornell.mannlib.vitro.webapp.beans.VClass; import edu.cornell.mannlib.vitro.webapp.beans.VClass;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
@ -21,7 +22,7 @@ public class GetSearchIndividualsByVClass extends JsonObjectProducer {
} }
@Override @Override
protected JSONObject process() throws Exception { protected ObjectNode process() throws Exception {
VClass vclass = null; VClass vclass = null;
String queryType = (String) vreq.getAttribute("queryType"); String queryType = (String) vreq.getAttribute("queryType");

View file

@ -5,9 +5,9 @@ package edu.cornell.mannlib.vitro.webapp.controller.json;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;
import edu.cornell.mannlib.vitro.webapp.beans.VClass; import edu.cornell.mannlib.vitro.webapp.beans.VClass;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
@ -25,7 +25,7 @@ public class GetSearchIndividualsByVClasses extends JsonObjectProducer {
} }
@Override @Override
protected JSONObject process() throws Exception { protected ObjectNode process() throws Exception {
log.debug("Executing retrieval of individuals by vclasses"); log.debug("Executing retrieval of individuals by vclasses");
VClass vclass=null; VClass vclass=null;
log.debug("Retrieving search individuals by vclasses"); log.debug("Retrieving search individuals by vclasses");

View file

@ -4,9 +4,11 @@ package edu.cornell.mannlib.vitro.webapp.controller.json;
import java.util.ArrayList; import java.util.ArrayList;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;
import edu.cornell.mannlib.vitro.webapp.beans.VClass; import edu.cornell.mannlib.vitro.webapp.beans.VClass;
import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup; import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup;
@ -26,8 +28,8 @@ public class GetVClassesForVClassGroup extends JsonObjectProducer {
} }
@Override @Override
protected JSONObject process() throws Exception { protected ObjectNode process() throws Exception {
JSONObject map = new JSONObject(); ObjectNode map = JsonNodeFactory.instance.objectNode();
String vcgUri = vreq.getParameter("classgroupUri"); String vcgUri = vreq.getParameter("classgroupUri");
if( vcgUri == null ){ if( vcgUri == null ){
throw new Exception("no URI passed for classgroupUri"); throw new Exception("no URI passed for classgroupUri");
@ -39,9 +41,9 @@ public class GetVClassesForVClassGroup extends JsonObjectProducer {
throw new Exception("Could not find vclassgroup: " + vcgUri); throw new Exception("Could not find vclassgroup: " + vcgUri);
} }
ArrayList<JSONObject> classes = new ArrayList<JSONObject>(vcg.size()); ArrayNode classes = JsonNodeFactory.instance.arrayNode();
for( VClass vc : vcg){ for( VClass vc : vcg){
JSONObject vcObj = new JSONObject(); ObjectNode vcObj = JsonNodeFactory.instance.objectNode();
vcObj.put("name", vc.getName()); vcObj.put("name", vc.getName());
vcObj.put("URI", vc.getURI()); vcObj.put("URI", vc.getURI());
vcObj.put("entityCount", vc.getEntityCount()); vcObj.put("entityCount", vc.getEntityCount());

View file

@ -8,10 +8,12 @@ import java.io.Writer;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import org.json.JSONObject;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
@ -42,10 +44,10 @@ public abstract class JsonObjectProducer extends JsonProducer {
* Sub-classes implement this method. Given the request, produce a JSON * Sub-classes implement this method. Given the request, produce a JSON
* object as the result. * object as the result.
*/ */
protected abstract JSONObject process() throws Exception; protected abstract ObjectNode process() throws Exception;
public final void process(HttpServletResponse resp) throws IOException { public final void process(HttpServletResponse resp) throws IOException {
JSONObject jsonObject = null; ObjectNode jsonObject = null;
String errorMessage = ""; String errorMessage = "";
try { try {
@ -57,20 +59,16 @@ public abstract class JsonObjectProducer extends JsonProducer {
} }
if (jsonObject == null) { if (jsonObject == null) {
jsonObject = new JSONObject(); jsonObject = JsonNodeFactory.instance.objectNode();
} }
log.debug("Response to JSON request: " + jsonObject.toString()); log.debug("Response to JSON request: " + jsonObject.toString());
try {
resp.setCharacterEncoding("UTF-8"); resp.setCharacterEncoding("UTF-8");
resp.setContentType("application/json;charset=UTF-8"); resp.setContentType("application/json;charset=UTF-8");
Writer writer = resp.getWriter(); Writer writer = resp.getWriter();
jsonObject.put("errorMessage", errorMessage); jsonObject.put("errorMessage", errorMessage);
writer.write(jsonObject.toString()); writer.write(jsonObject.toString());
} catch (JSONException e) {
log.error(e, e);
}
} }
} }

View file

@ -11,9 +11,9 @@ import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;
import edu.cornell.mannlib.vitro.webapp.beans.DataProperty; import edu.cornell.mannlib.vitro.webapp.beans.DataProperty;
import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.beans.Individual;
@ -82,7 +82,7 @@ public class JsonServlet extends VitroHttpServlet {
} }
public static JSONObject getSearchIndividualsByVClass(String vclassURI, HttpServletRequest req) throws Exception { public static ObjectNode getSearchIndividualsByVClass(String vclassURI, HttpServletRequest req) throws Exception {
List<String> vclassURIs = Collections.singletonList(vclassURI); List<String> vclassURIs = Collections.singletonList(vclassURI);
VitroRequest vreq = new VitroRequest(req); VitroRequest vreq = new VitroRequest(req);
@ -91,7 +91,7 @@ public class JsonServlet extends VitroHttpServlet {
return IndividualListResultsUtils.wrapIndividualListResultsInJson(vcResults, vreq, false); return IndividualListResultsUtils.wrapIndividualListResultsInJson(vcResults, vreq, false);
} }
public static JSONObject getSearchIndividualsByVClasses(List<String> vclassURIs, HttpServletRequest req) throws Exception { public static ObjectNode getSearchIndividualsByVClasses(List<String> vclassURIs, HttpServletRequest req) throws Exception {
VitroRequest vreq = new VitroRequest(req); VitroRequest vreq = new VitroRequest(req);
log.debug("Retrieve search results for vclasses" + vclassURIs.toString()); log.debug("Retrieve search results for vclasses" + vclassURIs.toString());
IndividualListResults vcResults = getSearchVClassIntersectionResults(vclassURIs, vreq); IndividualListResults vcResults = getSearchVClassIntersectionResults(vclassURIs, vreq);
@ -126,7 +126,7 @@ public class JsonServlet extends VitroHttpServlet {
return value; return value;
} }
public static JSONObject getRandomSearchIndividualsByVClass(String vclassURI, HttpServletRequest req) throws Exception { public static ObjectNode getRandomSearchIndividualsByVClass(String vclassURI, HttpServletRequest req) throws Exception {
VitroRequest vreq = new VitroRequest(req); VitroRequest vreq = new VitroRequest(req);
IndividualListResults vcResults = getRandomSearchVClassResults(vclassURI, vreq); IndividualListResults vcResults = getRandomSearchVClassResults(vclassURI, vreq);

View file

@ -28,10 +28,9 @@ import java.util.SortedMap;
import java.util.TreeMap; import java.util.TreeMap;
//import java.util.regex.Pattern; //import java.util.regex.Pattern;
import org.json.JSONArray; import com.fasterxml.jackson.databind.JsonNode;
import org.json.JSONException; import com.fasterxml.jackson.databind.node.ArrayNode;
import org.json.JSONObject; import com.fasterxml.jackson.databind.node.ObjectNode;
import org.json.JSONTokener;
//import org.springframework.extensions.surf.util.ISO8601DateFormat; //import org.springframework.extensions.surf.util.ISO8601DateFormat;
/** /**
@ -49,44 +48,27 @@ public final class JsonToFmModel
// //
// public static boolean autoConvertISO8601 = true; // public static boolean autoConvertISO8601 = true;
/**
* Convert JSON Object string to Freemarker-compatible data model
*
* @param jsonString JSON string
* @return model
* @throws JSONException
*/
public static Map<String, Object> convertJSONObjectToMap(String jsonString) throws JSONException
{
JSONObject jo = new JSONObject(new JSONTokener(jsonString));
return convertJSONObjectToMap(jo);
}
/** /**
* JSONObject is an unordered collection of name/value pairs -&gt; convert to Map (equivalent to Freemarker "hash") * JSONObject is an unordered collection of name/value pairs -&gt; convert to Map (equivalent to Freemarker "hash")
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static Map<String, Object> convertJSONObjectToMap(JSONObject jo) throws JSONException public static Map<String, Object> convertJSONObjectToMap(ObjectNode jo)
{ {
Map<String, Object> model = new HashMap<String, Object>(); Map<String, Object> model = new HashMap<String, Object>();
Iterator<String> itr = (Iterator<String>)jo.keys(); Iterator<String> itr = (Iterator<String>)jo.fieldNames();
while (itr.hasNext()) while (itr.hasNext())
{ {
String key = (String)itr.next(); String key = (String)itr.next();
Object o = jo.get(key); Object o = jo.get(key);
if (o instanceof JSONObject) if (o instanceof ObjectNode)
{ {
model.put(key, convertJSONObjectToMap((JSONObject)o)); model.put(key, convertJSONObjectToMap((ObjectNode)o));
} }
else if (o instanceof JSONArray) else if (o instanceof ArrayNode)
{ {
model.put(key, convertJSONArrayToList((JSONArray)o)); model.put(key, convertJSONArrayToList((ArrayNode)o));
}
else if (o == JSONObject.NULL)
{
model.put(key, null); // note: http://freemarker.org/docs/dgui_template_exp.html#dgui_template_exp_missing
} }
else else
{ {
@ -102,43 +84,24 @@ public final class JsonToFmModel
return model; return model;
} }
/**
* Convert JSON Array string to Freemarker-compatible data model
*
* @param jsonString JSON String
* @return model
* @throws JSONException
*/
public static Map<String, Object> convertJSONArrayToMap(String jsonString) throws JSONException
{
Map<String, Object> model = new HashMap<String, Object>();
JSONArray ja = new JSONArray(new JSONTokener(jsonString));
model.put(ROOT_ARRAY, convertJSONArrayToList(ja));
return model;
}
/** /**
* JSONArray is an ordered sequence of values -&gt; convert to List (equivalent to Freemarker "sequence") * JSONArray is an ordered sequence of values -&gt; convert to List (equivalent to Freemarker "sequence")
*/ */
public static List<Object> convertJSONArrayToList(JSONArray ja) throws JSONException public static List<Object> convertJSONArrayToList(ArrayNode ja)
{ {
List<Object> model = new ArrayList<Object>(); List<Object> model = new ArrayList<Object>();
for (int i = 0; i < ja.length(); i++) for (int i = 0; i < ja.size(); i++)
{ {
Object o = ja.get(i); JsonNode o = ja.get(i);
if (o instanceof JSONArray) if (o instanceof ArrayNode)
{ {
model.add(convertJSONArrayToList((JSONArray)o)); model.add(convertJSONArrayToList((ArrayNode) o));
} }
else if (o instanceof JSONObject) else if (o instanceof ObjectNode)
{ {
model.add(convertJSONObjectToMap((JSONObject)o)); model.add(convertJSONObjectToMap((ObjectNode) o));
}
else if (o == JSONObject.NULL)
{
model.add(null);
} }
else else
{ {

View file

@ -11,11 +11,11 @@ import java.util.Map;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individuallist.ListedIndividualBuilder; import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individuallist.ListedIndividualBuilder;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONObject;
import org.apache.jena.rdf.model.Model; import org.apache.jena.rdf.model.Model;
@ -109,12 +109,12 @@ public class BrowseDataGetter extends DataGetterBase implements DataGetter {
VClass vclass = vreq.getWebappDaoFactory().getVClassDao().getVClassByURI(classUri); VClass vclass = vreq.getWebappDaoFactory().getVClassDao().getVClassByURI(classUri);
map.put("class", new VClassTemplateModel(vclass)); map.put("class", new VClassTemplateModel(vclass));
JSONObject vclassRes = JsonServlet.getSearchIndividualsByVClass(vclass.getURI(), request); ObjectNode vclassRes = JsonServlet.getSearchIndividualsByVClass(vclass.getURI(), request);
map.put("totalCount", JsonToFmModel.convertJSONObjectToMap( (String) vclassRes.get("totalCount") )); map.put("totalCount", JsonToFmModel.convertJSONObjectToMap( (ObjectNode) vclassRes.get("totalCount") ));
map.put("alpha", JsonToFmModel.convertJSONObjectToMap( (String) vclassRes.get("alpha") )); map.put("alpha", JsonToFmModel.convertJSONObjectToMap( (ObjectNode) vclassRes.get("alpha") ));
map.put("individuals", JsonToFmModel.convertJSONArrayToList( (JSONArray) vclassRes.get("individuals") )); map.put("individuals", JsonToFmModel.convertJSONArrayToList( (ArrayNode) vclassRes.get("individuals") ));
map.put("pages", JsonToFmModel.convertJSONArrayToList( (JSONArray) vclassRes.get("pages") )); map.put("pages", JsonToFmModel.convertJSONArrayToList( (ArrayNode) vclassRes.get("pages") ));
map.put("letters", JsonToFmModel.convertJSONArrayToList( (JSONArray) vclassRes.get("letters") )); map.put("letters", JsonToFmModel.convertJSONArrayToList( (ArrayNode) vclassRes.get("letters") ));
return map; return map;
} }
@ -253,10 +253,7 @@ public class BrowseDataGetter extends DataGetterBase implements DataGetter {
/** /**
* For processig of JSONObject * For processig of JSONObject
*/ */
public JSONObject convertToJSON(Map<String, Object> dataMap, VitroRequest vreq) { public ObjectNode convertToJSON(Map<String, Object> dataMap, VitroRequest vreq) {
JSONObject rObj = null; return null;
return rObj;
} }
} }

View file

@ -11,11 +11,11 @@ import java.util.Map;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individuallist.ListedIndividualBuilder; import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individuallist.ListedIndividualBuilder;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONObject;
import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.VClass; import edu.cornell.mannlib.vitro.webapp.beans.VClass;
@ -84,12 +84,12 @@ public class BrowseWidget extends Widget {
VClass vclass = vreq.getWebappDaoFactory().getVClassDao().getVClassByURI(classUri); VClass vclass = vreq.getWebappDaoFactory().getVClassDao().getVClassByURI(classUri);
map.put("class", new VClassTemplateModel(vclass)); map.put("class", new VClassTemplateModel(vclass));
JSONObject vclassRes = JsonServlet.getSearchIndividualsByVClass(vclass.getURI(), request); ObjectNode vclassRes = JsonServlet.getSearchIndividualsByVClass(vclass.getURI(), request);
map.put("totalCount", JsonToFmModel.convertJSONObjectToMap( (String) vclassRes.get("totalCount") )); map.put("totalCount", JsonToFmModel.convertJSONObjectToMap( (ObjectNode) vclassRes.get("totalCount") ));
map.put("alpha", JsonToFmModel.convertJSONObjectToMap( (String) vclassRes.get("alpha") )); map.put("alpha", JsonToFmModel.convertJSONObjectToMap( (ObjectNode) vclassRes.get("alpha") ));
map.put("individuals", JsonToFmModel.convertJSONArrayToList( (JSONArray) vclassRes.get("individuals") )); map.put("individuals", JsonToFmModel.convertJSONArrayToList( (ArrayNode) vclassRes.get("individuals") ));
map.put("pages", JsonToFmModel.convertJSONArrayToList( (JSONArray) vclassRes.get("pages") )); map.put("pages", JsonToFmModel.convertJSONArrayToList( (ArrayNode) vclassRes.get("pages") ));
map.put("letters", JsonToFmModel.convertJSONArrayToList( (JSONArray) vclassRes.get("letters") )); map.put("letters", JsonToFmModel.convertJSONArrayToList( (ArrayNode) vclassRes.get("letters") ));
return map; return map;
} }

View file

@ -15,11 +15,12 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.dbcp.BasicDataSource; import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import org.json.JSONObject;
import edu.cornell.mannlib.vedit.beans.LoginStatusBean; import edu.cornell.mannlib.vedit.beans.LoginStatusBean;
import edu.cornell.mannlib.vitro.webapp.auth.identifier.RequestIdentifiers; import edu.cornell.mannlib.vitro.webapp.auth.identifier.RequestIdentifiers;
@ -189,20 +190,26 @@ public class OpenSocialManager {
// JSON Helper Functions // JSON Helper Functions
public static String buildJSONPersonIds(List<String> personIds, public static String buildJSONPersonIds(List<String> personIds,
String message) throws JSONException { String message) {
JSONObject json = new JSONObject(); ObjectMapper mapper = new ObjectMapper();
ObjectNode json = mapper.createObjectNode();
json.put("message", message); json.put("message", message);
json.put("personIds", personIds);
ArrayNode persons = mapper.createArrayNode();
for (String personId : personIds) {
persons.add(personId);
}
json.put("personIds", persons);
return json.toString(); return json.toString();
} }
public static String buildJSONPersonIds(String personId, String message) throws JSONException { public static String buildJSONPersonIds(String personId, String message) {
List<String> personIds = new ArrayList<String>(); List<String> personIds = new ArrayList<String>();
personIds.add(personId); personIds.add(personId);
return buildJSONPersonIds(personIds, message); return buildJSONPersonIds(personIds, message);
} }
public static String buildJSONPersonIds(Individual ind, String message) throws JSONException { public static String buildJSONPersonIds(Individual ind, String message) {
List<String> personIds = new ArrayList<String>(); List<String> personIds = new ArrayList<String>();
personIds.add(ind.getURI()); personIds.add(ind.getURI());
return buildJSONPersonIds(personIds, message); return buildJSONPersonIds(personIds, message);