Skip to content

Commit 1cd11c0

Browse files
committed
Improved the fluent API of Transform
1 parent adc0666 commit 1cd11c0

3 files changed

Lines changed: 39 additions & 49 deletions

File tree

‎transform/src/main/java/org/ocpsoft/rewrite/transform/Transform.java‎

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,61 +21,64 @@
2121
import org.ocpsoft.rewrite.servlet.http.event.HttpInboundServletRewrite;
2222
import org.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite;
2323

24+
/**
25+
* HTTP operation that applies one or more {@link Transformer} implementations to the response.
26+
*
27+
* @author Christian Kaltepoth
28+
*/
2429
public class Transform extends HttpOperation
2530
{
2631

2732
private PipelineContentInterceptor pipeline = new PipelineContentInterceptor();
2833

29-
public static Transform with(Class<? extends Transformer> transformerType)
34+
private Transform()
3035
{
31-
return new Transform().apply(transformerType);
32-
}
33-
34-
public Transform apply(Class<? extends Transformer> transformerType)
35-
{
36-
try {
37-
return apply(transformerType.newInstance());
38-
}
39-
catch (InstantiationException e) {
40-
throw new IllegalArgumentException(e);
41-
}
42-
catch (IllegalAccessException e) {
43-
throw new IllegalArgumentException(e);
44-
}
36+
// hide default constructor
4537
}
4638

47-
public Transform apply(Class<? extends Transformer> first, Class<? extends Transformer> second,
48-
Class<? extends Transformer> third)
39+
/**
40+
* Creates a transform instance that applies the given {@link Transformer}. Please note that the {@link Transformer}
41+
* implementation has to provide a public default constructor.
42+
*/
43+
public static Transform with(Class<? extends Transformer> transformer)
4944
{
50-
apply(first);
51-
apply(second);
52-
apply(third);
53-
return this;
45+
return new Transform().and(transformer);
5446
}
5547

56-
public Transform apply(Class<? extends Transformer> first, Class<? extends Transformer> second)
48+
/**
49+
* Creates a transform instance that applies the given {@link Transformer}.
50+
*/
51+
public static Transform with(Transformer transformer)
5752
{
58-
apply(first);
59-
apply(second);
60-
return this;
53+
return new Transform().and(transformer);
6154
}
6255

63-
/*
64-
* invoking this one will create compiler warnings :(
56+
/**
57+
* Adds another {@link Transformer} implementation to the transformer pipeline. The supplied transformer will be
58+
* executed AFTER all other transformer that have been added before. Please note that the {@link Transformer}
59+
* implementation has to provide a public default constructor.
6560
*/
66-
public Transform apply(Class<? extends Transformer>... transformerTypes)
61+
public Transform and(Class<? extends Transformer> transformerType)
6762
{
68-
for (Class<? extends Transformer> transformerType : transformerTypes) {
69-
apply(transformerType);
63+
try {
64+
return and(transformerType.newInstance());
65+
}
66+
catch (InstantiationException e) {
67+
throw new IllegalArgumentException(e);
68+
}
69+
catch (IllegalAccessException e) {
70+
throw new IllegalArgumentException("The default constructor of " + transformerType.getName()
71+
+ " is not accessible", e);
7072
}
71-
return this;
7273
}
7374

74-
public Transform apply(Transformer... transformers)
75+
/**
76+
* Adds another {@link Transformer} implementation to the transformer pipeline. The supplied transformer will be
77+
* executed AFTER all other transformer that have been added before.
78+
*/
79+
public Transform and(Transformer transformer)
7580
{
76-
for (Transformer transformer : transformers) {
77-
pipeline.add(transformer);
78-
}
81+
pipeline.add(transformer);
7982
return this;
8083
}
8184

‎transform/src/test/java/org/ocpsoft/rewrite/transform/TransformPipelineTest.java‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,6 @@ public static WebArchive getDeployment()
4848
return archive;
4949
}
5050

51-
@Test
52-
public void testPipelineWithoutTransformer() throws Exception
53-
{
54-
HttpAction<HttpGet> action = get("/test.none");
55-
assertEquals(200, action.getResponse().getStatusLine().getStatusCode());
56-
assertEquals("this is foo", action.getResponseContent());
57-
}
58-
5951
@Test
6052
public void testPipelineWithOneTransformer() throws Exception
6153
{

‎transform/src/test/java/org/ocpsoft/rewrite/transform/TransformPipelineTestProvider.java‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ public Configuration getConfiguration(final ServletContext context)
4545
return ConfigurationBuilder
4646
.begin()
4747

48-
// no transformer added
49-
.addRule()
50-
.when(Path.matches("/{basename}.none"))
51-
.perform(Forward.to("/{basename}.txt").and(new Transform()))
52-
5348
// one single transformer
5449
.addRule()
5550
.when(Path.matches("/{basename}.one"))
@@ -59,7 +54,7 @@ public Configuration getConfiguration(final ServletContext context)
5954
.addRule()
6055
.when(Path.matches("/{basename}.two"))
6156
.perform(Forward.to("/{basename}.txt").and(
62-
Transform.with(FooBarTransformer.class).apply(UppercaseTransformer.class)))
57+
Transform.with(FooBarTransformer.class).and(UppercaseTransformer.class)))
6358

6459
;
6560
}

0 commit comments

Comments
 (0)