Working on tomcat filter for menu page URL routing NIHVIVO-1383

This commit is contained in:
bdc34 2010-12-03 22:37:02 +00:00
parent e398bdd79a
commit 7831351b94
3 changed files with 69 additions and 40 deletions

View file

@ -2,6 +2,9 @@
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.LogFactory;
@ -14,12 +17,11 @@ import com.hp.hpl.jena.query.QuerySolutionMap;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Literal;
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.VitroVocabulary;
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 {
@ -34,7 +36,7 @@ public class MenuDaoJena extends JenaBaseDao implements MenuDao {
static final protected String menuQueryString =
prefixes + "\n" +
"SELECT ?menuItem ?linkText ?urlMapping WHERE {\n" +
"SELECT ?menuItem ?linkText ?urlMapping WHERE {\n" +
// " GRAPH ?g{\n"+
" ?menu rdf:type display:MainMenu .\n"+
" ?menu display:hasElement ?menuItem . \n"+
@ -67,7 +69,7 @@ public class MenuDaoJena extends JenaBaseDao implements MenuDao {
}
protected MainMenu getMenu(Model displayModel, String url){
protected MainMenu getMenu(Model displayModel, String url){
//setup query parameters
QuerySolutionMap initialBindings = new QuerySolutionMap();
@ -75,15 +77,26 @@ public class MenuDaoJena extends JenaBaseDao implements MenuDao {
QueryExecution qexec = QueryExecutionFactory.create(menuQuery, displayModel, initialBindings );
try{
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();
for( ; results.hasNext();){
QuerySolution soln = results.nextSolution();
Literal itemText = soln.getLiteral("linkText");
Literal itemLink = soln.getLiteral("urlMapping");
String text = itemText != null ? itemText.getLexicalForm():"(undefined text)";
String link = itemLink != null ? itemLink.getLexicalForm():"undefinedLink";
Literal itemLink = soln.getLiteral("urlMapping");
RDFNode menuItem = soln.getResource("menuItem");
menu.addItem(text,link, isActive( url, link ));
String text = itemText != null ? itemText.getLexicalForm():"(undefined text)";
String link = itemLink != null ? itemLink.getLexicalForm():"undefinedLink";
String menuItemUri = PageDaoJena.nodeToString(menuItem);
if( !seenMenuItems.contains(menuItemUri) ){
menu.addItem(text,link, isActive( url, link ));
seenMenuItems.add( menuItemUri );
}
}
return menu;
}catch(Throwable th){
@ -94,6 +107,9 @@ public class MenuDaoJena extends JenaBaseDao implements MenuDao {
protected boolean isActive(String url, String link){
return url.startsWith(link);
if( "/".equals(url) )
return "/".equals(link);
else
return url.startsWith(link);
}
}

View file

@ -188,7 +188,7 @@ public class PageDaoJena extends JenaBaseDao implements PageDao {
return map;
}
protected Object nodeToObject( RDFNode node ){
static protected Object nodeToObject( RDFNode node ){
if( node == null ){
return "";
}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 ){
return "";
}else if( node.isLiteral() ){

View file

@ -4,6 +4,8 @@ package edu.cornell.mannlib.vitro.webapp.filters;
import java.io.IOException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
@ -30,16 +32,19 @@ import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
* It should only be applied to requests, not forwards, includes or errors.
*/
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 HOME_CONTROLLER_NAME = "HomePageController";
protected final Pattern urlPartPattern = Pattern.compile(URL_PART_PATTERN);
@Override
public void init(FilterConfig arg0) throws ServletException {
this.filterConfig = arg0;
this.filterConfig = arg0;
log.debug("pageRoutingFilter setup");
}
@ -56,36 +61,44 @@ public class PageRoutingFilter implements Filter{
// check for first part of path
// ex. /hats/superHat -> /hats
String path1 = path;
if( path != null && path.indexOf("/",1) > 0 ){
path1 = path.substring(0,path1.indexOf("/"));
}
String pageUri = urlMappings.get(path1);
//try it with a leading slash
if( pageUri == null ){
pageUri = urlMappings.get("/"+path1);
}
if( pageUri != null && ! pageUri.isEmpty() ){
log.debug(path + "is a request to a page defined in the display model as " + pageUri );
//add the pageUri to the request scope for use by the PageController
PageController.putPageUri(req, pageUri);
Matcher m = urlPartPattern.matcher(path);
if( m.matches() && m.groupCount() >= 1){
String path1stPart = m.group(1);
String pageUri = urlMappings.get(path1stPart);
//This will send requests to HomePageController or PageController
String controllerName = getControllerToForwardTo(req, pageUri, pageDao);
log.debug(path + " is being forwarded to controller " + controllerName);
//try it with a leading slash?
if( pageUri == null )
pageUri = urlMappings.get("/"+path1stPart);
RequestDispatcher rd = filterConfig.getServletContext().getNamedDispatcher( controllerName );
rd.forward(req, response);
if( pageUri != null && ! pageUri.isEmpty() ){
log.debug(path + "is a request to a page defined in the display model as " + pageUri );
//add the pageUri to the request scope for use by the PageController
PageController.putPageUri(req, pageUri);
//This will send requests to HomePageController or PageController
String controllerName = getControllerToForwardTo(req, pageUri, pageDao);
log.debug(path + " is being forwarded to controller " + controllerName);
RequestDispatcher rd = filterConfig.getServletContext().getNamedDispatcher( controllerName );
rd.forward(req, response);
}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( HOME_CONTROLLER_NAME );
rd.forward(req, response);
}else{
doNonDisplayPage(path,arg0,arg1,chain);
}
}else{
log.debug(path + "this isn't a request to a page defined in the display model, handle it normally.");
chain.doFilter(arg0, arg1);
doNonDisplayPage(path,arg0,arg1,chain);
}
}
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,
String pageUri, PageDao pageDao) {
String homePageUri = pageDao.getHomePageUri();