Use FreeMarker controller and templates for About and Index pages. Improvements to FreeMarkerHttpServlet.getUrl() method. Unit tests for FreeMarkerHttpServlet static methods.

This commit is contained in:
rjy7 2010-05-17 17:26:40 +00:00
parent 92f9280af0
commit 046ca60439
9 changed files with 140 additions and 68 deletions

View file

@ -266,22 +266,13 @@
<servlet>
<servlet-name>AboutController</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.AboutController</servlet-class>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.AboutControllerFM</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AboutController</servlet-name>
<url-pattern>/about</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>AboutControllerFM</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.AboutControllerFM</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AboutControllerFM</servlet-name>
<url-pattern>/about-fm</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>FakeSelfEditController</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.FakeSelfEditController</servlet-class>
@ -968,16 +959,14 @@
<servlet>
<servlet-name>browsecontroller</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.BrowseController</servlet-class>
<load-on-startup>5</load-on-startup>
</servlet>
<servlet>
<servlet-name>browsecontrollerFM</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.BrowseControllerFM</servlet-class>
<load-on-startup>5</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>browsecontroller</servlet-name>
<url-pattern>/browse</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>pubsbyorg</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.vclass.PubsByDepartmentServlet</servlet-class>
@ -1090,14 +1079,6 @@
<servlet-name>logout</servlet-name>
<url-pattern>/logout</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>browsecontroller</servlet-name>
<url-pattern>/browsecontroller</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>browsecontrollerFM</servlet-name>
<url-pattern>/browse</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>pubsbyorg</servlet-name>
<url-pattern>/pubsbyorg</url-pattern>

View file

@ -15,8 +15,10 @@ import java.util.List;
*
*/
public class Controllers {
//servlet urls go here:
public class Controllers {
// Servlet urls
public static final String ENTITY = "/entity";
public static final String ENTITY_PROP_LIST = "/entityPropList";
public static final String ENTITY_LIST = "/EntityList";
@ -31,7 +33,12 @@ public class Controllers {
public static final String LOGIN = "/siteAdmin";
public static final String LOGOUT = "/siteAdmin";
public static final String EXPORT_RDF = "/export";
public static final String EXPORT_RDF = "/export";
public static final String CONTACT_URL = "/comments";
public static final String TERMS_OF_USE_URL = "/termsOfUse";
public static final String BROWSE = "/browse";
public static final String INDIVIDUAL_LIST_URL = "/entitylist"; // will change to individuallist
// jsps go here:
@ -86,9 +93,8 @@ public class Controllers {
public static final String TOGGLE_SCRIPT_ELEMENT = "<script language='JavaScript' type='text/javascript' src='js/toggle.js'></script>";
public static final Object SEARCH_ERROR_JSP = "/search_error.jsp";
public static final Object SEARCH_ERROR_JSP = "/search_error.jsp";
public static final String CONTACT_URL = "/comments";
//public static final String TAB_ENTITIES_LIST_JSP = "templates/tab/tabEntities.jsp";

View file

@ -6,11 +6,15 @@ import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
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.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -242,13 +246,16 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet {
if ( ! StringUtils.isEmpty(bannerImage)) {
root.put("bannerImage", getUrl(themeDir + "site_icons/" + bannerImage));
}
Map<String, String> portalParam = new HashMap<String, String>();
portalParam.put("home", "" + portalId);
urls.put("about", getUrl(Controllers.ABOUT + "?home=" + portalId));
urls.put("about", getUrl(Controllers.ABOUT, portalParam));
if (ContactMailServlet.getSmtpHostFromProperties() != null) {
urls.put("contact", getUrl(Controllers.CONTACT_URL + "?home=" + portalId));
urls.put("contact", getUrl(Controllers.CONTACT_URL, portalParam));
}
urls.put("search", getUrl(Controllers.SEARCH_URL));
urls.put("termsOfUse", getUrl("/termsOfUse?home=" + portalId));
urls.put("termsOfUse", getUrl(Controllers.TERMS_OF_USE_URL, portalParam));
urls.put("login", getUrl(Controllers.LOGIN));
urls.put("logout", getUrl(Controllers.LOGOUT));
urls.put("siteAdmin", getUrl(Controllers.SITE_ADMIN));
@ -318,14 +325,7 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet {
}
}
public static String getUrl(String path) {
if ( ! path.startsWith("/") ) {
path = "/" + path;
}
return contextPath + path;
}
private TabMenu getTabMenu(int portalId) {
return new TabMenu(vreq, portalId);
}
@ -343,4 +343,41 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet {
request.setAttribute("ftl_footer", fcg.getFooter());
}
/* ******************** 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;
}
}

View file

@ -35,6 +35,7 @@ public class IndividualView extends ViewObject {
// Or maybe getProfileUrl - there might be other kinds of urls
// e.g., getEditUrl, getDeleteUrl - these would return the computations of PropertyEditLinks
// Just call getUrl...
public String getProfileUrl() {
return contextPath + URL + ""; // ADD IN the label from the individual's uri
}

View file

@ -12,15 +12,6 @@ import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup;
/*
* A VClassGroupDisplay object is associated with a VClassGroup for display.
* It is an object that contains a linked list, rather than a type of linked list,
* so that JSP EL can access properties such as publicName.
*
* RY We may want an abstract display class as a superclass.
* RY We may want an interface that the superclass would implement.
* RY We may want to nest this class in the VClassGroup class.
*/
public class VClassGroupView extends ViewObject {
private static final Log log = LogFactory.getLog(VClassGroupView.class.getName());
@ -54,6 +45,7 @@ public class VClassGroupView extends ViewObject {
public List<VClassView> getClasses() {
// Do we need to store the classes as an instance member? Would we ever access this method more than once per template?
// Don't do this in the constructor, since we might not need it.
if (classes == null) {
List<VClass> classList = vClassGroup.getVitroClassList();
classes = new ArrayList<VClassView>();

View file

@ -2,15 +2,19 @@
package edu.cornell.mannlib.vitro.webapp.view;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
import edu.cornell.mannlib.vitro.webapp.controller.Controllers;
public class VClassView extends ViewObject {
private static final Log log = LogFactory.getLog(VClassView.class.getName());
private static final String URL = "/individuallistFM?vclassId=";
private static final String URL = Controllers.INDIVIDUAL_LIST_URL;
private VClass vclass;
@ -23,7 +27,9 @@ public class VClassView extends ViewObject {
}
public String getUrl() {
return contextPath + URL + encodeUrl(vclass.getURI());
Map<String, String> params = new HashMap<String, String>();
params.put("vclassId", vclass.getURI());
return getUrl(URL, params);
}
public int getEntityCount() {

View file

@ -2,36 +2,33 @@
package edu.cornell.mannlib.vitro.webapp.view;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
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.controller.FreeMarkerHttpServlet;
import edu.cornell.mannlib.vitro.webapp.utils.StringUtils;
// RY We may want an interface that the superclass would implement.
// RY Consider using FreeMarker's DisplayObjectWrapper instead, or extending it.
public abstract class ViewObject {
private static final Log log = LogFactory.getLog(ViewObject.class.getName());
// RY Can probably remove this, since we're using the FreeMarkerHttpServlet methods instead
public static String contextPath;
protected static String getUrl(String path) {
protected String getUrl(String path) {
return FreeMarkerHttpServlet.getUrl(path);
}
protected String encodeUrl(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;
protected String getUrl(String path, Map<String, String> params) {
return FreeMarkerHttpServlet.getUrl(path, params);
}
protected String urlEncode(String str) {
return FreeMarkerHttpServlet.urlEncode(str);
}
}

View file

@ -44,7 +44,7 @@ public class TabMenu extends MainMenu {
// Hard-coded tabs. It's not really a good idea to have these here, since any menu item that doesn't
// come from the db should be accessible to the template to change the text. But we need them here
// to apply the "active" mechanism.
addItem("Index", "/browsecontroller");
addItem("Index", "/browse");
}
}

View file

@ -0,0 +1,52 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.controller;
import java.util.HashMap;
import java.util.Map;
import junit.framework.Assert;
import org.junit.Test;
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
import freemarker.template.Configuration;
public class FreeMarkerHttpServletTest extends AbstractTestClass {
static {
Configuration cfg = new Configuration();
FreeMarkerHttpServlet.config = cfg;
FreeMarkerHttpServlet.contextPath = "/vivo";
}
@Test
public void testGetUrl() {
String path1 = "/individual";
Assert.assertEquals("/vivo/individual", FreeMarkerHttpServlet.getUrl(path1));
int portalId = 1;
String path2 = "/individual?home=" + portalId;
Assert.assertEquals("/vivo/individual?home=1", FreeMarkerHttpServlet.getUrl(path2));
}
@Test
public void testGetUrlWithParams() {
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));
}
@Test
public void testEncodeUrl() {
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));
}
}