You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by nb...@apache.org on 2007/11/02 17:49:10 UTC
svn commit: r591396 - in /velocity/tools/trunk/src:
java/org/apache/velocity/tools/view/tools/LinkTool.java
test/org/apache/velocity/tools/test/blackbox/LinkToolTests.java
Author: nbubna
Date: Fri Nov 2 09:49:05 2007
New Revision: 591396
URL: http://svn.apache.org/viewvc?rev=591396&view=rev
Log:
VELTOOLS-89 add 'auto ignore' to LinkTool param copying (thx to Chris Schultz)
Modified:
velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/LinkTool.java
velocity/tools/trunk/src/test/org/apache/velocity/tools/test/blackbox/LinkToolTests.java
Modified: velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/LinkTool.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/LinkTool.java?rev=591396&r1=591395&r2=591396&view=diff
==============================================================================
--- velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/LinkTool.java (original)
+++ velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/LinkTool.java Fri Nov 2 09:49:05 2007
@@ -24,6 +24,7 @@
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -58,6 +59,7 @@
*
* @author <a href="mailto:sidler@teamup.com">Gabe Sidler</a>
* @author Nathan Bubna
+ * @author Chris Schultz
* @since VelocityTools 1.0
* @version $Id$
*/
@@ -77,6 +79,12 @@
*/
public static final String SELF_INCLUDE_PARAMETERS_KEY = "self-include-parameters";
+ /**
+ * Parameter key for configuring {@link #setAutoIgnoreParameters} state.
+ * @since VelocityTools 1.4
+ */
+ public static final String AUTO_IGNORE_PARAMETERS_KEY = "auto-ignore-parameters";
+
/** Standard HTML delimiter for query data ('&') */
public static final String HTML_QUERY_DELIMITER = "&";
@@ -119,7 +127,15 @@
* @see #addIgnore(String)
* @see #addAllParameters()
*/
- private ArrayList parametersToIgnore;
+ private HashSet parametersToIgnore;
+
+ /**
+ * <code>true</code> if parameters explicitly set on this LinkTool
+ * should be added to the ignore list.
+ *
+ * @see #parametersToIgnore
+ */
+ private boolean autoIgnore = true;
/** Java 1.4 encode method to use instead of deprecated 1.3 version. */
private static Method encode = null;
@@ -205,6 +221,30 @@
this.selfParams = selfParams;
}
+ /**
+ * <p>Controls whether or not the {@link #addQueryData(String,Object)}
+ * and {@link #addQueryData(Map)} methods will
+ * automatically add the specified parameter(s) to the ignore list
+ * for {@link #addAllParameters()}</p>
+ *
+ * <p>The default for this setting is <code>true</code>.</p>
+ *
+ * @param autoIgnore if true, the {@link #addQueryData(String,Object)}
+ * and {@link #addQueryData(Map)} methods will
+ * automatically add the specified parameter(s) to the ignore list
+ * for {@link #addAllParameters()}; otherwise,
+ * {@link #addIgnore(String)} must be called explicitly for each
+ * parameter to ignore.</p>
+ *
+ * @see #addIgnore(String)
+ * @see #addAllParameters()
+ *
+ * @since VelocityTools 1.4
+ */
+ protected void setAutoIgnoreParameters(boolean autoIgnore)
+ {
+ this.autoIgnore = autoIgnore;
+ }
/**
* For internal use.
@@ -228,6 +268,27 @@
}
//add new pair to this LinkTool's query data
copy.queryData.add(pair);
+
+ // Respect autoIgnore setting
+ if(autoIgnore)
+ {
+ if(copy.parametersToIgnore != null)
+ {
+ // Only clone the ignore list if we're actually going
+ // to modify it.
+ if(!copy.parametersToIgnore.contains(pair.getKey()))
+ {
+ copy.parametersToIgnore = (HashSet)this.parametersToIgnore.clone();
+ copy.parametersToIgnore.add(pair.getKey());
+ }
+ }
+ else
+ {
+ copy.parametersToIgnore = new HashSet();
+ copy.parametersToIgnore.add(pair.getKey());
+ }
+ }
+
return copy;
}
@@ -253,12 +314,43 @@
{
copy.queryData = new ArrayList();
}
+
+ boolean clonedIgnore = false;
+
for (Iterator i = newQueryData.keySet().iterator(); i.hasNext(); )
{
Object key = i.next();
Object value = newQueryData.get(key);
copy.queryData.add(new QueryPair(String.valueOf(key), value));
+
+ if(this.autoIgnore)
+ {
+ if(copy.parametersToIgnore != null)
+ {
+ // Only clone the ignore list if we're actually going
+ // to modify it.
+ if(!copy.parametersToIgnore.contains(key))
+ {
+ if(!clonedIgnore)
+ {
+ copy.parametersToIgnore
+ = (HashSet)this.parametersToIgnore.clone();
+
+ clonedIgnore = true;
+ }
+
+ copy.parametersToIgnore.add(key);
+ }
+ }
+ else
+ {
+ copy.parametersToIgnore = new HashSet();
+ copy.parametersToIgnore.add(key);
+ clonedIgnore = true;
+ }
+ }
}
+
return copy;
}
@@ -305,7 +397,7 @@
* @return A new LinkTool with the specified parameterName added to the
* ignore list.
* @see #addAllParameters()
- * @see #addIgnore()
+ * @see #addIgnore(String)
*/
protected LinkTool copyWithIgnore(String parameterName)
{
@@ -313,11 +405,11 @@
if(copy.parametersToIgnore == null)
{
- copy.parametersToIgnore = new ArrayList();
+ copy.parametersToIgnore = new HashSet();
}
else
{
- copy.parametersToIgnore = (ArrayList)parametersToIgnore.clone();
+ copy.parametersToIgnore = (HashSet)parametersToIgnore.clone();
}
copy.parametersToIgnore.add(parameterName);
@@ -388,6 +480,11 @@
{
setSelfIncludeParameters(selfParams.booleanValue());
}
+ Boolean autoIgnoreParams = parser.getBoolean(AUTO_IGNORE_PARAMETERS_KEY);
+ if (autoIgnoreParams != null)
+ {
+ setAutoIgnoreParameters(autoIgnoreParams.booleanValue());
+ }
}
/**
@@ -920,7 +1017,7 @@
// want to ignore, and then use the copyWith(Map) method to
// copy all the parameters into this LinkTool.
Map params = new HashMap(request.getParameterMap());
- for (Iterator i = parametersToIgnore.iterator(); i.hasNext();)
+ for (Iterator i = parametersToIgnore.iterator(); i.hasNext(); )
{
params.remove(i.next());
}
@@ -945,7 +1042,6 @@
private final String key;
private final Object value;
-
/**
* Construct a new query pair.
*
@@ -956,6 +1052,14 @@
{
this.key = key;
this.value = value;
+ }
+
+ /**
+ * Gets the key for this QueryPair.
+ */
+ public String getKey()
+ {
+ return key;
}
/**
Modified: velocity/tools/trunk/src/test/org/apache/velocity/tools/test/blackbox/LinkToolTests.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/test/org/apache/velocity/tools/test/blackbox/LinkToolTests.java?rev=591396&r1=591395&r2=591396&view=diff
==============================================================================
--- velocity/tools/trunk/src/test/org/apache/velocity/tools/test/blackbox/LinkToolTests.java (original)
+++ velocity/tools/trunk/src/test/org/apache/velocity/tools/test/blackbox/LinkToolTests.java Fri Nov 2 09:49:05 2007
@@ -27,6 +27,7 @@
import org.apache.velocity.tools.view.tools.LinkTool;
import org.apache.velocity.tools.view.context.ViewContext;
import org.apache.velocity.tools.view.context.ChainedContext;
+import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -46,19 +47,7 @@
HashMap params = new HashMap();
params.put("a", "b");
InvocationHandler handler = new ServletAdaptor("/test", params);
- Object proxy
- = Proxy.newProxyInstance(this.getClass().getClassLoader(),
- new Class[] { HttpServletRequest.class,
- HttpServletResponse.class },
- handler);
-
- HttpServletRequest request = (HttpServletRequest)proxy;
- HttpServletResponse response = (HttpServletResponse)proxy;
- ViewContext vc = new ChainedContext(null, // Velocity
- request, // Request
- response, // Response
- null // application
- );
+ ViewContext vc = createViewContext(handler);
LinkTool link = new LinkTool();
link.init(vc);
@@ -77,19 +66,7 @@
HashMap params = new HashMap();
params.put("a", new String[] { "a", "b", "c" });
InvocationHandler handler = new ServletAdaptor("/test", params);
- Object proxy
- = Proxy.newProxyInstance(this.getClass().getClassLoader(),
- new Class[] { HttpServletRequest.class,
- HttpServletResponse.class },
- handler);
-
- HttpServletRequest request = (HttpServletRequest)proxy;
- HttpServletResponse response = (HttpServletResponse)proxy;
- ViewContext vc = new ChainedContext(null, // Velocity
- request, // Request
- response, // Response
- null // application
- );
+ ViewContext vc = createViewContext(handler);
LinkTool link = new LinkTool();
link.init(vc);
@@ -109,19 +86,8 @@
params.put("a", "b");
params.put("b", "c");
InvocationHandler handler = new ServletAdaptor("/test", params);
- Object proxy
- = Proxy.newProxyInstance(this.getClass().getClassLoader(),
- new Class[] { HttpServletRequest.class,
- HttpServletResponse.class },
- handler);
- HttpServletRequest request = (HttpServletRequest)proxy;
- HttpServletResponse response = (HttpServletResponse)proxy;
- ViewContext vc = new ChainedContext(null, // Velocity
- request, // Request
- response, // Response
- null // application
- );
+ ViewContext vc = createViewContext(handler);
LinkTool link = new LinkTool();
link.init(vc);
@@ -141,19 +107,7 @@
HashMap params = new HashMap();
params.put("a", "b");
InvocationHandler handler = new ServletAdaptor("/test", params);
- Object proxy
- = Proxy.newProxyInstance(this.getClass().getClassLoader(),
- new Class[] { HttpServletRequest.class,
- HttpServletResponse.class },
- handler);
-
- HttpServletRequest request = (HttpServletRequest)proxy;
- HttpServletResponse response = (HttpServletResponse)proxy;
- ViewContext vc = new ChainedContext(null, // Velocity
- request, // Request
- response, // Response
- null // application
- );
+ ViewContext vc = createViewContext(handler);
LinkTool link = new LinkTool();
link.init(vc);
@@ -167,4 +121,119 @@
Assert.assertEquals("/test/target?a=b&foo=bar&bar=baz", url);
}
+ public @Test void testAddAdditionalValue()
+ {
+ HashMap params = new HashMap();
+ params.put("a", "b");
+ InvocationHandler handler = new ServletAdaptor("/test", params);
+
+ ViewContext vc = createViewContext(handler);
+
+ LinkTool link = new LinkTool();
+ link.init(vc);
+ link.configure(Collections.singletonMap(LinkTool.AUTO_IGNORE_PARAMETERS_KEY, Boolean.FALSE));
+
+ String url = link.setRelative("/target")
+ .addQueryData("a", "c")
+ .addAllParameters()
+ .toString();
+
+ Assert.assertEquals("/test/target?a=c&a=b", url);
+ }
+
+ public @Test void testAddAdditionalValueAfter()
+ {
+ HashMap params = new HashMap();
+ params.put("a", "b");
+ InvocationHandler handler = new ServletAdaptor("/test", params);
+
+ ViewContext vc = createViewContext(handler);
+
+ LinkTool link = new LinkTool();
+ link.init(vc);
+ link.configure(Collections.singletonMap(LinkTool.AUTO_IGNORE_PARAMETERS_KEY, Boolean.FALSE));
+
+ String url = link.setRelative("/target")
+ .addAllParameters()
+ .addQueryData("a", "c")
+ .toString();
+
+ Assert.assertEquals("/test/target?a=b&a=c", url);
+ }
+
+ public @Test void testAutoIgnore()
+ {
+ HashMap params = new HashMap();
+ params.put("a", "b");
+ InvocationHandler handler = new ServletAdaptor("/test", params);
+
+ ViewContext vc = createViewContext(handler);
+
+ LinkTool link = new LinkTool();
+ link.init(vc);
+
+ String url = link.setRelative("/target")
+ .addQueryData("a", "c")
+ .toString();
+
+ Assert.assertEquals("/test/target?a=c", url);
+ }
+
+ public @Test void testAutoIgnoreMultiple()
+ {
+ HashMap params = new HashMap();
+ params.put("a", new String[] { "a", "b", "c" } );
+ InvocationHandler handler = new ServletAdaptor("/test", params);
+
+ ViewContext vc = createViewContext(handler);
+
+ LinkTool link = new LinkTool();
+ link.init(vc);
+
+ String url = link.setRelative("/target")
+ .addQueryData("a", "d")
+ .addAllParameters()
+ .toString();
+
+ Assert.assertEquals("/test/target?a=d", url);
+ }
+
+ public @Test void testNoIgnoreMultiple_WrongOrder()
+ {
+ HashMap params = new HashMap();
+ params.put("a", new String[] { "a", "b", "c" } );
+ InvocationHandler handler = new ServletAdaptor("/test", params);
+
+ ViewContext vc = createViewContext(handler);
+
+ LinkTool link = new LinkTool();
+ link.init(vc);
+
+ String url = link.setRelative("/target")
+ .addAllParameters()
+ .addQueryData("a", "d")
+ .toString();
+
+ Assert.assertEquals("/test/target?a=a&a=b&a=c&a=d", url);
+ }
+
+ private ViewContext createViewContext(InvocationHandler handler)
+ {
+ Object proxy
+ = Proxy.newProxyInstance(this.getClass().getClassLoader(),
+ new Class[] { HttpServletRequest.class,
+ HttpServletResponse.class,
+ ServletContext.class },
+ handler);
+
+ HttpServletRequest request = (HttpServletRequest)proxy;
+ HttpServletResponse response = (HttpServletResponse)proxy;
+ ServletContext application = (ServletContext)proxy;
+
+ return new ChainedContext(null, // Velocity
+ request,
+ response,
+ application
+ );
+ }
}