Skip to content

Commit 125bf94

Browse files
committed
Improved JSF phase action Outcome handling, added SendStatus.error()
1 parent 7d0954d commit 125bf94

10 files changed

Lines changed: 121 additions & 31 deletions

File tree

‎api-servlet/src/main/java/com/ocpsoft/rewrite/servlet/event/BaseRewrite.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ public Flow getFlow()
8282
return flow;
8383
}
8484

85+
public void setFlow(final Flow flow)
86+
{
87+
this.flow = flow;
88+
}
89+
8590
public String getDispatchResource()
8691
{
8792
return dispatchResource;

‎api-servlet/src/main/java/com/ocpsoft/rewrite/servlet/event/ServletRewrite.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,9 @@ public interface ServletRewrite<IN extends ServletRequest, OUT extends ServletRe
5757
* Get the current {@link Flow} state.
5858
*/
5959
public Flow getFlow();
60+
61+
/**
62+
* Set the current {@link Flow} state.
63+
*/
64+
public void setFlow(Flow unHandled);
6065
}

‎api-servlet/src/main/java/com/ocpsoft/rewrite/servlet/http/event/HttpInboundServletRewrite.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,16 @@ public interface HttpInboundServletRewrite extends InboundServletRewrite<HttpSer
7272

7373
/**
7474
* If the {@link HttpServletResponse} is not already committed, send an HTTP status code and and call
75-
* {@link #abort()}. Provide the given message to the browser. Provide the given message to the browser as
76-
* [text/html].
75+
* {@link #abort()}.
76+
* <p>
77+
* This method commits the response, after which no more information can be written and the response cannot be
78+
* modified.
79+
*/
80+
public void sendErrorCode(int code);
81+
82+
/**
83+
* If the {@link HttpServletResponse} is not already committed, send an HTTP status code and and call
84+
* {@link #abort()}. Provide the given message to the browser.
7785
* <p>
7886
* This method commits the response, after which no more information can be written and the response cannot be
7987
* modified.

‎impl-servlet/src/main/java/com/ocpsoft/rewrite/servlet/config/Forward.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,10 @@ public RegexOperationParameterBuilder where(final String param, final Binding bi
109109
{
110110
return where(param).bindsTo(binding);
111111
}
112+
113+
@Override
114+
public String toString()
115+
{
116+
return location.toString();
117+
}
112118
}

‎impl-servlet/src/main/java/com/ocpsoft/rewrite/servlet/config/SendStatus.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public void performHttp(final HttpServletRewrite event, final EvaluationContext
4444
((HttpInboundServletRewrite) event).sendStatusCode(code);
4545
}
4646

47+
protected int getCode()
48+
{
49+
return code;
50+
}
51+
4752
/**
4853
* Send an HTTP status code to the browser, then call {@link HttpInboundServletRewrite#abort()}
4954
*/
@@ -52,4 +57,24 @@ public static SendStatus code(final int code)
5257
return new SendStatus(code);
5358
}
5459

60+
public static SendStatus error(final int code)
61+
{
62+
return new SendError(code);
63+
}
64+
65+
public static class SendError extends SendStatus
66+
{
67+
public SendError(final int code)
68+
{
69+
super(code);
70+
}
71+
72+
@Override
73+
public void performHttp(final HttpServletRewrite event, final EvaluationContext context)
74+
{
75+
if (event instanceof HttpInboundServletRewrite)
76+
((HttpInboundServletRewrite) event).sendErrorCode(this.getCode());
77+
}
78+
}
79+
5580
}

‎impl-servlet/src/main/java/com/ocpsoft/rewrite/servlet/impl/HttpInboundRewriteImpl.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ public void sendStatusCode(final int code, final String message)
8989
}
9090
}
9191

92+
@Override
93+
public void sendErrorCode(final int code)
94+
{
95+
sendErrorCode(code, null);
96+
}
97+
9298
@Override
9399
public void sendErrorCode(final int code, final String message)
94100
{
@@ -118,7 +124,7 @@ public void sendErrorCode(final int code, final String message)
118124
}
119125
}
120126

121-
private void redirect(String location, final int code)
127+
private void redirect(final String location, final int code)
122128
{
123129
HttpServletResponse response = getResponse();
124130
if (response.isCommitted())

‎impl-servlet/src/main/java/com/ocpsoft/rewrite/servlet/impl/HttpRewriteWrappedRequest.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,10 @@ public Map<String, String[]> getModifiableParameters()
7979
{
8080
return modifiableParameters;
8181
}
82+
83+
@Override
84+
public String toString()
85+
{
86+
return super.getRequestURL().toString();
87+
}
8288
}

‎integration-faces/src/main/java/com/ocpsoft/rewrite/faces/FacesRewriteLifecycleListener.java‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class FacesRewriteLifecycleListener extends HttpRewriteLifecycleListener
3737
+ "_originalRequestURL";
3838

3939
@Override
40-
public void beforeInboundLifecycle(HttpServletRewrite event)
40+
public void beforeInboundLifecycle(final HttpServletRewrite event)
4141
{
4242
if (DispatcherType.REQUEST.equals(event.getRequest().getDispatcherType()))
4343
{
@@ -47,29 +47,29 @@ public void beforeInboundLifecycle(HttpServletRewrite event)
4747
}
4848
}
4949

50-
public static String getOriginalRequestURL(HttpServletRequest request)
50+
public static String getOriginalRequestURL(final HttpServletRequest request)
5151
{
5252
return (String) request.getAttribute(ORIGINAL_URL);
5353
}
5454

5555
@Override
56-
public void beforeInboundRewrite(HttpServletRewrite event)
56+
public void beforeInboundRewrite(final HttpServletRewrite event)
5757
{}
5858

5959
@Override
60-
public void afterInboundRewrite(HttpServletRewrite event)
60+
public void afterInboundRewrite(final HttpServletRewrite event)
6161
{}
6262

6363
@Override
64-
public void beforeOutboundRewrite(HttpServletRewrite event)
64+
public void beforeOutboundRewrite(final HttpServletRewrite event)
6565
{}
6666

6767
@Override
68-
public void afterOutboundRewrite(HttpServletRewrite event)
68+
public void afterOutboundRewrite(final HttpServletRewrite event)
6969
{}
7070

7171
@Override
72-
public void afterInboundLifecycle(HttpServletRewrite event)
72+
public void afterInboundLifecycle(final HttpServletRewrite event)
7373
{}
7474

7575
@Override

‎integration-faces/src/main/java/com/ocpsoft/rewrite/faces/RewritePhaseListener.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,5 @@ public void handleNavigation(final PhaseEvent event)
110110
navigationHandler.handleNavigation(facesContext, "", navigationCase);
111111
}
112112
}
113+
113114
}

‎integration-faces/src/main/java/com/ocpsoft/rewrite/faces/config/PhaseAction.java‎

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Arrays;
2626
import java.util.List;
2727

28+
import javax.faces.context.FacesContext;
2829
import javax.faces.event.PhaseId;
2930
import javax.servlet.http.HttpServletRequest;
3031

@@ -33,6 +34,7 @@
3334
import com.ocpsoft.rewrite.bind.Retrieval;
3435
import com.ocpsoft.rewrite.bind.Submission;
3536
import com.ocpsoft.rewrite.config.Invoke;
37+
import com.ocpsoft.rewrite.config.Operation;
3638
import com.ocpsoft.rewrite.config.OperationBuilder;
3739
import com.ocpsoft.rewrite.context.EvaluationContext;
3840
import 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

Comments
 (0)