2525import java .util .Arrays ;
2626import java .util .List ;
2727
28+ import javax .faces .context .FacesContext ;
2829import javax .faces .event .PhaseId ;
2930import javax .servlet .http .HttpServletRequest ;
3031
3334import com .ocpsoft .rewrite .bind .Retrieval ;
3435import com .ocpsoft .rewrite .bind .Submission ;
3536import com .ocpsoft .rewrite .config .Invoke ;
37+ import com .ocpsoft .rewrite .config .Operation ;
3638import com .ocpsoft .rewrite .config .OperationBuilder ;
3739import com .ocpsoft .rewrite .context .EvaluationContext ;
3840import com .ocpsoft .rewrite .event .Rewrite ;
@@ -85,6 +87,11 @@ public static List<QueuedPhaseAction> getQueuedPhaseActions(final HttpServletReq
8587 return actions ;
8688 }
8789
90+ public static void removeQueuedPhaseActions (final HttpServletRequest request )
91+ {
92+ request .removeAttribute (QUEUED_ACTIONS );
93+ }
94+
8895 @ SuppressWarnings ("unchecked" )
8996 public void invokeAction (final HttpServletRewrite event , final EvaluationContext context )
9097 {
@@ -105,39 +112,60 @@ else if (retrieval != null)
105112 log .warn ("No binding specified for Invocation." );
106113 }
107114
108- if (result != null )
115+ Flow savedState = event .getFlow ();
116+ event .setFlow (Flow .UN_HANDLED );
117+
118+ try
109119 {
110- ServiceLoader <InvocationResultHandler > providers = ServiceLoader .load (InvocationResultHandler .class );
111- if (!providers .iterator ().hasNext ())
120+ if (result instanceof Operation )
112121 {
113- log .debug ("No instances of [" + InvocationResultHandler .class .getName ()
114- + "] were registered to handing binding invocation result [" + result + "]" );
122+ ((Operation ) result ).perform (event , context );
115123 }
124+ else if (result != null )
125+ {
126+ ServiceLoader <InvocationResultHandler > providers = ServiceLoader .load (InvocationResultHandler .class );
127+ if (!providers .iterator ().hasNext ())
128+ {
129+ log .debug ("No instances of [" + InvocationResultHandler .class .getName ()
130+ + "] were registered to handing binding invocation result [" + result + "]" );
131+ }
116132
117- for (InvocationResultHandler handler : providers ) {
118- handler .handle (event , context , result );
133+ for (InvocationResultHandler handler : providers ) {
134+ handler .handle (event , context , result );
135+ }
119136 }
120137
121- try {
122- if (event .getFlow ().is (Flow .ABORT_REQUEST ))
123- {
124- if (event .getFlow ().is (Flow .FORWARD ))
138+ if (result != null )
139+ {
140+ try {
141+ if (event .getFlow ().is (Flow .ABORT_REQUEST ))
142+ {
143+ FacesContext facesContext = FacesContext .getCurrentInstance ();
144+ if (event .getFlow ().is (Flow .FORWARD ))
145+ {
146+ /*
147+ * We don't want to carry queued phase actions through more than one lifecycle.
148+ */
149+ PhaseAction .removeQueuedPhaseActions (event .getRequest ());
150+ String dispatchResource = ((HttpInboundServletRewrite ) event ).getDispatchResource ();
151+ facesContext .getExternalContext ().dispatch (dispatchResource );
152+ }
153+ facesContext .responseComplete ();
154+ }
155+ else if (event .getFlow ().is (Flow .INCLUDE ))
125156 {
126- event .getRequest ().getRequestDispatcher (((HttpInboundServletRewrite ) event ).getDispatchResource ())
127- .forward (event .getRequest (), event .getResponse ());
157+ throw new IllegalStateException ("Cannot issue INCLUDE directive within JSF lifecycle. Not supported." );
128158 }
129159 }
130- else if (event .getFlow ().is (Flow .INCLUDE ))
131- {
132- event .getRequest ().getRequestDispatcher (((HttpInboundServletRewrite ) event ).getDispatchResource ())
133- .include (event .getRequest (), event .getResponse ());
160+ catch (Exception e ) {
161+ throw new RewriteException (e );
134162 }
135-
136- }
137- catch (Exception e ) {
138- throw new RewriteException (e );
139163 }
140164 }
165+ finally
166+ {
167+ event .setFlow (savedState );
168+ }
141169 }
142170
143171 public List <PhaseId > getBeforePhases ()
0 commit comments