From 07fd4cc996b882f5133c551c0ea61ed70156b1ed Mon Sep 17 00:00:00 2001 From: Andrei Amariei Date: Sat, 22 Jun 2013 22:41:15 +0300 Subject: [PATCH] Fixes Issue #110 - View IDs with hard-coded query parameters were working in PF3.x, but no longer work in Rewrite --- .../outbound/ViewHardcodedParamsBean.java | 18 ++++ .../ViewHardcodedParamsOutboundTest.java | 86 +++++++++++++++++++ .../view-hardcoded-params-pretty-config.xml | 13 +++ .../outbound/view-hardcoded-params.xhtml | 44 ++++++++++ .../prettyfaces/UrlMappingRuleAdaptor.java | 70 +++++++++++++-- 5 files changed, 223 insertions(+), 8 deletions(-) create mode 100644 config-prettyfaces-tests/src/test/java/org/ocpsoft/rewrite/prettyfaces/outbound/ViewHardcodedParamsBean.java create mode 100644 config-prettyfaces-tests/src/test/java/org/ocpsoft/rewrite/prettyfaces/outbound/ViewHardcodedParamsOutboundTest.java create mode 100644 config-prettyfaces-tests/src/test/resources/outbound/view-hardcoded-params-pretty-config.xml create mode 100644 config-prettyfaces-tests/src/test/resources/outbound/view-hardcoded-params.xhtml diff --git a/config-prettyfaces-tests/src/test/java/org/ocpsoft/rewrite/prettyfaces/outbound/ViewHardcodedParamsBean.java b/config-prettyfaces-tests/src/test/java/org/ocpsoft/rewrite/prettyfaces/outbound/ViewHardcodedParamsBean.java new file mode 100644 index 000000000..604e8eec2 --- /dev/null +++ b/config-prettyfaces-tests/src/test/java/org/ocpsoft/rewrite/prettyfaces/outbound/ViewHardcodedParamsBean.java @@ -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¶m=value"; + } + + public String hCommandLinkExtra() { + return "view-hardcoded-params.jsf?faces-redirect=true¶m=value&extraParam=extraValue"; + } +} diff --git a/config-prettyfaces-tests/src/test/java/org/ocpsoft/rewrite/prettyfaces/outbound/ViewHardcodedParamsOutboundTest.java b/config-prettyfaces-tests/src/test/java/org/ocpsoft/rewrite/prettyfaces/outbound/ViewHardcodedParamsOutboundTest.java new file mode 100644 index 000000000..182f829fc --- /dev/null +++ b/config-prettyfaces-tests/src/test/java/org/ocpsoft/rewrite/prettyfaces/outbound/ViewHardcodedParamsOutboundTest.java @@ -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")); + } + +} diff --git a/config-prettyfaces-tests/src/test/resources/outbound/view-hardcoded-params-pretty-config.xml b/config-prettyfaces-tests/src/test/resources/outbound/view-hardcoded-params-pretty-config.xml new file mode 100644 index 000000000..fccf96cf2 --- /dev/null +++ b/config-prettyfaces-tests/src/test/resources/outbound/view-hardcoded-params-pretty-config.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/config-prettyfaces-tests/src/test/resources/outbound/view-hardcoded-params.xhtml b/config-prettyfaces-tests/src/test/resources/outbound/view-hardcoded-params.xhtml new file mode 100644 index 000000000..6f9a132bd --- /dev/null +++ b/config-prettyfaces-tests/src/test/resources/outbound/view-hardcoded-params.xhtml @@ -0,0 +1,44 @@ + + + + + Outbound Page + + + + + +
+ + +
+ + + prettylink + +
+ + + + prettylink-extra + +
+ + + + +
+ + +
+ +
+ + + + diff --git a/config-prettyfaces/src/main/java/org/ocpsoft/rewrite/prettyfaces/UrlMappingRuleAdaptor.java b/config-prettyfaces/src/main/java/org/ocpsoft/rewrite/prettyfaces/UrlMappingRuleAdaptor.java index 78159b534..fff0ca6d7 100644 --- a/config-prettyfaces/src/main/java/org/ocpsoft/rewrite/prettyfaces/UrlMappingRuleAdaptor.java +++ b/config-prettyfaces/src/main/java/org/ocpsoft/rewrite/prettyfaces/UrlMappingRuleAdaptor.java @@ -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"); } @@ -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 mappingViewParam : mappingViewQueryString.getParameterMap().entrySet()) + { + for (String mappingViewParamValue : mappingViewParam.getValue()) + { + boolean found = false; + for (Entry 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() { @@ -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; }