NIHVIVO-2343 Restructure SparqlQueryAjaxController so it can access different models. Create separate RequestedActions for QueryFullModel or QueryUserAccountsModel
This commit is contained in:
parent
4a78b32b34
commit
6e177f9de2
4 changed files with 117 additions and 24 deletions
|
@ -12,6 +12,8 @@ import edu.cornell.mannlib.vitro.webapp.auth.policy.ifaces.PolicyDecision;
|
||||||
import edu.cornell.mannlib.vitro.webapp.auth.policy.ifaces.PolicyIface;
|
import edu.cornell.mannlib.vitro.webapp.auth.policy.ifaces.PolicyIface;
|
||||||
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.admin.RebuildVClassGroupCache;
|
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.admin.RebuildVClassGroupCache;
|
||||||
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.ifaces.RequestedAction;
|
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.ifaces.RequestedAction;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.querymodel.QueryFullModel;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.querymodel.QueryUserAccountsModel;
|
||||||
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.usepages.AccessSpecialDataModels;
|
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.usepages.AccessSpecialDataModels;
|
||||||
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.usepages.EditIndividuals;
|
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.usepages.EditIndividuals;
|
||||||
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.usepages.EditOntology;
|
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.usepages.EditOntology;
|
||||||
|
@ -134,6 +136,12 @@ public class UseRestrictedPagesByRoleLevelPolicy implements PolicyIface {
|
||||||
} else if (whatToAuth instanceof ManageOwnProxies) {
|
} else if (whatToAuth instanceof ManageOwnProxies) {
|
||||||
result = isAuthorized(whatToAuth, RoleLevel.SELF, userRole);
|
result = isAuthorized(whatToAuth, RoleLevel.SELF, userRole);
|
||||||
|
|
||||||
|
} else if (whatToAuth instanceof QueryUserAccountsModel) {
|
||||||
|
result = isAuthorized(whatToAuth, RoleLevel.SELF, userRole);
|
||||||
|
|
||||||
|
} else if (whatToAuth instanceof QueryFullModel) {
|
||||||
|
result = isAuthorized(whatToAuth, RoleLevel.PUBLIC, userRole);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
result = defaultDecision("Unrecognized action");
|
result = defaultDecision("Unrecognized action");
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||||
|
|
||||||
|
package edu.cornell.mannlib.vitro.webapp.auth.requestedAction.querymodel;
|
||||||
|
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.ifaces.RequestedAction;
|
||||||
|
|
||||||
|
/** Should we allow the user to query the full data model? */
|
||||||
|
public class QueryFullModel extends RequestedAction {
|
||||||
|
// no fields
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||||
|
|
||||||
|
package edu.cornell.mannlib.vitro.webapp.auth.requestedAction.querymodel;
|
||||||
|
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.ifaces.RequestedAction;
|
||||||
|
|
||||||
|
/** Should we allow the user to query the User Accounts model? */
|
||||||
|
public class QueryUserAccountsModel extends RequestedAction {
|
||||||
|
// no fields
|
||||||
|
}
|
|
@ -9,6 +9,7 @@ import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
|
@ -26,8 +27,10 @@ import com.hp.hpl.jena.query.Syntax;
|
||||||
import com.hp.hpl.jena.rdf.model.Model;
|
import com.hp.hpl.jena.rdf.model.Model;
|
||||||
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.Actions;
|
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.Actions;
|
||||||
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.usepages.UseBasicAjaxControllers;
|
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.querymodel.QueryFullModel;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.querymodel.QueryUserAccountsModel;
|
||||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelector;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle an AJAX request for a SPARQL query. On entry, the "query" parameter
|
* Handle an AJAX request for a SPARQL query. On entry, the "query" parameter
|
||||||
|
@ -39,46 +42,96 @@ public class SparqlQueryAjaxController extends VitroAjaxController {
|
||||||
private static final Log log = LogFactory
|
private static final Log log = LogFactory
|
||||||
.getLog(SparqlQueryAjaxController.class);
|
.getLog(SparqlQueryAjaxController.class);
|
||||||
|
|
||||||
private static final String PARAMETER_QUERY = "query";
|
public static final String PARAMETER_QUERY = "query";
|
||||||
private static final String RESPONSE_MIME_TYPE = "application/javascript";
|
public static final String RESPONSE_MIME_TYPE = "application/javascript";
|
||||||
|
|
||||||
@Override
|
public static final String PARAMETER_MODEL = "model";
|
||||||
protected Actions requiredActions(VitroRequest vreq) {
|
public static final String OPTION_MODEL_FULL = "full";
|
||||||
return new Actions(new UseBasicAjaxControllers());
|
public static final String OPTION_MODEL_USER_ACCOUNTS = "userAccounts";
|
||||||
}
|
|
||||||
|
@Override
|
||||||
|
protected Actions requiredActions(VitroRequest vreq) {
|
||||||
|
String modelParam = getModelParam(vreq);
|
||||||
|
if (OPTION_MODEL_USER_ACCOUNTS.equals(modelParam)) {
|
||||||
|
return new Actions(new QueryUserAccountsModel());
|
||||||
|
} else {
|
||||||
|
return new Actions(new QueryFullModel());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doRequest(VitroRequest vreq, HttpServletResponse response)
|
protected void doRequest(VitroRequest vreq, HttpServletResponse response)
|
||||||
throws ServletException, IOException {
|
throws ServletException, IOException {
|
||||||
|
try {
|
||||||
Model model = vreq.getJenaOntModel();
|
String modelParam = getModelParam(vreq);
|
||||||
if (model == null) {
|
Model model = locateModel(modelParam);
|
||||||
log.error("JenaOntModel not found.");
|
String queryParam = locateQueryParam(vreq);
|
||||||
response.sendError(SC_INTERNAL_SERVER_ERROR,
|
Query query = createQuery(queryParam);
|
||||||
"JenaOntModel not found");
|
executeQuery(response, query, model);
|
||||||
return;
|
return;
|
||||||
|
} catch (AjaxControllerException e) {
|
||||||
|
log.error(e.getMessage());
|
||||||
|
response.sendError(e.getStatusCode());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getModelParam(HttpServletRequest req) {
|
||||||
|
String modelParam = req.getParameter(PARAMETER_MODEL);
|
||||||
|
log.debug("modelParam was: " + modelParam);
|
||||||
|
if ((modelParam != null) && (!modelParam.isEmpty())) {
|
||||||
|
return modelParam;
|
||||||
|
} else {
|
||||||
|
return OPTION_MODEL_FULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private Model locateModel(String modelParam) throws AjaxControllerException {
|
||||||
|
Object o = getServletContext().getAttribute("baseOntModelSelector");
|
||||||
|
if (!(o instanceof OntModelSelector)) {
|
||||||
|
throw new AjaxControllerException(SC_INTERNAL_SERVER_ERROR,
|
||||||
|
"OntModelSelector not found");
|
||||||
|
}
|
||||||
|
OntModelSelector oms = (OntModelSelector) o;
|
||||||
|
|
||||||
|
Model model = null;
|
||||||
|
if (OPTION_MODEL_USER_ACCOUNTS.equals(modelParam)) {
|
||||||
|
model = oms.getUserAccountsModel();
|
||||||
|
} else {
|
||||||
|
model = oms.getFullModel();
|
||||||
|
}
|
||||||
|
if (model == null) {
|
||||||
|
throw new AjaxControllerException(SC_INTERNAL_SERVER_ERROR,
|
||||||
|
"Model '' not found.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String locateQueryParam(VitroRequest vreq)
|
||||||
|
throws AjaxControllerException {
|
||||||
String queryParam = vreq.getParameter(PARAMETER_QUERY);
|
String queryParam = vreq.getParameter(PARAMETER_QUERY);
|
||||||
log.debug("queryParam was : " + queryParam);
|
log.debug("queryParam was: " + queryParam);
|
||||||
if ((queryParam == null) || queryParam.isEmpty()) {
|
if ((queryParam != null) && (!queryParam.isEmpty())) {
|
||||||
response.sendError(SC_NOT_FOUND, "'" + PARAMETER_QUERY
|
return queryParam;
|
||||||
+ "' parameter is required");
|
} else {
|
||||||
|
throw new AjaxControllerException(SC_NOT_FOUND, "'"
|
||||||
|
+ PARAMETER_QUERY + "' parameter is required");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Query createQuery(String queryParam) throws AjaxControllerException {
|
||||||
Query query = QueryFactory.create(queryParam, Syntax.syntaxARQ);
|
Query query = QueryFactory.create(queryParam, Syntax.syntaxARQ);
|
||||||
if (!query.isSelectType()) {
|
if (!query.isSelectType()) {
|
||||||
log.debug("Not a 'select' query.");
|
throw new AjaxControllerException(SC_NOT_FOUND,
|
||||||
response.sendError(SC_NOT_FOUND,
|
|
||||||
"Only 'select' queries are allowed.");
|
"Only 'select' queries are allowed.");
|
||||||
}
|
}
|
||||||
|
return query;
|
||||||
executeQuery(response, query, DatasetFactory.create(model));
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void executeQuery(HttpServletResponse response, Query query,
|
private void executeQuery(HttpServletResponse response, Query query,
|
||||||
Dataset dataset) throws IOException {
|
Model model) throws IOException {
|
||||||
|
Dataset dataset = DatasetFactory.create(model);
|
||||||
QueryExecution qe = QueryExecutionFactory.create(query, dataset);
|
QueryExecution qe = QueryExecutionFactory.create(query, dataset);
|
||||||
try {
|
try {
|
||||||
ResultSet results = qe.execSelect();
|
ResultSet results = qe.execSelect();
|
||||||
|
@ -90,4 +143,16 @@ public class SparqlQueryAjaxController extends VitroAjaxController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class AjaxControllerException extends Exception {
|
||||||
|
private final int statusCode;
|
||||||
|
|
||||||
|
AjaxControllerException(int statusCode, String message) {
|
||||||
|
super(message);
|
||||||
|
this.statusCode = statusCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getStatusCode() {
|
||||||
|
return statusCode;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue