Skip to content

Commit eb70f18

Browse files
committed
Added @IgnorePostback for @RequestAction
1 parent 511c0f8 commit eb70f18

6 files changed

Lines changed: 279 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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.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+
@Inherited
26+
@Documented
27+
@Target({ ElementType.METHOD, ElementType.FIELD })
28+
@Retention(RetentionPolicy.RUNTIME)
29+
public @interface IgnorePostback
30+
{
31+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
/*
17+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
18+
*
19+
* Licensed under the Apache License, Version 2.0 (the "License");
20+
* you may not use this file except in compliance with the License.
21+
* You may obtain a copy of the License at
22+
*
23+
* http://www.apache.org/licenses/LICENSE-2.0
24+
*
25+
* Unless required by applicable law or agreed to in writing, software
26+
* distributed under the License is distributed on an "AS IS" BASIS,
27+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28+
* See the License for the specific language governing permissions and
29+
* limitations under the License.
30+
*/
31+
package org.ocpsoft.rewrite.faces.annotation.handler;
32+
33+
import javax.faces.context.FacesContext;
34+
35+
import org.ocpsoft.rewrite.annotation.api.ClassContext;
36+
import org.ocpsoft.rewrite.annotation.api.HandlerChain;
37+
import org.ocpsoft.rewrite.annotation.api.MethodContext;
38+
import org.ocpsoft.rewrite.annotation.handler.HandlerWeights;
39+
import org.ocpsoft.rewrite.annotation.spi.AnnotationHandler;
40+
import org.ocpsoft.rewrite.config.Operation;
41+
import org.ocpsoft.rewrite.context.EvaluationContext;
42+
import org.ocpsoft.rewrite.event.Rewrite;
43+
import org.ocpsoft.rewrite.faces.annotation.IgnorePostback;
44+
45+
public class IgnorePostbackHandler implements AnnotationHandler<IgnorePostback>
46+
{
47+
48+
@Override
49+
public Class<IgnorePostback> handles()
50+
{
51+
return IgnorePostback.class;
52+
}
53+
54+
@Override
55+
public int priority()
56+
{
57+
// must execute after @Deferred
58+
return HandlerWeights.WEIGHT_TYPE_ENRICHING + 10;
59+
}
60+
61+
@Override
62+
public void process(ClassContext context, IgnorePostback annotation, HandlerChain chain)
63+
{
64+
65+
chain.proceed();
66+
67+
if (context instanceof MethodContext) {
68+
Operation operation = (Operation) context.get(Operation.class);
69+
if (operation != null) {
70+
// replace the operation with a wrapped one that ignores postbacks
71+
context.put(Operation.class, new IgnorePostbackOperation(operation));
72+
}
73+
}
74+
75+
}
76+
77+
private static class IgnorePostbackOperation implements Operation
78+
{
79+
80+
private final Operation delegate;
81+
82+
public IgnorePostbackOperation(Operation delegate)
83+
{
84+
this.delegate = delegate;
85+
}
86+
87+
@Override
88+
public void perform(Rewrite event, EvaluationContext context)
89+
{
90+
91+
FacesContext facesContext = FacesContext.getCurrentInstance();
92+
if (facesContext == null) {
93+
throw new IllegalStateException("FacesContext.getCurrentInstance() returned null");
94+
}
95+
96+
boolean postback = facesContext.getRenderKit().getResponseStateManager().isPostback(facesContext);
97+
if (!postback) {
98+
delegate.perform(event, context);
99+
}
100+
101+
}
102+
103+
}
104+
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
org.ocpsoft.rewrite.faces.annotation.handler.DeferredHandler
2+
org.ocpsoft.rewrite.faces.annotation.handler.IgnorePostbackHandler
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
/*
17+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
18+
*
19+
* Licensed under the Apache License, Version 2.0 (the "License");
20+
* you may not use this file except in compliance with the License.
21+
* You may obtain a copy of the License at
22+
*
23+
* http://www.apache.org/licenses/LICENSE-2.0
24+
*
25+
* Unless required by applicable law or agreed to in writing, software
26+
* distributed under the License is distributed on an "AS IS" BASIS,
27+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28+
* See the License for the specific language governing permissions and
29+
* limitations under the License.
30+
*/
31+
package org.ocpsoft.rewrite.faces.annotation.action;
32+
33+
import javax.faces.bean.ManagedBean;
34+
import javax.faces.bean.RequestScoped;
35+
36+
import org.ocpsoft.rewrite.annotation.Join;
37+
import org.ocpsoft.rewrite.annotation.RequestAction;
38+
import org.ocpsoft.rewrite.faces.annotation.Deferred;
39+
import org.ocpsoft.rewrite.faces.annotation.IgnorePostback;
40+
41+
@ManagedBean
42+
@RequestScoped
43+
@Join(path = "/action", to = "/faces/action.xhtml")
44+
public class IgnorePostbackBean
45+
{
46+
47+
private boolean actionDefault;
48+
private boolean actionIgnorePostback;
49+
50+
@RequestAction
51+
@Deferred
52+
public void action1()
53+
{
54+
actionDefault = true;
55+
}
56+
57+
@RequestAction
58+
@Deferred
59+
@IgnorePostback
60+
public void action2()
61+
{
62+
actionIgnorePostback = true;
63+
}
64+
65+
public boolean isActionDefault()
66+
{
67+
return actionDefault;
68+
}
69+
70+
public boolean isActionIgnorePostback()
71+
{
72+
return actionIgnorePostback;
73+
}
74+
75+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.ocpsoft.rewrite.faces.annotation.action;
2+
3+
import org.jboss.arquillian.container.test.api.Deployment;
4+
import org.jboss.arquillian.junit.Arquillian;
5+
import org.jboss.shrinkwrap.api.spec.WebArchive;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.ocpsoft.rewrite.faces.annotation.RewriteFacesAnnotationsTest;
10+
import org.ocpsoft.rewrite.test.RewriteTest;
11+
import org.ocpsoft.rewrite.test.RewriteTestBase;
12+
13+
import com.gargoylesoftware.htmlunit.html.HtmlPage;
14+
15+
@RunWith(Arquillian.class)
16+
public class IgnorePostbackTest extends RewriteTestBase
17+
{
18+
19+
@Deployment(testable = false)
20+
public static WebArchive getDeployment()
21+
{
22+
return RewriteTest.getDeployment()
23+
.addAsLibrary(RewriteFacesAnnotationsTest.getRewriteAnnotationArchive())
24+
.addAsLibrary(RewriteFacesAnnotationsTest.getRewriteFacesArchive())
25+
.addClass(IgnorePostbackBean.class)
26+
.addAsWebResource("action-postback.xhtml", "action.xhtml");
27+
}
28+
29+
@Test
30+
public void testActionPhases() throws Exception
31+
{
32+
33+
HtmlPage firstPage = getWebClient("/action").getPage();
34+
35+
// reload so we get a postback that visits all the phases
36+
HtmlPage secondPage = firstPage.getElementById("form:reload").click();
37+
38+
String secondPageContent = secondPage.getWebResponse().getContentAsString();
39+
assertContains(secondPageContent, "Default = [true]");
40+
assertContains(secondPageContent, "Ignore Postback = [false]");
41+
42+
}
43+
44+
protected static void assertContains(String text, String s)
45+
{
46+
if (text == null || s == null || !text.contains(s)) {
47+
Assert.fail("Could not find [" + s + "] in text: " + text);
48+
}
49+
}
50+
51+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2+
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html"
3+
xmlns:f="http://java.sun.com/jsf/core">
4+
<body>
5+
6+
<h:form id="form">
7+
8+
<p>Default = [#{ignorePostbackBean.actionDefault}]</p>
9+
10+
<p>Ignore Postback = [#{ignorePostbackBean.actionIgnorePostback}]</p>
11+
12+
<h:commandButton id="reload" value="Reload" />
13+
14+
</h:form>
15+
16+
</body>
17+
</html>

0 commit comments

Comments
 (0)