Moved url utility methods into Routes class

This commit is contained in:
rjy7 2010-05-26 16:04:46 +00:00
parent b7d2027da4
commit f6b3acd831
9 changed files with 131 additions and 109 deletions

View file

@ -235,7 +235,7 @@ public class ContactMailController extends FreeMarkerHttpServlet {
email.put("comments", comments); email.put("comments", comments);
email.put("ip", ipAddr); email.put("ip", ipAddr);
if ( !(originalReferer == null || originalReferer.equals("none")) ) { if ( !(originalReferer == null || originalReferer.equals("none")) ) {
email.put("referrer", urlDecode(originalReferer)); email.put("referrer", Routes.urlDecode(originalReferer));
} }
return mergeBodyToTemplate(template, email); return mergeBodyToTemplate(template, email);

View file

@ -6,17 +6,9 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; 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.Calendar;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletException; 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.view.menu.TabMenu;
import edu.cornell.mannlib.vitro.webapp.web.BreadCrumbsUtil; import edu.cornell.mannlib.vitro.webapp.web.BreadCrumbsUtil;
import edu.cornell.mannlib.vitro.webapp.web.PortalWebUtil; import edu.cornell.mannlib.vitro.webapp.web.PortalWebUtil;
import freemarker.cache.ClassTemplateLoader; import freemarker.cache.ClassTemplateLoader;
import freemarker.cache.FileTemplateLoader; import freemarker.cache.FileTemplateLoader;
import freemarker.cache.MultiTemplateLoader; import freemarker.cache.MultiTemplateLoader;
@ -56,7 +47,6 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet {
private static final int FILTER_SECURITY_LEVEL = LoginFormBean.EDITOR; private static final int FILTER_SECURITY_LEVEL = LoginFormBean.EDITOR;
protected static Configuration config = null; protected static Configuration config = null;
protected static String contextPath = null;
protected static ServletContext context = null; protected static ServletContext context = null;
protected VitroRequest vreq; 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. // view to control which links go where, and the link text and title.
Map<String, String> urls = new HashMap<String, String>(); Map<String, String> urls = new HashMap<String, String>();
String rootBreadCrumbUrl = portal.getRootBreadCrumbURL(); urls.put("home", Routes.getHomeUrl(portal));
String homeUrl = StringUtils.isEmpty(rootBreadCrumbUrl) ? contextPath : rootBreadCrumbUrl;
urls.put("home", homeUrl);
String bannerImage = portal.getBannerImage(); String bannerImage = portal.getBannerImage();
if ( ! StringUtils.isEmpty(bannerImage)) { if ( ! StringUtils.isEmpty(bannerImage)) {
root.put("bannerImage", getUrl(themeDir + "site_icons/" + bannerImage)); root.put("bannerImage", Routes.getUrl(themeDir + "site_icons/" + bannerImage));
} }
Map<String, String> portalParam = new HashMap<String, String>(); Map<String, String> portalParam = new HashMap<String, String>();
portalParam.put("home", "" + portalId); portalParam.put("home", "" + portalId);
urls.put("about", getUrl(Routes.ABOUT, portalParam)); urls.put("about", Routes.getUrl(Routes.ABOUT, portalParam));
if (ContactMailServlet.getSmtpHostFromProperties() != null) { 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("search", Routes.getUrl(Routes.SEARCH));
urls.put("termsOfUse", getUrl(Routes.TERMS_OF_USE, portalParam)); urls.put("termsOfUse", Routes.getUrl(Routes.TERMS_OF_USE, portalParam));
urls.put("login", getUrl(Routes.LOGIN)); urls.put("login", Routes.getUrl(Routes.LOGIN));
Map<String, String> logoutParams = new HashMap<String, String>(portalParam); Map<String, String> logoutParams = new HashMap<String, String>(portalParam);
logoutParams.put("loginSubmitMode", "Log Out"); 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); 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. // attribute from the string passed in by the template automatically add the context path.
setSharedVariable("stylesheetDir", themeDir + "/css"); 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. // This value is used only in stylesheets.ftl and already contains the context path.
root.put("stylesheetPath", themeDirWithContext + "/css"); root.put("stylesheetPath", themeDirWithContext + "/css");
@ -370,51 +358,4 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet {
request.setAttribute("ftl_footer", fcg.getFooter()); 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<String, String> params) {
String url = getUrl(path);
Iterator<String> 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;
}
} }

View file

@ -51,11 +51,9 @@ public class FreeMarkerSetup implements ServletContextListener {
} }
FreeMarkerHttpServlet.config = cfg; FreeMarkerHttpServlet.config = cfg;
FreeMarkerHttpServlet.context = sc;
String contextPath = sc.getContextPath(); Routes.contextPath = sc.getContextPath();
FreeMarkerHttpServlet.contextPath = contextPath;
FreeMarkerHttpServlet.context = sc;
// ViewObject.contextPath = contextPath;
} }

View file

@ -2,23 +2,88 @@
package edu.cornell.mannlib.vitro.webapp.controller.freemarker; 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, import java.io.UnsupportedEncodingException;
// if we go that route. Separating from Controllers to keep track of which ones are being used with FreeMarker import java.net.URLDecoder;
// Controllers; can recombine later if desired. import java.net.URLEncoder;
import java.util.Map;
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 { public class Routes {
public static final String ABOUT = "/about"; private Routes() {
public static final String BROWSE = "/browse"; throw new AssertionError();
public static final String CONTACT = "/contact"; }
public static final String INDIVIDUAL = "/individual";
public static final String INDIVIDUAL_LIST = "/entitylist"; //"/individuallist"; private static final Log log = LogFactory.getLog(Routes.class.getName());
public static final String SEARCH = "/search";
public static final String TERMS_OF_USE = "/termsOfUse"; 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/... // Put these under /admin/...
// Currently login and site admin are on the same page, but they don't have to be. // Currently login and site admin are on the same page, but they don't have to be.
public static final String LOGIN = "/siteAdmin"; protected static final String LOGIN = "/siteAdmin";
public static final String LOGOUT = "/login_process.jsp"; protected static final String LOGOUT = "/login_process.jsp";
public static final String SITE_ADMIN = "/siteAdmin"; 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<String, String> 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;
}
} }

View file

@ -8,6 +8,10 @@ import java.util.List;
public class StringUtils { public class StringUtils {
private StringUtils() {
throw new AssertionError();
}
public static String capitalize(String s) { public static String capitalize(String s) {
return s.substring(0, 1).toUpperCase() + s.substring(1); return s.substring(0, 1).toUpperCase() + s.substring(1);
} }

View file

@ -10,6 +10,10 @@ import javax.servlet.ServletContext;
public class ThemeUtils { public class ThemeUtils {
private ThemeUtils() {
throw new AssertionError();
}
public static ArrayList<String> getThemes(ServletContext sc, boolean doSort) { public static ArrayList<String> getThemes(ServletContext sc, boolean doSort) {
// Find the themes directory on the file system // Find the themes directory on the file system

View file

@ -44,13 +44,13 @@ public class IndividualView extends ViewObject {
return getUrl("/individual/" + individual.getLocalName()); return getUrl("/individual/" + individual.getLocalName());
} }
public String getShortView() { public String getListView() {
// TODO // TODO
// iterate through class hierarchy looking for a custom short view. If none, use // 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 // default individual short view. template will just do an include on individual.shortView
// Use individual.getVClasses() - this is the class hierarchy // 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. // 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() { public String getCustomView() {

View file

@ -2,14 +2,12 @@
package edu.cornell.mannlib.vitro.webapp.view; package edu.cornell.mannlib.vitro.webapp.view;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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 We may want an interface that the superclass would implement.
// RY Consider using FreeMarker's object wrappers instead. // RY Consider using FreeMarker's object wrappers instead.
@ -22,15 +20,15 @@ public abstract class ViewObject {
// public static String contextPath; // public static String contextPath;
protected String getUrl(String path) { protected String getUrl(String path) {
return FreeMarkerHttpServlet.getUrl(path); return Routes.getUrl(path);
} }
protected String getUrl(String path, Map<String, String> params) { protected String getUrl(String path, Map<String, String> params) {
return FreeMarkerHttpServlet.getUrl(path, params); return Routes.getUrl(path, params);
} }
protected String urlEncode(String str) { protected String urlEncode(String str) {
return FreeMarkerHttpServlet.urlEncode(str); return Routes.urlEncode(str);
} }
/* /*

View file

@ -13,41 +13,53 @@ import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreeMarkerHttpServlet; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreeMarkerHttpServlet;
import freemarker.template.Configuration; import freemarker.template.Configuration;
public class FreeMarkerHttpServletTest extends AbstractTestClass { public class RoutesTest extends AbstractTestClass {
static {
Configuration cfg = new Configuration();
FreeMarkerHttpServlet.config = cfg;
FreeMarkerHttpServlet.contextPath = "/vivo";
}
@Test @Test
public void testGetUrl() { public void testGetUrl() {
Routes.contextPath = "/vivo";
String path1 = "/individual"; String path1 = "/individual";
Assert.assertEquals("/vivo/individual", FreeMarkerHttpServlet.getUrl(path1)); Assert.assertEquals("/vivo/individual", Routes.getUrl(path1));
int portalId = 1; int portalId = 1;
String path2 = "/individual?home=" + portalId; 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 @Test
public void testGetUrlWithParams() { public void testGetUrlWithParams() {
Routes.contextPath = "/vivo";
String path = "/individual"; String path = "/individual";
Map<String, String> params = new HashMap<String, String>(); Map<String, String> params = new HashMap<String, String>();
int portalId = 1; int portalId = 1;
params.put("home", "" + portalId); params.put("home", "" + portalId);
params.put("name", "Tom"); 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 @Test
public void testEncodeUrl() { public void testEncodeUrl() {
Routes.contextPath = "/vivo";
String path = "/individuallist"; String path = "/individuallist";
Map<String, String> params = new HashMap<String, String>(); Map<String, String> params = new HashMap<String, String>();
String vClassUri = "http://vivoweb.org/ontology/core#FacultyMember"; String vClassUri = "http://vivoweb.org/ontology/core#FacultyMember";
params.put("vclassId", vClassUri); 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));
} }
} }