Skip to content

Commit fe5f93c

Browse files
committed
New @URLMappings annotation to place multiple mappings on one class
1 parent 607eb34 commit fe5f93c

3 files changed

Lines changed: 241 additions & 55 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
* Container annotation for multiple {@link URLMapping} annotations placed on
28+
* a single class.
29+
* </p>
30+
*
31+
* @author Christian Kaltepoth
32+
* @see URLMapping
33+
* @since 3.2.0
34+
*/
35+
@Retention(RetentionPolicy.RUNTIME)
36+
@Target(ElementType.TYPE)
37+
@Inherited
38+
@Documented
39+
public @interface URLMappings {
40+
41+
/**
42+
* <p>
43+
* List of {@link URLMapping} annotations applied to the class.
44+
* </p>
45+
*/
46+
URLMapping[] mappings() default {};
47+
}

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

Lines changed: 89 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.ocpsoft.pretty.faces.annotation.URLActions;
3131
import com.ocpsoft.pretty.faces.annotation.URLBeanName;
3232
import com.ocpsoft.pretty.faces.annotation.URLMapping;
33+
import com.ocpsoft.pretty.faces.annotation.URLMappings;
3334
import com.ocpsoft.pretty.faces.annotation.URLQueryParameter;
3435
import com.ocpsoft.pretty.faces.annotation.URLValidator;
3536
import com.ocpsoft.pretty.faces.config.PrettyConfigBuilder;
@@ -105,21 +106,21 @@ public void processClass(Class clazz)
105106

106107
// scan for PrettyAnnotation class
107108
// returns the mapping ID, if an annotation was found
108-
String classMappingId = processPrettyMappingAnnotation(clazz);
109+
String[] classMappingIds = processClassMappingAnnotations(clazz);
109110

110111
// scan for PrettyBean annotation
111112
processPrettyBeanAnnotation(clazz);
112113

113114
// process annotations on public methods
114115
for (Method method : clazz.getMethods())
115116
{
116-
processMethodAnnotations(method, new String[] { classMappingId } );
117+
processMethodAnnotations(method, classMappingIds);
117118
}
118119

119120
// loop over fields to find URLQueryParameter annotations
120121
for (Field field : clazz.getDeclaredFields())
121122
{
122-
processFieldAnnotations(field, new String[] { classMappingId } );
123+
processFieldAnnotations(field, classMappingIds);
123124
}
124125

125126
}
@@ -132,86 +133,119 @@ public void processClass(Class clazz)
132133
}
133134

134135
/**
135-
* Checks the class for a {@link URLMapping} annotation.
136+
* Checks for PrettyFaces mapping annotations on a single class
136137
*
137-
* @param clazz Class to scan
138-
* @return The mapping ID or <code>null</code> if no mapping was found
138+
* @param clazz
139+
* Class to scan
140+
* @return The IDs of the mappings found on the class
139141
*/
140-
private String processPrettyMappingAnnotation(Class clazz)
142+
public String[] processClassMappingAnnotations(Class clazz)
141143
{
142144

145+
// list of all mapping IDs found on the class
146+
List<String> classMappingIds = new ArrayList<String>();
147+
143148
// get reference to @URLMapping annotation
144149
URLMapping mappingAnnotation = (URLMapping) clazz.getAnnotation(URLMapping.class);
145150

146151
// process annotation if it exists
147152
if (mappingAnnotation != null)
148153
{
154+
String mappingId = processPrettyMappingAnnotation(clazz, mappingAnnotation);
155+
classMappingIds.add(mappingId);
156+
}
149157

150-
// log class name
151-
if (log.isTraceEnabled())
158+
// container annotation
159+
URLMappings mappingsAnnotation = (URLMappings) clazz.getAnnotation(URLMappings.class);
160+
161+
if (mappingsAnnotation != null)
162+
{
163+
164+
// process all contained @URLMapping annotations
165+
for (URLMapping child : mappingsAnnotation.mappings())
152166
{
153-
log.trace("Found @URLMapping annotation on class: " + clazz.getName());
167+
String mappingId = processPrettyMappingAnnotation(clazz, child);
168+
classMappingIds.add(mappingId);
154169
}
155170

156-
// create UrlMapping from annotation
157-
UrlMapping mapping = new UrlMapping();
158-
mapping.setId(mappingAnnotation.id());
159-
mapping.setParentId(mappingAnnotation.parentId());
160-
mapping.setPattern(mappingAnnotation.pattern());
161-
mapping.setViewId(mappingAnnotation.viewId());
162-
mapping.setOutbound(mappingAnnotation.outbound());
163-
mapping.setOnPostback(mappingAnnotation.onPostback());
171+
}
164172

165-
// register mapping
166-
Object existingMapping = urlMappings.put(mapping.getId(), mapping);
173+
// return list of mappings found
174+
return classMappingIds.toArray(new String[classMappingIds.size()]);
167175

168-
// fail if a mapping with this ID already existed
169-
if (existingMapping != null)
170-
{
171-
throw new IllegalArgumentException("Duplicated mapping id: " + mapping.getId());
172-
}
176+
}
177+
178+
/**
179+
* Process a single {@link URLMapping} annotation.
180+
*
181+
* @param clazz The class that the annotation was found on
182+
* @param mappingAnnotation The annotation to process
183+
* @return The mapping ID of the mapping found
184+
*/
185+
private String processPrettyMappingAnnotation(Class clazz, URLMapping mappingAnnotation)
186+
{
173187

174-
// At bean name to lookup map if it has been specified
175-
if ((mappingAnnotation.beanName() != null) && (mappingAnnotation.beanName().length() > 0))
176-
{
177-
beanNameMap.put(clazz, mappingAnnotation.beanName());
178-
}
188+
// log class name
189+
if (log.isTraceEnabled())
190+
{
191+
log.trace("Found @URLMapping annotation on class: " + clazz.getName());
192+
}
179193

180-
// process validations
181-
for (URLValidator validationAnnotation : mappingAnnotation.validation())
182-
{
194+
// create UrlMapping from annotation
195+
UrlMapping mapping = new UrlMapping();
196+
mapping.setId(mappingAnnotation.id());
197+
mapping.setParentId(mappingAnnotation.parentId());
198+
mapping.setPattern(mappingAnnotation.pattern());
199+
mapping.setViewId(mappingAnnotation.viewId());
200+
mapping.setOutbound(mappingAnnotation.outbound());
201+
mapping.setOnPostback(mappingAnnotation.onPostback());
183202

184-
// index attribute is required in this case
185-
if (validationAnnotation.index() < 0)
186-
{
187-
throw new IllegalArgumentException(
188-
"Please set the index of the path parameter you want to validate with the @URLValidator specified on mapping: " + mapping.getId());
189-
}
203+
// register mapping
204+
Object existingMapping = urlMappings.put(mapping.getId(), mapping);
190205

191-
// prepare PathValidator
192-
PathValidator pathValidator = new PathValidator();
193-
pathValidator.setIndex(validationAnnotation.index());
194-
pathValidator.setOnError(validationAnnotation.onError());
195-
pathValidator.setValidatorIds(join(validationAnnotation.validatorIds(), " "));
206+
// fail if a mapping with this ID already existed
207+
if (existingMapping != null)
208+
{
209+
throw new IllegalArgumentException("Duplicated mapping id: " + mapping.getId());
210+
}
196211

197-
// optional validator method
198-
if (!isBlank(validationAnnotation.validator()))
199-
{
200-
pathValidator.setValidatorExpression(new ConstantExpression(validationAnnotation.validator()));
201-
}
212+
// At bean name to lookup map if it has been specified
213+
if ((mappingAnnotation.beanName() != null) && (mappingAnnotation.beanName().length() > 0))
214+
{
215+
beanNameMap.put(clazz, mappingAnnotation.beanName());
216+
}
202217

203-
// add PathValidator to the mapping
204-
mapping.getPathValidators().add(pathValidator);
218+
// process validations
219+
for (URLValidator validationAnnotation : mappingAnnotation.validation())
220+
{
205221

222+
// index attribute is required in this case
223+
if (validationAnnotation.index() < 0)
224+
{
225+
throw new IllegalArgumentException(
226+
"Please set the index of the path parameter you want to validate with the @URLValidator specified on mapping: " + mapping.getId());
206227
}
207228

208-
// return mapping id
209-
return mapping.getId();
229+
// prepare PathValidator
230+
PathValidator pathValidator = new PathValidator();
231+
pathValidator.setIndex(validationAnnotation.index());
232+
pathValidator.setOnError(validationAnnotation.onError());
233+
pathValidator.setValidatorIds(join(validationAnnotation.validatorIds(), " "));
234+
235+
// optional validator method
236+
if (!isBlank(validationAnnotation.validator()))
237+
{
238+
pathValidator.setValidatorExpression(new ConstantExpression(validationAnnotation.validator()));
239+
}
240+
241+
// add PathValidator to the mapping
242+
mapping.getPathValidators().add(pathValidator);
210243

211244
}
212245

213-
// we have not found a mapping
214-
return null;
246+
// return mapping id
247+
return mapping.getId().trim();
248+
215249
}
216250

217251
/**

‎core/src/test/java/com/ocpsoft/pretty/faces/config/annotation/PrettyAnnotationHandlerTest.java‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@
1818
import static org.junit.Assert.assertEquals;
1919
import static org.junit.Assert.assertNotNull;
2020

21+
import java.util.Arrays;
22+
import java.util.Collections;
23+
import java.util.List;
24+
2125
import org.junit.Test;
2226

2327
import com.ocpsoft.pretty.faces.annotation.URLAction;
2428
import com.ocpsoft.pretty.faces.annotation.URLAction.PhaseId;
2529
import com.ocpsoft.pretty.faces.annotation.URLActions;
2630
import com.ocpsoft.pretty.faces.annotation.URLBeanName;
2731
import com.ocpsoft.pretty.faces.annotation.URLMapping;
32+
import com.ocpsoft.pretty.faces.annotation.URLMappings;
2833
import com.ocpsoft.pretty.faces.annotation.URLQueryParameter;
2934
import com.ocpsoft.pretty.faces.annotation.URLValidator;
3035
import com.ocpsoft.pretty.faces.config.PrettyConfig;
@@ -335,6 +340,72 @@ public void testMappingWithSingleQueryParameterAndValidation()
335340

336341
}
337342

343+
@Test
344+
public void testClassWithMultipleMappings()
345+
{
346+
347+
// create handler and process class
348+
PrettyAnnotationHandler handler = new PrettyAnnotationHandler(null);
349+
handler.processClass(ClassWithMultipleMappings.class);
350+
351+
// build configuration
352+
PrettyConfigBuilder configBuilder = new PrettyConfigBuilder();
353+
handler.build(configBuilder);
354+
PrettyConfig config = configBuilder.build();
355+
356+
// no mappings added
357+
assertNotNull(config.getMappings());
358+
assertEquals(2, config.getMappings().size());
359+
360+
// validate mapping properties for mappingA
361+
UrlMapping mappingA = config.getMappingById("mappingA");
362+
assertNotNull(mappingA);
363+
assertEquals("mappingA", mappingA.getId());
364+
assertEquals("/some/url/a", mappingA.getPattern());
365+
assertEquals("/view.jsf", mappingA.getViewId());
366+
assertEquals(true, mappingA.isOutbound());
367+
assertEquals(true, mappingA.isOnPostback());
368+
assertEquals(1, mappingA.getActions().size());
369+
assertEquals(2, mappingA.getQueryParams().size());
370+
assertEquals(0, mappingA.getPathValidators().size());
371+
assertEquals("#{multiMappingBean.actionForBoth}",
372+
mappingA.getActions().get(0).getAction().getELExpression());
373+
374+
// we don't know the order in which the query parameters are added
375+
List<String> queryParamExpressionsA = Arrays.asList(
376+
mappingA.getQueryParams().get(0).getExpression().getELExpression(),
377+
mappingA.getQueryParams().get(1).getExpression().getELExpression()
378+
);
379+
Collections.sort(queryParamExpressionsA);
380+
assertEquals("#{multiMappingBean.queryParameterForA}", queryParamExpressionsA.get(0));
381+
assertEquals("#{multiMappingBean.queryParameterForBoth}", queryParamExpressionsA.get(1));
382+
383+
// validate mapping properties for mappingB
384+
UrlMapping mappingB = config.getMappingById("mappingB");
385+
assertNotNull(mappingB);
386+
assertEquals("mappingB", mappingB.getId());
387+
assertEquals("/some/url/b", mappingB.getPattern());
388+
assertEquals("/view.jsf", mappingB.getViewId());
389+
assertEquals(true, mappingB.isOutbound());
390+
assertEquals(true, mappingB.isOnPostback());
391+
assertEquals(2, mappingB.getActions().size());
392+
assertEquals(1, mappingB.getQueryParams().size());
393+
assertEquals(0, mappingB.getPathValidators().size());
394+
assertEquals("#{multiMappingBean.queryParameterForBoth}",
395+
mappingB.getQueryParams().get(0).getExpression().getELExpression());
396+
397+
// we don't know the order in which the actions are added
398+
List<String> actionExpressionsB = Arrays.asList(
399+
mappingB.getActions().get(0).getAction().getELExpression(),
400+
mappingB.getActions().get(1).getAction().getELExpression()
401+
);
402+
Collections.sort(actionExpressionsB);
403+
assertEquals("#{multiMappingBean.actionForBoth}", actionExpressionsB.get(0));
404+
assertEquals("#{multiMappingBean.actionForB}", actionExpressionsB.get(1));
405+
406+
}
407+
408+
338409
/**
339410
* Simple class without any PrettyFaces annotations
340411
*/
@@ -447,4 +518,38 @@ public class ClassWithSingleQueryParameterAndValidation
447518

448519
}
449520

521+
/*
522+
* Class with two mappings
523+
*/
524+
@URLMappings(mappings={
525+
@URLMapping(id = "mappingA", pattern = "/some/url/a", viewId = "/view.jsf"),
526+
@URLMapping(id = "mappingB", pattern = "/some/url/b", viewId = "/view.jsf")
527+
})
528+
@URLBeanName("multiMappingBean")
529+
public class ClassWithMultipleMappings
530+
{
531+
532+
// assigned to both mappings
533+
@URLQueryParameter("q1")
534+
@SuppressWarnings("unused")
535+
private String queryParameterForBoth;
536+
537+
// assigned to both mappings
538+
@URLQueryParameter(value="q2", mappingId="mappingA")
539+
@SuppressWarnings("unused")
540+
private String queryParameterForA;
541+
542+
// assigned to both mappings
543+
@URLAction
544+
public void actionForBoth() {
545+
// nothing
546+
}
547+
548+
// assigned to both mappings
549+
@URLAction(mappingId="mappingB")
550+
public void actionForB() {
551+
// nothing
552+
}
553+
554+
}
450555
}

0 commit comments

Comments
 (0)