You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by nb...@apache.org on 2006/11/14 21:35:07 UTC
svn commit: r474952 - in
/jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/servlet:
ServletToolInfo.java ServletToolboxManager.java ServletToolboxRuleSet.java
Author: nbubna
Date: Tue Nov 14 12:35:06 2006
New Revision: 474952
URL: http://svn.apache.org/viewvc?view=rev&rev=474952
Log:
VELTOOLS-67: allow users to configure a request path restriction for request and session scoped tools, thanks to Claude Brisson for the initial patch
Modified:
jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/servlet/ServletToolInfo.java
jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/servlet/ServletToolboxManager.java
jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/servlet/ServletToolboxRuleSet.java
Modified: jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/servlet/ServletToolInfo.java
URL: http://svn.apache.org/viewvc/jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/servlet/ServletToolInfo.java?view=diff&rev=474952&r1=474951&r2=474952
==============================================================================
--- jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/servlet/ServletToolInfo.java (original)
+++ jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/servlet/ServletToolInfo.java Tue Nov 14 12:35:06 2006
@@ -18,6 +18,7 @@
package org.apache.velocity.tools.view.servlet;
+import javax.servlet.http.HttpServletRequest;
import org.apache.velocity.tools.view.ViewToolInfo;
@@ -53,22 +54,86 @@
{
private String scope;
-
-
- public ServletToolInfo() {}
+ private boolean exactPath;
+ private String path;
public void setScope(String scope) {
this.scope = scope;
}
-
/**
* @return the scope of the tool
*/
public String getScope()
{
return scope;
+ }
+
+ /**
+ * @param path the full or partial request path restriction of the tool
+ * @since VelocityTools 1.3
+ */
+ public void setRequestPath(String path)
+ {
+ // make sure all paths begin with slash
+ if (!path.startsWith("/"))
+ {
+ path = "/" + path;
+ }
+
+ if (path.equals("/*"))
+ {
+ // match all paths
+ this.path = null;
+ }
+ else if(path.endsWith("*"))
+ {
+ // match some paths
+ exactPath = false;
+ this.path = path.substring(0, scope.length() - 1);
+ }
+ else
+ {
+ // match one path
+ exactPath = true;
+ this.path = path;
+ }
+ }
+
+ /**
+ * @return request path restriction for this tool
+ * @since VelocityTools 1.3
+ */
+ public String getRequestPath()
+ {
+ return this.path;
+ }
+
+ /**
+ * @param requestedPath the path of the current servlet request
+ * @return <code>true</code> if the path of the specified
+ * request path matches the request path of this tool.
+ * If there is no request path restriction for this tool,
+ * it will always return <code>true</code>.
+ * @since VelocityTools 1.3
+ */
+ public boolean allowsRequestPath(String requestedPath)
+ {
+ if (this.path == null)
+ {
+ return true;
+ }
+
+ if (exactPath)
+ {
+ return this.path.equals(requestedPath);
+ }
+ else if (requestedPath != null)
+ {
+ return requestedPath.startsWith(this.path);
+ }
+ return false;
}
}
Modified: jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/servlet/ServletToolboxManager.java
URL: http://svn.apache.org/viewvc/jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/servlet/ServletToolboxManager.java?view=diff&rev=474952&r1=474951&r2=474952
==============================================================================
--- jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/servlet/ServletToolboxManager.java (original)
+++ jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/servlet/ServletToolboxManager.java Tue Nov 14 12:35:06 2006
@@ -268,6 +268,33 @@
return servletRuleSet;
}
+ /**
+ * Ensures that application-scoped tools do not have request path
+ * restrictions set for them, as those will not be enforced.
+ *
+ * @param info a ToolInfo object
+ * @return true if the ToolInfo is valid
+ * @since VelocityTools 1.3
+ */
+ protected boolean validateToolInfo(ToolInfo info)
+ {
+ if (!super.validateToolInfo(info))
+ {
+ return false;
+ }
+ if (info instanceof ServletToolInfo)
+ {
+ ServletToolInfo sti = (ServletToolInfo)info;
+ if (ViewContext.APPLICATION.equalsIgnoreCase(sti.getScope()) &&
+ sti.getRequestPath() != null)
+ {
+ LOG.error("Application-scoped tool " + sti.getKey() + " cannot have a request path restriction!");
+ return false;
+ }
+ }
+ return true;
+ }
+
/**
* Overrides XMLToolboxManager to separate tools by scope.
@@ -341,7 +368,8 @@
{
//we know the initData is a ViewContext
ViewContext ctx = (ViewContext)initData;
-
+ String requestPath = ServletUtils.getPath(ctx.getRequest());
+System.out.println("path: "+requestPath);
//create the toolbox map with the application tools in it
Map toolbox = new HashMap(appTools);
@@ -362,8 +390,11 @@
Iterator i = sessionToolInfo.iterator();
while(i.hasNext())
{
- ToolInfo ti = (ToolInfo)i.next();
- stmap.put(ti.getKey(), ti.getInstance(ctx));
+ ServletToolInfo sti = (ServletToolInfo)i.next();
+ if (sti.allowsRequestPath(requestPath))
+ {
+ stmap.put(sti.getKey(), sti.getInstance(ctx));
+ }
}
session.setAttribute(SESSION_TOOLS_KEY, stmap);
}
@@ -378,6 +409,14 @@
while(i.hasNext())
{
ToolInfo info = (ToolInfo)i.next();
+ if (info instanceof ServletToolInfo)
+ {
+ ServletToolInfo sti = (ServletToolInfo)info;
+ if (!sti.allowsRequestPath(requestPath))
+ {
+ continue;
+ }
+ }
toolbox.put(info.getKey(), info.getInstance(ctx));
}
Modified: jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/servlet/ServletToolboxRuleSet.java
URL: http://svn.apache.org/viewvc/jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/servlet/ServletToolboxRuleSet.java?view=diff&rev=474952&r1=474951&r2=474952
==============================================================================
--- jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/servlet/ServletToolboxRuleSet.java (original)
+++ jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/servlet/ServletToolboxRuleSet.java Tue Nov 14 12:35:06 2006
@@ -57,6 +57,7 @@
{
super.addToolRules(digester);
digester.addBeanPropertySetter("toolbox/tool/scope", "scope");
+ digester.addBeanPropertySetter("toolbox/tool/request-path", "requestPath");
}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org