[VIVO-1320] Convert some org.json usages to Jackson
This commit is contained in:
parent
916640ef43
commit
bf450aca21
6 changed files with 195 additions and 178 deletions
|
@ -5,6 +5,7 @@ package edu.cornell.mannlib.vitro.webapp.controller.grefine;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
@ -14,11 +15,13 @@ 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.ObjectMapper;
|
||||||
|
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.utils.json.JacksonUtils;
|
||||||
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.config.ConfigurationProperties;
|
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
|
||||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet;
|
import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet;
|
||||||
|
@ -57,7 +60,7 @@ public class GrefineMqlreadServlet extends VitroHttpServlet {
|
||||||
try {
|
try {
|
||||||
if (vreq.getParameter("query") != null) {
|
if (vreq.getParameter("query") != null) {
|
||||||
|
|
||||||
JSONObject qJson = getResult(vreq, req, resp);
|
ObjectNode qJson = getResult(vreq, req, resp);
|
||||||
log.debug("result: " + qJson.toString());
|
log.debug("result: " + qJson.toString());
|
||||||
String responseStr = (vreq.getParameter("callback") == null) ? qJson
|
String responseStr = (vreq.getParameter("callback") == null) ? qJson
|
||||||
.toString() : vreq.getParameter("callback") + "("
|
.toString() : vreq.getParameter("callback") + "("
|
||||||
|
@ -72,55 +75,50 @@ public class GrefineMqlreadServlet extends VitroHttpServlet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private JSONObject getResult(VitroRequest vreq, HttpServletRequest req,
|
private ObjectNode getResult(VitroRequest vreq, HttpServletRequest req,
|
||||||
HttpServletResponse resp) throws ServletException {
|
HttpServletResponse resp) throws ServletException {
|
||||||
|
|
||||||
JSONObject resultAllJson = new JSONObject();
|
ObjectNode resultAllJson = JsonNodeFactory.instance.objectNode();
|
||||||
|
|
||||||
try {
|
|
||||||
// parse query
|
// parse query
|
||||||
ArrayList<String> subjectUriList = new ArrayList<String>();
|
ArrayList<String> subjectUriList = new ArrayList<String>();
|
||||||
Map<String, JSONArray> propertyUriMap = new HashMap<String, JSONArray>();
|
Map<String, ArrayNode> propertyUriMap = new HashMap<String, ArrayNode>();
|
||||||
String query = vreq.getParameter("query");
|
String query = vreq.getParameter("query");
|
||||||
parseQuery(query, subjectUriList, propertyUriMap);
|
parseQuery(query, subjectUriList, propertyUriMap);
|
||||||
|
|
||||||
// run SPARQL query to get the results
|
// run SPARQL query to get the results
|
||||||
JSONArray resultAllJsonArr = new JSONArray();
|
ArrayNode resultAllJsonArr = JsonNodeFactory.instance.arrayNode();
|
||||||
DataPropertyStatementDao dpsDao = vreq.getUnfilteredWebappDaoFactory().getDataPropertyStatementDao();
|
DataPropertyStatementDao dpsDao = vreq.getUnfilteredWebappDaoFactory().getDataPropertyStatementDao();
|
||||||
for (String subjectUri: subjectUriList) {
|
for (String subjectUri: subjectUriList) {
|
||||||
JSONObject subjectPropertyResultJson = new JSONObject();
|
ObjectNode subjectPropertyResultJson = JsonNodeFactory.instance.objectNode();
|
||||||
subjectPropertyResultJson.put("id", subjectUri);
|
subjectPropertyResultJson.put("id", subjectUri);
|
||||||
for (Map.Entry<String, JSONArray> entry : propertyUriMap.entrySet()) {
|
for (Map.Entry<String, ArrayNode> entry : propertyUriMap.entrySet()) {
|
||||||
int limit = 200; // default
|
int limit = 200; // default
|
||||||
String propertyUri = entry.getKey();
|
String propertyUri = entry.getKey();
|
||||||
JSONArray propertyUriOptions = entry.getValue();
|
ArrayNode propertyUriOptions = entry.getValue();
|
||||||
for (int i=0; i<propertyUriOptions.length(); i++) {
|
for (int i=0; i<propertyUriOptions.size(); i++) {
|
||||||
JSONObject propertyUriOption = (JSONObject)propertyUriOptions.get(i);
|
ObjectNode propertyUriOption = (ObjectNode) propertyUriOptions.get(i);
|
||||||
limit = ((Integer)propertyUriOption.get("limit")).intValue();
|
limit = propertyUriOption.get("limit").asInt();
|
||||||
}
|
}
|
||||||
List<Literal> literals = dpsDao.getDataPropertyValuesForIndividualByProperty(subjectUri, propertyUri);
|
List<Literal> literals = dpsDao.getDataPropertyValuesForIndividualByProperty(subjectUri, propertyUri);
|
||||||
// Make sure the subject has a value for this property
|
// Make sure the subject has a value for this property
|
||||||
if (literals.size() > 0) {
|
if (literals.size() > 0) {
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
JSONArray valueJsonArr = new JSONArray();
|
ArrayNode valueJsonArr = JsonNodeFactory.instance.arrayNode();
|
||||||
for (Literal literal: literals) {
|
for (Literal literal: literals) {
|
||||||
if (counter <= limit) {
|
if (counter <= limit) {
|
||||||
String value = literal.getLexicalForm();
|
String value = literal.getLexicalForm();
|
||||||
valueJsonArr.put(value);
|
valueJsonArr.add(value);
|
||||||
}
|
}
|
||||||
counter++;
|
counter++;
|
||||||
}
|
}
|
||||||
subjectPropertyResultJson.put(propertyUri, valueJsonArr);
|
subjectPropertyResultJson.put(propertyUri, valueJsonArr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
resultAllJsonArr.put(subjectPropertyResultJson);
|
resultAllJsonArr.add(subjectPropertyResultJson);
|
||||||
}
|
}
|
||||||
resultAllJson.put("result", resultAllJsonArr);
|
resultAllJson.put("result", resultAllJsonArr);
|
||||||
|
|
||||||
} catch (JSONException e) {
|
|
||||||
log.error("GrefineMqlreadServlet getResult JSONException: " + e);
|
|
||||||
}
|
|
||||||
|
|
||||||
// System.out.println(resultAllJson);
|
// System.out.println(resultAllJson);
|
||||||
return resultAllJson;
|
return resultAllJson;
|
||||||
}
|
}
|
||||||
|
@ -131,31 +129,32 @@ public class GrefineMqlreadServlet extends VitroHttpServlet {
|
||||||
* @param subjectUriList Subject URIs
|
* @param subjectUriList Subject URIs
|
||||||
* @param propertyUriMap Property maps
|
* @param propertyUriMap Property maps
|
||||||
*/
|
*/
|
||||||
private void parseQuery(String query, ArrayList<String> subjectUriList, Map<String, JSONArray> propertyUriMap) {
|
private void parseQuery(String query, ArrayList<String> subjectUriList, Map<String, ArrayNode> propertyUriMap) {
|
||||||
try {
|
try {
|
||||||
JSONObject rawJson = new JSONObject(query);
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
JSONArray qJsonArr = rawJson.getJSONArray("query");
|
ObjectNode rawJson = (ObjectNode)mapper.readTree(query);
|
||||||
for (int i=0; i<qJsonArr.length(); i++) {
|
ArrayNode qJsonArr = (ArrayNode)rawJson.get("query");
|
||||||
|
for (int i=0; i<qJsonArr.size(); i++) {
|
||||||
Object obj = qJsonArr.get(i);
|
Object obj = qJsonArr.get(i);
|
||||||
|
|
||||||
if (obj instanceof JSONObject) {
|
if (obj instanceof ObjectNode) {
|
||||||
JSONObject jsonObj = (JSONObject)obj;
|
ObjectNode jsonObj = (ObjectNode) obj;
|
||||||
JSONArray jsonObjNames = jsonObj.names();
|
Iterator<String> jsonObjNames = jsonObj.fieldNames();
|
||||||
for (int j=0; j<jsonObjNames.length(); j++) {
|
while (jsonObjNames.hasNext()) {
|
||||||
String objName = (String)jsonObjNames.get(j);
|
String objName = (String)jsonObjNames.next();
|
||||||
if (objName.contains("http://")) { // most likely this is a propertyUri
|
if (objName.contains("http://")) { // most likely this is a propertyUri
|
||||||
// e.g. http://weill.cornell.edu/vivo/ontology/wcmc#cwid
|
// e.g. http://weill.cornell.edu/vivo/ontology/wcmc#cwid
|
||||||
Object propertyUriObj = jsonObj.get(objName);
|
Object propertyUriObj = jsonObj.get(objName);
|
||||||
if (propertyUriObj instanceof JSONArray) {
|
if (propertyUriObj instanceof ArrayNode) {
|
||||||
propertyUriMap.put(objName, (JSONArray)propertyUriObj);
|
propertyUriMap.put(objName, (ArrayNode) propertyUriObj);
|
||||||
}
|
}
|
||||||
} else if ("id".equals(objName)) { // id
|
} else if ("id".equals(objName)) { // id
|
||||||
Object idObj = jsonObj.get(objName); // TODO: This is a String object but not sure what it is for
|
Object idObj = jsonObj.get(objName); // TODO: This is a String object but not sure what it is for
|
||||||
} else if ("id|=".equals(objName)) { // list of subject uri
|
} else if ("id|=".equals(objName)) { // list of subject uri
|
||||||
Object subjectUriObj = jsonObj.get(objName);
|
Object subjectUriObj = jsonObj.get(objName);
|
||||||
if (subjectUriObj instanceof JSONArray) {
|
if (subjectUriObj instanceof ArrayNode) {
|
||||||
JSONArray subjectUriUriArr = (JSONArray)subjectUriObj;
|
ArrayNode subjectUriUriArr = (ArrayNode) subjectUriObj;
|
||||||
for (int k=0; k<subjectUriUriArr.length(); k++) {
|
for (int k=0; k<subjectUriUriArr.size(); k++) {
|
||||||
// e.g. http://vivo.med.cornell.edu/individual/cwid-jsd2002
|
// e.g. http://vivo.med.cornell.edu/individual/cwid-jsd2002
|
||||||
Object subjectUriUriObj = subjectUriUriArr.get(k);
|
Object subjectUriUriObj = subjectUriUriArr.get(k);
|
||||||
if (subjectUriUriObj instanceof String) {
|
if (subjectUriUriObj instanceof String) {
|
||||||
|
@ -167,7 +166,7 @@ public class GrefineMqlreadServlet extends VitroHttpServlet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (JSONException e) {
|
} catch (IOException e) {
|
||||||
log.error("GrefineMqlreadServlet parseQuery JSONException: " + e);
|
log.error("GrefineMqlreadServlet parseQuery JSONException: " + e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,12 +18,12 @@ 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.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.JSONObject;
|
|
||||||
|
|
||||||
import org.apache.jena.vocabulary.OWL;
|
import org.apache.jena.vocabulary.OWL;
|
||||||
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.beans.DataProperty;
|
import edu.cornell.mannlib.vitro.webapp.beans.DataProperty;
|
||||||
|
@ -81,8 +81,8 @@ public class GrefinePropertyListServlet extends VitroHttpServlet {
|
||||||
|
|
||||||
|
|
||||||
// Construct json String
|
// Construct json String
|
||||||
JSONObject completeJson = new JSONObject();
|
ObjectNode completeJson = JsonNodeFactory.instance.objectNode();
|
||||||
JSONArray propertiesJsonArr = new JSONArray();
|
ArrayNode propertiesJsonArr = JsonNodeFactory.instance.arrayNode();
|
||||||
if (classPropertiesMap.size() > 0) {
|
if (classPropertiesMap.size() > 0) {
|
||||||
for (Iterator<VClass> iter = classPropertiesMap.keySet().iterator(); iter.hasNext();) { // add results to schema
|
for (Iterator<VClass> iter = classPropertiesMap.keySet().iterator(); iter.hasNext();) { // add results to schema
|
||||||
VClass vc = (VClass) iter.next();
|
VClass vc = (VClass) iter.next();
|
||||||
|
@ -95,24 +95,24 @@ public class GrefinePropertyListServlet extends VitroHttpServlet {
|
||||||
//System.out.println("--- uri: " + prop.getURI());
|
//System.out.println("--- uri: " + prop.getURI());
|
||||||
//System.out.println("--- name: " + nameStr);
|
//System.out.println("--- name: " + nameStr);
|
||||||
// top level
|
// top level
|
||||||
JSONObject propertiesItemJson = new JSONObject();
|
ObjectNode propertiesItemJson = JsonNodeFactory.instance.objectNode();
|
||||||
JSONObject rootSchemaJson = new JSONObject();
|
ObjectNode rootSchemaJson = JsonNodeFactory.instance.objectNode();
|
||||||
rootSchemaJson.put("id", vc.getURI());
|
rootSchemaJson.put("id", vc.getURI());
|
||||||
rootSchemaJson.put("name", vc.getName());
|
rootSchemaJson.put("name", vc.getName());
|
||||||
rootSchemaJson.put("alias", new JSONArray());
|
rootSchemaJson.put("alias", JsonNodeFactory.instance.arrayNode());
|
||||||
propertiesItemJson.put("schema", rootSchemaJson);
|
propertiesItemJson.put("schema", rootSchemaJson);
|
||||||
// second level
|
// second level
|
||||||
propertiesItemJson.put("id", prop.getURI());
|
propertiesItemJson.put("id", prop.getURI());
|
||||||
propertiesItemJson.put("name", nameStr);
|
propertiesItemJson.put("name", nameStr);
|
||||||
propertiesItemJson.put("alias", new JSONArray());
|
propertiesItemJson.put("alias", JsonNodeFactory.instance.arrayNode());
|
||||||
|
|
||||||
JSONObject expectsJson = new JSONObject();
|
ObjectNode expectsJson = JsonNodeFactory.instance.objectNode();
|
||||||
expectsJson.put("id", prop.getURI());
|
expectsJson.put("id", prop.getURI());
|
||||||
expectsJson.put("name", nameStr);
|
expectsJson.put("name", nameStr);
|
||||||
expectsJson.put("alias", new JSONArray());
|
expectsJson.put("alias", JsonNodeFactory.instance.arrayNode());
|
||||||
propertiesItemJson.put("expects", expectsJson);
|
propertiesItemJson.put("expects", expectsJson);
|
||||||
|
|
||||||
propertiesJsonArr.put(propertiesItemJson);
|
propertiesJsonArr.add(propertiesItemJson);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -154,30 +154,30 @@ public class GrefinePropertyListServlet extends VitroHttpServlet {
|
||||||
for (DataProperty prop: vcProps) {
|
for (DataProperty prop: vcProps) {
|
||||||
String nameStr = prop.getPublicName()==null ? prop.getName()==null ? null : prop.getName() : prop.getPublicName();
|
String nameStr = prop.getPublicName()==null ? prop.getName()==null ? null : prop.getName() : prop.getPublicName();
|
||||||
// top level
|
// top level
|
||||||
JSONObject propertiesItemJson = new JSONObject();
|
ObjectNode propertiesItemJson = JsonNodeFactory.instance.objectNode();
|
||||||
|
|
||||||
JSONObject rootSchemaJson = new JSONObject();
|
ObjectNode rootSchemaJson = JsonNodeFactory.instance.objectNode();
|
||||||
rootSchemaJson.put("id", topClass.getURI());
|
rootSchemaJson.put("id", topClass.getURI());
|
||||||
rootSchemaJson.put("name", topClass.getName());
|
rootSchemaJson.put("name", topClass.getName());
|
||||||
rootSchemaJson.put("alias", new JSONArray());
|
rootSchemaJson.put("alias", JsonNodeFactory.instance.arrayNode());
|
||||||
propertiesItemJson.put("schema", rootSchemaJson);
|
propertiesItemJson.put("schema", rootSchemaJson);
|
||||||
|
|
||||||
// second level
|
// second level
|
||||||
propertiesItemJson.put("id", vc.getURI());
|
propertiesItemJson.put("id", vc.getURI());
|
||||||
propertiesItemJson.put("name", vc.getName());
|
propertiesItemJson.put("name", vc.getName());
|
||||||
propertiesItemJson.put("alias", new JSONArray());
|
propertiesItemJson.put("alias", JsonNodeFactory.instance.arrayNode());
|
||||||
|
|
||||||
propertiesItemJson.put("id2", prop.getURI());
|
propertiesItemJson.put("id2", prop.getURI());
|
||||||
propertiesItemJson.put("name2", nameStr);
|
propertiesItemJson.put("name2", nameStr);
|
||||||
propertiesItemJson.put("alias2", new JSONArray());
|
propertiesItemJson.put("alias2", JsonNodeFactory.instance.arrayNode());
|
||||||
|
|
||||||
JSONObject expectsJson = new JSONObject();
|
ObjectNode expectsJson = JsonNodeFactory.instance.objectNode();
|
||||||
expectsJson.put("id", prop.getURI());
|
expectsJson.put("id", prop.getURI());
|
||||||
expectsJson.put("name", nameStr);
|
expectsJson.put("name", nameStr);
|
||||||
expectsJson.put("alias", new JSONArray());
|
expectsJson.put("alias", JsonNodeFactory.instance.arrayNode());
|
||||||
propertiesItemJson.put("expects", expectsJson);
|
propertiesItemJson.put("expects", expectsJson);
|
||||||
|
|
||||||
propertiesJsonArr.put(propertiesItemJson);
|
propertiesJsonArr.add(propertiesItemJson);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,12 +16,15 @@ 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.JsonNode;
|
||||||
|
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 edu.cornell.mannlib.vitro.webapp.utils.json.JacksonUtils;
|
||||||
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.application.ApplicationUtils;
|
import edu.cornell.mannlib.vitro.webapp.application.ApplicationUtils;
|
||||||
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
|
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
|
||||||
|
@ -45,6 +48,8 @@ import edu.cornell.mannlib.vitro.webapp.search.VitroSearchTermNames;
|
||||||
@WebServlet(name = "JSON Reconcile Service", urlPatterns = {"/reconcile"} )
|
@WebServlet(name = "JSON Reconcile Service", urlPatterns = {"/reconcile"} )
|
||||||
public class JSONReconcileServlet extends VitroHttpServlet {
|
public class JSONReconcileServlet extends VitroHttpServlet {
|
||||||
|
|
||||||
|
private static final String INTERNAL_QUERY_NAME = "A0B1C2";
|
||||||
|
|
||||||
private static final String PARAM_QUERY = "term";
|
private static final String PARAM_QUERY = "term";
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
private static String QUERY_PARAMETER_NAME = "term";
|
private static String QUERY_PARAMETER_NAME = "term";
|
||||||
|
@ -70,7 +75,7 @@ public class JSONReconcileServlet extends VitroHttpServlet {
|
||||||
try {
|
try {
|
||||||
if (vreq.getParameter("query") != null
|
if (vreq.getParameter("query") != null
|
||||||
|| vreq.getParameter("queries") != null) {
|
|| vreq.getParameter("queries") != null) {
|
||||||
JSONObject qJson = getResult(vreq, req, resp);
|
ObjectNode qJson = getResult(vreq, req, resp);
|
||||||
log.debug("result: " + qJson.toString());
|
log.debug("result: " + qJson.toString());
|
||||||
String responseStr = (vreq.getParameter("callback") == null) ? qJson
|
String responseStr = (vreq.getParameter("callback") == null) ? qJson
|
||||||
.toString() : vreq.getParameter("callback") + "("
|
.toString() : vreq.getParameter("callback") + "("
|
||||||
|
@ -89,7 +94,7 @@ public class JSONReconcileServlet extends VitroHttpServlet {
|
||||||
}
|
}
|
||||||
defaultTypeList = ConfigurationProperties.getBean(req).getProperty("Vitro.reconcile.defaultTypeList");
|
defaultTypeList = ConfigurationProperties.getBean(req).getProperty("Vitro.reconcile.defaultTypeList");
|
||||||
serverName = req.getServerName();
|
serverName = req.getServerName();
|
||||||
JSONObject metaJson = getMetadata(req, resp, defaultNamespace, defaultTypeList, serverName, serverPort);
|
ObjectNode metaJson = getMetadata(req, resp, defaultNamespace, defaultTypeList, serverName, serverPort);
|
||||||
String callbackStr = (vreq.getParameter("callback") == null) ? ""
|
String callbackStr = (vreq.getParameter("callback") == null) ? ""
|
||||||
: vreq.getParameter("callback");
|
: vreq.getParameter("callback");
|
||||||
ServletOutputStream out = resp.getOutputStream();
|
ServletOutputStream out = resp.getOutputStream();
|
||||||
|
@ -100,16 +105,21 @@ public class JSONReconcileServlet extends VitroHttpServlet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private JSONObject getResult(VitroRequest vreq, HttpServletRequest req,
|
private ObjectNode getResult(VitroRequest vreq, HttpServletRequest req,
|
||||||
HttpServletResponse resp) throws ServletException {
|
HttpServletResponse resp) throws ServletException {
|
||||||
|
|
||||||
HashMap<String, JSONObject> searchWithTypeMap = new HashMap<String, JSONObject>();
|
HashMap<String, JsonNode> searchWithTypeMap = new HashMap<String, JsonNode>();
|
||||||
HashMap<String, JSONObject> searchNoTypeMap = new HashMap<String, JSONObject>();
|
HashMap<String, JsonNode> searchNoTypeMap = new HashMap<String, JsonNode>();
|
||||||
ArrayList<String> queries = new ArrayList<String>();
|
ArrayList<String> queries = new ArrayList<String>();
|
||||||
Object qObj = vreq.getParameter("queries");
|
Object qObj = vreq.getParameter("queries");
|
||||||
|
|
||||||
if (qObj == null) {
|
if (qObj == null) {
|
||||||
qObj = vreq.getParameter("query");
|
qObj = vreq.getParameter("query");
|
||||||
|
if (qObj instanceof String) {
|
||||||
|
if (!((String)qObj).contains("{")) {
|
||||||
|
qObj = "{ \"query\" : \"" + qObj + "\" }";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (qObj != null && qObj instanceof String) {
|
if (qObj != null && qObj instanceof String) {
|
||||||
|
@ -126,21 +136,21 @@ public class JSONReconcileServlet extends VitroHttpServlet {
|
||||||
try {
|
try {
|
||||||
for (int i = 0; i < queries.size(); i++) {
|
for (int i = 0; i < queries.size(); i++) {
|
||||||
String queryStr = queries.get(i);
|
String queryStr = queries.get(i);
|
||||||
JSONObject json = new JSONObject(queryStr);
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
JsonNode json = mapper.readTree(queryStr);
|
||||||
|
|
||||||
if (json.has("query")) { // single query
|
if (json.has("query")) { // single query
|
||||||
if (json.has("type")) {
|
if (json.has("type")) {
|
||||||
searchWithTypeMap.put("query", json);
|
searchWithTypeMap.put(INTERNAL_QUERY_NAME, json);
|
||||||
} else {
|
} else {
|
||||||
// user did not specify a type
|
// user did not specify a type
|
||||||
searchNoTypeMap.put("query", json);
|
searchNoTypeMap.put(INTERNAL_QUERY_NAME, json);
|
||||||
}
|
}
|
||||||
} else { // multiple queries
|
} else { // multiple queries
|
||||||
for (Iterator<String> iter = json.keys(); iter.hasNext();) {
|
for (Iterator<String> iter = json.fieldNames(); iter.hasNext();) {
|
||||||
ArrayList<JSONObject> jsonList = new ArrayList<JSONObject>();
|
ArrayList<JsonNode> jsonList = new ArrayList<JsonNode>();
|
||||||
String key = iter.next();
|
String key = iter.next();
|
||||||
Object obj = json.get(key);
|
JsonNode jsonLvl2 = json.get(key);
|
||||||
JSONObject jsonLvl2 = (JSONObject) obj;
|
|
||||||
if (jsonLvl2.has("query")) {
|
if (jsonLvl2.has("query")) {
|
||||||
if (jsonLvl2.has("type")) {
|
if (jsonLvl2.has("type")) {
|
||||||
searchWithTypeMap.put(key, jsonLvl2);
|
searchWithTypeMap.put(key, jsonLvl2);
|
||||||
|
@ -152,14 +162,14 @@ public class JSONReconcileServlet extends VitroHttpServlet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (JSONException ex) {
|
} catch (IOException ex) {
|
||||||
log.error("JSONException: " + ex);
|
log.error("JSONException: " + ex);
|
||||||
throw new ServletException("JSONReconcileServlet JSONException: "
|
throw new ServletException("JSONReconcileServlet JSONException: "
|
||||||
+ ex);
|
+ ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run index search
|
// Run index search
|
||||||
JSONObject qJson = null;
|
ObjectNode qJson = null;
|
||||||
if (searchWithTypeMap.size() > 0) {
|
if (searchWithTypeMap.size() > 0) {
|
||||||
qJson = runSearch(searchWithTypeMap);
|
qJson = runSearch(searchWithTypeMap);
|
||||||
} else {
|
} else {
|
||||||
|
@ -176,19 +186,18 @@ public class JSONReconcileServlet extends VitroHttpServlet {
|
||||||
*
|
*
|
||||||
* @param req Servlet Request
|
* @param req Servlet Request
|
||||||
* @param resp Servlet Response
|
* @param resp Servlet Response
|
||||||
* @throws ServletException
|
|
||||||
*/
|
*/
|
||||||
protected JSONObject getMetadata(HttpServletRequest req, HttpServletResponse resp, String defaultNamespace,
|
protected ObjectNode getMetadata(HttpServletRequest req, HttpServletResponse resp, String defaultNamespace,
|
||||||
String defaultTypeList, String serverName, int serverPort) throws ServletException {
|
String defaultTypeList, String serverName, int serverPort) throws ServletException {
|
||||||
|
|
||||||
JSONObject json = new JSONObject();
|
ObjectNode json = JsonNodeFactory.instance.objectNode();
|
||||||
try {
|
|
||||||
json.put("name", "VIVO Reconciliation Service");
|
json.put("name", "VIVO Reconciliation Service");
|
||||||
if (defaultNamespace != null) {
|
if (defaultNamespace != null) {
|
||||||
json.put("identifierSpace", defaultNamespace);
|
json.put("identifierSpace", defaultNamespace);
|
||||||
json.put("schemaSpace", defaultNamespace);
|
json.put("schemaSpace", defaultNamespace);
|
||||||
}
|
}
|
||||||
JSONObject viewJson = new JSONObject();
|
ObjectNode viewJson = JsonNodeFactory.instance.objectNode();
|
||||||
StringBuffer urlBuf = new StringBuffer();
|
StringBuffer urlBuf = new StringBuffer();
|
||||||
urlBuf.append("http://" + serverName);
|
urlBuf.append("http://" + serverName);
|
||||||
if (serverPort == 8080) {
|
if (serverPort == 8080) {
|
||||||
|
@ -208,33 +217,29 @@ public class JSONReconcileServlet extends VitroHttpServlet {
|
||||||
idNameArray[i] = splitList[i].split(",");
|
idNameArray[i] = splitList[i].split(",");
|
||||||
}
|
}
|
||||||
// process and add to json defaultTypes
|
// process and add to json defaultTypes
|
||||||
JSONArray defaultTypesJsonArr = new JSONArray();
|
ArrayNode defaultTypesJsonArr = JsonNodeFactory.instance.arrayNode();
|
||||||
for (int i = 0; i<idNameArray.length; i++) {
|
for (int i = 0; i<idNameArray.length; i++) {
|
||||||
JSONObject defaultTypesJson = new JSONObject();
|
ObjectNode defaultTypesJson = JsonNodeFactory.instance.objectNode();
|
||||||
defaultTypesJson.put("id", idNameArray[i][0].trim());
|
defaultTypesJson.put("id", idNameArray[i][0].trim());
|
||||||
defaultTypesJson.put("name", idNameArray[i][1].trim());
|
defaultTypesJson.put("name", idNameArray[i][1].trim());
|
||||||
defaultTypesJsonArr.put(defaultTypesJson);
|
defaultTypesJsonArr.add(defaultTypesJson);
|
||||||
}
|
}
|
||||||
json.put("defaultTypes", defaultTypesJsonArr);
|
json.put("defaultTypes", defaultTypesJsonArr);
|
||||||
}
|
}
|
||||||
} catch (JSONException ex) {
|
|
||||||
throw new ServletException(
|
|
||||||
"JSONReconcileServlet: Could not create metadata: " + ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
private JSONObject runSearch(HashMap<String, JSONObject> currMap) throws ServletException {
|
private ObjectNode runSearch(HashMap<String, JsonNode> currMap) throws ServletException {
|
||||||
JSONObject qJson = new JSONObject();
|
ObjectNode qJson = JsonNodeFactory.instance.objectNode();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
for (Map.Entry<String, JSONObject> entry : currMap.entrySet()) {
|
for (Map.Entry<String, JsonNode> entry : currMap.entrySet()) {
|
||||||
JSONObject resultAllJson = new JSONObject();
|
ObjectNode resultAllJson = JsonNodeFactory.instance.objectNode();
|
||||||
String key = entry.getKey();
|
String key = entry.getKey();
|
||||||
JSONObject json = entry.getValue();
|
JsonNode json = entry.getValue();
|
||||||
String queryVal = json.getString("query");
|
String queryVal = json.get("query").asText();
|
||||||
|
|
||||||
// System.out.println("query: " + json.toString());
|
// System.out.println("query: " + json.toString());
|
||||||
|
|
||||||
|
@ -245,22 +250,24 @@ public class JSONReconcileServlet extends VitroHttpServlet {
|
||||||
ArrayList<String[]> propertiesList = new ArrayList<String[]>();
|
ArrayList<String[]> propertiesList = new ArrayList<String[]>();
|
||||||
|
|
||||||
if (json.has("type")) {
|
if (json.has("type")) {
|
||||||
searchType = json.getString("type");
|
searchType = json.get("type").asText();
|
||||||
}
|
}
|
||||||
if (json.has("limit")) {
|
if (json.has("limit")) {
|
||||||
limit = json.getInt("limit");
|
limit = json.get("limit").asInt();
|
||||||
}
|
}
|
||||||
if (json.has("type_strict")) { // Not sure what this variable
|
if (json.has("type_strict")) { // Not sure what this variable
|
||||||
// represents. Skip for now.
|
// represents. Skip for now.
|
||||||
typeStrict = json.getString("type_strict");
|
typeStrict = json.get("type_strict").asText();
|
||||||
}
|
}
|
||||||
if (json.has("properties")) {
|
if (json.has("properties")) {
|
||||||
JSONArray properties = json.getJSONArray("properties");
|
JsonNode propertiesNode = json.get("properties");
|
||||||
for (int i = 0; i < properties.length(); i++) {
|
if (propertiesNode instanceof ArrayNode) {
|
||||||
|
ArrayNode properties = (ArrayNode)propertiesNode;
|
||||||
|
for (int i = 0; i < properties.size(); i++) {
|
||||||
String[] pvPair = new String[2];
|
String[] pvPair = new String[2];
|
||||||
JSONObject jsonProperty = properties.getJSONObject(i);
|
JsonNode jsonProperty = properties.get(i);
|
||||||
String pid = jsonProperty.getString("pid");
|
String pid = JacksonUtils.getString(jsonProperty, "pid");
|
||||||
String v = jsonProperty.getString("v");
|
String v = JacksonUtils.getString(jsonProperty, "v");
|
||||||
if (pid != null && v != null) {
|
if (pid != null && v != null) {
|
||||||
pvPair[0] = pid;
|
pvPair[0] = pid;
|
||||||
pvPair[1] = v;
|
pvPair[1] = v;
|
||||||
|
@ -268,9 +275,10 @@ public class JSONReconcileServlet extends VitroHttpServlet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// begin search
|
// begin search
|
||||||
JSONArray resultJsonArr = new JSONArray();
|
ArrayNode resultJsonArr = JsonNodeFactory.instance.arrayNode();
|
||||||
|
|
||||||
SearchQuery query = getQuery(queryVal, searchType, limit, propertiesList);
|
SearchQuery query = getQuery(queryVal, searchType, limit, propertiesList);
|
||||||
SearchResponse queryResponse = null;
|
SearchResponse queryResponse = null;
|
||||||
|
@ -299,14 +307,19 @@ public class JSONReconcileServlet extends VitroHttpServlet {
|
||||||
SearchResult result = new SearchResult(name, uri);
|
SearchResult result = new SearchResult(name, uri);
|
||||||
|
|
||||||
// populate result for Google Refine
|
// populate result for Google Refine
|
||||||
JSONObject resultJson = new JSONObject();
|
ObjectNode resultJson = JsonNodeFactory.instance.objectNode();
|
||||||
resultJson.put("score", doc.getFirstValue("score"));
|
Object score = doc.getFirstValue("score");
|
||||||
|
if (score instanceof Double) {
|
||||||
|
resultJson.put("score", (Double)score);
|
||||||
|
} else if (score instanceof Float) {
|
||||||
|
resultJson.put("score", (Float)score);
|
||||||
|
}
|
||||||
String modUri = result.getUri().replace("#", "%23");
|
String modUri = result.getUri().replace("#", "%23");
|
||||||
resultJson.put("id", modUri);
|
resultJson.put("id", modUri);
|
||||||
resultJson.put("name", result.getLabel());
|
resultJson.put("name", result.getLabel());
|
||||||
|
|
||||||
Collection<Object> rdfTypes = doc.getFieldValues(VitroSearchTermNames.RDFTYPE);
|
Collection<Object> rdfTypes = doc.getFieldValues(VitroSearchTermNames.RDFTYPE);
|
||||||
JSONArray typesJsonArr = new JSONArray();
|
ArrayNode typesJsonArr = JsonNodeFactory.instance.arrayNode();
|
||||||
if (rdfTypes != null) {
|
if (rdfTypes != null) {
|
||||||
|
|
||||||
for (Object rdfType : rdfTypes) {
|
for (Object rdfType : rdfTypes) {
|
||||||
|
@ -317,17 +330,17 @@ public class JSONReconcileServlet extends VitroHttpServlet {
|
||||||
int lastIndex2 = type.lastIndexOf('/') + 1;
|
int lastIndex2 = type.lastIndexOf('/') + 1;
|
||||||
String typeName = type.substring(lastIndex2);
|
String typeName = type.substring(lastIndex2);
|
||||||
typeName = typeName.replace("#", ":");
|
typeName = typeName.replace("#", ":");
|
||||||
JSONObject typesJson = new JSONObject();
|
ObjectNode typesJson = JsonNodeFactory.instance.objectNode();
|
||||||
typesJson.put("id", type);
|
typesJson.put("id", type);
|
||||||
typesJson.put("name", typeName);
|
typesJson.put("name", typeName);
|
||||||
typesJsonArr.put(typesJson);
|
typesJsonArr.add(typesJson);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
resultJson.put("type", typesJsonArr);
|
resultJson.put("type", typesJsonArr);
|
||||||
resultJson.put("match", "false");
|
resultJson.put("match", "false");
|
||||||
resultJsonArr.put(resultJson);
|
resultJsonArr.add(resultJson);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("problem getting usable individuals from search "
|
log.error("problem getting usable individuals from search "
|
||||||
|
@ -342,16 +355,16 @@ public class JSONReconcileServlet extends VitroHttpServlet {
|
||||||
// System.out.println("results: " + qJson.toString());
|
// System.out.println("results: " + qJson.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (JSONException ex) {
|
|
||||||
log.error("JSONException: " + ex);
|
|
||||||
throw new ServletException("JSONReconcileServlet JSONException: "
|
|
||||||
+ ex);
|
|
||||||
} catch (SearchEngineException ex) {
|
} catch (SearchEngineException ex) {
|
||||||
log.error("JSONException: " + ex);
|
log.error("JSONException: " + ex);
|
||||||
throw new ServletException("JSONReconcileServlet SearchEngineException: "
|
throw new ServletException("JSONReconcileServlet SearchEngineException: "
|
||||||
+ ex);
|
+ ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (qJson.has(INTERNAL_QUERY_NAME)) {
|
||||||
|
return (ObjectNode)qJson.get(INTERNAL_QUERY_NAME);
|
||||||
|
}
|
||||||
|
|
||||||
return qJson;
|
return qJson;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -59,6 +59,14 @@ public class JacksonUtils {
|
||||||
return strings;
|
return strings;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getString(JsonNode node, String name) {
|
||||||
|
if (node.has(name)) {
|
||||||
|
return node.get(name).asText();
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public static class JacksonUtilsException extends RuntimeException {
|
public static class JacksonUtilsException extends RuntimeException {
|
||||||
|
|
||||||
public JacksonUtilsException() {
|
public JacksonUtilsException() {
|
||||||
|
|
|
@ -10,8 +10,7 @@ import java.util.regex.PatternSyntaxException;
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
|
|
||||||
import org.json.JSONException;
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
import org.json.JSONObject;
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -53,14 +52,12 @@ public class JSONReconcileServletTest extends AbstractTestClass {
|
||||||
String defaultTypeList = null;
|
String defaultTypeList = null;
|
||||||
String serverName = null;
|
String serverName = null;
|
||||||
String schemaSpaceOutput = null;
|
String schemaSpaceOutput = null;
|
||||||
JSONObject jsonResult = null;
|
ObjectNode jsonResult = null;
|
||||||
try {
|
try {
|
||||||
jsonResult = reconcile.getMetadata(request, response, defaultNamespace, defaultTypeList, serverName, serverPort);
|
jsonResult = reconcile.getMetadata(request, response, defaultNamespace, defaultTypeList, serverName, serverPort);
|
||||||
schemaSpaceOutput = jsonResult.getString("schemaSpace");
|
schemaSpaceOutput = jsonResult.get("schemaSpace").asText();
|
||||||
} catch (ServletException e) {
|
} catch (ServletException e) {
|
||||||
System.err.println("JSONReconcileServletTest getMetadata ServletException: " + e);
|
System.err.println("JSONReconcileServletTest getMetadata ServletException: " + e);
|
||||||
} catch (JSONException e) {
|
|
||||||
System.err.println("JSONReconcileServletTest getMetadata JSONException: " + e);
|
|
||||||
}
|
}
|
||||||
Assert.assertNotNull("output should not be null", jsonResult);
|
Assert.assertNotNull("output should not be null", jsonResult);
|
||||||
Assert.assertEquals("schemaSpaceOutput", defaultNamespace, schemaSpaceOutput);
|
Assert.assertEquals("schemaSpaceOutput", defaultNamespace, schemaSpaceOutput);
|
||||||
|
|
4
dependencies/pom.xml
vendored
4
dependencies/pom.xml
vendored
|
@ -203,11 +203,11 @@
|
||||||
<artifactId>jackson-core</artifactId>
|
<artifactId>jackson-core</artifactId>
|
||||||
<version>2.7.4</version>
|
<version>2.7.4</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<!-- dependency>
|
||||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||||
<artifactId>jackson-datatype-json-org</artifactId>
|
<artifactId>jackson-datatype-json-org</artifactId>
|
||||||
<version>2.7.4</version>
|
<version>2.7.4</version>
|
||||||
</dependency>
|
</dependency -->
|
||||||
<!-- dependency>
|
<!-- dependency>
|
||||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||||
<artifactId>jackson-datatype-jsr353</artifactId>
|
<artifactId>jackson-datatype-jsr353</artifactId>
|
||||||
|
|
Loading…
Add table
Reference in a new issue