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

This commit is contained in:
Graham Triggs 2017-09-15 16:28:24 +01:00
parent 826fb9c570
commit 5175ef9c50
4 changed files with 47 additions and 58 deletions

View file

@ -11,11 +11,11 @@ import java.util.ListIterator;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
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.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;
@ -50,7 +50,7 @@ public class GetEntitiesByVClass extends JsonArrayProducer {
} }
@Override @Override
protected JSONArray process() throws ServletException { protected ArrayNode process() throws ServletException {
log.debug("in getEntitiesByVClass()"); log.debug("in getEntitiesByVClass()");
String vclassURI = vreq.getParameter("vclassURI"); String vclassURI = vreq.getParameter("vclassURI");
WebappDaoFactory daos = vreq.getUnfilteredWebappDaoFactory(); WebappDaoFactory daos = vreq.getUnfilteredWebappDaoFactory();
@ -96,26 +96,22 @@ public class GetEntitiesByVClass extends JsonArrayProducer {
//put all the entities on the JSON array //put all the entities on the JSON array
JSONArray ja = individualsToJson( entsToReturn ); ArrayNode ja = individualsToJson( entsToReturn );
//put the responseGroup number on the end of the JSON array //put the responseGroup number on the end of the JSON array
if( more ){ if( more ){
try{ ObjectNode obj = JsonNodeFactory.instance.objectNode();
JSONObject obj = new JSONObject(); obj.put("resultGroup", "true");
obj.put("resultGroup", "true"); obj.put("size", count);
obj.put("size", count); obj.put("total", numberOfEntsInVClass);
obj.put("total", numberOfEntsInVClass);
StringBuffer nextUrlStr = vreq.getRequestURL(); StringBuffer nextUrlStr = vreq.getRequestURL();
nextUrlStr.append("?") nextUrlStr.append("?")
.append("getEntitiesByVClass").append( "=1&" ) .append("getEntitiesByVClass").append( "=1&" )
.append("resultKey=").append( requestHash ); .append("resultKey=").append( requestHash );
obj.put("nextUrl", nextUrlStr.toString()); obj.put("nextUrl", nextUrlStr.toString());
ja.put(obj); ja.add(obj);
}catch(JSONException je ){
throw new ServletException("unable to create continuation as JSON: " + je.getMessage());
}
} }
log.debug("done with getEntitiesByVClass()"); log.debug("done with getEntitiesByVClass()");

View file

@ -11,11 +11,11 @@ import java.util.ListIterator;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
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.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;
@ -32,7 +32,7 @@ public class GetEntitiesByVClassContinuation extends JsonArrayProducer {
} }
@Override @Override
protected JSONArray process() throws ServletException { protected ArrayNode process() throws ServletException {
log.debug("in getEntitiesByVClassContinuation()"); log.debug("in getEntitiesByVClassContinuation()");
String resKey = vreq.getParameter("resultKey"); String resKey = vreq.getParameter("resultKey");
if( resKey == null ) if( resKey == null )
@ -68,25 +68,21 @@ public class GetEntitiesByVClassContinuation extends JsonArrayProducer {
} }
//put all the entities on the JSON array //put all the entities on the JSON array
JSONArray ja = individualsToJson( entsToReturn ); ArrayNode ja = individualsToJson( entsToReturn );
//put the responseGroup number on the end of the JSON array //put the responseGroup number on the end of the JSON array
if( more ){ if( more ){
try{ ObjectNode obj = JsonNodeFactory.instance.objectNode();
JSONObject obj = new JSONObject(); obj.put("resultGroup", "true");
obj.put("resultGroup", "true"); obj.put("size", count);
obj.put("size", count);
StringBuffer nextUrlStr = vreq.getRequestURL(); StringBuffer nextUrlStr = vreq.getRequestURL();
nextUrlStr.append("?") nextUrlStr.append("?")
.append("getEntitiesByVClass").append( "=1&" ) .append("getEntitiesByVClass").append( "=1&" )
.append("resultKey=").append( resKey ); .append("resultKey=").append( resKey );
obj.put("nextUrl", nextUrlStr.toString()); obj.put("nextUrl", nextUrlStr.toString());
ja.put(obj); ja.add(obj);
}catch(JSONException je ){
throw new ServletException(je.getMessage());
}
} }
log.debug("done with getEntitiesByVClassContinuation()"); log.debug("done with getEntitiesByVClassContinuation()");
return ja; return ja;

View file

@ -8,9 +8,10 @@ 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.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
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 edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
@ -35,10 +36,10 @@ public abstract class JsonArrayProducer 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 JSONArray process() throws Exception; protected abstract ArrayNode process() throws Exception;
public final void process(HttpServletResponse resp) throws IOException { public final void process(HttpServletResponse resp) throws IOException {
JSONArray jsonArray = null; ArrayNode jsonArray = null;
try { try {
jsonArray = process(); jsonArray = process();
} catch (Exception e) { } catch (Exception e) {
@ -47,7 +48,7 @@ public abstract class JsonArrayProducer extends JsonProducer {
} }
if (jsonArray == null) { if (jsonArray == null) {
jsonArray = new JSONArray(); jsonArray = JsonNodeFactory.instance.arrayNode();
} }
log.debug("Response to JSON request: " + jsonArray.toString()); log.debug("Response to JSON request: " + jsonArray.toString());

View file

@ -7,12 +7,12 @@ import java.util.List;
import javax.servlet.ServletException; import javax.servlet.ServletException;
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.StringUtils; import org.apache.commons.lang3.StringUtils;
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;
@ -27,19 +27,15 @@ public abstract class JsonProducer {
/** /**
* Process a list of Individuals into a JSON array that holds the Names and URIs. * Process a list of Individuals into a JSON array that holds the Names and URIs.
*/ */
protected JSONArray individualsToJson(List<Individual> individuals) throws ServletException { protected ArrayNode individualsToJson(List<Individual> individuals) throws ServletException {
try{ ArrayNode ja = JsonNodeFactory.instance.arrayNode();
JSONArray ja = new JSONArray(); for (Individual ent: individuals) {
for (Individual ent: individuals) { ObjectNode entJ = JsonNodeFactory.instance.objectNode();
JSONObject entJ = new JSONObject(); entJ.put("name", ent.getName());
entJ.put("name", ent.getName()); entJ.put("URI", ent.getURI());
entJ.put("URI", ent.getURI()); ja.add( entJ );
ja.put( entJ ); }
} return ja;
return ja;
}catch(JSONException ex){
throw new ServletException("could not convert list of Individuals into JSON: " + ex);
}
} }
/** /**