Minor code cleanup.

This commit is contained in:
j2blake 2012-04-26 16:47:19 +00:00
parent 4b90513f1d
commit d19b5687bc

View file

@ -2,12 +2,15 @@
package edu.cornell.mannlib.vitro.webapp.controller; package edu.cornell.mannlib.vitro.webapp.controller;
import static edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary.DISPLAY_ONT_MODEL;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.Writer; import java.io.Writer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.ListIterator; import java.util.ListIterator;
@ -27,7 +30,6 @@ import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import com.hp.hpl.jena.ontology.OntModel; import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.Literal;
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;
@ -43,8 +45,6 @@ import edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.SelectListG
import edu.cornell.mannlib.vitro.webapp.search.beans.ProhibitedFromSearch; import edu.cornell.mannlib.vitro.webapp.search.beans.ProhibitedFromSearch;
import edu.cornell.mannlib.vitro.webapp.utils.pageDataGetter.PageDataGetterUtils; import edu.cornell.mannlib.vitro.webapp.utils.pageDataGetter.PageDataGetterUtils;
import static edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary.DISPLAY_ONT_MODEL;
/** /**
* This servlet is for servicing requests for JSON objects/data. * This servlet is for servicing requests for JSON objects/data.
* It could be generalized to get other types of data ex. XML, HTML etc * It could be generalized to get other types of data ex. XML, HTML etc
@ -162,40 +162,17 @@ public class JsonServlet extends VitroHttpServlet {
log.error(ex,ex); log.error(ex,ex);
} }
if( rObj == null ) writeJsonResponse(rObj, errorMessage, resp);
rObj = new JSONObject();
try{
resp.setCharacterEncoding("UTF-8");
resp.setContentType("application/json;charset=UTF-8");
if( errorMessage != null ){
rObj.put("errorMessage", errorMessage);
resp.setStatus(500 /*HttpURLConnection.HTTP_SERVER_ERROR*/);
}else{
rObj.put("errorMessage", "");
}
Writer writer = resp.getWriter();
writer.write(rObj.toString());
}catch(JSONException jse){
log.error(jse,jse);
} catch (IOException e) {
log.error(e,e);
}
} }
public static JSONObject getSolrIndividualsByVClass(String vclassURI, HttpServletRequest req, ServletContext context) throws Exception { public static JSONObject getSolrIndividualsByVClass(String vclassURI, HttpServletRequest req, ServletContext context) throws Exception {
List<String> vclassURIs = new ArrayList<String>(); List<String> vclassURIs = Collections.singletonList(vclassURI);
vclassURIs.add(vclassURI);
VitroRequest vreq = new VitroRequest(req); VitroRequest vreq = new VitroRequest(req);
VClass vclass=null;
JSONObject rObj = new JSONObject();
Map<String, Object> map = getSolrVClassIntersectionResults(vclassURIs, vreq, context); Map<String, Object> map = getSolrVClassIntersectionResults(vclassURIs, vreq, context);
//last parameter indicates single vclass instead of multiple vclasses //last parameter indicates single vclass instead of multiple vclasses
rObj = processVClassResults(map, vreq, context, false); return processVClassResults(map, vreq, context, false);
return rObj;
} }
@ -230,26 +207,7 @@ public class JsonServlet extends VitroHttpServlet {
log.error(ex,ex); log.error(ex,ex);
} }
if( rObj == null ) writeJsonResponse(rObj, errorMessage, resp);
rObj = new JSONObject();
try{
resp.setCharacterEncoding("UTF-8");
resp.setContentType("application/json;charset=UTF-8");
if( errorMessage != null ){
rObj.put("errorMessage", errorMessage);
resp.setStatus(500 /*HttpURLConnection.HTTP_SERVER_ERROR*/);
}else{
rObj.put("errorMessage", "");
}
Writer writer = resp.getWriter();
writer.write(rObj.toString());
}catch(JSONException jse){
log.error(jse,jse);
} catch (IOException e) {
log.error(e,e);
}
} }
public static JSONObject getSolrIndividualsByVClasses(List<String> vclassURIs, HttpServletRequest req, ServletContext context) throws Exception { public static JSONObject getSolrIndividualsByVClasses(List<String> vclassURIs, HttpServletRequest req, ServletContext context) throws Exception {
@ -543,45 +501,49 @@ public class JsonServlet extends VitroHttpServlet {
} }
} }
if( rObj == null ) writeJsonResponse(rObj, errorMessage, resp);
rObj = new JSONObject();
//Send object
try{
resp.setCharacterEncoding("UTF-8");
resp.setContentType("application/json;charset=UTF-8");
if( errorMessage != null ){
rObj.put("errorMessage", errorMessage);
resp.setStatus(500 /*HttpURLConnection.HTTP_SERVER_ERROR*/);
}else{
rObj.put("errorMessage", "");
}
Writer writer = resp.getWriter();
writer.write(rObj.toString());
}catch(JSONException jse){
log.error(jse,jse);
} catch (IOException e) {
log.error(e,e);
}
} }
private JSONArray individualsToJson(List<Individual> individuals) throws ServletException { private JSONArray individualsToJson(List<Individual> individuals) throws ServletException {
JSONArray ja = new JSONArray();
Iterator<Individual> it = individuals.iterator();
try{ try{
while(it.hasNext()){ JSONArray ja = new JSONArray();
Individual ent = (Individual) it.next(); for (Individual ent: individuals) {
JSONObject entJ = new JSONObject(); 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.put( entJ ); ja.put( entJ );
} }
return ja;
}catch(JSONException ex){ }catch(JSONException ex){
throw new ServletException("could not convert list of Individuals into JSON: " + ex); throw new ServletException("could not convert list of Individuals into JSON: " + ex);
} }
return ja;
} }
private void writeJsonResponse(JSONObject rObj, String errorMessage,
HttpServletResponse resp) {
if( rObj == null ) {
rObj = new JSONObject();
}
try{
resp.setCharacterEncoding("UTF-8");
resp.setContentType("application/json;charset=UTF-8");
if( errorMessage != null ){
rObj.put("errorMessage", errorMessage);
resp.setStatus(500 /*HttpURLConnection.HTTP_SERVER_ERROR*/);
}else{
rObj.put("errorMessage", "");
}
Writer writer = resp.getWriter();
writer.write(rObj.toString());
}catch(JSONException jse){
log.error(jse,jse);
} catch (IOException e) {
log.error(e,e);
}
}
} }