Clarify logic in VitroRequestPrep.checkForSpecialWDF
This commit is contained in:
parent
cc4e9c96bb
commit
dfc09f6ac7
1 changed files with 122 additions and 77 deletions
|
@ -3,6 +3,11 @@
|
||||||
package edu.cornell.mannlib.vitro.webapp.filters;
|
package edu.cornell.mannlib.vitro.webapp.filters;
|
||||||
|
|
||||||
import static edu.cornell.mannlib.vitro.webapp.controller.VitroRequest.SPECIAL_WRITE_MODEL;
|
import static edu.cornell.mannlib.vitro.webapp.controller.VitroRequest.SPECIAL_WRITE_MODEL;
|
||||||
|
import static edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary.CONTEXT_DISPLAY_TBOX;
|
||||||
|
import static edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary.SWITCH_TO_DISPLAY_MODEL;
|
||||||
|
import static edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary.USE_DISPLAY_MODEL_PARAM;
|
||||||
|
import static edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary.USE_MODEL_PARAM;
|
||||||
|
import static edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary.USE_TBOX_MODEL_PARAM;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
|
@ -16,6 +21,7 @@ import javax.servlet.ServletException;
|
||||||
import javax.servlet.ServletRequest;
|
import javax.servlet.ServletRequest;
|
||||||
import javax.servlet.ServletResponse;
|
import javax.servlet.ServletResponse;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import org.apache.commons.dbcp.BasicDataSource;
|
import org.apache.commons.dbcp.BasicDataSource;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
|
@ -32,7 +38,6 @@ import edu.cornell.mannlib.vitro.webapp.auth.policy.ServletPolicyList;
|
||||||
import edu.cornell.mannlib.vitro.webapp.beans.ApplicationBean;
|
import edu.cornell.mannlib.vitro.webapp.beans.ApplicationBean;
|
||||||
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
|
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
|
||||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.filtering.WebappDaoFactoryFiltering;
|
import edu.cornell.mannlib.vitro.webapp.dao.filtering.WebappDaoFactoryFiltering;
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.filtering.filters.FilterFactory;
|
import edu.cornell.mannlib.vitro.webapp.dao.filtering.filters.FilterFactory;
|
||||||
|
@ -89,6 +94,7 @@ public class VitroRequestPrep implements Filter {
|
||||||
FilterChain chain) throws IOException, ServletException {
|
FilterChain chain) throws IOException, ServletException {
|
||||||
// If this isn't an HttpServletRequest, we might as well fail now.
|
// If this isn't an HttpServletRequest, we might as well fail now.
|
||||||
HttpServletRequest req = (HttpServletRequest) request;
|
HttpServletRequest req = (HttpServletRequest) request;
|
||||||
|
HttpServletResponse resp = (HttpServletResponse) response;
|
||||||
logRequestUriForDebugging(req);
|
logRequestUriForDebugging(req);
|
||||||
|
|
||||||
//don't waste time running this filter again.
|
//don't waste time running this filter again.
|
||||||
|
@ -173,82 +179,120 @@ public class VitroRequestPrep implements Filter {
|
||||||
// Nothing to do.
|
// Nothing to do.
|
||||||
}
|
}
|
||||||
|
|
||||||
//check if special model - this is for enabling the use of a different model for menu management
|
/**
|
||||||
//Also enables the use of a completely different model and tbox if uris are passed
|
* Check if special model is requested - this is for enabling the use of a different
|
||||||
|
* model for menu management. Also enables the use of a completely different
|
||||||
|
* model and tbox if uris are passed.
|
||||||
|
*/
|
||||||
private WebappDaoFactory checkForSpecialWDF(VitroRequest vreq, WebappDaoFactory inputWadf) {
|
private WebappDaoFactory checkForSpecialWDF(VitroRequest vreq, WebappDaoFactory inputWadf) {
|
||||||
String useMenuModelParam = vreq.getParameter(DisplayVocabulary.SWITCH_TO_DISPLAY_MODEL);
|
// If this isn't a Jena WADF, then there's nothing to be done.
|
||||||
boolean useMenu = (useMenuModelParam != null);
|
if (!(inputWadf instanceof WebappDaoFactoryJena)) {
|
||||||
//other parameters to be passed in in case want to use specific models
|
log.warn("Can't set special models: " +
|
||||||
String useMainModelUri = vreq.getParameter(DisplayVocabulary.USE_MODEL_PARAM);
|
"WebappDaoFactory is not a WebappDaoFactoryJena");
|
||||||
String useTboxModelUri = vreq.getParameter(DisplayVocabulary.USE_TBOX_MODEL_PARAM);
|
removeSpecialWriteModel(vreq);
|
||||||
String useDisplayModelUri = vreq.getParameter(DisplayVocabulary.USE_DISPLAY_MODEL_PARAM);
|
return inputWadf;
|
||||||
|
}
|
||||||
|
|
||||||
if(useMenu || (useMainModelUri != null && !useMainModelUri.isEmpty() && useTboxModelUri != null && !useTboxModelUri.isEmpty())) {
|
WebappDaoFactoryJena wadf = (WebappDaoFactoryJena) inputWadf;
|
||||||
log.debug("Menu switching parameters exist, Use Menu: " + useMenu + " - UseModelUri:" + useMainModelUri + " - UseTboxModelUri:" + useTboxModelUri + " - useDisplayModelUri:" + useDisplayModelUri);
|
|
||||||
if(inputWadf instanceof WebappDaoFactoryJena) {
|
// If they asked for the display model, give it to them.
|
||||||
//Create a copy of the input WADF to be sent over and then set
|
if (isParameterPresent(vreq, SWITCH_TO_DISPLAY_MODEL)) {
|
||||||
WebappDaoFactoryJena wadfj = new WebappDaoFactoryJena((WebappDaoFactoryJena) inputWadf);
|
OntModel mainOntModel = (OntModel)_context.getAttribute("displayOntModel");
|
||||||
log.debug("Created copy of input webapp dao factory to be overwritten");
|
OntModel tboxOntModel = (OntModel) _context.getAttribute(CONTEXT_DISPLAY_TBOX);
|
||||||
//WebappDaoFactoryJena wadfj = (WebappDaoFactoryJena) wadf;
|
setSpecialWriteModel(vreq, mainOntModel);
|
||||||
OntModel useMainOntModel = null, useTboxOntModel = null, useDisplayOntModel = null;
|
return createNewWebappDaoFactory(wadf, mainOntModel, tboxOntModel, null);
|
||||||
Model tboxModel = null, displayModel = null;
|
}
|
||||||
|
|
||||||
|
// If they asked for other models by URI, set them.
|
||||||
|
if (anyOtherSpecialProperties(vreq)) {
|
||||||
BasicDataSource bds = JenaDataSourceSetupBase.getApplicationDataSource(_context);
|
BasicDataSource bds = JenaDataSourceSetupBase.getApplicationDataSource(_context);
|
||||||
String dbType = ConfigurationProperties.getBean(_context).getProperty( // database type
|
String dbType = ConfigurationProperties.getBean(_context)
|
||||||
"VitroConnection.DataSource.dbtype", "MySQL");
|
.getProperty("VitroConnection.DataSource.dbtype", "MySQL");
|
||||||
if(useMenu) {
|
|
||||||
log.debug("Display model editing mode");
|
OntModel mainOntModel = createSpecialModel(vreq, USE_MODEL_PARAM, bds, dbType);
|
||||||
//if using special models for menu management, get main menu model from context and set tbox and display uris to be used
|
OntModel tboxOntModel = createSpecialModel(vreq, USE_TBOX_MODEL_PARAM, bds, dbType);
|
||||||
useMainOntModel = (OntModel) _context.getAttribute("displayOntModel");
|
OntModel displayOntModel = createSpecialModel(vreq, USE_DISPLAY_MODEL_PARAM, bds, dbType);
|
||||||
//Hardcoding tbox model uri for now
|
setSpecialWriteModel(vreq, mainOntModel);
|
||||||
useTboxModelUri = DisplayVocabulary.DISPLAY_TBOX_MODEL_URI;
|
return createNewWebappDaoFactory(wadf, mainOntModel, tboxOntModel, displayOntModel);
|
||||||
useDisplayModelUri = DisplayVocabulary.DISPLAY_DISPLAY_MODEL_URI;
|
}
|
||||||
//Get tbox and display display model from servlet context otherwise load in directly from database
|
|
||||||
useTboxOntModel = (OntModel) _context.getAttribute(DisplayVocabulary.CONTEXT_DISPLAY_TBOX);
|
// Otherwise, there's nothing special about this request.
|
||||||
useDisplayOntModel = (OntModel) _context.getAttribute(DisplayVocabulary.CONTEXT_DISPLAY_DISPLAY);
|
removeSpecialWriteModel(vreq);
|
||||||
|
return wadf;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean anyOtherSpecialProperties(VitroRequest vreq) {
|
||||||
|
return isParameterPresent(vreq, USE_MODEL_PARAM)
|
||||||
|
|| isParameterPresent(vreq, USE_TBOX_MODEL_PARAM)
|
||||||
|
|| isParameterPresent(vreq, USE_DISPLAY_MODEL_PARAM);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If the request asks for a special model by URI, create it from the
|
||||||
|
* Database.
|
||||||
|
*
|
||||||
|
* @return the model they asked for, or null if they didn't ask for one.
|
||||||
|
* @throws IllegalStateException
|
||||||
|
* if it's not found.
|
||||||
|
*/
|
||||||
|
private OntModel createSpecialModel(VitroRequest vreq, String key,
|
||||||
|
BasicDataSource bds, String dbType) {
|
||||||
|
if (!isParameterPresent(vreq, key)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
String modelUri = vreq.getParameter(key);
|
||||||
|
Model model = JenaDataSourceSetupBase.makeDBModel(bds, modelUri,
|
||||||
|
OntModelSpec.OWL_MEM,
|
||||||
|
JenaDataSourceSetupBase.TripleStoreType.RDB, dbType, _context);
|
||||||
|
if (model != null) {
|
||||||
|
return ModelFactory
|
||||||
|
.createOntologyModel(OntModelSpec.OWL_MEM, model);
|
||||||
} else {
|
} else {
|
||||||
log.debug("Display model editing mode not triggered, using model uri " + useMainModelUri);
|
throw new IllegalStateException("Main Model Uri " + modelUri
|
||||||
//If main model uri passed as parameter then retrieve model from parameter
|
+ " did not retrieve model");
|
||||||
Model mainModel = JenaDataSourceSetupBase.makeDBModel(bds, useMainModelUri, OntModelSpec.OWL_MEM, JenaDataSourceSetupBase.TripleStoreType.RDB, dbType, _context);
|
|
||||||
//if this uri exists and model exists, then set up ont model version
|
|
||||||
if(mainModel != null) {
|
|
||||||
log.debug("main model uri exists");
|
|
||||||
useMainOntModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, mainModel);
|
|
||||||
} else {
|
|
||||||
log.error("Main Model Uri " + useMainModelUri + " did not retrieve model");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void removeSpecialWriteModel(VitroRequest vreq) {
|
||||||
if(!useMenu || useTboxOntModel == null){
|
|
||||||
tboxModel = JenaDataSourceSetupBase.makeDBModel(bds, useTboxModelUri, OntModelSpec.OWL_MEM, JenaDataSourceSetupBase.TripleStoreType.RDB, dbType, _context);
|
|
||||||
useTboxOntModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, tboxModel);
|
|
||||||
}
|
|
||||||
if(!useMenu || useDisplayOntModel == null) {
|
|
||||||
//Set "display model" for display model
|
|
||||||
displayModel = JenaDataSourceSetupBase.makeDBModel(bds, useDisplayModelUri, OntModelSpec.OWL_MEM, JenaDataSourceSetupBase.TripleStoreType.RDB, dbType, _context);
|
|
||||||
useDisplayOntModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, displayModel);
|
|
||||||
}
|
|
||||||
//Set special model for wadfj
|
|
||||||
if(useMainOntModel != null) {
|
|
||||||
log.debug("Switching to use of input model");
|
|
||||||
//If menu model, preserve existing display model so the navigation elements remain
|
|
||||||
if(useMenu) {
|
|
||||||
useDisplayOntModel = null;
|
|
||||||
}
|
|
||||||
//Changes will be made to the copy, not the original from the servlet context
|
|
||||||
wadfj.setSpecialDataModel(useMainOntModel, useTboxOntModel, useDisplayOntModel);
|
|
||||||
//Set attribute on VitroRequest that saves special write model
|
|
||||||
vreq.setAttribute(SPECIAL_WRITE_MODEL, useMainOntModel);
|
|
||||||
return wadfj;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//if no parameters exist for switching models, return the original webapp dao factory object
|
|
||||||
//ensure no attribute there if special write model not being utilized
|
|
||||||
if (vreq.getAttribute(SPECIAL_WRITE_MODEL) != null) {
|
if (vreq.getAttribute(SPECIAL_WRITE_MODEL) != null) {
|
||||||
vreq.removeAttribute(SPECIAL_WRITE_MODEL);
|
vreq.removeAttribute(SPECIAL_WRITE_MODEL);
|
||||||
}
|
}
|
||||||
return inputWadf;
|
}
|
||||||
|
|
||||||
|
private void setSpecialWriteModel(VitroRequest vreq, OntModel mainOntModel) {
|
||||||
|
if (mainOntModel != null) {
|
||||||
|
vreq.setAttribute(SPECIAL_WRITE_MODEL, mainOntModel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a copy of the WADF, and set the special models onto it.
|
||||||
|
* If a model is null, it will have no effect.
|
||||||
|
*/
|
||||||
|
private WebappDaoFactory createNewWebappDaoFactory(
|
||||||
|
WebappDaoFactoryJena inputWadf, OntModel mainOntModel,
|
||||||
|
OntModel tboxOntModel, OntModel displayOntModel) {
|
||||||
|
WebappDaoFactoryJena wadfj = new WebappDaoFactoryJena(inputWadf);
|
||||||
|
wadfj.setSpecialDataModel(mainOntModel, tboxOntModel, displayOntModel);
|
||||||
|
return wadfj;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isParameterPresent(HttpServletRequest req, String key) {
|
||||||
|
return getNonEmptyParameter(req, key) != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a non-empty parameter from the request, or a null.
|
||||||
|
*/
|
||||||
|
private String getNonEmptyParameter(HttpServletRequest req, String key) {
|
||||||
|
String value = req.getParameter(key);
|
||||||
|
if ((value == null) || value.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void logRequestUriForDebugging(HttpServletRequest req) {
|
private void logRequestUriForDebugging(HttpServletRequest req) {
|
||||||
|
@ -265,4 +309,5 @@ public class VitroRequestPrep implements Filter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue