Working on tomcat filter for menu page URL routing NIHVIVO-1383
This commit is contained in:
parent
e398bdd79a
commit
7831351b94
3 changed files with 69 additions and 40 deletions
|
@ -2,6 +2,9 @@
|
||||||
|
|
||||||
package edu.cornell.mannlib.vitro.webapp.dao.jena;
|
package edu.cornell.mannlib.vitro.webapp.dao.jena;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
@ -14,12 +17,11 @@ import com.hp.hpl.jena.query.QuerySolutionMap;
|
||||||
import com.hp.hpl.jena.query.ResultSet;
|
import com.hp.hpl.jena.query.ResultSet;
|
||||||
import com.hp.hpl.jena.rdf.model.Literal;
|
import com.hp.hpl.jena.rdf.model.Literal;
|
||||||
import com.hp.hpl.jena.rdf.model.Model;
|
import com.hp.hpl.jena.rdf.model.Model;
|
||||||
import com.hp.hpl.jena.rdf.model.ResourceFactory;
|
import com.hp.hpl.jena.rdf.model.RDFNode;
|
||||||
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.MenuDao;
|
import edu.cornell.mannlib.vitro.webapp.dao.MenuDao;
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
|
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
|
||||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.menu.MainMenu;
|
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.menu.MainMenu;
|
||||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.menu.Menu;
|
|
||||||
|
|
||||||
public class MenuDaoJena extends JenaBaseDao implements MenuDao {
|
public class MenuDaoJena extends JenaBaseDao implements MenuDao {
|
||||||
|
|
||||||
|
@ -34,7 +36,7 @@ public class MenuDaoJena extends JenaBaseDao implements MenuDao {
|
||||||
|
|
||||||
static final protected String menuQueryString =
|
static final protected String menuQueryString =
|
||||||
prefixes + "\n" +
|
prefixes + "\n" +
|
||||||
"SELECT ?menuItem ?linkText ?urlMapping WHERE {\n" +
|
"SELECT ?menuItem ?linkText ?urlMapping WHERE {\n" +
|
||||||
// " GRAPH ?g{\n"+
|
// " GRAPH ?g{\n"+
|
||||||
" ?menu rdf:type display:MainMenu .\n"+
|
" ?menu rdf:type display:MainMenu .\n"+
|
||||||
" ?menu display:hasElement ?menuItem . \n"+
|
" ?menu display:hasElement ?menuItem . \n"+
|
||||||
|
@ -75,15 +77,26 @@ public class MenuDaoJena extends JenaBaseDao implements MenuDao {
|
||||||
QueryExecution qexec = QueryExecutionFactory.create(menuQuery, displayModel, initialBindings );
|
QueryExecution qexec = QueryExecutionFactory.create(menuQuery, displayModel, initialBindings );
|
||||||
try{
|
try{
|
||||||
MainMenu menu = new MainMenu();
|
MainMenu menu = new MainMenu();
|
||||||
|
|
||||||
|
/* bdc34: currently there is no good way to decide which url to show
|
||||||
|
* on the menu when a page has multiple urls. */
|
||||||
|
Set <String>seenMenuItems = new HashSet<String>();
|
||||||
|
|
||||||
ResultSet results =qexec.execSelect();
|
ResultSet results =qexec.execSelect();
|
||||||
for( ; results.hasNext();){
|
for( ; results.hasNext();){
|
||||||
QuerySolution soln = results.nextSolution();
|
QuerySolution soln = results.nextSolution();
|
||||||
Literal itemText = soln.getLiteral("linkText");
|
Literal itemText = soln.getLiteral("linkText");
|
||||||
Literal itemLink = soln.getLiteral("urlMapping");
|
Literal itemLink = soln.getLiteral("urlMapping");
|
||||||
|
RDFNode menuItem = soln.getResource("menuItem");
|
||||||
|
|
||||||
String text = itemText != null ? itemText.getLexicalForm():"(undefined text)";
|
String text = itemText != null ? itemText.getLexicalForm():"(undefined text)";
|
||||||
String link = itemLink != null ? itemLink.getLexicalForm():"undefinedLink";
|
String link = itemLink != null ? itemLink.getLexicalForm():"undefinedLink";
|
||||||
|
String menuItemUri = PageDaoJena.nodeToString(menuItem);
|
||||||
|
|
||||||
menu.addItem(text,link, isActive( url, link ));
|
if( !seenMenuItems.contains(menuItemUri) ){
|
||||||
|
menu.addItem(text,link, isActive( url, link ));
|
||||||
|
seenMenuItems.add( menuItemUri );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return menu;
|
return menu;
|
||||||
}catch(Throwable th){
|
}catch(Throwable th){
|
||||||
|
@ -94,6 +107,9 @@ public class MenuDaoJena extends JenaBaseDao implements MenuDao {
|
||||||
|
|
||||||
|
|
||||||
protected boolean isActive(String url, String link){
|
protected boolean isActive(String url, String link){
|
||||||
return url.startsWith(link);
|
if( "/".equals(url) )
|
||||||
|
return "/".equals(link);
|
||||||
|
else
|
||||||
|
return url.startsWith(link);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -188,7 +188,7 @@ public class PageDaoJena extends JenaBaseDao implements PageDao {
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Object nodeToObject( RDFNode node ){
|
static protected Object nodeToObject( RDFNode node ){
|
||||||
if( node == null ){
|
if( node == null ){
|
||||||
return "";
|
return "";
|
||||||
}else if( node.isLiteral() ){
|
}else if( node.isLiteral() ){
|
||||||
|
@ -205,7 +205,7 @@ public class PageDaoJena extends JenaBaseDao implements PageDao {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String nodeToString( RDFNode node ){
|
static protected String nodeToString( RDFNode node ){
|
||||||
if( node == null ){
|
if( node == null ){
|
||||||
return "";
|
return "";
|
||||||
}else if( node.isLiteral() ){
|
}else if( node.isLiteral() ){
|
||||||
|
|
|
@ -4,6 +4,8 @@ package edu.cornell.mannlib.vitro.webapp.filters;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import javax.servlet.Filter;
|
import javax.servlet.Filter;
|
||||||
import javax.servlet.FilterChain;
|
import javax.servlet.FilterChain;
|
||||||
|
@ -30,13 +32,16 @@ import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
||||||
* It should only be applied to requests, not forwards, includes or errors.
|
* It should only be applied to requests, not forwards, includes or errors.
|
||||||
*/
|
*/
|
||||||
public class PageRoutingFilter implements Filter{
|
public class PageRoutingFilter implements Filter{
|
||||||
FilterConfig filterConfig;
|
protected FilterConfig filterConfig;
|
||||||
|
|
||||||
private final static Log log = LogFactory.getLog( PageRoutingFilter.class)
|
private final static Log log = LogFactory.getLog( PageRoutingFilter.class);
|
||||||
;
|
|
||||||
|
protected final static String URL_PART_PATTERN = "(/[^/]*).*";
|
||||||
protected final static String PAGE_CONTROLLER_NAME = "PageController";
|
protected final static String PAGE_CONTROLLER_NAME = "PageController";
|
||||||
protected final static String HOME_CONTROLLER_NAME = "HomePageController";
|
protected final static String HOME_CONTROLLER_NAME = "HomePageController";
|
||||||
|
|
||||||
|
protected final Pattern urlPartPattern = Pattern.compile(URL_PART_PATTERN);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(FilterConfig arg0) throws ServletException {
|
public void init(FilterConfig arg0) throws ServletException {
|
||||||
this.filterConfig = arg0;
|
this.filterConfig = arg0;
|
||||||
|
@ -56,36 +61,44 @@ public class PageRoutingFilter implements Filter{
|
||||||
|
|
||||||
// check for first part of path
|
// check for first part of path
|
||||||
// ex. /hats/superHat -> /hats
|
// ex. /hats/superHat -> /hats
|
||||||
String path1 = path;
|
Matcher m = urlPartPattern.matcher(path);
|
||||||
if( path != null && path.indexOf("/",1) > 0 ){
|
if( m.matches() && m.groupCount() >= 1){
|
||||||
path1 = path.substring(0,path1.indexOf("/"));
|
String path1stPart = m.group(1);
|
||||||
}
|
String pageUri = urlMappings.get(path1stPart);
|
||||||
|
|
||||||
String pageUri = urlMappings.get(path1);
|
//try it with a leading slash?
|
||||||
|
if( pageUri == null )
|
||||||
|
pageUri = urlMappings.get("/"+path1stPart);
|
||||||
|
|
||||||
//try it with a leading slash
|
if( pageUri != null && ! pageUri.isEmpty() ){
|
||||||
if( pageUri == null ){
|
log.debug(path + "is a request to a page defined in the display model as " + pageUri );
|
||||||
pageUri = urlMappings.get("/"+path1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if( pageUri != null && ! pageUri.isEmpty() ){
|
//add the pageUri to the request scope for use by the PageController
|
||||||
log.debug(path + "is a request to a page defined in the display model as " + pageUri );
|
PageController.putPageUri(req, pageUri);
|
||||||
|
|
||||||
//add the pageUri to the request scope for use by the PageController
|
//This will send requests to HomePageController or PageController
|
||||||
PageController.putPageUri(req, pageUri);
|
String controllerName = getControllerToForwardTo(req, pageUri, pageDao);
|
||||||
|
log.debug(path + " is being forwarded to controller " + controllerName);
|
||||||
|
|
||||||
//This will send requests to HomePageController or PageController
|
RequestDispatcher rd = filterConfig.getServletContext().getNamedDispatcher( controllerName );
|
||||||
String controllerName = getControllerToForwardTo(req, pageUri, pageDao);
|
rd.forward(req, response);
|
||||||
log.debug(path + " is being forwarded to controller " + controllerName);
|
}else if( "/".equals( path ) || path.isEmpty() ){
|
||||||
|
log.debug("url '" +path + "' is being forward to home controller even though there is no mapping to home" );
|
||||||
RequestDispatcher rd = filterConfig.getServletContext().getNamedDispatcher( controllerName );
|
RequestDispatcher rd = filterConfig.getServletContext().getNamedDispatcher( HOME_CONTROLLER_NAME );
|
||||||
rd.forward(req, response);
|
rd.forward(req, response);
|
||||||
|
}else{
|
||||||
|
doNonDisplayPage(path,arg0,arg1,chain);
|
||||||
|
}
|
||||||
}else{
|
}else{
|
||||||
log.debug(path + "this isn't a request to a page defined in the display model, handle it normally.");
|
doNonDisplayPage(path,arg0,arg1,chain);
|
||||||
chain.doFilter(arg0, arg1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void doNonDisplayPage(String path, ServletRequest arg0, ServletResponse arg1, FilterChain chain) throws IOException, ServletException{
|
||||||
|
log.debug(path + "this isn't a request to a page defined in the display model, handle it normally.");
|
||||||
|
chain.doFilter(arg0, arg1);
|
||||||
|
}
|
||||||
|
|
||||||
protected String getControllerToForwardTo(HttpServletRequest req,
|
protected String getControllerToForwardTo(HttpServletRequest req,
|
||||||
String pageUri, PageDao pageDao) {
|
String pageUri, PageDao pageDao) {
|
||||||
String homePageUri = pageDao.getHomePageUri();
|
String homePageUri = pageDao.getHomePageUri();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue