Moved url utility methods into Routes class
This commit is contained in:
parent
b7d2027da4
commit
f6b3acd831
9 changed files with 131 additions and 109 deletions
|
@ -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);
|
||||
|
|
|
@ -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<String, String> urls = new HashMap<String, String>();
|
||||
|
||||
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<String, String> portalParam = new HashMap<String, String>();
|
||||
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<String, String> logoutParams = new HashMap<String, String>(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<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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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) {
|
||||
|
|
|
@ -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<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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -9,6 +9,10 @@ import java.util.Collections;
|
|||
import javax.servlet.ServletContext;
|
||||
|
||||
public class ThemeUtils {
|
||||
|
||||
private ThemeUtils() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
public static ArrayList<String> getThemes(ServletContext sc, boolean doSort) {
|
||||
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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<String, String> params) {
|
||||
return FreeMarkerHttpServlet.getUrl(path, params);
|
||||
return Routes.getUrl(path, params);
|
||||
}
|
||||
|
||||
protected String urlEncode(String str) {
|
||||
return FreeMarkerHttpServlet.urlEncode(str);
|
||||
return Routes.urlEncode(str);
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -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<String, String> params = new HashMap<String, String>();
|
||||
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<String, String> params = new HashMap<String, String>();
|
||||
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));
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue