VIVO-82 Merge the filters that set up VitroRequest models
VitroRequestPrep and WebappDaoFactorySDBPrep merge to RequestModelsPrep. WebappDaoFactorySparqlPrep goes away.
This commit is contained in:
parent
a19227188b
commit
ed95df1637
6 changed files with 395 additions and 728 deletions
|
@ -0,0 +1,182 @@
|
||||||
|
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||||
|
|
||||||
|
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.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 javax.servlet.ServletContext;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
import com.hp.hpl.jena.ontology.OntModel;
|
||||||
|
import com.hp.hpl.jena.ontology.OntModelSpec;
|
||||||
|
import com.hp.hpl.jena.rdf.model.Model;
|
||||||
|
import com.hp.hpl.jena.rdf.model.ModelFactory;
|
||||||
|
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.auth.permissions.SimplePermission;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.auth.policy.PolicyHelper;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.dao.ModelAccess;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.dao.ModelAccess.ModelID;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.dao.jena.VitroModelSource;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.dao.jena.WebappDaoFactoryJena;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.servlet.setup.JenaDataSourceSetupBase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle model switching, if requested for the editing framework.
|
||||||
|
*/
|
||||||
|
public class ModelSwitcher {
|
||||||
|
private static final Log log = LogFactory.getLog(ModelSwitcher.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Are they authorized for whatever models they are asking for?
|
||||||
|
*/
|
||||||
|
public static boolean authorizedForSpecialModel(HttpServletRequest req) {
|
||||||
|
if (isParameterPresent(req, SWITCH_TO_DISPLAY_MODEL)) {
|
||||||
|
return PolicyHelper.isAuthorizedForActions(req, SimplePermission.MANAGE_MENUS.ACTIONS);
|
||||||
|
} else if (anyOtherSpecialProperties(req)){
|
||||||
|
return PolicyHelper.isAuthorizedForActions(req, SimplePermission.ACCESS_SPECIAL_DATA_MODELS.ACTIONS);
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean anyOtherSpecialProperties(HttpServletRequest req) {
|
||||||
|
return isParameterPresent(req, USE_MODEL_PARAM)
|
||||||
|
|| isParameterPresent(req, USE_TBOX_MODEL_PARAM)
|
||||||
|
|| isParameterPresent(req, USE_DISPLAY_MODEL_PARAM);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isParameterPresent(HttpServletRequest req, String key) {
|
||||||
|
return StringUtils.isNotEmpty(req.getParameter(key));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
public WebappDaoFactory checkForModelSwitching(VitroRequest vreq, WebappDaoFactory inputWadf) {
|
||||||
|
ServletContext _context = vreq.getSession().getServletContext();
|
||||||
|
//TODO: Does the dataset in the vreq get set when using a special WDF? Does it need to?
|
||||||
|
//TODO: Does the unfiltered WDF get set when using a special WDF? Does it need to?
|
||||||
|
|
||||||
|
// If this isn't a Jena WADF, then there's nothing to be done.
|
||||||
|
if (!(inputWadf instanceof WebappDaoFactoryJena)) {
|
||||||
|
log.warn("Can't set special models: " +
|
||||||
|
"WebappDaoFactory is not a WebappDaoFactoryJena");
|
||||||
|
removeSpecialWriteModel(vreq);
|
||||||
|
return inputWadf;
|
||||||
|
}
|
||||||
|
|
||||||
|
WebappDaoFactoryJena wadf = (WebappDaoFactoryJena) inputWadf;
|
||||||
|
|
||||||
|
// If they asked for the display model, give it to them.
|
||||||
|
if (isParameterPresent(vreq, SWITCH_TO_DISPLAY_MODEL)) {
|
||||||
|
OntModel mainOntModel = ModelAccess.on(_context).getDisplayModel();
|
||||||
|
OntModel tboxOntModel = ModelAccess.on(_context).getOntModel(ModelID.DISPLAY_TBOX);
|
||||||
|
setSpecialWriteModel(vreq, mainOntModel);
|
||||||
|
|
||||||
|
vreq.setAttribute(VitroRequest.ID_FOR_ABOX_MODEL, VitroModelSource.ModelName.DISPLAY.toString());
|
||||||
|
vreq.setAttribute(VitroRequest.ID_FOR_TBOX_MODEL, VitroModelSource.ModelName.DISPLAY_TBOX.toString());
|
||||||
|
vreq.setAttribute(VitroRequest.ID_FOR_DISPLAY_MODEL, VitroModelSource.ModelName.DISPLAY_DISPLAY.toString());
|
||||||
|
vreq.setAttribute(VitroRequest.ID_FOR_WRITE_MODEL, VitroModelSource.ModelName.DISPLAY.toString());
|
||||||
|
|
||||||
|
return createNewWebappDaoFactory(wadf, mainOntModel, tboxOntModel, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If they asked for other models by URI, set them.
|
||||||
|
if (anyOtherSpecialProperties(vreq)) {
|
||||||
|
DataSource bds = JenaDataSourceSetupBase.getApplicationDataSource(_context);
|
||||||
|
String dbType = ConfigurationProperties.getBean(_context)
|
||||||
|
.getProperty("VitroConnection.DataSource.dbtype", "MySQL");
|
||||||
|
|
||||||
|
OntModel mainOntModel = createSpecialModel(vreq, USE_MODEL_PARAM, bds, dbType);
|
||||||
|
OntModel tboxOntModel = createSpecialModel(vreq, USE_TBOX_MODEL_PARAM, bds, dbType);
|
||||||
|
OntModel displayOntModel = createSpecialModel(vreq, USE_DISPLAY_MODEL_PARAM, bds, dbType);
|
||||||
|
|
||||||
|
vreq.setAttribute(VitroRequest.ID_FOR_ABOX_MODEL, vreq.getParameter(USE_MODEL_PARAM));
|
||||||
|
vreq.setAttribute(VitroRequest.ID_FOR_WRITE_MODEL, vreq.getParameter(USE_MODEL_PARAM));
|
||||||
|
vreq.setAttribute(VitroRequest.ID_FOR_TBOX_MODEL, vreq.getParameter(USE_TBOX_MODEL_PARAM));
|
||||||
|
vreq.setAttribute(VitroRequest.ID_FOR_DISPLAY_MODEL, vreq.getParameter(USE_DISPLAY_MODEL_PARAM));
|
||||||
|
|
||||||
|
setSpecialWriteModel(vreq, mainOntModel);
|
||||||
|
return createNewWebappDaoFactory(wadf, mainOntModel, tboxOntModel, displayOntModel);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, there's nothing special about this request.
|
||||||
|
removeSpecialWriteModel(vreq);
|
||||||
|
return wadf;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setSpecialWriteModel(VitroRequest vreq, OntModel mainOntModel) {
|
||||||
|
if (mainOntModel != null) {
|
||||||
|
ModelAccess.on(vreq).setJenaOntModel(mainOntModel);
|
||||||
|
vreq.setAttribute(SPECIAL_WRITE_MODEL, mainOntModel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void removeSpecialWriteModel(VitroRequest vreq) {
|
||||||
|
if (vreq.getAttribute(SPECIAL_WRITE_MODEL) != null) {
|
||||||
|
vreq.removeAttribute(SPECIAL_WRITE_MODEL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The goal here is to return a new WDF that is set to
|
||||||
|
* have the mainOntModel as its ABox, the tboxOntModel as it
|
||||||
|
* TBox and displayOntModel as it display model.
|
||||||
|
*
|
||||||
|
* Right now this is achieved by creating a copy of
|
||||||
|
* the WADF, and setting 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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,
|
||||||
|
DataSource 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, vreq.getSession().getServletContext());
|
||||||
|
if (model != null) {
|
||||||
|
return ModelFactory
|
||||||
|
.createOntologyModel(OntModelSpec.OWL_MEM, model);
|
||||||
|
} else {
|
||||||
|
throw new IllegalStateException("Main Model Uri " + modelUri
|
||||||
|
+ " did not retrieve model");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,210 @@
|
||||||
|
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||||
|
|
||||||
|
package edu.cornell.mannlib.vitro.webapp.filters;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import javax.servlet.Filter;
|
||||||
|
import javax.servlet.FilterChain;
|
||||||
|
import javax.servlet.FilterConfig;
|
||||||
|
import javax.servlet.ServletContext;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.ServletRequest;
|
||||||
|
import javax.servlet.ServletResponse;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import javax.servlet.http.HttpSession;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
import com.hp.hpl.jena.ontology.OntModelSpec;
|
||||||
|
import com.hp.hpl.jena.query.Dataset;
|
||||||
|
import com.hp.hpl.jena.rdf.model.ModelFactory;
|
||||||
|
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.auth.identifier.RequestIdentifiers;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.auth.policy.ServletPolicyList;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.dao.ModelAccess;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactoryConfig;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.dao.filtering.WebappDaoFactoryFiltering;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.dao.filtering.filters.HideFromDisplayByPolicyFilter;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelector;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.dao.jena.RDFServiceDataset;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.dao.jena.WebappDaoFactorySDB;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.dao.jena.WebappDaoFactorySDB.SDBDatasetMode;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.rdfservice.filter.LanguageFilteringRDFService;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.rdfservice.filter.LanguageFilteringUtils;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.RDFServiceUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This sets up several objects in the Request scope for each incoming HTTP
|
||||||
|
* request. This is done in a Filter so that controllers and JSPs get the same
|
||||||
|
* setup.
|
||||||
|
*
|
||||||
|
* This code configures the WebappDaoFactory for each request.
|
||||||
|
*/
|
||||||
|
public class RequestModelsPrep implements Filter {
|
||||||
|
private final static Log log = LogFactory.getLog(RequestModelsPrep.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The filter will be applied to all incoming urls, this is a list of URI
|
||||||
|
* patterns to skip. These are matched against the requestURI sans query
|
||||||
|
* parameters, e.g. "/vitro/index.jsp" "/vitro/themes/enhanced/css/edit.css"
|
||||||
|
*
|
||||||
|
* These patterns are from VitroRequestPrep.java
|
||||||
|
*/
|
||||||
|
private final static Pattern[] skipPatterns = {
|
||||||
|
Pattern.compile(".*\\.(gif|GIF|jpg|jpeg)$"),
|
||||||
|
Pattern.compile(".*\\.css$"), Pattern.compile(".*\\.js$"),
|
||||||
|
Pattern.compile("/.*/themes/.*/site_icons/.*"),
|
||||||
|
Pattern.compile("/.*/images/.*") };
|
||||||
|
|
||||||
|
private ServletContext ctx;
|
||||||
|
private ConfigurationProperties props;
|
||||||
|
private String defaultNamespace;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(FilterConfig fc) throws ServletException {
|
||||||
|
ctx = fc.getServletContext();
|
||||||
|
props = ConfigurationProperties.getBean(ctx);
|
||||||
|
defaultNamespace = props.getProperty("Vitro.defaultNamespace");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doFilter(ServletRequest request, ServletResponse response,
|
||||||
|
FilterChain filterChain) throws IOException, ServletException {
|
||||||
|
HttpServletRequest req = (HttpServletRequest) request;
|
||||||
|
HttpServletResponse resp = (HttpServletResponse) response;
|
||||||
|
|
||||||
|
// If we're not authorized for this request, skip the chain and
|
||||||
|
// redirect.
|
||||||
|
if (!ModelSwitcher.authorizedForSpecialModel(req)) {
|
||||||
|
VitroHttpServlet.redirectUnauthorizedRequest(req, resp);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!thisRequestNeedsModels(req) || modelsAreAlreadySetUp(req)) {
|
||||||
|
filterChain.doFilter(req, resp);
|
||||||
|
} else {
|
||||||
|
RDFService rdfService = RDFServiceUtils.getRDFServiceFactory(ctx)
|
||||||
|
.getShortTermRDFService();
|
||||||
|
try {
|
||||||
|
setUpTheRequestModels(rdfService, req);
|
||||||
|
filterChain.doFilter(req, resp);
|
||||||
|
} finally {
|
||||||
|
rdfService.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean thisRequestNeedsModels(HttpServletRequest req) {
|
||||||
|
String requestURI = req.getRequestURI();
|
||||||
|
for (Pattern skipPattern : skipPatterns) {
|
||||||
|
if (skipPattern.matcher(requestURI).matches()) {
|
||||||
|
log.debug("request matched skipPattern '" + skipPattern
|
||||||
|
+ "', skipping VitroRequestPrep");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean modelsAreAlreadySetUp(HttpServletRequest req) {
|
||||||
|
String attributeName = RequestModelsPrep.class.getName() + "-setup";
|
||||||
|
if (req.getAttribute(attributeName) != null) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
req.setAttribute(attributeName, Boolean.TRUE);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setUpTheRequestModels(RDFService rawRdfService,
|
||||||
|
HttpServletRequest req) {
|
||||||
|
HttpSession session = req.getSession();
|
||||||
|
VitroRequest vreq = new VitroRequest(req);
|
||||||
|
|
||||||
|
vreq.setUnfilteredRDFService(rawRdfService);
|
||||||
|
|
||||||
|
List<String> langs = getPreferredLanguages(req);
|
||||||
|
RDFService rdfService = addLanguageAwareness(langs, rawRdfService);
|
||||||
|
vreq.setRDFService(rdfService);
|
||||||
|
|
||||||
|
Dataset dataset = new RDFServiceDataset(rdfService);
|
||||||
|
vreq.setDataset(dataset);
|
||||||
|
|
||||||
|
OntModelSelector oms = ModelAccess.on(ctx).getUnionOntModelSelector();
|
||||||
|
WebappDaoFactoryConfig config = createWadfConfig(langs);
|
||||||
|
WebappDaoFactory wadf = new WebappDaoFactorySDB(rdfService, oms, config);
|
||||||
|
vreq.setUnfilteredWebappDaoFactory(wadf);
|
||||||
|
|
||||||
|
WebappDaoFactory assertions = new WebappDaoFactorySDB(rdfService,
|
||||||
|
ModelAccess.on(ctx).getBaseOntModelSelector(), config,
|
||||||
|
SDBDatasetMode.ASSERTIONS_ONLY);
|
||||||
|
ModelAccess.on(vreq).setBaseWebappDaoFactory(assertions);
|
||||||
|
|
||||||
|
ModelAccess.on(vreq).setJenaOntModel(
|
||||||
|
ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM,
|
||||||
|
dataset.getDefaultModel()));
|
||||||
|
|
||||||
|
if (isLanguageAwarenessEnabled()) {
|
||||||
|
ModelAccess.on(vreq).setDisplayModel(
|
||||||
|
LanguageFilteringUtils.wrapOntModelInALanguageFilter(
|
||||||
|
ModelAccess.on(session).getDisplayModel(), req));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do model switching and replace the WebappDaoFactory with
|
||||||
|
// a different version if requested by parameters
|
||||||
|
WebappDaoFactory switchedWadf = new ModelSwitcher()
|
||||||
|
.checkForModelSwitching(vreq, wadf);
|
||||||
|
|
||||||
|
HideFromDisplayByPolicyFilter filter = new HideFromDisplayByPolicyFilter(
|
||||||
|
RequestIdentifiers.getIdBundleForRequest(req),
|
||||||
|
ServletPolicyList.getPolicies(ctx));
|
||||||
|
WebappDaoFactoryFiltering filteredWadf = new WebappDaoFactoryFiltering(
|
||||||
|
switchedWadf, filter);
|
||||||
|
ModelAccess.on(vreq).setWebappDaoFactory(filteredWadf);
|
||||||
|
}
|
||||||
|
|
||||||
|
private WebappDaoFactoryConfig createWadfConfig(List<String> langs) {
|
||||||
|
WebappDaoFactoryConfig config = new WebappDaoFactoryConfig();
|
||||||
|
config.setDefaultNamespace(defaultNamespace);
|
||||||
|
config.setPreferredLanguages(langs);
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<String> getPreferredLanguages(HttpServletRequest req) {
|
||||||
|
log.debug("Accept-Language: " + req.getHeader("Accept-Language"));
|
||||||
|
return LanguageFilteringUtils.localesToLanguages(req.getLocales());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Language awareness is enabled unless they explicitly disable it.
|
||||||
|
*/
|
||||||
|
private Boolean isLanguageAwarenessEnabled() {
|
||||||
|
return Boolean.valueOf(props.getProperty("RDFService.languageFilter",
|
||||||
|
"true"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private RDFService addLanguageAwareness(List<String> langs,
|
||||||
|
RDFService rawRDFService) {
|
||||||
|
if (isLanguageAwarenessEnabled()) {
|
||||||
|
return new LanguageFilteringRDFService(rawRDFService, langs);
|
||||||
|
} else {
|
||||||
|
return rawRDFService;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void destroy() {
|
||||||
|
// Nothing to destroy
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,354 +0,0 @@
|
||||||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
|
||||||
|
|
||||||
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.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.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
import javax.servlet.Filter;
|
|
||||||
import javax.servlet.FilterChain;
|
|
||||||
import javax.servlet.FilterConfig;
|
|
||||||
import javax.servlet.ServletContext;
|
|
||||||
import javax.servlet.ServletException;
|
|
||||||
import javax.servlet.ServletRequest;
|
|
||||||
import javax.servlet.ServletResponse;
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
|
||||||
import org.apache.commons.logging.LogFactory;
|
|
||||||
|
|
||||||
import com.hp.hpl.jena.ontology.OntModel;
|
|
||||||
import com.hp.hpl.jena.ontology.OntModelSpec;
|
|
||||||
import com.hp.hpl.jena.query.Dataset;
|
|
||||||
import com.hp.hpl.jena.rdf.model.Model;
|
|
||||||
import com.hp.hpl.jena.rdf.model.ModelFactory;
|
|
||||||
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.auth.identifier.RequestIdentifiers;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.auth.permissions.SimplePermission;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.auth.policy.PolicyHelper;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.auth.policy.ServletPolicyList;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.ModelAccess;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.ModelAccess.ModelID;
|
|
||||||
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.filters.FilterFactory;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.filtering.filters.HideFromDisplayByPolicyFilter;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.filtering.filters.VitroFilters;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.VitroModelSource;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.WebappDaoFactoryJena;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.WebappDaoFactorySDB;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.rdfservice.filter.LanguageFilteringUtils;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.RDFServiceUtils;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.servlet.setup.JenaDataSourceSetupBase;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This sets up several objects in the Request scope for each
|
|
||||||
* incoming HTTP request. This is done in a Filter so
|
|
||||||
* that controllers and JSPs get the same setup.
|
|
||||||
*
|
|
||||||
* This code configures the WebappDaoFactory for each request.
|
|
||||||
*
|
|
||||||
* @author bdc34
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class VitroRequestPrep implements Filter {
|
|
||||||
private static final Log log = LogFactory.getLog(VitroRequestPrep.class.getName());
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The filter will be applied to all incoming requests, but should skip any
|
|
||||||
* request whose URI matches any of these patterns. These are matched
|
|
||||||
* against the requestURI without query parameters, e.g. "/vitro/index.jsp"
|
|
||||||
* "/vitro/themes/enhanced/css/edit.css"
|
|
||||||
*/
|
|
||||||
private static final Pattern[] skipPatterns = {
|
|
||||||
Pattern.compile(".*\\.(gif|GIF|jpg|jpeg)$"),
|
|
||||||
Pattern.compile(".*\\.css$"),
|
|
||||||
Pattern.compile(".*\\.js$"),
|
|
||||||
Pattern.compile("/.*/themes/.*/site_icons/.*"),
|
|
||||||
Pattern.compile("/.*/images/.*")
|
|
||||||
};
|
|
||||||
|
|
||||||
private ServletContext _context;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init(FilterConfig filterConfig) throws ServletException {
|
|
||||||
_context = filterConfig.getServletContext();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void doFilter(ServletRequest request, ServletResponse response,
|
|
||||||
FilterChain chain) throws IOException, ServletException {
|
|
||||||
// If this isn't an HttpServletRequest, we might as well fail now.
|
|
||||||
HttpServletRequest req = (HttpServletRequest) request;
|
|
||||||
HttpServletResponse resp = (HttpServletResponse) response;
|
|
||||||
logRequestUriForDebugging(req);
|
|
||||||
|
|
||||||
//don't waste time running this filter again.
|
|
||||||
if( req.getAttribute("VitroRequestPrep.setup") != null ){
|
|
||||||
log.debug("VitroRequestPrep has already been executed at least once, not re-executing.");
|
|
||||||
Integer a =(Integer) req.getAttribute("VitroRequestPrep.setup");
|
|
||||||
req.setAttribute("VitroRequestPrep.setup", new Integer( a + 1 ) );
|
|
||||||
chain.doFilter(req, response);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// don't run this filter for image files, CSS files, etc.
|
|
||||||
for( Pattern skipPattern : skipPatterns){
|
|
||||||
Matcher match =skipPattern.matcher( req.getRequestURI() );
|
|
||||||
if( match.matches() ){
|
|
||||||
log.debug("request matched a skipPattern, skipping VitroRequestPrep");
|
|
||||||
chain.doFilter(req, response);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we're not authorized for this request, skip the chain and redirect.
|
|
||||||
if (!authorizedForSpecialModel(req)) {
|
|
||||||
VitroHttpServlet.redirectUnauthorizedRequest(req, resp);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
VitroRequest vreq = new VitroRequest(req);
|
|
||||||
|
|
||||||
//-- setup DAO factory --//
|
|
||||||
WebappDaoFactory wdf = ModelAccess.on(vreq.getSession()).getWebappDaoFactory();
|
|
||||||
|
|
||||||
// Set up the DisplayModel with language filtering, if appropriate.
|
|
||||||
ConfigurationProperties props = ConfigurationProperties.getBean(req);
|
|
||||||
Boolean languageFilteringEnabled = Boolean.valueOf(props.getProperty("RDFService.languageFilter", "true"));
|
|
||||||
if (languageFilteringEnabled) {
|
|
||||||
OntModel displayModel = ModelAccess.on(req.getSession()).getDisplayModel();
|
|
||||||
OntModel filteredDisplayModel = LanguageFilteringUtils.wrapOntModelInALanguageFilter(displayModel, req);
|
|
||||||
ModelAccess.on(req).setDisplayModel(filteredDisplayModel);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Do model switching and replace the WebappDaoFactory with
|
|
||||||
//a different version if requested by parameters
|
|
||||||
wdf = checkForModelSwitching(vreq, wdf);
|
|
||||||
|
|
||||||
//get any filters from the ContextFitlerFactory
|
|
||||||
VitroFilters filters = getFiltersFromContextFilterFactory(req, wdf);
|
|
||||||
if( filters != null ){
|
|
||||||
log.debug("Wrapping WebappDaoFactory in filters from ContextFitlerFactory");
|
|
||||||
wdf = new WebappDaoFactoryFiltering(wdf, filters);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* display filtering happens now at any level, all the time; editing
|
|
||||||
* pages get their WebappDaoFactories differently
|
|
||||||
*/
|
|
||||||
HideFromDisplayByPolicyFilter filter = new HideFromDisplayByPolicyFilter(
|
|
||||||
RequestIdentifiers.getIdBundleForRequest(req),
|
|
||||||
ServletPolicyList.getPolicies(_context));
|
|
||||||
ModelAccess.on(vreq).setWebappDaoFactory(new WebappDaoFactoryFiltering(wdf, filter));
|
|
||||||
|
|
||||||
// support for Dataset interface if using Jena in-memory model
|
|
||||||
if (vreq.getDataset() == null) {
|
|
||||||
Dataset dataset = WebappDaoFactoryJena.makeInMemoryDataset(
|
|
||||||
vreq.getAssertionsOntModel(), vreq.getInferenceOntModel());
|
|
||||||
vreq.setDataset(dataset);
|
|
||||||
}
|
|
||||||
|
|
||||||
ServletContext ctx = vreq.getSession().getServletContext();
|
|
||||||
|
|
||||||
if (vreq.getUnfilteredWebappDaoFactory() == null) {
|
|
||||||
vreq.setUnfilteredWebappDaoFactory(new WebappDaoFactorySDB(
|
|
||||||
RDFServiceUtils.getRDFServiceFactory(ctx).getRDFService(),
|
|
||||||
ModelAccess.on(ctx).getUnionOntModelSelector()));
|
|
||||||
}
|
|
||||||
|
|
||||||
req.setAttribute("VitroRequestPrep.setup", new Integer(1));
|
|
||||||
chain.doFilter(req, response);
|
|
||||||
}
|
|
||||||
|
|
||||||
private VitroFilters getFiltersFromContextFilterFactory( HttpServletRequest request, WebappDaoFactory wdf){
|
|
||||||
FilterFactory ff = (FilterFactory)_context.getAttribute("FilterFactory");
|
|
||||||
if( ff == null ){
|
|
||||||
return null;
|
|
||||||
} else {
|
|
||||||
return ff.getFilters(request, wdf);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean authorizedForSpecialModel(HttpServletRequest req) {
|
|
||||||
if (isParameterPresent(req, SWITCH_TO_DISPLAY_MODEL)) {
|
|
||||||
return PolicyHelper.isAuthorizedForActions(req, SimplePermission.MANAGE_MENUS.ACTIONS);
|
|
||||||
} else if (anyOtherSpecialProperties(req)){
|
|
||||||
return PolicyHelper.isAuthorizedForActions(req, SimplePermission.ACCESS_SPECIAL_DATA_MODELS.ACTIONS);
|
|
||||||
} else {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void destroy() {
|
|
||||||
// Nothing to do.
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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 checkForModelSwitching(VitroRequest vreq, WebappDaoFactory inputWadf) {
|
|
||||||
//TODO: Does the dataset in the vreq get set when using a special WDF? Does it need to?
|
|
||||||
//TODO: Does the unfiltered WDF get set when using a special WDF? Does it need to?
|
|
||||||
|
|
||||||
// If this isn't a Jena WADF, then there's nothing to be done.
|
|
||||||
if (!(inputWadf instanceof WebappDaoFactoryJena)) {
|
|
||||||
log.warn("Can't set special models: " +
|
|
||||||
"WebappDaoFactory is not a WebappDaoFactoryJena");
|
|
||||||
removeSpecialWriteModel(vreq);
|
|
||||||
return inputWadf;
|
|
||||||
}
|
|
||||||
|
|
||||||
WebappDaoFactoryJena wadf = (WebappDaoFactoryJena) inputWadf;
|
|
||||||
|
|
||||||
// If they asked for the display model, give it to them.
|
|
||||||
if (isParameterPresent(vreq, SWITCH_TO_DISPLAY_MODEL)) {
|
|
||||||
OntModel mainOntModel = ModelAccess.on(_context).getDisplayModel();
|
|
||||||
OntModel tboxOntModel = ModelAccess.on(_context).getOntModel(ModelID.DISPLAY_TBOX);
|
|
||||||
setSpecialWriteModel(vreq, mainOntModel);
|
|
||||||
|
|
||||||
vreq.setAttribute(VitroRequest.ID_FOR_ABOX_MODEL, VitroModelSource.ModelName.DISPLAY.toString());
|
|
||||||
vreq.setAttribute(VitroRequest.ID_FOR_TBOX_MODEL, VitroModelSource.ModelName.DISPLAY_TBOX.toString());
|
|
||||||
vreq.setAttribute(VitroRequest.ID_FOR_DISPLAY_MODEL, VitroModelSource.ModelName.DISPLAY_DISPLAY.toString());
|
|
||||||
vreq.setAttribute(VitroRequest.ID_FOR_WRITE_MODEL, VitroModelSource.ModelName.DISPLAY.toString());
|
|
||||||
|
|
||||||
return createNewWebappDaoFactory(wadf, mainOntModel, tboxOntModel, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If they asked for other models by URI, set them.
|
|
||||||
if (anyOtherSpecialProperties(vreq)) {
|
|
||||||
DataSource bds = JenaDataSourceSetupBase.getApplicationDataSource(_context);
|
|
||||||
String dbType = ConfigurationProperties.getBean(_context)
|
|
||||||
.getProperty("VitroConnection.DataSource.dbtype", "MySQL");
|
|
||||||
|
|
||||||
OntModel mainOntModel = createSpecialModel(vreq, USE_MODEL_PARAM, bds, dbType);
|
|
||||||
OntModel tboxOntModel = createSpecialModel(vreq, USE_TBOX_MODEL_PARAM, bds, dbType);
|
|
||||||
OntModel displayOntModel = createSpecialModel(vreq, USE_DISPLAY_MODEL_PARAM, bds, dbType);
|
|
||||||
|
|
||||||
vreq.setAttribute(VitroRequest.ID_FOR_ABOX_MODEL, vreq.getParameter(USE_MODEL_PARAM));
|
|
||||||
vreq.setAttribute(VitroRequest.ID_FOR_WRITE_MODEL, vreq.getParameter(USE_MODEL_PARAM));
|
|
||||||
vreq.setAttribute(VitroRequest.ID_FOR_TBOX_MODEL, vreq.getParameter(USE_TBOX_MODEL_PARAM));
|
|
||||||
vreq.setAttribute(VitroRequest.ID_FOR_DISPLAY_MODEL, vreq.getParameter(USE_DISPLAY_MODEL_PARAM));
|
|
||||||
|
|
||||||
setSpecialWriteModel(vreq, mainOntModel);
|
|
||||||
return createNewWebappDaoFactory(wadf, mainOntModel, tboxOntModel, displayOntModel);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, there's nothing special about this request.
|
|
||||||
removeSpecialWriteModel(vreq);
|
|
||||||
return wadf;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean anyOtherSpecialProperties(HttpServletRequest req) {
|
|
||||||
return isParameterPresent(req, USE_MODEL_PARAM)
|
|
||||||
|| isParameterPresent(req, USE_TBOX_MODEL_PARAM)
|
|
||||||
|| isParameterPresent(req, 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,
|
|
||||||
DataSource 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 {
|
|
||||||
throw new IllegalStateException("Main Model Uri " + modelUri
|
|
||||||
+ " did not retrieve model");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeSpecialWriteModel(VitroRequest vreq) {
|
|
||||||
if (vreq.getAttribute(SPECIAL_WRITE_MODEL) != null) {
|
|
||||||
vreq.removeAttribute(SPECIAL_WRITE_MODEL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setSpecialWriteModel(VitroRequest vreq, OntModel mainOntModel) {
|
|
||||||
if (mainOntModel != null) {
|
|
||||||
ModelAccess.on(vreq).setJenaOntModel(mainOntModel);
|
|
||||||
vreq.setAttribute(SPECIAL_WRITE_MODEL, mainOntModel);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The goal here is to return a new WDF that is set to
|
|
||||||
* have the mainOntModel as its ABox, the tboxOntModel as it
|
|
||||||
* TBox and displayOntModel as it display model.
|
|
||||||
*
|
|
||||||
* Right now this is achieved by creating a copy of
|
|
||||||
* the WADF, and setting 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) {
|
|
||||||
if (log.isDebugEnabled()) {
|
|
||||||
try {
|
|
||||||
String uriString = req.getRequestURI();
|
|
||||||
String queryString = req.getQueryString();
|
|
||||||
if ((queryString != null) && (queryString.length() > 0)) {
|
|
||||||
uriString += "?" + queryString;
|
|
||||||
}
|
|
||||||
log.debug("RequestURI: " + uriString);
|
|
||||||
} catch (Exception e) {
|
|
||||||
// Don't want to kill the request if the logging fails.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,160 +0,0 @@
|
||||||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
|
||||||
|
|
||||||
package edu.cornell.mannlib.vitro.webapp.filters;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
import javax.servlet.Filter;
|
|
||||||
import javax.servlet.FilterChain;
|
|
||||||
import javax.servlet.FilterConfig;
|
|
||||||
import javax.servlet.ServletContext;
|
|
||||||
import javax.servlet.ServletException;
|
|
||||||
import javax.servlet.ServletRequest;
|
|
||||||
import javax.servlet.ServletResponse;
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
|
||||||
import org.apache.commons.logging.LogFactory;
|
|
||||||
|
|
||||||
import com.hp.hpl.jena.ontology.OntModel;
|
|
||||||
import com.hp.hpl.jena.ontology.OntModelSpec;
|
|
||||||
import com.hp.hpl.jena.query.Dataset;
|
|
||||||
import com.hp.hpl.jena.rdf.model.ModelFactory;
|
|
||||||
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.ModelAccess;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactoryConfig;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelector;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.RDFServiceDataset;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.WebappDaoFactorySDB;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.WebappDaoFactorySDB.SDBDatasetMode;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceFactory;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.rdfservice.filter.LanguageFilteringRDFService;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.rdfservice.filter.LanguageFilteringUtils;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.RDFServiceUtils;
|
|
||||||
|
|
||||||
public class WebappDaoFactorySDBPrep implements Filter {
|
|
||||||
|
|
||||||
private final static Log log = LogFactory.getLog(WebappDaoFactorySDBPrep.class);
|
|
||||||
|
|
||||||
ServletContext _ctx;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The filter will be applied to all incoming urls,
|
|
||||||
this is a list of URI patterns to skip. These are
|
|
||||||
matched against the requestURI sans query parameters,
|
|
||||||
* e.g.
|
|
||||||
* "/vitro/index.jsp"
|
|
||||||
* "/vitro/themes/enhanced/css/edit.css"
|
|
||||||
*
|
|
||||||
* These patterns are from VitroRequestPrep.java
|
|
||||||
*/
|
|
||||||
Pattern[] skipPatterns = {
|
|
||||||
Pattern.compile(".*\\.(gif|GIF|jpg|jpeg)$"),
|
|
||||||
Pattern.compile(".*\\.css$"),
|
|
||||||
Pattern.compile(".*\\.js$"),
|
|
||||||
Pattern.compile("/.*/themes/.*/site_icons/.*"),
|
|
||||||
Pattern.compile("/.*/images/.*")
|
|
||||||
};
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void doFilter(ServletRequest request, ServletResponse response,
|
|
||||||
FilterChain filterChain) throws IOException, ServletException {
|
|
||||||
|
|
||||||
if ( request.getAttribute("WebappDaoFactorySDBPrep.setup") != null ) {
|
|
||||||
// don't run multiple times
|
|
||||||
filterChain.doFilter(request, response);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for( Pattern skipPattern : skipPatterns){
|
|
||||||
Matcher match =skipPattern.matcher( ((HttpServletRequest)request).getRequestURI() );
|
|
||||||
if( match.matches() ){
|
|
||||||
log.debug("request matched a skipPattern, skipping VitroRequestPrep");
|
|
||||||
filterChain.doFilter(request, response);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
String defaultNamespace = (String) _ctx.getAttribute("defaultNamespace");
|
|
||||||
WebappDaoFactory wadf = null;
|
|
||||||
VitroRequest vreq = new VitroRequest((HttpServletRequest) request);
|
|
||||||
|
|
||||||
log.debug("Accept-Language: " + vreq.getHeader("Accept-Language"));
|
|
||||||
List<String> langs = LanguageFilteringUtils.localesToLanguages(vreq.getLocales());
|
|
||||||
|
|
||||||
WebappDaoFactoryConfig config = new WebappDaoFactoryConfig();
|
|
||||||
config.setDefaultNamespace(defaultNamespace);
|
|
||||||
config.setPreferredLanguages(langs);
|
|
||||||
|
|
||||||
RDFServiceFactory factory = RDFServiceUtils.getRDFServiceFactory(_ctx);
|
|
||||||
|
|
||||||
//RDFService rdfService = factory.getRDFService();
|
|
||||||
RDFService unfilteredRDFService = factory.getShortTermRDFService();
|
|
||||||
RDFService rdfService = null;
|
|
||||||
|
|
||||||
if (Boolean.valueOf(ConfigurationProperties.getBean(vreq).getProperty(
|
|
||||||
"RDFService.languageFilter", "true"))) {
|
|
||||||
rdfService = new LanguageFilteringRDFService(unfilteredRDFService, langs);
|
|
||||||
|
|
||||||
OntModel rawDisplayModel = ModelAccess.on(vreq.getSession()).getDisplayModel();
|
|
||||||
OntModel filteredDisplayModel = LanguageFilteringUtils.wrapOntModelInALanguageFilter(rawDisplayModel, request);
|
|
||||||
ModelAccess.on(vreq).setDisplayModel(filteredDisplayModel);
|
|
||||||
} else {
|
|
||||||
rdfService = unfilteredRDFService;
|
|
||||||
}
|
|
||||||
|
|
||||||
Dataset dataset = new RDFServiceDataset(rdfService);
|
|
||||||
|
|
||||||
OntModelSelector oms = ModelAccess.on(_ctx).getUnionOntModelSelector();
|
|
||||||
wadf = new WebappDaoFactorySDB(rdfService, oms, config);
|
|
||||||
ModelAccess.on(vreq).setWebappDaoFactory(wadf);
|
|
||||||
|
|
||||||
OntModelSelector baseOms = ModelAccess.on(_ctx).getBaseOntModelSelector();
|
|
||||||
WebappDaoFactory assertions = new WebappDaoFactorySDB(
|
|
||||||
rdfService, baseOms, config, SDBDatasetMode.ASSERTIONS_ONLY);
|
|
||||||
|
|
||||||
vreq.setRDFService(rdfService);
|
|
||||||
vreq.setUnfilteredRDFService(unfilteredRDFService);
|
|
||||||
ModelAccess.on(vreq).setBaseWebappDaoFactory(assertions);
|
|
||||||
vreq.setUnfilteredWebappDaoFactory(new WebappDaoFactorySDB(
|
|
||||||
rdfService, ModelAccess.on(_ctx).getUnionOntModelSelector()));
|
|
||||||
vreq.setDataset(dataset);
|
|
||||||
|
|
||||||
ModelAccess.on(vreq).setJenaOntModel(
|
|
||||||
ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, dataset.getDefaultModel()));
|
|
||||||
|
|
||||||
request.setAttribute("WebappDaoFactorySDBPrep.setup", 1);
|
|
||||||
|
|
||||||
try {
|
|
||||||
filterChain.doFilter(request, response);
|
|
||||||
return;
|
|
||||||
} finally {
|
|
||||||
if (wadf != null) {
|
|
||||||
wadf.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init(FilterConfig filterConfig) throws ServletException {
|
|
||||||
try {
|
|
||||||
_ctx = filterConfig.getServletContext();
|
|
||||||
} catch (Throwable t) {
|
|
||||||
log.error("Unable to initialize WebappDaoFactorySDBPrep", t);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void destroy() {
|
|
||||||
// no destroy actions
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,202 +0,0 @@
|
||||||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
|
||||||
|
|
||||||
package edu.cornell.mannlib.vitro.webapp.filters;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Enumeration;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Locale;
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
import javax.servlet.Filter;
|
|
||||||
import javax.servlet.FilterChain;
|
|
||||||
import javax.servlet.FilterConfig;
|
|
||||||
import javax.servlet.ServletContext;
|
|
||||||
import javax.servlet.ServletException;
|
|
||||||
import javax.servlet.ServletRequest;
|
|
||||||
import javax.servlet.ServletResponse;
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
|
||||||
import org.apache.commons.logging.LogFactory;
|
|
||||||
|
|
||||||
import com.hp.hpl.jena.graph.Graph;
|
|
||||||
import com.hp.hpl.jena.ontology.OntModel;
|
|
||||||
import com.hp.hpl.jena.ontology.OntModelSpec;
|
|
||||||
import com.hp.hpl.jena.query.Dataset;
|
|
||||||
import com.hp.hpl.jena.query.DatasetFactory;
|
|
||||||
import com.hp.hpl.jena.rdf.model.Model;
|
|
||||||
import com.hp.hpl.jena.rdf.model.ModelFactory;
|
|
||||||
import com.hp.hpl.jena.sdb.SDBFactory;
|
|
||||||
import com.hp.hpl.jena.sdb.Store;
|
|
||||||
import com.hp.hpl.jena.sdb.StoreDesc;
|
|
||||||
import com.hp.hpl.jena.sdb.sql.SDBConnection;
|
|
||||||
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.ModelAccess;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.ModelAccess.ModelID;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactoryConfig;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelector;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.SparqlDatasetGraph;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.SparqlGraphMultilingual;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.WebappDaoFactoryJena;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.servlet.setup.JenaDataSourceSetupBase;
|
|
||||||
|
|
||||||
public class WebappDaoFactorySparqlPrep implements Filter {
|
|
||||||
|
|
||||||
private final static Log log = LogFactory.getLog(WebappDaoFactorySparqlPrep.class);
|
|
||||||
|
|
||||||
ServletContext _ctx;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The filter will be applied to all incoming urls,
|
|
||||||
this is a list of URI patterns to skip. These are
|
|
||||||
matched against the requestURI sans query parameters,
|
|
||||||
* e.g.
|
|
||||||
* "/vitro/index.jsp"
|
|
||||||
* "/vitro/themes/enhanced/css/edit.css"
|
|
||||||
*
|
|
||||||
* These patterns are from VitroRequestPrep.java
|
|
||||||
*/
|
|
||||||
Pattern[] skipPatterns = {
|
|
||||||
Pattern.compile(".*\\.(gif|GIF|jpg|jpeg)$"),
|
|
||||||
Pattern.compile(".*\\.css$"),
|
|
||||||
Pattern.compile(".*\\.js$"),
|
|
||||||
Pattern.compile("/.*/themes/.*/site_icons/.*"),
|
|
||||||
Pattern.compile("/.*/images/.*")
|
|
||||||
};
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void doFilter(ServletRequest request, ServletResponse response,
|
|
||||||
FilterChain filterChain) throws IOException, ServletException {
|
|
||||||
|
|
||||||
if ( request.getAttribute("WebappDaoFactorySDBPrep.setup") != null ) {
|
|
||||||
// don't run multiple times
|
|
||||||
filterChain.doFilter(request, response);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for( Pattern skipPattern : skipPatterns){
|
|
||||||
Matcher match =skipPattern.matcher( ((HttpServletRequest)request).getRequestURI() );
|
|
||||||
if( match.matches() ){
|
|
||||||
log.debug("request matched a skipPattern, skipping VitroRequestPrep");
|
|
||||||
filterChain.doFilter(request, response);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
javax.sql.DataSource ds = JenaDataSourceSetupBase.getApplicationDataSource(_ctx);
|
|
||||||
StoreDesc storeDesc = (StoreDesc) _ctx.getAttribute("storeDesc");
|
|
||||||
String defaultNamespace = (String) _ctx.getAttribute("defaultNamespace");
|
|
||||||
Connection sqlConn = null;
|
|
||||||
SDBConnection conn = null;
|
|
||||||
Store store = null;
|
|
||||||
Dataset dataset = null;
|
|
||||||
WebappDaoFactory wadf = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (ds == null || storeDesc == null) {
|
|
||||||
throw new RuntimeException("SDB store not property set up");
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
sqlConn = ds.getConnection();
|
|
||||||
conn = new SDBConnection(sqlConn) ;
|
|
||||||
} catch (SQLException sqe) {
|
|
||||||
throw new RuntimeException("Unable to connect to database", sqe);
|
|
||||||
}
|
|
||||||
if (conn != null) {
|
|
||||||
store = SDBFactory.connectStore(conn, storeDesc);
|
|
||||||
dataset = SDBFactory.connectDataset(store);
|
|
||||||
VitroRequest vreq = new VitroRequest((HttpServletRequest) request);
|
|
||||||
|
|
||||||
log.info("---------");
|
|
||||||
|
|
||||||
Enumeration<String> headStrs = vreq.getHeaderNames();
|
|
||||||
while (headStrs.hasMoreElements()) {
|
|
||||||
String head = headStrs.nextElement();
|
|
||||||
log.info(head + " : " + vreq.getHeader(head));
|
|
||||||
}
|
|
||||||
|
|
||||||
List<String> langs = new ArrayList<String>();
|
|
||||||
|
|
||||||
log.info("Accept-Language: " + vreq.getHeader("Accept-Language"));
|
|
||||||
Enumeration<Locale> locs = vreq.getLocales();
|
|
||||||
while (locs.hasMoreElements()) {
|
|
||||||
Locale locale = locs.nextElement();
|
|
||||||
langs.add(locale.toString().replace("_", "-"));
|
|
||||||
log.info(locale.toString() + " / " + locale.getLanguage() + " + " + locale.getCountry() + " : " + locale.getDisplayCountry() + " | " + locale.getLanguage() + " : " + locale.getDisplayLanguage());
|
|
||||||
}
|
|
||||||
WebappDaoFactoryConfig config = new WebappDaoFactoryConfig();
|
|
||||||
config.setDefaultNamespace(defaultNamespace);
|
|
||||||
config.setPreferredLanguages(langs);
|
|
||||||
|
|
||||||
//okay let's make a graph-backed model
|
|
||||||
String endpointURI = ConfigurationProperties.getBean(
|
|
||||||
request).getProperty("VitroConnection.DataSource.endpointURI");
|
|
||||||
|
|
||||||
Graph g = new SparqlGraphMultilingual(endpointURI, langs);
|
|
||||||
//Graph g = new SparqlGraph(endpointURI);
|
|
||||||
|
|
||||||
Model m = ModelFactory.createModelForGraph(g);
|
|
||||||
OntModel om = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, m);
|
|
||||||
ModelAccess.on(vreq).setOntModel(ModelID.UNION_ABOX, om);
|
|
||||||
ModelAccess.on(vreq).setOntModel(ModelID.UNION_TBOX, om);
|
|
||||||
ModelAccess.on(vreq).setOntModel(ModelID.UNION_FULL, om);
|
|
||||||
|
|
||||||
OntModelSelector oms = ModelAccess.on(vreq).getOntModelSelector();
|
|
||||||
wadf = new WebappDaoFactoryJena(oms, config);
|
|
||||||
ModelAccess.on(vreq).setWebappDaoFactory(wadf);
|
|
||||||
ModelAccess.on(vreq).setBaseWebappDaoFactory(wadf);
|
|
||||||
vreq.setUnfilteredWebappDaoFactory(wadf);
|
|
||||||
|
|
||||||
dataset = DatasetFactory.create(new SparqlDatasetGraph(endpointURI));
|
|
||||||
vreq.setDataset(dataset);
|
|
||||||
}
|
|
||||||
} catch (Throwable t) {
|
|
||||||
log.error("Unable to filter request to set up SDB connection", t);
|
|
||||||
}
|
|
||||||
|
|
||||||
request.setAttribute("WebappDaoFactorySDBPrep.setup", 1);
|
|
||||||
|
|
||||||
try {
|
|
||||||
filterChain.doFilter(request, response);
|
|
||||||
return;
|
|
||||||
} finally {
|
|
||||||
if (conn != null) {
|
|
||||||
conn.close();
|
|
||||||
}
|
|
||||||
if (dataset != null) {
|
|
||||||
dataset.close();
|
|
||||||
}
|
|
||||||
if (store != null) {
|
|
||||||
store.close();
|
|
||||||
}
|
|
||||||
if (wadf != null) {
|
|
||||||
wadf.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init(FilterConfig filterConfig) throws ServletException {
|
|
||||||
try {
|
|
||||||
_ctx = filterConfig.getServletContext();
|
|
||||||
} catch (Throwable t) {
|
|
||||||
log.error("Unable to initialize WebappDaoFactorySDBPrep", t);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void destroy() {
|
|
||||||
// no destroy actions
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -118,20 +118,11 @@
|
||||||
</filter-mapping>
|
</filter-mapping>
|
||||||
|
|
||||||
<filter>
|
<filter>
|
||||||
<filter-name>WebappDaoFactorySDBPrep</filter-name>
|
<filter-name>RequestModelsPrep</filter-name>
|
||||||
<filter-class>edu.cornell.mannlib.vitro.webapp.filters.WebappDaoFactorySDBPrep</filter-class>
|
<filter-class>edu.cornell.mannlib.vitro.webapp.filters.RequestModelsPrep</filter-class>
|
||||||
</filter>
|
</filter>
|
||||||
<filter-mapping>
|
<filter-mapping>
|
||||||
<filter-name>WebappDaoFactorySDBPrep</filter-name>
|
<filter-name>RequestModelsPrep</filter-name>
|
||||||
<url-pattern>/*</url-pattern>
|
|
||||||
</filter-mapping>
|
|
||||||
|
|
||||||
<filter>
|
|
||||||
<filter-name>VitroRequestPrep</filter-name>
|
|
||||||
<filter-class>edu.cornell.mannlib.vitro.webapp.filters.VitroRequestPrep</filter-class>
|
|
||||||
</filter>
|
|
||||||
<filter-mapping>
|
|
||||||
<filter-name>VitroRequestPrep</filter-name>
|
|
||||||
<url-pattern>/*</url-pattern>
|
<url-pattern>/*</url-pattern>
|
||||||
<dispatcher>REQUEST</dispatcher>
|
<dispatcher>REQUEST</dispatcher>
|
||||||
<dispatcher>FORWARD</dispatcher>
|
<dispatcher>FORWARD</dispatcher>
|
||||||
|
|
Loading…
Add table
Reference in a new issue