From f6b3acd8314ca85749fdee9688e4a5c47eb3a22f Mon Sep 17 00:00:00 2001 From: rjy7 Date: Wed, 26 May 2010 16:04:46 +0000 Subject: [PATCH] Moved url utility methods into Routes class --- .../freemarker/ContactMailController.java | 2 +- .../freemarker/FreeMarkerHttpServlet.java | 79 ++-------------- .../freemarker/FreeMarkerSetup.java | 10 +- .../webapp/controller/freemarker/Routes.java | 93 ++++++++++++++++--- .../vitro/webapp/utils/StringUtils.java | 4 + .../vitro/webapp/utils/ThemeUtils.java | 4 + .../vitro/webapp/view/IndividualView.java | 4 +- .../mannlib/vitro/webapp/view/ViewObject.java | 10 +- ...erHttpServletTest.java => RoutesTest.java} | 34 ++++--- 9 files changed, 131 insertions(+), 109 deletions(-) rename webapp/test/edu/cornell/mannlib/vitro/webapp/controller/freemarker/{FreeMarkerHttpServletTest.java => RoutesTest.java} (58%) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ContactMailController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ContactMailController.java index 972a569e9..f06123c81 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ContactMailController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ContactMailController.java @@ -235,7 +235,7 @@ public class ContactMailController extends FreeMarkerHttpServlet { email.put("comments", comments); email.put("ip", ipAddr); if ( !(originalReferer == null || originalReferer.equals("none")) ) { - email.put("referrer", urlDecode(originalReferer)); + email.put("referrer", Routes.urlDecode(originalReferer)); } return mergeBodyToTemplate(template, email); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreeMarkerHttpServlet.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreeMarkerHttpServlet.java index ad990268c..347e56786 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreeMarkerHttpServlet.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreeMarkerHttpServlet.java @@ -6,17 +6,9 @@ import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; -import java.io.UnsupportedEncodingException; -import java.net.URLDecoder; -import java.net.URLEncoder; -import java.util.ArrayList; import java.util.Calendar; import java.util.HashMap; -import java.util.Iterator; -import java.util.List; import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import javax.servlet.ServletContext; import javax.servlet.ServletException; @@ -39,7 +31,6 @@ import edu.cornell.mannlib.vitro.webapp.view.fileList.StylesheetList; import edu.cornell.mannlib.vitro.webapp.view.menu.TabMenu; import edu.cornell.mannlib.vitro.webapp.web.BreadCrumbsUtil; import edu.cornell.mannlib.vitro.webapp.web.PortalWebUtil; - import freemarker.cache.ClassTemplateLoader; import freemarker.cache.FileTemplateLoader; import freemarker.cache.MultiTemplateLoader; @@ -56,7 +47,6 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet { private static final int FILTER_SECURITY_LEVEL = LoginFormBean.EDITOR; protected static Configuration config = null; - protected static String contextPath = null; protected static ServletContext context = null; protected VitroRequest vreq; @@ -179,31 +169,29 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet { // view to control which links go where, and the link text and title. Map urls = new HashMap(); - String rootBreadCrumbUrl = portal.getRootBreadCrumbURL(); - String homeUrl = StringUtils.isEmpty(rootBreadCrumbUrl) ? contextPath : rootBreadCrumbUrl; - urls.put("home", homeUrl); + urls.put("home", Routes.getHomeUrl(portal)); String bannerImage = portal.getBannerImage(); if ( ! StringUtils.isEmpty(bannerImage)) { - root.put("bannerImage", getUrl(themeDir + "site_icons/" + bannerImage)); + root.put("bannerImage", Routes.getUrl(themeDir + "site_icons/" + bannerImage)); } Map portalParam = new HashMap(); portalParam.put("home", "" + portalId); - urls.put("about", getUrl(Routes.ABOUT, portalParam)); + urls.put("about", Routes.getUrl(Routes.ABOUT, portalParam)); if (ContactMailServlet.getSmtpHostFromProperties() != null) { - urls.put("contact", getUrl(Routes.CONTACT, portalParam)); + urls.put("contact", Routes.getUrl(Routes.CONTACT, portalParam)); } - urls.put("search", getUrl(Routes.SEARCH)); - urls.put("termsOfUse", getUrl(Routes.TERMS_OF_USE, portalParam)); - urls.put("login", getUrl(Routes.LOGIN)); + urls.put("search", Routes.getUrl(Routes.SEARCH)); + urls.put("termsOfUse", Routes.getUrl(Routes.TERMS_OF_USE, portalParam)); + urls.put("login", Routes.getUrl(Routes.LOGIN)); Map logoutParams = new HashMap(portalParam); logoutParams.put("loginSubmitMode", "Log Out"); - urls.put("logout", getUrl(Routes.LOGOUT, logoutParams)); + urls.put("logout", Routes.getUrl(Routes.LOGOUT, logoutParams)); - urls.put("siteAdmin", getUrl(Routes.SITE_ADMIN)); + urls.put("siteAdmin", Routes.getUrl(Routes.SITE_ADMIN)); setSharedVariable("urls", urls); } @@ -254,7 +242,7 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet { // attribute from the string passed in by the template automatically add the context path. setSharedVariable("stylesheetDir", themeDir + "/css"); - String themeDirWithContext = getUrl(themeDir); + String themeDirWithContext = Routes.getUrl(themeDir); // This value is used only in stylesheets.ftl and already contains the context path. root.put("stylesheetPath", themeDirWithContext + "/css"); @@ -370,51 +358,4 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet { request.setAttribute("ftl_footer", fcg.getFooter()); } - /* ******************** Static utilities ******************* */ - - public static String getUrl(String path) { - if ( ! path.startsWith("/") ) { - path = "/" + path; - } - return contextPath + path; - } - - public static String getUrl(String path, Map params) { - String url = getUrl(path); - - Iterator i = params.keySet().iterator(); - String key, value; - String glue = "?"; - while (i.hasNext()) { - key = i.next(); - value = params.get(key); - url += glue + key + "=" + urlEncode(value); - glue = "&"; - } - - return url; - } - - public static String urlEncode(String url) { - String encoding = "ISO-8859-1"; - String encodedUrl = null; - try { - encodedUrl = URLEncoder.encode(url, encoding); - } catch (UnsupportedEncodingException e) { - log.error("Error encoding url " + url + " with encoding " + encoding + ": Unsupported encoding."); - } - return encodedUrl; - } - - public static String urlDecode(String url) { - String encoding = "ISO-8859-1"; - String decodedUrl = null; - try { - decodedUrl = URLDecoder.decode(url, encoding); - } catch (UnsupportedEncodingException e) { - log.error("Error decoding url " + url + " with encoding " + encoding + ": Unsupported encoding."); - } - return decodedUrl; - } - } \ No newline at end of file diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreeMarkerSetup.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreeMarkerSetup.java index fe87e997b..fe3f4cc93 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreeMarkerSetup.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreeMarkerSetup.java @@ -51,12 +51,10 @@ public class FreeMarkerSetup implements ServletContextListener { } FreeMarkerHttpServlet.config = cfg; - - String contextPath = sc.getContextPath(); - FreeMarkerHttpServlet.contextPath = contextPath; - FreeMarkerHttpServlet.context = sc; - // ViewObject.contextPath = contextPath; - + FreeMarkerHttpServlet.context = sc; + + Routes.contextPath = sc.getContextPath(); + } public void contextDestroyed(ServletContextEvent event) { diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/Routes.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/Routes.java index 481e293bc..a3bc2e061 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/Routes.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/Routes.java @@ -2,23 +2,88 @@ package edu.cornell.mannlib.vitro.webapp.controller.freemarker; -// For now this class just holds constants for creating links. Could later be used to implement custom routing, -// if we go that route. Separating from Controllers to keep track of which ones are being used with FreeMarker -// Controllers; can recombine later if desired. -public class Routes { +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.net.URLEncoder; +import java.util.Map; - public static final String ABOUT = "/about"; - public static final String BROWSE = "/browse"; - public static final String CONTACT = "/contact"; - public static final String INDIVIDUAL = "/individual"; - public static final String INDIVIDUAL_LIST = "/entitylist"; //"/individuallist"; - public static final String SEARCH = "/search"; - public static final String TERMS_OF_USE = "/termsOfUse"; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import edu.cornell.mannlib.vitro.webapp.beans.Portal; +import edu.cornell.mannlib.vitro.webapp.utils.StringUtils; + +public class Routes { + + private Routes() { + throw new AssertionError(); + } + + private static final Log log = LogFactory.getLog(Routes.class.getName()); + + protected static String contextPath = null; + + protected static final String ABOUT = "/about"; + protected static final String BROWSE = "/browse"; + protected static final String CONTACT = "/contact"; + protected static final String SEARCH = "/search"; + protected static final String TERMS_OF_USE = "/termsOfUse"; // Put these under /admin/... // Currently login and site admin are on the same page, but they don't have to be. - public static final String LOGIN = "/siteAdmin"; - public static final String LOGOUT = "/login_process.jsp"; - public static final String SITE_ADMIN = "/siteAdmin"; + protected static final String LOGIN = "/siteAdmin"; + protected static final String LOGOUT = "/login_process.jsp"; + protected static final String SITE_ADMIN = "/siteAdmin"; + + // Public values are used by view objects + public static final String INDIVIDUAL = "/individual"; + public static final String INDIVIDUAL_LIST = "/entitylist"; // "/entitylist"; "/individuallist"; + + + public static String getHomeUrl(Portal portal) { + String rootBreadCrumbUrl = portal.getRootBreadCrumbURL(); + return StringUtils.isEmpty(rootBreadCrumbUrl) ? contextPath : rootBreadCrumbUrl; + } + + public static String getUrl(String path) { + if ( ! path.startsWith("/") ) { + path = "/" + path; + } + return contextPath + path; + } + + public static String getUrl(String path, Map params) { + + String url = getUrl(path); + String glue = "?"; + for (String key : params.keySet()) { + url += glue + key + "=" + urlEncode(params.get(key)); + glue = "&"; + } + return url; + } + + public static String urlEncode(String url) { + String encoding = "ISO-8859-1"; + String encodedUrl = null; + try { + encodedUrl = URLEncoder.encode(url, encoding); + } catch (UnsupportedEncodingException e) { + log.error("Error encoding url " + url + " with encoding " + encoding + ": Unsupported encoding."); + } + return encodedUrl; + } + + public static String urlDecode(String url) { + String encoding = "ISO-8859-1"; + String decodedUrl = null; + try { + decodedUrl = URLDecoder.decode(url, encoding); + } catch (UnsupportedEncodingException e) { + log.error("Error decoding url " + url + " with encoding " + encoding + ": Unsupported encoding."); + } + return decodedUrl; + } + } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/StringUtils.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/StringUtils.java index 9dfd13e69..17c739b14 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/StringUtils.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/StringUtils.java @@ -7,6 +7,10 @@ import java.util.Arrays; import java.util.List; public class StringUtils { + + private StringUtils() { + throw new AssertionError(); + } public static String capitalize(String s) { return s.substring(0, 1).toUpperCase() + s.substring(1); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/ThemeUtils.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/ThemeUtils.java index 1700187ff..0fa3038c3 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/ThemeUtils.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/ThemeUtils.java @@ -9,6 +9,10 @@ import java.util.Collections; import javax.servlet.ServletContext; public class ThemeUtils { + + private ThemeUtils() { + throw new AssertionError(); + } public static ArrayList getThemes(ServletContext sc, boolean doSort) { diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/view/IndividualView.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/view/IndividualView.java index 7aa43b381..42aae0e6f 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/view/IndividualView.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/view/IndividualView.java @@ -44,13 +44,13 @@ public class IndividualView extends ViewObject { return getUrl("/individual/" + individual.getLocalName()); } - public String getShortView() { + public String getListView() { // TODO // iterate through class hierarchy looking for a custom short view. If none, use // default individual short view. template will just do an include on individual.shortView // Use individual.getVClasses() - this is the class hierarchy // Question: what order are they returned in ? If from specific to general, break out of the iteration as soon as we find one. - return null; + return "defaultIndividualListView.ftl"; } public String getCustomView() { diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/view/ViewObject.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/view/ViewObject.java index 1df6ef2e0..6627b6ff6 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/view/ViewObject.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/view/ViewObject.java @@ -2,14 +2,12 @@ package edu.cornell.mannlib.vitro.webapp.view; -import java.util.ArrayList; -import java.util.List; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreeMarkerHttpServlet; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.Routes; // RY We may want an interface that the superclass would implement. // RY Consider using FreeMarker's object wrappers instead. @@ -22,15 +20,15 @@ public abstract class ViewObject { // public static String contextPath; protected String getUrl(String path) { - return FreeMarkerHttpServlet.getUrl(path); + return Routes.getUrl(path); } protected String getUrl(String path, Map params) { - return FreeMarkerHttpServlet.getUrl(path, params); + return Routes.getUrl(path, params); } protected String urlEncode(String str) { - return FreeMarkerHttpServlet.urlEncode(str); + return Routes.urlEncode(str); } /* diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreeMarkerHttpServletTest.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/controller/freemarker/RoutesTest.java similarity index 58% rename from webapp/test/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreeMarkerHttpServletTest.java rename to webapp/test/edu/cornell/mannlib/vitro/webapp/controller/freemarker/RoutesTest.java index 688b3eddc..3456f88b8 100644 --- a/webapp/test/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreeMarkerHttpServletTest.java +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/controller/freemarker/RoutesTest.java @@ -13,41 +13,53 @@ import edu.cornell.mannlib.vitro.testing.AbstractTestClass; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreeMarkerHttpServlet; import freemarker.template.Configuration; -public class FreeMarkerHttpServletTest extends AbstractTestClass { - - static { - Configuration cfg = new Configuration(); - FreeMarkerHttpServlet.config = cfg; - FreeMarkerHttpServlet.contextPath = "/vivo"; - } +public class RoutesTest extends AbstractTestClass { @Test public void testGetUrl() { + Routes.contextPath = "/vivo"; + String path1 = "/individual"; - Assert.assertEquals("/vivo/individual", FreeMarkerHttpServlet.getUrl(path1)); + Assert.assertEquals("/vivo/individual", Routes.getUrl(path1)); int portalId = 1; String path2 = "/individual?home=" + portalId; - Assert.assertEquals("/vivo/individual?home=1", FreeMarkerHttpServlet.getUrl(path2)); + Assert.assertEquals("/vivo/individual?home=1", Routes.getUrl(path2)); + } + + @Test + public void testGetUrlWithEmptyContext() { + Routes.contextPath = ""; + String path = "/individual"; + Assert.assertEquals(path, Routes.getUrl(path)); } @Test public void testGetUrlWithParams() { + Routes.contextPath = "/vivo"; String path = "/individual"; Map params = new HashMap(); int portalId = 1; params.put("home", "" + portalId); params.put("name", "Tom"); - Assert.assertEquals("/vivo/individual?home=1&name=Tom", FreeMarkerHttpServlet.getUrl(path, params)); + Assert.assertEquals("/vivo/individual?home=1&name=Tom", Routes.getUrl(path, params)); } @Test public void testEncodeUrl() { + Routes.contextPath = "/vivo"; String path = "/individuallist"; Map params = new HashMap(); String vClassUri = "http://vivoweb.org/ontology/core#FacultyMember"; params.put("vclassId", vClassUri); - Assert.assertEquals("/vivo/individuallist?vclassId=http%3A%2F%2Fvivoweb.org%2Fontology%2Fcore%23FacultyMember", FreeMarkerHttpServlet.getUrl(path, params)); + Assert.assertEquals("/vivo/individuallist?vclassId=http%3A%2F%2Fvivoweb.org%2Fontology%2Fcore%23FacultyMember", Routes.getUrl(path, params)); + } + + @Test + public void testDecodeUrl() { + String vClassUri = "http://vivoweb.org/ontology/core#FacultyMember"; + String vClassUriEncoded = "http%3A%2F%2Fvivoweb.org%2Fontology%2Fcore%23FacultyMember"; + Assert.assertEquals(vClassUri, Routes.urlDecode(vClassUriEncoded)); } }