Skip to content

Commit 797a93e

Browse files
committed
Added support for custom converters for query and path parameters
1 parent e509e1b commit 797a93e

17 files changed

Lines changed: 747 additions & 8 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
package com.ocpsoft.pretty.faces.annotation;
17+
18+
import java.lang.annotation.Documented;
19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Inherited;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
/**
26+
* <p>
27+
* Annotation representing a converter attached to an individual path or a query parameter.
28+
* </p>
29+
*
30+
* @author Christian Kaltepoth <christian@kaltepoth.de>
31+
*/
32+
@Retention(RetentionPolicy.RUNTIME)
33+
@Target({ ElementType.PARAMETER, ElementType.FIELD })
34+
@Inherited
35+
@Documented
36+
public @interface URLConverter
37+
{
38+
39+
/**
40+
* Converters may be attached to individual parameters of a URL mapping. Only used for path parameters.
41+
*/
42+
int index() default -1;
43+
44+
/**
45+
* The ID of the JSF converter to use for the parameter.
46+
*/
47+
String converterId();
48+
49+
}

‎core/src/main/java/com/ocpsoft/pretty/faces/annotation/URLMapping.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@
9797
* </p>
9898
*/
9999
String viewId();
100+
101+
/**
102+
* <p>
103+
* Specify the URL converters for this mapping.
104+
* </p>
105+
* <p>
106+
* Please not that the <code>index</code> property of the
107+
* {@link URLConverter} annotations must be set to identify the path
108+
* parameter you want the converter to refer to.
109+
* </p>
110+
*/
111+
URLConverter[] converter() default {};
100112

101113
/**
102114
* <p>

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

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.ocpsoft.logging.Logger;
2424
import com.ocpsoft.pretty.PrettyContext;
2525
import com.ocpsoft.pretty.PrettyException;
26+
import com.ocpsoft.pretty.faces.config.mapping.PathConverter;
2627
import com.ocpsoft.pretty.faces.config.mapping.PathParameter;
2728
import com.ocpsoft.pretty.faces.config.mapping.QueryParameter;
2829
import com.ocpsoft.pretty.faces.config.mapping.UrlMapping;
@@ -73,9 +74,30 @@ private void injectPathParams(final FacesContext context, final URL url, final U
7374
try
7475
{
7576

76-
// get the type of the referenced property and try to obtain a converter for it
77-
Class<?> expectedType = elUtils.getExpectedType(context, el);
78-
Converter converter = context.getApplication().createConverter(expectedType);
77+
// we will use this converter for the parameter
78+
Converter converter = null;
79+
80+
// look for a user-specific converter
81+
PathConverter pathConverter = mapping.getPathConverterForPathParam(param);
82+
if(pathConverter != null && pathConverter.getConverterId() != null && pathConverter.getConverterId().trim().length() > 0) {
83+
84+
converter = context.getApplication().createConverter(pathConverter.getConverterId().trim());
85+
86+
// fail if the converter does not exist
87+
if(converter == null) {
88+
throw new IllegalStateException("Cannot find JSF converter: "+pathConverter.getConverterId());
89+
}
90+
91+
}
92+
93+
// use the default converter for the type otherwise
94+
else {
95+
96+
// get the type of the referenced property and try to obtain a converter for it
97+
Class<?> expectedType = elUtils.getExpectedType(context, el);
98+
converter = context.getApplication().createConverter(expectedType);
99+
100+
}
79101

80102
// Use the convert to create the correct type
81103
if (converter != null)
@@ -130,10 +152,31 @@ private void injectQueryParams(final FacesContext context, final UrlMapping mapp
130152

131153
String valueAsString = queryString.getParameter(name);
132154

133-
// get the type of the referenced property and try to obtain a converter for it
134-
Class<?> expectedType = elUtils.getExpectedType(context, el);
135-
Converter converter = context.getApplication().createConverter(expectedType);
136-
155+
156+
// we will use this converter for the parameter
157+
Converter converter = null;
158+
159+
// look for a user-specific converter
160+
if(param.getConverterId() != null && param.getConverterId().trim().length() > 0) {
161+
162+
converter = context.getApplication().createConverter(param.getConverterId().trim());
163+
164+
// fail if the converter does not exist
165+
if(converter == null) {
166+
throw new IllegalStateException("Cannot find JSF converter: "+param.getConverterId());
167+
}
168+
169+
}
170+
171+
// use the default converter for the type otherwise
172+
else {
173+
174+
// get the type of the referenced property and try to obtain a converter for it
175+
Class<?> expectedType = elUtils.getExpectedType(context, el);
176+
converter = context.getApplication().createConverter(expectedType);
177+
178+
}
179+
137180
// Use the convert to create the correct type
138181
if (converter != null)
139182
{

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727
import com.ocpsoft.pretty.faces.annotation.URLAction.PhaseId;
2828
import com.ocpsoft.pretty.faces.annotation.URLActions;
2929
import com.ocpsoft.pretty.faces.annotation.URLBeanName;
30+
import com.ocpsoft.pretty.faces.annotation.URLConverter;
3031
import com.ocpsoft.pretty.faces.annotation.URLMapping;
3132
import com.ocpsoft.pretty.faces.annotation.URLMappings;
3233
import com.ocpsoft.pretty.faces.annotation.URLQueryParameter;
3334
import com.ocpsoft.pretty.faces.annotation.URLValidator;
3435
import com.ocpsoft.pretty.faces.config.PrettyConfigBuilder;
36+
import com.ocpsoft.pretty.faces.config.mapping.PathConverter;
3537
import com.ocpsoft.pretty.faces.config.mapping.PathValidator;
3638
import com.ocpsoft.pretty.faces.config.mapping.QueryParameter;
3739
import com.ocpsoft.pretty.faces.config.mapping.UrlAction;
@@ -40,8 +42,9 @@
4042
import com.ocpsoft.pretty.faces.el.LazyBeanNameFinder;
4143
import com.ocpsoft.pretty.faces.el.LazyExpression;
4244
import com.ocpsoft.pretty.faces.el.PrettyExpression;
45+
import com.ocpsoft.pretty.faces.util.StringUtils;
4346

44-
@SuppressWarnings("unchecked")
47+
@SuppressWarnings({"unchecked", "rawtypes"})
4548
public class PrettyAnnotationHandler
4649
{
4750

@@ -239,6 +242,28 @@ private String processPrettyMappingAnnotation(final Class clazz, final URLMappin
239242
mapping.getPathValidators().add(pathValidator);
240243

241244
}
245+
246+
// process converters
247+
for (URLConverter converterAnnotation : mappingAnnotation.converter())
248+
{
249+
250+
// index attribute is required in this case
251+
if (converterAnnotation.index() < 0)
252+
{
253+
throw new IllegalArgumentException(
254+
"Please set the index of the path parameter you want to convert with the @URLConverter specified on mapping: "
255+
+ mapping.getId());
256+
}
257+
258+
// prepare PathValidator
259+
PathConverter pathConverter = new PathConverter();
260+
pathConverter.setIndex(converterAnnotation.index());
261+
pathConverter.setConverterId(StringUtils.trimToNull(converterAnnotation.converterId()));
262+
263+
// add PathValidator to the mapping
264+
mapping.addPathConverter(pathConverter);
265+
266+
}
242267

243268
// return mapping id
244269
return mapping.getId().trim();
@@ -354,6 +379,12 @@ else if ((classMappingIds != null) && (classMappingIds.length > 0))
354379
queryParam.setValidator(validationAnnotation.validator());
355380

356381
}
382+
383+
// check if there is also a converter annotation placed on the field
384+
URLConverter converterAnnotation = field.getAnnotation(URLConverter.class);
385+
if (converterAnnotation != null) {
386+
queryParam.setConverterId(converterAnnotation.converterId().trim());
387+
}
357388

358389
// add the new spec object to the list of specs
359390
queryParamList.add(queryParam);
@@ -494,6 +525,7 @@ public void build(final PrettyConfigBuilder builder)
494525
QueryParameter queryParam = new QueryParameter();
495526
queryParam.setName(queryParamSpec.getName());
496527
queryParam.setOnError(queryParamSpec.getOnError());
528+
queryParam.setConverterId(StringUtils.trimToNull(queryParamSpec.getConverterId()));
497529
queryParam.setValidatorIds(join(queryParamSpec.getValidatorIds(), " "));
498530
queryParam.setOnPostback(queryParamSpec.isOnPostback());
499531

@@ -668,6 +700,7 @@ private static class QueryParamSpec
668700
private String[] mappingIds;
669701
private String name;
670702
private String onError;
703+
private String converterId;
671704
private String[] validatorIds = {};
672705
private String validator;
673706
private boolean onPostback;
@@ -751,6 +784,16 @@ public void setMappingIds(final String[] mappingIds)
751784
{
752785
this.mappingIds = mappingIds;
753786
}
787+
788+
public String getConverterId()
789+
{
790+
return converterId;
791+
}
792+
793+
public void setConverterId(String converterId)
794+
{
795+
this.converterId = converterId;
796+
}
754797
}
755798

756799
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
package com.ocpsoft.pretty.faces.config.mapping;
17+
18+
import com.ocpsoft.pretty.faces.config.types.ConvertElement;
19+
import com.ocpsoft.pretty.faces.util.StringUtils;
20+
21+
/**
22+
* @author Christian Kaltepoth <christian@kaltepoth.de>
23+
*/
24+
public class PathConverter
25+
{
26+
private int index;
27+
private String converterId;
28+
29+
public PathConverter()
30+
{
31+
// nothing
32+
}
33+
34+
/**
35+
* Creates a path converter from the supplied JAXB object
36+
*
37+
* @param converterElement The JAXB object
38+
*/
39+
public PathConverter(ConvertElement converterElement)
40+
{
41+
index = converterElement.getIndex();
42+
if (StringUtils.isNotBlank(converterElement.getConverterId()))
43+
{
44+
converterId = converterElement.getConverterId().trim();
45+
}
46+
}
47+
48+
public int getIndex()
49+
{
50+
return index;
51+
}
52+
53+
public void setIndex(int index)
54+
{
55+
this.index = index;
56+
}
57+
58+
public String getConverterId()
59+
{
60+
return converterId;
61+
}
62+
63+
public void setConverterId(String converterId)
64+
{
65+
this.converterId = converterId;
66+
}
67+
68+
@Override
69+
public int hashCode()
70+
{
71+
final int prime = 31;
72+
int result = 1;
73+
result = prime * result + ((converterId == null) ? 0 : converterId.hashCode());
74+
result = prime * result + index;
75+
return result;
76+
}
77+
78+
@Override
79+
public boolean equals(Object obj)
80+
{
81+
if (this == obj)
82+
return true;
83+
if (obj == null)
84+
return false;
85+
if (getClass() != obj.getClass())
86+
return false;
87+
PathConverter other = (PathConverter) obj;
88+
if (converterId == null) {
89+
if (other.converterId != null)
90+
return false;
91+
}
92+
else if (!converterId.equals(other.converterId))
93+
return false;
94+
if (index != other.index)
95+
return false;
96+
return true;
97+
}
98+
99+
@Override
100+
public String toString()
101+
{
102+
return "PathConverter [index=" + index + ", converterId=" + converterId + "]";
103+
}
104+
105+
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
import com.ocpsoft.pretty.faces.config.types.QueryParamElement;
2323
import com.ocpsoft.pretty.faces.el.ConstantExpression;
2424
import com.ocpsoft.pretty.faces.el.PrettyExpression;
25+
import com.ocpsoft.pretty.faces.util.StringUtils;
2526

2627
/**
2728
* @author Lincoln Baxter, III <lincoln@ocpsoft.com>
2829
*/
2930
public class QueryParameter extends RequestParameter
3031
{
32+
private String converterId;
3133
private String validatorIds = "";
3234
private PrettyExpression validatorExpression;
3335
private String onError = "";
@@ -46,6 +48,9 @@ public QueryParameter()
4648
*/
4749
public QueryParameter(QueryParamElement queryParamElement)
4850
{
51+
if(StringUtils.isNotBlank(queryParamElement.getConverterId())) {
52+
converterId = queryParamElement.getConverterId().trim();
53+
}
4954
if (queryParamElement.getValidatorIds() != null)
5055
{
5156
validatorIds = queryParamElement.getValidatorIds().trim();
@@ -147,4 +152,14 @@ public void setOnPostback(boolean onPostback)
147152
this.onPostback = onPostback;
148153
}
149154

155+
public String getConverterId()
156+
{
157+
return converterId;
158+
}
159+
160+
public void setConverterId(String converterId)
161+
{
162+
this.converterId = converterId;
163+
}
164+
150165
}

0 commit comments

Comments
 (0)