Skip to content

Commit 3fa6be4

Browse files
committed
Added @AfterPhase and @BeforePhase support for @URLAction (rewrite #62)
1 parent ed4c99d commit 3fa6be4

7 files changed

Lines changed: 163 additions & 13 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.ocpsoft.prettyfaces.annotation;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Inherited;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.RetentionPolicy;
8+
import java.lang.annotation.Target;
9+
10+
@Inherited
11+
@Documented
12+
@Target(ElementType.METHOD)
13+
@Retention(RetentionPolicy.RUNTIME)
14+
public @interface AfterPhase
15+
{
16+
17+
Phase value();
18+
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.ocpsoft.prettyfaces.annotation;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Inherited;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.RetentionPolicy;
8+
import java.lang.annotation.Target;
9+
10+
@Inherited
11+
@Documented
12+
@Target(ElementType.METHOD)
13+
@Retention(RetentionPolicy.RUNTIME)
14+
public @interface BeforePhase
15+
{
16+
17+
Phase value();
18+
19+
}

‎annotations/src/main/java/org/ocpsoft/prettyfaces/annotation/URLAction.java‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
public @interface URLAction
1515
{
1616

17-
Phase before() default Phase.NONE;
18-
19-
Phase after() default Phase.NONE;
20-
2117
boolean onPostback() default true;
2218

2319
}

‎annotations/src/main/java/org/ocpsoft/prettyfaces/annotation/handlers/URLActionHandler.java‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import java.lang.reflect.Method;
44

55
import javax.faces.context.FacesContext;
6+
import javax.faces.event.PhaseId;
67

7-
import org.ocpsoft.prettyfaces.annotation.Phase;
8+
import org.ocpsoft.prettyfaces.annotation.AfterPhase;
9+
import org.ocpsoft.prettyfaces.annotation.BeforePhase;
810
import org.ocpsoft.prettyfaces.annotation.URLAction;
911
import org.ocpsoft.rewrite.annotation.api.MethodContext;
1012
import org.ocpsoft.rewrite.annotation.spi.MethodAnnotationHandler;
@@ -39,21 +41,24 @@ public void process(MethodContext context, Method method, URLAction annotation)
3941

4042
// the action invocation must be deferred to get executed inside the JSF lifecycle
4143
PhaseOperation<?> deferredOperation = PhaseAction.enqueue(invocation);
42-
44+
4345
// queue the operation for a specific time in the JSF lifecycle
44-
if (annotation.after() == Phase.NONE && annotation.before() == Phase.NONE) {
45-
deferredOperation.after(javax.faces.event.PhaseId.RESTORE_VIEW);
46+
BeforePhase beforePhase = method.getAnnotation(BeforePhase.class);
47+
AfterPhase afterPhase = method.getAnnotation(AfterPhase.class);
48+
if (beforePhase == null && afterPhase == null) {
49+
deferredOperation.after(PhaseId.RESTORE_VIEW);
4650
}
47-
else if (annotation.after() == Phase.NONE && annotation.before() != Phase.NONE) {
48-
deferredOperation.before(annotation.before().getPhaseId());
51+
else if (beforePhase == null && afterPhase != null) {
52+
deferredOperation.after(afterPhase.value().getPhaseId());
4953
}
50-
else if (annotation.after() != Phase.NONE && annotation.before() == Phase.NONE) {
51-
deferredOperation.after(annotation.after().getPhaseId());
54+
else if (beforePhase != null && afterPhase == null) {
55+
deferredOperation.before(beforePhase.value().getPhaseId());
5256
}
5357
else {
5458
throw new IllegalStateException("Error processing @" + handles().getSimpleName() + " annotation on method "
5559
+ method.getDeclaringClass().getName() + "#" + method.getName()
56-
+ ": You cannot use 'before' and 'after' at the same time.");
60+
+ ": You cannot use @" + BeforePhase.class.getSimpleName() + " and @"
61+
+ AfterPhase.class.getSimpleName() + " at the same time.");
5762
}
5863

5964
// append this operation to the rule
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.ocpsoft.prettyfaces.annotation.basic.action;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import javax.faces.bean.ManagedBean;
7+
import javax.faces.bean.RequestScoped;
8+
import javax.faces.context.FacesContext;
9+
10+
import org.ocpsoft.prettyfaces.annotation.AfterPhase;
11+
import org.ocpsoft.prettyfaces.annotation.BeforePhase;
12+
import org.ocpsoft.prettyfaces.annotation.ForwardTo;
13+
import org.ocpsoft.prettyfaces.annotation.Phase;
14+
import org.ocpsoft.prettyfaces.annotation.URLAction;
15+
import org.ocpsoft.prettyfaces.annotation.URLPattern;
16+
17+
@ManagedBean
18+
@RequestScoped
19+
@URLPattern("/action")
20+
@ForwardTo("/faces/action-phases.xhtml")
21+
public class ActionPhasesBean
22+
{
23+
24+
private final List<String> log = new ArrayList<String>();
25+
26+
@URLAction
27+
public void actionDefaultPhase()
28+
{
29+
log.add("actionDefaultPhase:" + FacesContext.getCurrentInstance().getCurrentPhaseId().toString());
30+
}
31+
32+
@URLAction
33+
@BeforePhase(Phase.RENDER_RESPONSE)
34+
public void actionBeforeRenderResponse()
35+
{
36+
log.add("actionBeforeRenderResponse:" + FacesContext.getCurrentInstance().getCurrentPhaseId().toString());
37+
}
38+
39+
@URLAction
40+
@AfterPhase(Phase.INVOKE_APPLICATION)
41+
public void actionAfterInvokeApplication()
42+
{
43+
log.add("actionAfterInvokeApplication:" + FacesContext.getCurrentInstance().getCurrentPhaseId().toString());
44+
}
45+
46+
public List<String> getLog()
47+
{
48+
return log;
49+
}
50+
51+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.ocpsoft.prettyfaces.annotation.basic.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.prettyfaces.test.PrettyFacesTest;
10+
11+
import com.gargoylesoftware.htmlunit.html.HtmlPage;
12+
13+
@RunWith(Arquillian.class)
14+
public class ActionPhasesTest extends PrettyFacesTest
15+
{
16+
17+
@Deployment(testable = false)
18+
public static WebArchive getDeployment()
19+
{
20+
return getBaseDeployment()
21+
.addClass(ActionPhasesBean.class)
22+
.addAsWebResource("basic/action-phases.xhtml", "action-phases.xhtml");
23+
}
24+
25+
@Test
26+
public void testActionPhases() throws Exception
27+
{
28+
29+
// initial load of page
30+
HtmlPage firstPage = getWebClient("/action").getPage();
31+
32+
// reload so we get a postback that visits all the phases
33+
HtmlPage secondPage = firstPage.getElementById("form:reload").click();
34+
35+
// assert actions invoked in correct phases
36+
String secondPageContent = secondPage.getWebResponse().getContentAsString();
37+
Assert.assertTrue(secondPageContent.contains("actionDefaultPhase:RESTORE_VIEW"));
38+
Assert.assertTrue(secondPageContent.contains("actionBeforeRenderResponse:RENDER_RESPONSE"));
39+
Assert.assertTrue(secondPageContent.contains("actionAfterInvokeApplication:INVOKE_APPLICATION"));
40+
41+
}
42+
43+
}
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"
3+
xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core">
4+
<body>
5+
6+
<h:form id="form">
7+
8+
<ui:repeat var="entry" value="#{actionPhasesBean.log}">
9+
<p>#{entry}</p>
10+
</ui:repeat>
11+
12+
<h:commandButton id="reload" value="Reload" />
13+
14+
</h:form>
15+
16+
</body>
17+
</html>

0 commit comments

Comments
 (0)