Skip to content

Commit 323d38d

Browse files
committed
Initial version of Navigate class for building action outcomes
1 parent 9f652eb commit 323d38d

3 files changed

Lines changed: 280 additions & 0 deletions

File tree

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
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 org.ocpsoft.rewrite.faces.navigate;
17+
18+
import java.io.UnsupportedEncodingException;
19+
import java.net.URLEncoder;
20+
import java.util.List;
21+
import java.util.Map.Entry;
22+
23+
import org.ocpsoft.common.util.Assert;
24+
25+
/**
26+
* Helper class to build JSF action outcomes.
27+
*
28+
* @author Christian Kaltepoth
29+
*/
30+
public class Navigate
31+
{
32+
33+
private final String viewId;
34+
35+
private final ParameterMap parameters = new ParameterMap();
36+
37+
private boolean redirect = true;
38+
39+
private Navigate(String viewId)
40+
{
41+
this.viewId = viewId;
42+
}
43+
44+
public static Navigate to(String viewId)
45+
{
46+
Assert.notNull(viewId, "viewId must not be null");
47+
return new Navigate(viewId);
48+
}
49+
50+
public Navigate with(CharSequence name, Object value)
51+
{
52+
Assert.notNull(name, "name must not be null");
53+
if (value != null) {
54+
parameters.put(name.toString(), value.toString());
55+
}
56+
return this;
57+
}
58+
59+
public Navigate withoutRedirect()
60+
{
61+
redirect = false;
62+
return this;
63+
}
64+
65+
public String build()
66+
{
67+
68+
ParameterMap query = parameters.copy();
69+
70+
if (redirect) {
71+
query.put("faces-redirect", "true");
72+
}
73+
74+
StringBuilder outcome = new StringBuilder();
75+
outcome.append(viewId);
76+
77+
boolean first = true;
78+
for (Entry<String, List<String>> param : query.entrySet()) {
79+
80+
for (String value : param.getValue()) {
81+
82+
outcome.append(first ? '?' : '&');
83+
84+
outcome.append(encodeQuery(param.getKey()));
85+
outcome.append('=');
86+
outcome.append(encodeQuery(value));
87+
88+
first = false;
89+
90+
}
91+
92+
}
93+
94+
return outcome.toString();
95+
96+
}
97+
98+
private static String encodeQuery(String str)
99+
{
100+
try {
101+
return URLEncoder.encode(str, "UTF-8");
102+
}
103+
catch (UnsupportedEncodingException e) {
104+
throw new IllegalArgumentException(e);
105+
}
106+
}
107+
108+
@Override
109+
public String toString()
110+
{
111+
return build();
112+
}
113+
114+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
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 org.ocpsoft.rewrite.faces.navigate;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
import java.util.Map;
21+
import java.util.Map.Entry;
22+
import java.util.Set;
23+
import java.util.TreeMap;
24+
25+
/**
26+
* Multimap for storing multiple values for each parameter
27+
*
28+
* @author Christian Kaltepoth
29+
*/
30+
class ParameterMap
31+
{
32+
33+
private final Map<String, List<String>> map;
34+
35+
public ParameterMap()
36+
{
37+
map = new TreeMap<String, List<String>>();
38+
}
39+
40+
public ParameterMap(ParameterMap source)
41+
{
42+
map = new TreeMap<String, List<String>>(source.map);
43+
}
44+
45+
public void put(String key, String value)
46+
{
47+
48+
List<String> values = map.get(key);
49+
if (values == null) {
50+
values = new ArrayList<String>();
51+
map.put(key, values);
52+
}
53+
54+
values.add(value);
55+
56+
}
57+
58+
public ParameterMap copy()
59+
{
60+
return new ParameterMap(this);
61+
}
62+
63+
public Set<Entry<String, List<String>>> entrySet()
64+
{
65+
return map.entrySet();
66+
}
67+
68+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
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 org.ocpsoft.rewrite.faces.navigate;
17+
18+
import static org.junit.Assert.assertEquals;
19+
20+
import org.junit.Test;
21+
22+
public class NavigateTest
23+
{
24+
25+
@Test
26+
public void testViewWithoutParameters()
27+
{
28+
assertEquals("/faces/some-view.xhtml?faces-redirect=true",
29+
Navigate.to("/faces/some-view.xhtml")
30+
.build());
31+
}
32+
33+
@Test
34+
public void testViewWithoutRedirect()
35+
{
36+
assertEquals("/faces/some-view.xhtml",
37+
Navigate.to("/faces/some-view.xhtml")
38+
.withoutRedirect()
39+
.build());
40+
}
41+
42+
@Test
43+
public void testViewWithSingleParameter()
44+
{
45+
assertEquals("/faces/some-view.xhtml?faces-redirect=true&param=value",
46+
Navigate.to("/faces/some-view.xhtml")
47+
.with("param", "value")
48+
.build());
49+
}
50+
51+
@Test
52+
public void testViewWithSingleIntegerParameter()
53+
{
54+
assertEquals("/faces/some-view.xhtml?faces-redirect=true&param=123",
55+
Navigate.to("/faces/some-view.xhtml")
56+
.with("param", 123)
57+
.build());
58+
}
59+
60+
@Test
61+
public void testViewWithMultipleParameters()
62+
{
63+
assertEquals("/faces/some-view.xhtml?faces-redirect=true&param=123&param=456",
64+
Navigate.to("/faces/some-view.xhtml")
65+
.with("param", 123)
66+
.with("param", 456)
67+
.build());
68+
}
69+
70+
@Test
71+
public void testViewWithEmptyStringParameters()
72+
{
73+
assertEquals("/faces/some-view.xhtml?faces-redirect=true&param=&param2=",
74+
Navigate.to("/faces/some-view.xhtml")
75+
.with("param", "")
76+
.with("param2", "")
77+
.build());
78+
}
79+
80+
@Test
81+
public void testViewWithNullParameter()
82+
{
83+
assertEquals("/faces/some-view.xhtml?faces-redirect=true",
84+
Navigate.to("/faces/some-view.xhtml")
85+
.with("param", null)
86+
.build());
87+
}
88+
89+
@Test
90+
public void testParameterEncoding()
91+
{
92+
assertEquals("/faces/some-view.xhtml?faces-redirect=true&param=a+b+%C3%A4",
93+
Navigate.to("/faces/some-view.xhtml")
94+
.with("param", "a b \u00e4")
95+
.build());
96+
}
97+
98+
}

0 commit comments

Comments
 (0)