Modified Servlet class to replace deprecated call to the QueryParser constructor and added a simple unit test.

This commit is contained in:
runeliza 2011-04-06 15:27:59 +00:00
parent 3c40f7ad2a
commit 1de0056c3c
2 changed files with 93 additions and 22 deletions

View file

@ -30,6 +30,7 @@ import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs; import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.WildcardQuery; import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.util.Version;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -45,7 +46,7 @@ import edu.cornell.mannlib.vitro.webapp.search.lucene.LuceneIndexFactory;
import edu.cornell.mannlib.vitro.webapp.search.lucene.LuceneSetup; import edu.cornell.mannlib.vitro.webapp.search.lucene.LuceneSetup;
/** /**
* This servlet is for servicing JSON requests for Google Refine's * This servlet is for servicing JSON requests from Google Refine's
* Reconciliation Service. * Reconciliation Service.
* *
* @author Eliza Chan (elc2013@med.cornell.edu) * @author Eliza Chan (elc2013@med.cornell.edu)
@ -53,8 +54,6 @@ import edu.cornell.mannlib.vitro.webapp.search.lucene.LuceneSetup;
*/ */
public class JSONReconcileServlet extends VitroHttpServlet { public class JSONReconcileServlet extends VitroHttpServlet {
private String defaultNamespace;
private String defaultTypeList;
private static String QUERY_PARAMETER_NAME = "term"; private static String QUERY_PARAMETER_NAME = "term";
public static final int MAX_QUERY_LENGTH = 500; public static final int MAX_QUERY_LENGTH = 500;
private static final Log log = LogFactory.getLog(JSONReconcileServlet.class.getName()); private static final Log log = LogFactory.getLog(JSONReconcileServlet.class.getName());
@ -62,6 +61,7 @@ public class JSONReconcileServlet extends VitroHttpServlet {
@Override @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException { throws ServletException, IOException {
//resp.setContentType("application/json");
super.doPost(req, resp); super.doPost(req, resp);
} }
@ -69,9 +69,10 @@ public class JSONReconcileServlet extends VitroHttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException { throws ServletException, IOException {
super.doGet(req, resp); super.doGet(req, resp);
resp.setContentType("application/json");
VitroRequest vreq = new VitroRequest(req); VitroRequest vreq = new VitroRequest(req);
this.defaultNamespace = vreq.getWebappDaoFactory().getDefaultNamespace(); System.out.println("vreq");
this.defaultTypeList = ConfigurationProperties.getBean(req).getProperty("Vitro.reconcile.defaultTypeList"); System.out.println(vreq.getWebappDaoFactory());
try { try {
if (vreq.getParameter("query") != null if (vreq.getParameter("query") != null
@ -81,14 +82,22 @@ public class JSONReconcileServlet extends VitroHttpServlet {
String responseStr = (vreq.getParameter("callback") == null) ? qJson String responseStr = (vreq.getParameter("callback") == null) ? qJson
.toString() : vreq.getParameter("callback") + "(" .toString() : vreq.getParameter("callback") + "("
+ qJson.toString() + ")"; + qJson.toString() + ")";
resp.setContentType("application/json");
ServletOutputStream out = resp.getOutputStream(); ServletOutputStream out = resp.getOutputStream();
out.print(responseStr); out.print(responseStr);
} else { // metadata } else { // metadata
JSONObject metaJson = getMetadata(req, resp); String defaultNamespace = null;
String defaultTypeList = null;
String serverName = null;
int serverPort = req.getServerPort();
if (vreq.getWebappDaoFactory() != null) {
defaultNamespace = vreq.getWebappDaoFactory().getDefaultNamespace();
}
defaultTypeList = ConfigurationProperties.getBean(req).getProperty("Vitro.reconcile.defaultTypeList");
serverName = req.getServerName();
JSONObject 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");
resp.setContentType("application/json");
ServletOutputStream out = resp.getOutputStream(); ServletOutputStream out = resp.getOutputStream();
out.print(callbackStr + "(" + metaJson.toString() + ")"); out.print(callbackStr + "(" + metaJson.toString() + ")");
} }
@ -97,7 +106,7 @@ public class JSONReconcileServlet extends VitroHttpServlet {
} }
} }
private JSONObject getResult(VitroRequest vreq, HttpServletRequest req, protected JSONObject getResult(VitroRequest vreq, HttpServletRequest req,
HttpServletResponse resp) throws ServletException { HttpServletResponse resp) throws ServletException {
HashMap<String, JSONObject> searchWithTypeMap = new HashMap<String, JSONObject>(); HashMap<String, JSONObject> searchWithTypeMap = new HashMap<String, JSONObject>();
@ -173,20 +182,21 @@ public class JSONReconcileServlet extends VitroHttpServlet {
* @return * @return
* @throws ServletException * @throws ServletException
*/ */
private JSONObject getMetadata(HttpServletRequest req, protected JSONObject getMetadata(HttpServletRequest req, HttpServletResponse resp, String defaultNamespace,
HttpServletResponse resp) throws ServletException { String defaultTypeList, String serverName, int serverPort) throws ServletException {
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
try { try {
json.put("name", "VIVO Reconciliation Service"); json.put("name", "VIVO Reconciliation Service");
if (this.defaultNamespace != null) { if (defaultNamespace != null) {
json.put("identifierSpace", this.defaultNamespace); json.put("identifierSpace", defaultNamespace);
json.put("schemaSpace", this.defaultNamespace); json.put("schemaSpace", defaultNamespace);
} }
JSONObject viewJson = new JSONObject(); JSONObject viewJson = new JSONObject();
StringBuffer urlBuf = new StringBuffer(); StringBuffer urlBuf = new StringBuffer();
urlBuf.append("http://" + req.getServerName()); urlBuf.append("http://" + serverName);
if (req.getServerPort() == 8080) { if (serverPort == 8080) {
urlBuf.append(":" + req.getServerPort()); urlBuf.append(":" + serverPort);
} }
if (req.getContextPath() != null) { if (req.getContextPath() != null) {
urlBuf.append(req.getContextPath()); urlBuf.append(req.getContextPath());
@ -195,8 +205,8 @@ public class JSONReconcileServlet extends VitroHttpServlet {
json.put("view", viewJson); json.put("view", viewJson);
// parse defaultTypeList from deploy.properties // parse defaultTypeList from deploy.properties
if (this.defaultTypeList != null) { if (defaultTypeList != null) {
String[] splitList = this.defaultTypeList.split(";"); String[] splitList = defaultTypeList.split(";");
String[][] idNameArray = new String[splitList.length][splitList.length]; String[][] idNameArray = new String[splitList.length][splitList.length];
for(int i = 0; i<splitList.length; i++) { for(int i = 0; i<splitList.length; i++) {
idNameArray[i] = splitList[i].split(","); idNameArray[i] = splitList[i].split(",");
@ -215,6 +225,7 @@ public class JSONReconcileServlet extends VitroHttpServlet {
throw new ServletException( throw new ServletException(
"JSONReconcileServlet: Could not create metadata: " + ex); "JSONReconcileServlet: Could not create metadata: " + ex);
} }
return json; return json;
} }
@ -284,8 +295,8 @@ public class JSONReconcileServlet extends VitroHttpServlet {
Individual ind = iDao.getIndividualByURI(uri); Individual ind = iDao.getIndividualByURI(uri);
if (ind != null) { if (ind != null) {
String name = ind.getName(); String name = ind.getName();
// encode e.g. # to %23 // encode # to %23
String modUri = java.net.URLEncoder.encode(uri, "ISO-8859-1"); String modUri = uri.replace("#", "%23");
resultJson.put("id", modUri); resultJson.put("id", modUri);
resultJson.put("name", name); resultJson.put("name", name);
} }
@ -421,7 +432,7 @@ public class JSONReconcileServlet extends VitroHttpServlet {
// indicated in the query string. // indicated in the query string.
// The analyzer is needed so that we use the same analyzer on the search queries as // The analyzer is needed so that we use the same analyzer on the search queries as
// was used on the text that was indexed. // was used on the text that was indexed.
QueryParser qp = new QueryParser(searchField,analyzer); QueryParser qp = new QueryParser(Version.LUCENE_29, searchField,analyzer);
//this sets the query parser to AND all of the query terms it finds. //this sets the query parser to AND all of the query terms it finds.
qp.setDefaultOperator(QueryParser.AND_OPERATOR); qp.setDefaultOperator(QueryParser.AND_OPERATOR);
return qp; return qp;

View file

@ -0,0 +1,60 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.controller;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.net.URL;
import javax.servlet.ServletException;
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
import stubs.javax.servlet.http.HttpServletRequestStub;
import stubs.javax.servlet.http.HttpServletResponseStub;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
/**
* Tests on various methods in the class.
* @author Eliza Chan (elc2013@med.cornell.edu)
*
*/
public class JSONReconcileServletTest extends AbstractTestClass {
private HttpServletRequestStub request;
private HttpServletResponseStub response;
private JSONReconcileServlet reconcile;
@Before
public void setup() throws Exception {
request = new HttpServletRequestStub();
request.setRequestUrl(new URL("http://vivo.this.that/reconcile"));
request.setMethod("POST");
response = new HttpServletResponseStub();
reconcile = new JSONReconcileServlet();
}
@Test
public void getMetadata() {
int serverPort = 8080;
String defaultNamespace = "http://vivo.this.that/individual/";
String defaultTypeList = null;
String serverName = null;
String schemaSpaceOutput = null;
JSONObject jsonResult = null;
try {
jsonResult = reconcile.getMetadata(request, response, defaultNamespace, defaultTypeList, serverName, serverPort);
schemaSpaceOutput = jsonResult.getString("schemaSpace");
} catch (ServletException e) {
System.err.println("JSONReconcileServletTest ServletException: " + e);
} catch (JSONException e) {
System.err.println("JSONReconcileServletTest JSONException: " + e);
}
assertNotNull("output should not be null", jsonResult);
assertEquals("schemaSpaceOutput", defaultNamespace, schemaSpaceOutput);
}
}