[prettyfaces] r458 committed - Fixed issue 64: http://code.google.com/p/prettyfaces/issues/detail?id=...

1 view
Skip to first unread message

prett...@googlecode.com

unread,
Oct 19, 2010, 12:12:14 PM10/19/10
to prettyfac...@googlegroups.com
Revision: 458
Author: lincolnbaxter
Date: Tue Oct 19 09:11:21 2010
Log: Fixed issue 64:
http://code.google.com/p/prettyfaces/issues/detail?id=64 - added regression
test to Arquillian project && Added test to URLPatternParser.
http://code.google.com/p/prettyfaces/source/detail?r=458

Modified:
/prettyfaces/trunk/core
/prettyfaces/trunk/core/src
/prettyfaces/trunk/core/src/main/java/com/ocpsoft/pretty/PrettyFilter.java

/prettyfaces/trunk/core/src/main/java/com/ocpsoft/pretty/faces/beans/ExtractedValuesURLBuilder.java

/prettyfaces/trunk/core/src/main/java/com/ocpsoft/pretty/faces/event/PrettyPhaseListener.java
/prettyfaces/trunk/core/src/main/java/com/ocpsoft/pretty/faces/url/URL.java

/prettyfaces/trunk/core/src/main/java/com/ocpsoft/pretty/faces/url/URLPatternParser.java

/prettyfaces/trunk/core/src/main/java/com/ocpsoft/pretty/faces/util/PrettyURLBuilder.java

/prettyfaces/trunk/core/src/test/java/com/ocpsoft/pretty/faces/url/URLPatternParserRegexOverrideTest.java

/prettyfaces/trunk/core/src/test/java/com/ocpsoft/pretty/faces/url/URLTest.java

/prettyfaces/trunk/impl-jsf12/src/main/java/com/ocpsoft/pretty/faces/application/PrettyViewHandler.java
/prettyfaces/trunk/impl-jsf2

/prettyfaces/trunk/tests-jsf2/src/test/java/com/ocpsoft/pretty/faces/test/encoding/URLEncodingTest.java

/prettyfaces/trunk/tests-jsf2/src/test/resources/encoding/encoding-pretty-config.xml

=======================================
---
/prettyfaces/trunk/core/src/main/java/com/ocpsoft/pretty/PrettyFilter.java
Mon Oct 18 13:38:25 2010
+++
/prettyfaces/trunk/core/src/main/java/com/ocpsoft/pretty/PrettyFilter.java
Tue Oct 19 09:11:21 2010
@@ -95,7 +95,7 @@
query.getParameterMap());

log.trace("Sending mapped request [" + url.toURL() + "]
to resource [" + viewId + "]");
- if (url.getDecodedURL().matches(viewId))
+ if (url.decode().toURL().matches(viewId))
{
chain.doFilter(wrappedRequest, response);
}
@@ -146,8 +146,8 @@
}

// TODO test this now that query string is included in rewrites
- String decodedUrl = url.getDecodedURL() + queryString;
- String newUrl = decodedUrl;
+ String originalUrl = url.toURL() + queryString;
+ String newUrl = originalUrl;
for (RewriteRule rule : getConfig().getGlobalRewriteRules())
{
if (rule.matches(newUrl))
@@ -160,24 +160,23 @@
* have a url or if the current url has been modified.
*/
String ruleUrl = rule.getUrl();
- if (((ruleUrl == null) || "".equals(ruleUrl.trim()))
&& !decodedUrl.equals(newUrl))
+ if (((ruleUrl == null) || "".equals(ruleUrl.trim()))
&& !originalUrl.equals(newUrl))
{
/*
- * The current URL has been rewritten - URLEncode
the
- * path and redirect
+ * The current URL has been rewritten - do redirect
*/
// TODO fix this garbage
String[] parts = newUrl.split("\\?", 2);
- URL decoded = new URL(parts[0]);
- decoded.setEncoding(url.getEncoding());
+ URL path = new URL(parts[0]);
+ path.setEncoding(url.getEncoding());
if (parts.length == 2)
{
- newUrl = decoded.getEncodedURL() + ((parts[1]
== null) || "".equals(parts[1]) ? "" : "?")
+ newUrl = path.toURL() + ((parts[1] == null) |
| "".equals(parts[1]) ? "" : "?")
+ parts[1];
}
else
{
- newUrl = decoded.getEncodedURL();
+ newUrl = path.toURL();
}
String redirectURL =
resp.encodeRedirectURL(req.getContextPath() + newUrl);
resp.setHeader("Location", redirectURL);
@@ -201,7 +200,7 @@
}
}

- if (!decodedUrl.equals(newUrl) && !resp.isCommitted())
+ if (!originalUrl.equals(newUrl) && !resp.isCommitted())
{
/*
* The URL was modified, but no redirect occurred; forward
=======================================
---
/prettyfaces/trunk/core/src/main/java/com/ocpsoft/pretty/faces/beans/ExtractedValuesURLBuilder.java
Wed Jun 30 11:26:10 2010
+++
/prettyfaces/trunk/core/src/main/java/com/ocpsoft/pretty/faces/beans/ExtractedValuesURLBuilder.java
Tue Oct 19 09:11:21 2010
@@ -103,7 +103,7 @@
}
}

- result = prettyContext.getContextPath() +
parser.getMappedURL(parameterValues).getEncodedURL() +
QueryString.build(queryParameterValues);
+ result = prettyContext.getContextPath() +
parser.getMappedURL(parameterValues).toURL() +
QueryString.build(queryParameterValues);
}
catch (ELException e)
{
=======================================
---
/prettyfaces/trunk/core/src/main/java/com/ocpsoft/pretty/faces/event/PrettyPhaseListener.java
Mon Aug 30 11:25:36 2010
+++
/prettyfaces/trunk/core/src/main/java/com/ocpsoft/pretty/faces/event/PrettyPhaseListener.java
Tue Oct 19 09:11:21 2010
@@ -86,8 +86,8 @@
{
// TODO Test that validation occurs before injection
/*
- * Parameter validation and injection must occur after
RESTORE_VIEW in order to participate in
- * faces-navigation.
+ * Parameter validation and injection must occur after
RESTORE_VIEW in
+ * order to participate in faces-navigation.
*/
validator.validateParameters(event.getFacesContext());
injector.injectParameters(event.getFacesContext());
@@ -110,7 +110,7 @@
*/
private void processDynaView(final PrettyContext prettyContext, final
FacesContext facesContext)
{
- log.trace("Requesting DynaView processing for: " +
prettyContext.getRequestURL().getDecodedURL());
+ log.trace("Requesting DynaView processing for: " +
prettyContext.getRequestURL());
String viewId = "";
try
{
@@ -135,12 +135,15 @@
}

/**
- * Calculate the Faces ViewId to which this request URI resolves. This
method will recursively call any dynamic
- * mapping viewId functions as needed until a String viewId is
returned, or supplied by a static mapping.
+ * Calculate the Faces ViewId to which this request URI resolves. This
method
+ * will recursively call any dynamic mapping viewId functions as needed
until
+ * a String viewId is returned, or supplied by a static mapping.
* <p>
- * This phase does not support FacesNavigation or PrettyRedirecting.
Its SOLE purpose is to resolve a viewId.
+ * This phase does not support FacesNavigation or PrettyRedirecting.
Its SOLE
+ * purpose is to resolve a viewId.
* <p>
- * <i><b>Note:</b> Precondition - parameter injection must take place
before this</i>
+ * <i><b>Note:</b> Precondition - parameter injection must take place
before
+ * this</i>
* <p>
* <i>Postcondition - currentViewId is set to computed View Id</i>
*
=======================================
---
/prettyfaces/trunk/core/src/main/java/com/ocpsoft/pretty/faces/url/URL.java
Mon Oct 18 10:53:27 2010
+++
/prettyfaces/trunk/core/src/main/java/com/ocpsoft/pretty/faces/url/URL.java
Tue Oct 19 09:11:21 2010
@@ -64,49 +64,6 @@
this.segments = segments;
this.originalURL = metadata.buildURLFromSegments(segments);
}
-
- /**
- * Decode each segment and return the URL built using this instance's
- * {@link Metadata}
- */
- public String getDecodedURL()
- {
- String encoding = metadata.getEncoding();
- if (!decodedURLs.containsKey(encoding))
- {
- List<String> resultSegments = new ArrayList<String>();
- for (String segment : segments)
- {
- try
- {
- String encoded = URLDecoder.decode(segment, encoding);
- resultSegments.add(encoded);
- }
- catch (UnsupportedEncodingException e)
- {
- throw new PrettyException("Could not decode URL with
specified format: " + encoding, e);
- }
- }
- URL result = decode();
- decodedURLs.put(encoding, result);
- }
- return decodedURLs.get(encoding).toURL();
- }
-
- /**
- * Encode each segment and return the URL built using this instance's
- * {@link Metadata}
- */
- public String getEncodedURL()
- {
- String encoding = metadata.getEncoding();
- if (!encodedURLs.containsKey(encoding))
- {
- URL result = encode();
- encodedURLs.put(encoding, result);
- }
- return encodedURLs.get(encoding).toURL();
- }

/**
* Get a list of all decoded segments (separated by '/') in this URL.
=======================================
---
/prettyfaces/trunk/core/src/main/java/com/ocpsoft/pretty/faces/url/URLPatternParser.java
Mon Oct 18 10:53:27 2010
+++
/prettyfaces/trunk/core/src/main/java/com/ocpsoft/pretty/faces/url/URLPatternParser.java
Tue Oct 19 09:11:21 2010
@@ -36,8 +36,10 @@
private List<PathParameter> pathParameters = new
ArrayList<PathParameter>();

/**
- * Set the pattern for which this parser will match. Find and replace
all el expressions with regular expressions to
- * extract values from parsed URLs. Also extract all parameter names
from expressions, and replace with valid EL
+ * Set the pattern for which this parser will match. Find and replace
all el
+ * expressions with regular expressions to extract values from parsed
URLs.
+ * Also extract all parameter names from expressions, and replace with
valid
+ * EL
*
* @param pattern Pattern to use as a parse template
*/
@@ -67,7 +69,8 @@
URL segmentedPattern = new URL(segmentableExpressions.toString());

/*
- * Extract path segments, overlaying regexes found during parameter
discovery.
+ * Extract path segments, overlaying regexes found during parameter
+ * discovery.
*/
int segmentIndex = 0;
for (String segmentPattern : segmentedPattern.getSegments())
@@ -128,8 +131,9 @@
}

/**
- * Builds a list of PathParameters for this UrlPattern, extracted from
the provided URL (assuming a match is found).
- * This list excludes duplicate named parameters.
+ * Builds a list of PathParameters for this UrlPattern, extracted from
the
+ * provided URL (assuming a match is found). This list excludes
duplicate
+ * named parameters.
*/
public List<PathParameter> parse(final URL url)
{
@@ -142,7 +146,14 @@
while (iter.hasNext())
{
Segment segment = iter.next();
- String regex = segment.getRegex();
+
+ String regex = "";
+ if (url.hasLeadingSlash() && !"/".equals(url.toURL()))
+ {
+ regex += "/";
+ }
+
+ regex += segment.getRegex();
if (iter.hasNext() || url.hasTrailingSlash())
{
regex += "/";
@@ -161,7 +172,7 @@

int regionEnd = segmentMatcher.end();

- inboundUrl = inboundUrl.substring(regionEnd);
+ inboundUrl = inboundUrl.substring(regionEnd - 1);
}
else
{
@@ -181,15 +192,20 @@
/**
* URL encoding/decoding is not a concern of this method.
*
- * @param params Array of Object parameters, in order, to be
substituted for mapping pattern values or el
- * expressions. This method will call the toString() method
on each object provided.
+ * @param params Array of Object parameters, in order, to be
substituted for
+ * mapping pattern values or el expressions. This method will
call
+ * the toString() method on each object provided.
* <p>
- * If only one param is specified and it is an instance of
List, the list items will be used as parameters
- * instead. An empty list or a single null parameter are both
treated as if no parameters were specified.
+ * If only one param is specified and it is an instance of
List,
+ * the list items will be used as parameters instead. An
empty list
+ * or a single null parameter are both treated as if no
parameters
+ * were specified.
* </p>
- * E.g: getMappedUrl(12,55,"foo","bar") for a pattern of
/#{el.one}/#{el.two}/#{el.three}/#{el.four}/ will
- * return the String: "/12/55/foo/bar/"
- * @return A URL based on this object's urlPatten, with values
substituted for el expressions in the order provided
+ * E.g: getMappedUrl(12,55,"foo","bar") for a pattern of
+ * /#{el.one}/#{el.two}/#{el.three}/#{el.four}/ will return
the
+ * String: "/12/55/foo/bar/"
+ * @return A URL based on this object's urlPatten, with values
substituted
+ * for el expressions in the order provided
*/
public URL getMappedURL(final Object... params)
{
@@ -258,7 +274,8 @@
}

/**
- * Get the number of URL parameters that this parser expects to find in
any given input string
+ * Get the number of URL parameters that this parser expects to find in
any
+ * given input string
*
* @return Number of parameters
*/
=======================================
---
/prettyfaces/trunk/core/src/main/java/com/ocpsoft/pretty/faces/util/PrettyURLBuilder.java
Mon Jun 7 15:59:05 2010
+++
/prettyfaces/trunk/core/src/main/java/com/ocpsoft/pretty/faces/util/PrettyURLBuilder.java
Tue Oct 19 09:11:21 2010
@@ -116,6 +116,7 @@
*/
public String build(final UrlMapping urlMapping, final
List<UIParameter> parameters)
{
+ String result = "";
if (urlMapping != null)
{
URLPatternParser parser = new
URLPatternParser(urlMapping.getPattern());
@@ -130,7 +131,7 @@
if (firstParam.getValue() instanceof List<?>)
{
URL url = parser.getMappedURL(firstParam.getValue());
- return url.getEncodedURL();
+ return url.toURL();
}
else if (firstParam.getValue().getClass().isArray())
{
@@ -138,7 +139,7 @@
// getValue() as a single Object.
List<Object> list = Arrays.asList((Object[])
firstParam.getValue());
URL url = parser.getMappedURL(list);
- return url.getEncodedURL();
+ return url.toURL();
}
}
}
@@ -188,8 +189,8 @@
}
}

- return parser.getMappedURL(pathParams.toArray()).getEncodedURL()
+ QueryString.build(queryParams).toQueryString();
- }
- return "";
+ result = parser.getMappedURL(pathParams.toArray()).toURL() +
QueryString.build(queryParams).toQueryString();
+ }
+ return result;
}
}
=======================================
---
/prettyfaces/trunk/core/src/test/java/com/ocpsoft/pretty/faces/url/URLPatternParserRegexOverrideTest.java
Fri Jul 23 08:45:24 2010
+++
/prettyfaces/trunk/core/src/test/java/com/ocpsoft/pretty/faces/url/URLPatternParserRegexOverrideTest.java
Tue Oct 19 09:11:21 2010
@@ -56,6 +56,21 @@
assertEquals("#{named}", p.getExpression().getELExpression());
assertEquals(".*", p.getRegex());
}
+
+ @Test
+ public void testLeadingTrailingSlashWithInternalSlashes() throws
Exception
+ {
+ URLPatternParser parser = new URLPatternParser("/#{ /.*/ named}/");
+ List<PathParameter> params = parser.parse(new
URL("/foo/love/again/"));
+ assertEquals(1, params.size());
+
+ PathParameter p = params.get(0);
+ assertEquals(0, p.getPosition());
+ assertEquals("foo/love/again", p.getValue());
+ assertEquals("named", p.getName());
+ assertEquals("#{named}", p.getExpression().getELExpression());
+ assertEquals(".*", p.getRegex());
+ }

@Test
public void testMultiURLSegmentParsingInjected() throws Exception
=======================================
---
/prettyfaces/trunk/core/src/test/java/com/ocpsoft/pretty/faces/url/URLTest.java
Mon Oct 18 10:53:27 2010
+++
/prettyfaces/trunk/core/src/test/java/com/ocpsoft/pretty/faces/url/URLTest.java
Tue Oct 19 09:11:21 2010
@@ -22,7 +22,7 @@
String value = "/com/ocpsoft/pretty/";
URL url = new URL(value);
url.setEncoding("UTF-8");
- assertEquals(value, url.getDecodedURL());
+ assertEquals(value, url.decode().toURL());
}

@Test
@@ -33,7 +33,7 @@
URL url = new URL(new ArrayList<String>(), metadata);

assertEquals("/", url.toURL());
- assertEquals("/", url.getDecodedURL());
+ assertEquals("/", url.decode().toURL());
}

@Test
=======================================
---
/prettyfaces/trunk/impl-jsf12/src/main/java/com/ocpsoft/pretty/faces/application/PrettyViewHandler.java
Fri Oct 15 08:43:10 2010
+++
/prettyfaces/trunk/impl-jsf12/src/main/java/com/ocpsoft/pretty/faces/application/PrettyViewHandler.java
Tue Oct 19 09:11:21 2010
@@ -69,7 +69,7 @@
QueryString query = prettyContext.getRequestQueryString();
String contextPath = prettyContext.getContextPath();

- String target = contextPath + url.getDecodedURL() +
query.toQueryString();
+ String target = contextPath + url + query.toQueryString();
return target;
}
else
=======================================
---
/prettyfaces/trunk/tests-jsf2/src/test/java/com/ocpsoft/pretty/faces/test/encoding/URLEncodingTest.java
Mon Oct 18 10:53:27 2010
+++
/prettyfaces/trunk/tests-jsf2/src/test/java/com/ocpsoft/pretty/faces/test/encoding/URLEncodingTest.java
Tue Oct 19 09:11:21 2010
@@ -35,7 +35,6 @@
import org.junit.runner.RunWith;

import com.ocpsoft.pretty.PrettyContext;
-import com.ocpsoft.pretty.faces.url.URL;

@RunWith(Arquillian.class)
public class URLEncodingTest
@@ -71,6 +70,28 @@
assertEquals(expected, prettyContext.getRequestURL().toString());
assertEquals(prettyContext.getContextPath() + expected, action);
}
+
+ @Test
+ // http://code.google.com/p/prettyfaces/issues/detail?id=64
+ public void
testPrettyFacesFormActionURLEncodesProperlyWithCustomRegexAndMultiplePathSegments()
throws Exception
+ {
+ String expected = "/foo/bar/baz/car/";
+
+ JSFSession jsfSession = new JSFSession(expected);
+ JSFServerSession server = jsfSession.getJSFServerSession();
+
+ JSFClientSession client = jsfSession.getJSFClientSession();
+ String action = client.getElement("form").getAttribute("action");
+
+ FacesContext context = server.getFacesContext();
+ PrettyContext prettyContext =
PrettyContext.getCurrentInstance(context);
+
+ assertEquals(expected, prettyContext.getRequestURL().toString());
+ assertEquals(prettyContext.getContextPath() + expected, action);
+
+ String value = (String)
server.getManagedBeanValue("#{encodingBean.pathText}");
+ assertEquals("foo/bar/baz/car", value);
+ }

@Test
public void testNonMappedRequestRendersRewrittenURL() throws Exception
@@ -100,9 +121,6 @@

// Test a managed bean
Object val1 = server.getManagedBeanValue("#{encodingBean.pathText}");
- URL url = new URL((String) val1);
- String encodedURL = url.getEncodedURL();
- String decodedURL = url.getDecodedURL();
assertEquals("Vračar", val1);
}

=======================================
---
/prettyfaces/trunk/tests-jsf2/src/test/resources/encoding/encoding-pretty-config.xml
Mon Oct 18 10:53:27 2010
+++
/prettyfaces/trunk/tests-jsf2/src/test/resources/encoding/encoding-pretty-config.xml
Tue Oct 19 09:11:21 2010
@@ -23,11 +23,9 @@
<view-id> /encoding.jsf </view-id>
</url-mapping>

- <url-mapping id="decodequery">
- <pattern value="/decodequery" />
- <query-param name="input0">#{exampleBean.param0}</query-param>
- <query-param name="input1"
decode="false">#{exampleBean.param1}</query-param>
+ <url-mapping id="formencoding">
+ <pattern value="/#{/.*/ encodingBean.pathText}/" />
<view-id> /encoding.jsf </view-id>
</url-mapping>
-
+
</pretty-config>
Reply all
Reply to author
Forward
0 new messages