Skip to content

Commit c8f24aa

Browse files
committed
Added support for parsing URL-mappings with JAXBPrettyConfigParser
1 parent 23c576f commit c8f24aa

7 files changed

Lines changed: 249 additions & 0 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.ocpsoft.pretty.faces.config.rewrite.RewriteRule;
2929
import com.ocpsoft.pretty.faces.config.types.PrettyConfigElement;
3030
import com.ocpsoft.pretty.faces.config.types.RewriteElement;
31+
import com.ocpsoft.pretty.faces.config.types.UrlMappingElement;
3132
import com.ocpsoft.pretty.faces.url.URL;
3233

3334
public class PrettyConfig
@@ -58,6 +59,10 @@ public PrettyConfig(PrettyConfigElement prettyConfigElement)
5859
{
5960
globalRewriteRules.add(new RewriteRule(rewriteElement));
6061
}
62+
for (UrlMappingElement mappingElement : prettyConfigElement.getUrlMapping())
63+
{
64+
mappings.add(new UrlMapping(mappingElement));
65+
}
6166
}
6267

6368
/**

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Collections;
2020
import java.util.List;
2121

22+
import com.ocpsoft.pretty.faces.config.types.ValidateElement;
2223
import com.ocpsoft.pretty.faces.el.ConstantExpression;
2324
import com.ocpsoft.pretty.faces.el.PrettyExpression;
2425

@@ -42,6 +43,29 @@ public PathValidator(final int index, final String validatorIds, final String on
4243
this.onError = onError;
4344
}
4445

46+
/**
47+
* Creates a path validator from the supplied JAXB object
48+
*
49+
* @param validateElement
50+
* The JAXB object
51+
*/
52+
public PathValidator(ValidateElement validateElement)
53+
{
54+
index = validateElement.getIndex();
55+
if (validateElement.getOnError() != null)
56+
{
57+
onError = validateElement.getOnError();
58+
}
59+
if (validateElement.getValidatorIds() != null)
60+
{
61+
validatorIds = validateElement.getValidatorIds();
62+
}
63+
if (validateElement.getValidator() != null)
64+
{
65+
validatorExpression = new ConstantExpression(validateElement.getValidator());
66+
}
67+
}
68+
4569
public boolean hasValidators()
4670
{
4771
return validatorIds.trim().length() > 0;

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Collections;
2020
import java.util.List;
2121

22+
import com.ocpsoft.pretty.faces.config.types.QueryParamElement;
2223
import com.ocpsoft.pretty.faces.el.ConstantExpression;
2324
import com.ocpsoft.pretty.faces.el.PrettyExpression;
2425

@@ -37,6 +38,40 @@ public QueryParameter()
3738
super();
3839
}
3940

41+
/**
42+
* Creates an URL action from the supplied JAXB object
43+
*
44+
* @param actionElement
45+
* The JAXB object
46+
*/
47+
public QueryParameter(QueryParamElement queryParamElement)
48+
{
49+
if (queryParamElement.getValidatorIds() != null)
50+
{
51+
validatorIds = queryParamElement.getValidatorIds().trim();
52+
}
53+
if (queryParamElement.getValidator() != null)
54+
{
55+
validatorExpression = new ConstantExpression(queryParamElement.getValidator().trim());
56+
}
57+
if (queryParamElement.getOnError() != null)
58+
{
59+
onError = queryParamElement.getOnError().trim();
60+
}
61+
if (queryParamElement.isOnPostback() != null)
62+
{
63+
onPostback = queryParamElement.isOnPostback();
64+
}
65+
if (queryParamElement.getName() != null)
66+
{
67+
setName(queryParamElement.getName().trim());
68+
}
69+
if (queryParamElement.getValue() != null)
70+
{
71+
setExpression(new ConstantExpression(queryParamElement.getValue().trim()));
72+
}
73+
}
74+
4075
public QueryParameter(final String name, final String value)
4176
{
4277
super(name, value);

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.ocpsoft.pretty.faces.config.mapping;
1717

1818
import com.ocpsoft.pretty.faces.annotation.URLAction.PhaseId;
19+
import com.ocpsoft.pretty.faces.config.types.ActionElement;
1920
import com.ocpsoft.pretty.faces.el.ConstantExpression;
2021
import com.ocpsoft.pretty.faces.el.PrettyExpression;
2122

@@ -34,6 +35,28 @@ public class UrlAction
3435
public UrlAction()
3536
{
3637
}
38+
39+
/**
40+
* Creates an URL action from the supplied JAXB object
41+
*
42+
* @param actionElement
43+
* The JAXB object
44+
*/
45+
public UrlAction(ActionElement actionElement)
46+
{
47+
if (actionElement.getValue() != null)
48+
{
49+
action = new ConstantExpression(actionElement.getValue());
50+
}
51+
if (actionElement.isOnPostback() != null)
52+
{
53+
onPostback = actionElement.isOnPostback();
54+
}
55+
if (actionElement.getPhaseId() != null)
56+
{
57+
phaseId = PhaseId.valueOf(actionElement.getPhaseId().trim().toUpperCase());
58+
}
59+
}
3760

3861
/**
3962
* Creates a new {@link UrlAction} and creates a {@link ConstantExpression}

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
import java.util.ArrayList;
1919
import java.util.List;
2020

21+
import com.ocpsoft.pretty.faces.config.types.ActionElement;
22+
import com.ocpsoft.pretty.faces.config.types.QueryParamElement;
23+
import com.ocpsoft.pretty.faces.config.types.UrlMappingElement;
24+
import com.ocpsoft.pretty.faces.config.types.ValidateElement;
2125
import com.ocpsoft.pretty.faces.el.Expressions;
2226
import com.ocpsoft.pretty.faces.url.URL;
2327
import com.ocpsoft.pretty.faces.url.URLPatternParser;
@@ -39,6 +43,66 @@ public class UrlMapping
3943
private URLPatternParser parser;
4044
private boolean dirty = true;
4145

46+
/**
47+
* Creates an empty {@link UrlMapping}
48+
*/
49+
public UrlMapping()
50+
{
51+
// nothing
52+
}
53+
54+
/**
55+
* Creates an URL mapping from the supplied JAXB object
56+
*
57+
* @param mappingElement
58+
* The JAXB object
59+
*/
60+
public UrlMapping(UrlMappingElement mappingElement)
61+
{
62+
if (mappingElement.getId() != null)
63+
{
64+
id = mappingElement.getId();
65+
}
66+
if (mappingElement.getParentId() != null)
67+
{
68+
parentId = mappingElement.getParentId();
69+
}
70+
if (mappingElement.isOutbound() != null)
71+
{
72+
outbound = mappingElement.isOutbound();
73+
}
74+
if (mappingElement.getViewId() != null)
75+
{
76+
viewId = mappingElement.getViewId().getValue();
77+
}
78+
if (mappingElement.isOnPostback() != null)
79+
{
80+
onPostback = mappingElement.isOnPostback();
81+
}
82+
if (mappingElement.getPattern() != null)
83+
{
84+
if (mappingElement.getPattern().getValue() != null)
85+
{
86+
pattern = mappingElement.getPattern().getValue();
87+
}
88+
if (mappingElement.getPattern().getValidate() != null)
89+
{
90+
for (ValidateElement validateElement : mappingElement.getPattern().getValidate())
91+
{
92+
pathValidators.add(new PathValidator(validateElement));
93+
}
94+
}
95+
}
96+
for (ActionElement actionElement : mappingElement.getAction())
97+
{
98+
actions.add(new UrlAction(actionElement));
99+
}
100+
for (QueryParamElement queryParamElement : mappingElement.getQueryParam())
101+
{
102+
queryParams.add(new QueryParameter(queryParamElement));
103+
}
104+
}
105+
42106
/**
43107
* Return whether or not this Mapping requires DynaView capabilities
44108
*

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import org.junit.Before;
99
import org.junit.Test;
1010

11+
import com.ocpsoft.pretty.faces.annotation.URLAction.PhaseId;
12+
import com.ocpsoft.pretty.faces.config.mapping.UrlMapping;
1113
import com.ocpsoft.pretty.faces.config.rewrite.Case;
1214
import com.ocpsoft.pretty.faces.config.rewrite.Redirect;
1315
import com.ocpsoft.pretty.faces.config.rewrite.RewriteRule;
@@ -28,6 +30,77 @@ public void setUp() throws Exception
2830
config = builder.build();
2931
}
3032

33+
@Test
34+
public void testParseMappings()
35+
{
36+
assertNotNull("PrettyConfig has not been created!", config);
37+
assertNotNull("No mappings parsed", config.getGlobalRewriteRules());
38+
assertEquals(3, config.getMappings().size());
39+
40+
/*
41+
* an empty mapping
42+
*/
43+
UrlMapping emptyMapping = config.getMappings().get(0);
44+
assertEquals("", emptyMapping.getId());
45+
assertEquals("", emptyMapping.getParentId());
46+
assertEquals(true, emptyMapping.isOutbound());
47+
assertEquals("", emptyMapping.getViewId());
48+
assertEquals("", emptyMapping.getPattern());
49+
assertEquals(true, emptyMapping.isOnPostback());
50+
assertEquals(0, emptyMapping.getPathValidators().size());
51+
assertEquals(0, emptyMapping.getActions().size());
52+
assertEquals(0, emptyMapping.getQueryParams().size());
53+
54+
/*
55+
* some standard mapping
56+
*/
57+
UrlMapping storeMapping = config.getMappings().get(1);
58+
assertEquals("store", storeMapping.getId());
59+
assertEquals("", storeMapping.getParentId());
60+
assertEquals(true, storeMapping.isOutbound());
61+
assertEquals("/faces/shop/store.jsf", storeMapping.getViewId());
62+
assertEquals("/store/", storeMapping.getPattern());
63+
assertEquals(true, storeMapping.isOnPostback());
64+
assertEquals(0, storeMapping.getActions().size());
65+
assertEquals(0, storeMapping.getPathValidators().size());
66+
67+
assertEquals(1, storeMapping.getQueryParams().size());
68+
assertEquals("language", storeMapping.getQueryParams().get(0).getName());
69+
assertEquals("#{bean.language}", storeMapping.getQueryParams().get(0).getExpression().getELExpression());
70+
71+
/*
72+
* a complete mapping
73+
*/
74+
UrlMapping completeMapping = config.getMappings().get(2);
75+
assertEquals("complete", completeMapping.getId());
76+
assertEquals("parentId", completeMapping.getParentId());
77+
assertEquals(false, completeMapping.isOutbound());
78+
assertEquals("/faces/validate.jsf", completeMapping.getViewId());
79+
assertEquals("/#{bean.path}", completeMapping.getPattern());
80+
assertEquals(false, completeMapping.isOnPostback());
81+
82+
assertEquals(1, completeMapping.getActions().size());
83+
assertEquals("#{bean.action}", completeMapping.getActions().get(0).getAction().getELExpression());
84+
assertEquals(PhaseId.RENDER_RESPONSE, completeMapping.getActions().get(0).getPhaseId());
85+
assertEquals(false, completeMapping.getActions().get(0).onPostback());
86+
87+
assertEquals(1, completeMapping.getQueryParams().size());
88+
assertEquals("q", completeMapping.getQueryParams().get(0).getName());
89+
assertEquals("#{bean.query}", completeMapping.getQueryParams().get(0).getExpression().getELExpression());
90+
assertEquals("pretty:error", completeMapping.getQueryParams().get(0).getOnError());
91+
assertEquals("validatorId", completeMapping.getQueryParams().get(0).getValidatorIds());
92+
assertEquals("#{bean.validate}", completeMapping.getQueryParams().get(0).getValidatorExpression().getELExpression());
93+
assertEquals(false, completeMapping.getQueryParams().get(0).isOnPostback());
94+
95+
assertEquals(1, completeMapping.getPathValidators().size());
96+
assertEquals(0, completeMapping.getPathValidators().get(0).getIndex());
97+
assertEquals("#{bean.validatorError}", completeMapping.getPathValidators().get(0).getOnError());
98+
assertEquals("validator1", completeMapping.getPathValidators().get(0).getValidatorIds());
99+
assertEquals("#{bean.validate}", completeMapping.getPathValidators().get(0).getValidatorExpression().getELExpression());
100+
101+
}
102+
103+
31104
@Test
32105
public void testParseRewriteRules()
33106
{

‎core/src/test/resources/complete-pretty-config.xml‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,31 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://ocpsoft.com/prettyfaces/3.1.1 http://ocpsoft.com/xml/ns/prettyfaces/ocpsoft-pretty-faces-3.1.1.xsd">
55

6+
<!-- minimal mapping -->
7+
<url-mapping/>
8+
9+
<!-- some sample mapping -->
10+
<url-mapping id="store">
11+
<pattern value="/store/" />
12+
<view-id value="/faces/shop/store.jsf" />
13+
<query-param name="language"> #{bean.language} </query-param>
14+
</url-mapping>
15+
16+
<!-- complete mapping -->
17+
<url-mapping id="complete" onPostback="false" outbound="false"
18+
parentId="parentId" inbound="false">
19+
<pattern value="/#{bean.path}">
20+
<validate index="0" validatorIds="validator1"
21+
onError="#{bean.validatorError}" validator="#{bean.validate}" />
22+
</pattern>
23+
<query-param name="q" validatorIds="validatorId"
24+
onError="pretty:error" validator="#{bean.validate}" onPostback="false">
25+
#{bean.query}
26+
</query-param>
27+
<action onPostback="false" phaseId="RENDER_RESPONSE">#{bean.action}</action>
28+
<view-id value="/faces/validate.jsf" />
29+
</url-mapping>
30+
631
<!-- minimal configuration -->
732
<rewrite />
833

0 commit comments

Comments
 (0)