Skip to content

Commit c4a7724

Browse files
committed
Issue 125 Encoding Context Param/Flag
1 parent 13f744f commit c4a7724

7 files changed

Lines changed: 125 additions & 122 deletions

File tree

‎core/src/main/java/com/ocpsoft/pretty/faces/application/PrettyRedirector.java‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,18 @@ public boolean redirect(final FacesContext context, final String action)
5959

6060
String target = contextPath + url.encode() + query.toQueryString();
6161
log.trace("Refreshing requested page [" + url + "]");
62-
String redirectUrl = externalContext.encodeActionURL(target);
63-
redirectUrl = ((HttpServletResponse) externalContext.getResponse()).encodeRedirectURL(redirectUrl);
64-
externalContext.redirect(redirectUrl);
62+
encodeURL(externalContext, config, target);
6563
return true;
6664
}
6765
else if (isPrettyNavigationCase(prettyContext, action))
6866
{
6967
UrlMapping mapping = config.getMappingById(action);
7068
if (mapping != null)
7169
{
72-
String url = contextPath + builder.buildURL(mapping).encode()
70+
String target = contextPath + builder.buildURL(mapping).encode()
7371
+ builder.buildQueryString(mapping);
74-
log.trace("Redirecting to mappingId [" + mapping.getId() + "], [" + url + "]");
75-
String redirectUrl = externalContext.encodeActionURL(url);
76-
redirectUrl = ((HttpServletResponse) externalContext.getResponse()).encodeRedirectURL(redirectUrl);
77-
externalContext.redirect(redirectUrl);
72+
log.trace("Redirecting to mappingId [" + mapping.getId() + "], [" + target + "]");
73+
encodeURL(externalContext, config, target);
7874
}
7975
else
8076
{
@@ -90,6 +86,20 @@ else if (isPrettyNavigationCase(prettyContext, action))
9086
return false;
9187
}
9288

89+
private void encodeURL(ExternalContext externalContext, PrettyConfig config, String target) throws IOException
90+
{
91+
String redirectUrl = externalContext.encodeActionURL(target);
92+
if (config.isUseEncodeUrlForRedirects())
93+
{
94+
redirectUrl = ((HttpServletResponse) externalContext.getResponse()).encodeURL(redirectUrl);
95+
}
96+
else
97+
{
98+
redirectUrl = ((HttpServletResponse) externalContext.getResponse()).encodeRedirectURL(redirectUrl);
99+
}
100+
externalContext.redirect(redirectUrl);
101+
}
102+
93103
public void send404(final FacesContext facesContext)
94104
{
95105
try

‎core/src/main/java/com/ocpsoft/pretty/faces/config/PrettyConfig.java‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class PrettyConfig
3838
private List<RewriteRule> globalRewriteRules = new ArrayList<RewriteRule>();
3939
private String dynaviewId = "";
4040
private final Map<String, UrlMapping> cachedMappings = new HashMap<String, UrlMapping>();
41+
private boolean useEncodeUrlForRedirects = false;
4142

4243
/**
4344
* Creates an empty {@link PrettyConfig} object
@@ -50,8 +51,7 @@ public PrettyConfig()
5051
/**
5152
* Initializes the instance using the supplied JAXB object
5253
*
53-
* @param prettyConfigElement
54-
* The JAXB object
54+
* @param prettyConfigElement The JAXB object
5555
*/
5656
public PrettyConfig(PrettyConfigElement prettyConfigElement)
5757
{
@@ -211,6 +211,16 @@ public UrlMapping getMappingById(String id)
211211
return null;
212212
}
213213

214+
public boolean isUseEncodeUrlForRedirects()
215+
{
216+
return useEncodeUrlForRedirects;
217+
}
218+
219+
public void setUseEncodeUrlForRedirects(boolean useEncodeUrlForRedirects)
220+
{
221+
this.useEncodeUrlForRedirects = useEncodeUrlForRedirects;
222+
}
223+
214224
@Override
215225
public String toString()
216226
{
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2010 Lincoln Baxter, III
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.ocpsoft.pretty.faces.config.spi;
18+
19+
import javax.servlet.ServletContext;
20+
21+
import com.ocpsoft.pretty.faces.config.PrettyConfig;
22+
import com.ocpsoft.pretty.faces.spi.ConfigurationPostProcessor;
23+
24+
/**
25+
* Set various flags from the ServletContext into the configuration.
26+
*
27+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
28+
*
29+
*/
30+
public class ContextParamsPostProcessor implements ConfigurationPostProcessor
31+
{
32+
public static final String USE_ENCODE_URL_FOR_REDIRECTS = "com.ocpsoft.pretty.USE_ENCODE_URL_FOR_REDIRECTS";
33+
34+
@Override
35+
public PrettyConfig processConfiguration(ServletContext context, PrettyConfig config)
36+
{
37+
String useEncodeURL = context.getInitParameter(USE_ENCODE_URL_FOR_REDIRECTS);
38+
if ((useEncodeURL != null) && "true".equalsIgnoreCase(useEncodeURL.trim()))
39+
{
40+
config.setUseEncodeUrlForRedirects(true);
41+
}
42+
43+
return config;
44+
}
45+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.ocpsoft.pretty.faces.config.spi.ContextParamsPostProcessor

‎docs/pom.xml‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,40 @@
119119
</plugin>
120120

121121
</plugins>
122+
<pluginManagement>
123+
<plugins>
124+
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
125+
<plugin>
126+
<groupId>org.eclipse.m2e</groupId>
127+
<artifactId>lifecycle-mapping</artifactId>
128+
<version>1.0.0</version>
129+
<configuration>
130+
<lifecycleMappingMetadata>
131+
<pluginExecutions>
132+
<pluginExecution>
133+
<pluginExecutionFilter>
134+
<groupId>
135+
org.jboss.maven.plugins
136+
</groupId>
137+
<artifactId>
138+
maven-jdocbook-plugin
139+
</artifactId>
140+
<versionRange>[2.2.3,)</versionRange>
141+
<goals>
142+
<goal>generate</goal>
143+
<goal>resources</goal>
144+
</goals>
145+
</pluginExecutionFilter>
146+
<action>
147+
<ignore></ignore>
148+
</action>
149+
</pluginExecution>
150+
</pluginExecutions>
151+
</lifecycleMappingMetadata>
152+
</configuration>
153+
</plugin>
154+
</plugins>
155+
</pluginManagement>
122156
</build>
123157

124158
<pluginRepositories>

‎docs/src/main/docbook/en-US/FAQ.xml‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,21 @@ log4j.logger.com.ocpsoft=debug</programlisting>
140140
</example>
141141
</step>
142142

143+
<step>
144+
<title>Q. My Orchestra conversationContext parameter is not working with PrettyFaces?</title>
145+
<para>
146+
A. This can happen depending on JSF version and Orchestra version where Orchestra does not properly override encodeRedirectURL.
147+
To solve this problem, enable the following context parameter in your web.xml:
148+
</para>
149+
<example>
150+
<title>web.xml</title>
151+
<programlisting><![CDATA[<context-param>
152+
<param-name>com.ocpsoft.pretty.USE_ENCODE_URL_FOR_REDIRECTS</param-name>
153+
<param-value>true</param-value>
154+
</context-param>]]></programlisting>
155+
</example>
156+
</step>
157+
143158

144159
</procedure>
145160
</chapter>

‎impl-jsf2/src/main/java/com/ocpsoft/pretty/faces/application/PrettyRedirector.java‎

Lines changed: 0 additions & 112 deletions
This file was deleted.

0 commit comments

Comments
 (0)