Skip to content

Commit c6873b9

Browse files
committed
Use JSF converters for path and query parameter conversion
1 parent d3dabe1 commit c6873b9

3 files changed

Lines changed: 61 additions & 5 deletions

File tree

‎CHANGELOG.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
VERSION 3.2.1
33
------------------------------------
44

5+
Features & Enhancements:
6+
* Use JSF converters for path and query parameter conversion.
7+
58
Bugfixes:
69
* Fixed incorrect encoding of URLs when using the 'url' attribute for rewrite rules (#91)
710
* MultiPageMessagesSupport doesn't duplicate messages any more if using MyFaces (#89)

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import javax.el.ELException;
2222
import javax.faces.context.FacesContext;
23+
import javax.faces.convert.Converter;
2324

2425
import com.ocpsoft.pretty.PrettyException;
2526
import com.ocpsoft.pretty.faces.config.mapping.PathParameter;
@@ -29,6 +30,7 @@
2930
import com.ocpsoft.pretty.faces.url.URL;
3031
import com.ocpsoft.pretty.faces.url.URLPatternParser;
3132
import com.ocpsoft.pretty.faces.util.FacesElUtils;
33+
import com.ocpsoft.pretty.faces.util.NullComponent;
3234

3335
/**
3436
* @author Lincoln Baxter, III <lincoln@ocpsoft.com>
@@ -59,14 +61,32 @@ public URL buildURL(final UrlMapping mapping)
5961
List<String> parameterValues = new ArrayList<String>();
6062
for (PathParameter injection : parameters)
6163
{
64+
// read value of the path parameter
6265
expression = injection.getExpression().getELExpression();
6366
value = elUtils.getValue(context, expression);
6467
if (value == null)
6568
{
6669
throw new PrettyException("PrettyFaces: Exception occurred while building URL for MappingId < "
6770
+ mapping.getId() + " >, Required value " + " < " + expression + " > was null");
6871
}
69-
parameterValues.add(value.toString());
72+
73+
// convert the value to a string using the correct converter
74+
Converter converter = context.getApplication().createConverter(value.getClass());
75+
if (converter != null)
76+
{
77+
String valueAsString = converter.getAsString(context, new NullComponent(), value);
78+
if (valueAsString == null)
79+
{
80+
throw new PrettyException("PrettyFaces: The converter <" + converter.getClass().getName()
81+
+ "> returned null while converting the object <" + value.toString() + ">!");
82+
}
83+
parameterValues.add(valueAsString);
84+
}
85+
else
86+
{
87+
parameterValues.add(value.toString());
88+
}
89+
7090
}
7191

7292
result = parser.getMappedURL(parameterValues);

‎core/src/main/java/com/ocpsoft/pretty/faces/beans/ParameterInjector.java‎

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.List;
1919

2020
import javax.faces.context.FacesContext;
21+
import javax.faces.convert.Converter;
2122

2223
import org.apache.commons.logging.Log;
2324
import org.apache.commons.logging.LogFactory;
@@ -31,6 +32,7 @@
3132
import com.ocpsoft.pretty.faces.url.URL;
3233
import com.ocpsoft.pretty.faces.util.FacesElUtils;
3334
import com.ocpsoft.pretty.faces.util.FacesStateUtils;
35+
import com.ocpsoft.pretty.faces.util.NullComponent;
3436

3537
/**
3638
* @author Lincoln Baxter, III <lincoln@ocpsoft.com>
@@ -69,10 +71,25 @@ private void injectPathParams(final FacesContext context, final URL url, final U
6971
String el = param.getExpression().getELExpression();
7072
if ((el != null) && !"".equals(el.trim()))
7173
{
72-
String value = param.getValue();
74+
String valueAsString = param.getValue();
7375
try
7476
{
75-
elUtils.setValue(context, el, value);
77+
78+
// get the type of the referenced property and try to obtain a converter for it
79+
Class<?> expectedType = elUtils.getExpectedType(context, el);
80+
Converter converter = context.getApplication().createConverter(expectedType);
81+
82+
// Use the convert to create the correct type
83+
if (converter != null)
84+
{
85+
Object convertedValue = converter.getAsObject(context, new NullComponent(), valueAsString);
86+
elUtils.setValue(context, el, convertedValue);
87+
}
88+
else
89+
{
90+
elUtils.setValue(context, el, valueAsString);
91+
}
92+
7693
}
7794
catch (Exception e)
7895
{
@@ -112,8 +129,24 @@ private void injectQueryParams(final FacesContext context, final UrlMapping mapp
112129
}
113130
else
114131
{
115-
String value = queryString.getParameter(name);
116-
elUtils.setValue(context, el, value);
132+
133+
String valueAsString = queryString.getParameter(name);
134+
135+
// get the type of the referenced property and try to obtain a converter for it
136+
Class<?> expectedType = elUtils.getExpectedType(context, el);
137+
Converter converter = context.getApplication().createConverter(expectedType);
138+
139+
// Use the convert to create the correct type
140+
if (converter != null)
141+
{
142+
Object convertedValue = converter.getAsObject(context, new NullComponent(), valueAsString);
143+
elUtils.setValue(context, el, convertedValue);
144+
}
145+
else
146+
{
147+
elUtils.setValue(context, el, valueAsString);
148+
}
149+
117150
}
118151
}
119152
catch (Exception e)

0 commit comments

Comments
 (0)