Skip to content

Commit 9c8262e

Browse files
committed
Issue 30 - Can now perform tasks in a subflow without affecting the actual Flow state
1 parent 436bb6a commit 9c8262e

17 files changed

Lines changed: 314 additions & 23 deletions

File tree

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
22
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
7+
*
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -24,7 +24,7 @@
2424

2525
/**
2626
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
27-
*
27+
*
2828
*/
2929
public interface ServletRewrite<IN extends ServletRequest, OUT extends ServletResponse>
3030
extends Rewrite
@@ -61,5 +61,5 @@ public interface ServletRewrite<IN extends ServletRequest, OUT extends ServletRe
6161
/**
6262
* Set the current {@link Flow} state.
6363
*/
64-
public void setFlow(Flow unHandled);
64+
public void setFlow(Flow flow);
6565
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.ocpsoft.rewrite.servlet.event;
2+
3+
import org.ocpsoft.rewrite.context.EvaluationContext;
4+
import org.ocpsoft.rewrite.servlet.event.BaseRewrite.Flow;
5+
6+
/**
7+
* Defines a task to be performed within a sub-{@link Flow} of a given {@link ServletRewrite}
8+
*
9+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
10+
*
11+
*/
12+
public abstract class SubflowTask
13+
{
14+
15+
/**
16+
* Perform the given {@link SubflowTask} using the given {@link ServletRewrite} event. If modified, the original
17+
* {@link Flow} will be restored after task completion.
18+
*
19+
* @param event the {@link ServletRewrite} event used in the given {@link SubflowTask}
20+
*
21+
* @return the resultant {@link Flow} from the {@link SubflowTask} execution.
22+
*/
23+
public static Flow perform(ServletRewrite<?, ?> rewrite, EvaluationContext context,
24+
SubflowTask subflowTask)
25+
{
26+
return perform(rewrite, context, rewrite.getFlow(), subflowTask);
27+
}
28+
29+
/**
30+
* Perform the given {@link SubflowTask} using the given {@link ServletRewrite} event initialized with a new
31+
* {@link Flow}. The original {@link Flow} will be restored after task completion.
32+
*
33+
* @param event the {@link ServletRewrite} event used in the given {@link SubflowTask}
34+
* @param subflow the {@link Flow} to be us as the initial state for the given {@link SubflowTask}
35+
*
36+
* @return the resultant {@link Flow} from the {@link SubflowTask} execution.
37+
*/
38+
public static Flow perform(ServletRewrite<?, ?> event, EvaluationContext context, Flow subflow,
39+
SubflowTask subflowTask)
40+
{
41+
Flow flow = event.getFlow();
42+
try
43+
{
44+
event.setFlow(subflow);
45+
subflowTask.performInSubflow(event, context);
46+
return event.getFlow();
47+
}
48+
finally
49+
{
50+
event.setFlow(flow);
51+
}
52+
}
53+
54+
/**
55+
* The task to be performed in a sub-{@link Flow}.
56+
*/
57+
public abstract void performInSubflow(ServletRewrite<?, ?> event, EvaluationContext context);
58+
59+
}

‎api-tests/src/test/java/org/ocpsoft/rewrite/bind/BindingsTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.ocpsoft.rewrite.event.Rewrite;
2424
import org.ocpsoft.rewrite.mock.MockBinding;
2525
import org.ocpsoft.rewrite.mock.MockEvaluationContext;
26-
import org.ocpsoft.rewrite.mock.MockRewrite;
26+
import org.ocpsoft.rewrite.test.MockRewrite;
2727

2828
/**
2929
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>

‎api-tests/src/test/java/org/ocpsoft/rewrite/config/AndTest.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.junit.Assert;
1919
import org.junit.Test;
20+
import org.ocpsoft.rewrite.test.MockRewrite;
2021

2122
/**
2223
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>

‎api-tests/src/test/java/org/ocpsoft/rewrite/config/ConfigurationBuilderTest.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.ocpsoft.rewrite.event.InboundRewrite;
2424
import org.ocpsoft.rewrite.event.Rewrite;
2525
import org.ocpsoft.rewrite.mock.MockEvaluationContext;
26+
import org.ocpsoft.rewrite.test.MockInboundRewrite;
2627

2728
/**
2829
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>

‎api-tests/src/test/java/org/ocpsoft/rewrite/config/DirectionTest.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import org.junit.Test;
2020

2121
import org.ocpsoft.rewrite.mock.MockEvaluationContext;
22+
import org.ocpsoft.rewrite.test.MockInboundRewrite;
23+
import org.ocpsoft.rewrite.test.MockOutboundRewrite;
24+
import org.ocpsoft.rewrite.test.MockRewrite;
2225

2326
/**
2427
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>

‎api-tests/src/test/java/org/ocpsoft/rewrite/config/NotTest.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.junit.Test;
2020

2121
import org.ocpsoft.rewrite.mock.MockEvaluationContext;
22+
import org.ocpsoft.rewrite.test.MockRewrite;
2223

2324
/**
2425
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>

‎api-tests/src/test/java/org/ocpsoft/rewrite/config/OrTest.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.junit.Test;
2020

2121
import org.ocpsoft.rewrite.mock.MockEvaluationContext;
22+
import org.ocpsoft.rewrite.test.MockRewrite;
2223

2324
/**
2425
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>

‎api-tests/src/test/java/org/ocpsoft/rewrite/param/ParameterBuilderTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import org.ocpsoft.rewrite.context.EvaluationContext;
99
import org.ocpsoft.rewrite.event.Rewrite;
1010
import org.ocpsoft.rewrite.mock.MockEvaluationContext;
11-
import org.ocpsoft.rewrite.mock.MockRewrite;
11+
import org.ocpsoft.rewrite.test.MockRewrite;
1212

1313
public class ParameterBuilderTest
1414
{
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package org.ocpsoft.rewrite.servlet.event;
2+
3+
import junit.framework.Assert;
4+
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
import org.ocpsoft.rewrite.context.EvaluationContext;
8+
import org.ocpsoft.rewrite.mock.MockEvaluationContext;
9+
import org.ocpsoft.rewrite.servlet.event.BaseRewrite.Flow;
10+
import org.ocpsoft.rewrite.test.MockServletRewrite;
11+
12+
public class SubflowTaskTest
13+
{
14+
private ServletRewrite<?, ?> rewrite;
15+
16+
@Before
17+
public void before()
18+
{
19+
rewrite = new MockServletRewrite(null, null);
20+
}
21+
22+
@Test
23+
public void testSubflowRestoresOriginalFlowState()
24+
{
25+
rewrite.setFlow(Flow.REDIRECT_PERMANENT);
26+
27+
Flow result = SubflowTask.perform(rewrite, new MockEvaluationContext(), new SubflowTask() {
28+
29+
@Override
30+
public void performInSubflow(ServletRewrite<?, ?> event, EvaluationContext context)
31+
{
32+
Assert.assertEquals(Flow.REDIRECT_PERMANENT, rewrite.getFlow());
33+
event.setFlow(Flow.HANDLED);
34+
}
35+
});
36+
37+
Assert.assertEquals(Flow.HANDLED, result);
38+
Assert.assertEquals(Flow.REDIRECT_PERMANENT, rewrite.getFlow());
39+
}
40+
41+
@Test
42+
public void testSubflowRestoresOriginalFlowStateWithInitializedFlow()
43+
{
44+
rewrite.setFlow(Flow.REDIRECT_PERMANENT);
45+
46+
Flow result = SubflowTask.perform(rewrite, new MockEvaluationContext(), Flow.INCLUDE, new SubflowTask() {
47+
48+
@Override
49+
public void performInSubflow(ServletRewrite<?, ?> event, EvaluationContext context)
50+
{
51+
Assert.assertEquals(Flow.INCLUDE, rewrite.getFlow());
52+
event.setFlow(Flow.HANDLED);
53+
}
54+
});
55+
56+
Assert.assertEquals(Flow.HANDLED, result);
57+
Assert.assertEquals(Flow.REDIRECT_PERMANENT, rewrite.getFlow());
58+
}
59+
60+
@Test
61+
public void testSubflowRestoresOriginalFlowStateAfterException()
62+
{
63+
rewrite.setFlow(Flow.REDIRECT_PERMANENT);
64+
65+
Flow result = null;
66+
try {
67+
result = SubflowTask.perform(rewrite, new MockEvaluationContext(), new SubflowTask() {
68+
69+
@Override
70+
public void performInSubflow(ServletRewrite<?, ?> event, EvaluationContext context)
71+
{
72+
Assert.assertEquals(Flow.REDIRECT_PERMANENT, rewrite.getFlow());
73+
event.setFlow(Flow.HANDLED);
74+
throw new RuntimeException();
75+
}
76+
77+
});
78+
Assert.fail();
79+
}
80+
catch (RuntimeException e) {}
81+
82+
Assert.assertNull(result);
83+
Assert.assertEquals(Flow.REDIRECT_PERMANENT, rewrite.getFlow());
84+
}
85+
86+
@Test
87+
public void testSubflowRestoresOriginalFlowStateAfterExceptionWithInitializedFlow()
88+
{
89+
rewrite.setFlow(Flow.REDIRECT_PERMANENT);
90+
91+
Flow result = null;
92+
try {
93+
result = SubflowTask.perform(rewrite, new MockEvaluationContext(), Flow.INCLUDE, new SubflowTask() {
94+
95+
@Override
96+
public void performInSubflow(ServletRewrite<?, ?> event, EvaluationContext context)
97+
{
98+
Assert.assertEquals(Flow.INCLUDE, rewrite.getFlow());
99+
event.setFlow(Flow.HANDLED);
100+
throw new RuntimeException();
101+
}
102+
103+
});
104+
Assert.fail();
105+
}
106+
catch (RuntimeException e) {}
107+
108+
Assert.assertNull(result);
109+
Assert.assertEquals(Flow.REDIRECT_PERMANENT, rewrite.getFlow());
110+
}
111+
112+
}

0 commit comments

Comments
 (0)