Skip to content

Commit

Permalink
Fixes Issue ocpsoft#110 - View IDs with hard-coded query parameters w…
Browse files Browse the repository at this point in the history
…ere working in PF3.x, but no longer work in Rewrite
  • Loading branch information
andreiamariei committed Jun 22, 2013
1 parent 92e67ee commit 07fd4cc
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.ocpsoft.rewrite.prettyfaces.outbound;

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@Named("outbound")
@RequestScoped
public class ViewHardcodedParamsBean
{

public String hCommandLink() {
return "view-hardcoded-params.jsf?faces-redirect=true&param=value";
}

public String hCommandLinkExtra() {
return "view-hardcoded-params.jsf?faces-redirect=true&param=value&extraParam=extraValue";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.ocpsoft.rewrite.prettyfaces.outbound;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.drone.api.annotation.Drone;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ocpsoft.rewrite.prettyfaces.PrettyFacesTestBase;
import org.ocpsoft.rewrite.test.RewriteTestBase;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

@RunWith(Arquillian.class)
public class ViewHardcodedParamsOutboundTest extends RewriteTestBase
{

@Deployment(testable = false)
public static WebArchive createDeployment()
{
return PrettyFacesTestBase.getDeployment()
.addClass(ViewHardcodedParamsBean.class)
.addAsWebResource("outbound/view-hardcoded-params.xhtml", "index.xhtml")
.addAsWebResource("outbound/view-hardcoded-params.xhtml", "view-hardcoded-params.xhtml")
.addAsWebInfResource("outbound/view-hardcoded-params-pretty-config.xml", "pretty-config.xml");
}

@Drone
WebDriver browser;

@Test
public void testHLink() throws Exception
{
browser.get(getBaseURL() + getContextPath() + "/index");
String hlink = browser.findElement(By.id("hLink")).getAttribute("href");
Assert.assertTrue(hlink.endsWith("/view-hardcoded-params"));
}

@Test
public void testHLinkExtraParams() throws Exception
{
browser.get(getBaseURL() + getContextPath() + "/index");
String hlink = browser.findElement(By.id("hLink-extra")).getAttribute("href");
Assert.assertTrue(hlink.endsWith("/view-hardcoded-params?extraParam=extraValue"));
}

@Test
public void testPLink() throws Exception
{
browser.get(getBaseURL() + getContextPath() + "/index");
String url = browser.findElement(By.id("prettyLink")).getAttribute("href");
Assert.assertTrue(url.endsWith("/view-hardcoded-params"));
}

@Test
public void testPLinkExtraParams() throws Exception
{
browser.get(getBaseURL() + getContextPath() + "/index");
String url = browser.findElement(By.id("prettyLink-extra")).getAttribute("href");
Assert.assertTrue(url.endsWith("/view-hardcoded-params?extraParam=extraValue"));
}

@Test
public void testHCommandLink() throws Exception
{
Assume.assumeTrue(browser instanceof HtmlUnitDriver);
((HtmlUnitDriver)browser).setJavascriptEnabled(true);
browser.get(getBaseURL() + getContextPath() + "/index");
browser.findElement(By.id("hCommandLink")).click();
Assert.assertTrue(browser.getCurrentUrl().endsWith("/view-hardcoded-params"));
}

@Test
public void testHCommandLinkExtraParams() throws Exception
{
Assume.assumeTrue(browser instanceof HtmlUnitDriver);
((HtmlUnitDriver)browser).setJavascriptEnabled(true);
browser.get(getBaseURL() + getContextPath() + "/index");
browser.findElement(By.id("hCommandLink-extra")).click();
Assert.assertTrue(browser.getCurrentUrl().endsWith("/view-hardcoded-params?extraParam=extraValue"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<pretty-config xmlns="http://ocpsoft.com/prettyfaces/3.3.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<url-mapping id="index">
<pattern value="/index" />
<view-id value="/index.jsf" />
</url-mapping>

<url-mapping id="view-hardcoded-params">
<pattern value="/view-hardcoded-params" />
<view-id value="/view-hardcoded-params.jsf?param=value" />
</url-mapping>

</pretty-config>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:pretty="http://ocpsoft.com/prettyfaces"
xmlns:ui="http://java.sun.com/jsf/facelets">

<head>
<title>Outbound Page</title>
</head>

<body>

<h:link id="hLink" value="hlink" outcome="view-hardcoded-params.jsf?param=value"/>
<br/>

<h:link id="hLink-extra" value="hlink-extra" outcome="view-hardcoded-params.jsf?param=value&amp;extraParam=extraValue"/>
<br/>

<pretty:link id="prettyLink" mappingId="view-hardcoded-params">
prettylink
</pretty:link>
<br/>

<pretty:link id="prettyLink-extra" mappingId="view-hardcoded-params">
<f:param name="extraParam" value="extraValue"/>
prettylink-extra
</pretty:link>
<br/>

<h:form prependId="false">

<h:commandLink id="hCommandLink" action="#{outbound.hCommandLink}" value="hCommandLink"/>
<br/>

<h:commandLink id="hCommandLink-extra" action="#{outbound.hCommandLinkExtra}" value="hCommandLink-extra"/>
<br/>

</h:form>

</body>

</html>
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ private String rewritePrettyMappings(final PrettyConfig config, final String con
{
qs.addParameters(url);

QueryString mappingViewQueryString = QueryString.build(mapping.getViewId());
for (String mappingViewParam : mappingViewQueryString.getParameterMap().keySet()) {
qs.removeParameter(mappingViewParam);
}

// remove own own metadata
qs.removeParameter("com.ocpsoft.mappingId");
}
Expand Down Expand Up @@ -115,6 +120,62 @@ private String rewritePrettyMappings(final PrettyConfig config, final String con
return result;
}

private boolean evaluateOutbound(String outboundURL) {
QueryString outboundQueryString = QueryString.build(outboundURL);
String cachedMappingId = outboundQueryString.getParameter(REWRITE_MAPPING_ID_KEY);
if (cachedMappingId != null)
{
return mapping.getId().equals(cachedMappingId);
}

String outboundPath = outboundURL;
String mappingViewId = mapping.getViewId();
int outboundQueryStringStart = outboundPath.indexOf("?");
if (outboundQueryStringStart != -1)
{
outboundPath = outboundPath.substring(0, outboundQueryStringStart);
}
int mappingViewQueryStringStart = mappingViewId.indexOf("?");
if (mappingViewQueryStringStart != -1)
{
mappingViewId = mappingViewId.substring(0, mappingViewQueryStringStart);
}

if (!mappingViewId.equals(outboundPath))
{
return false;
}

QueryString mappingViewQueryString = QueryString.build(mapping.getViewId());
for (Entry<String, String[]> mappingViewParam : mappingViewQueryString.getParameterMap().entrySet())
{
for (String mappingViewParamValue : mappingViewParam.getValue())
{
boolean found = false;
for (Entry<String, String[]> outboundParam : outboundQueryString.getParameterMap().entrySet())
{
if (!mappingViewParam.getKey().equals(outboundParam.getKey()))
{
continue;
}
for (String outboundParamValue : outboundParam.getValue())
{
if ((mappingViewParamValue == outboundParamValue)
|| (mappingViewParamValue != null && mappingViewParamValue.equals(outboundParamValue)))
{
found = true;
break;
}
}
}
if (!found) {
return false;
}
}
}
return true;
}

@Override
public String getId()
{
Expand All @@ -139,14 +200,7 @@ else if ((event instanceof HttpOutboundServletRewrite)
outboundURL = outboundURL.substring(((HttpServletRewrite) event).getContextPath().length());
}

QueryString queryString = QueryString.build(outboundURL);
String mappingId = queryString.getParameter(REWRITE_MAPPING_ID_KEY);

if (((mappingId == null) && outboundURL.startsWith(mapping.getViewId()))
|| mapping.getId().equals(mappingId))
{
return true;
}
return evaluateOutbound(outboundURL);
}
return false;
}
Expand Down

0 comments on commit 07fd4cc

Please sign in to comment.