|
21 | 21 | import org.ocpsoft.rewrite.servlet.http.event.HttpInboundServletRewrite; |
22 | 22 | import org.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite; |
23 | 23 |
|
| 24 | +/** |
| 25 | + * HTTP operation that applies one or more {@link Transformer} implementations to the response. |
| 26 | + * |
| 27 | + * @author Christian Kaltepoth |
| 28 | + */ |
24 | 29 | public class Transform extends HttpOperation |
25 | 30 | { |
26 | 31 |
|
27 | 32 | private PipelineContentInterceptor pipeline = new PipelineContentInterceptor(); |
28 | 33 |
|
29 | | - public static Transform with(Class<? extends Transformer> transformerType) |
| 34 | + private Transform() |
30 | 35 | { |
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 |
45 | 37 | } |
46 | 38 |
|
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) |
49 | 44 | { |
50 | | - apply(first); |
51 | | - apply(second); |
52 | | - apply(third); |
53 | | - return this; |
| 45 | + return new Transform().and(transformer); |
54 | 46 | } |
55 | 47 |
|
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) |
57 | 52 | { |
58 | | - apply(first); |
59 | | - apply(second); |
60 | | - return this; |
| 53 | + return new Transform().and(transformer); |
61 | 54 | } |
62 | 55 |
|
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. |
65 | 60 | */ |
66 | | - public Transform apply(Class<? extends Transformer>... transformerTypes) |
| 61 | + public Transform and(Class<? extends Transformer> transformerType) |
67 | 62 | { |
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); |
70 | 72 | } |
71 | | - return this; |
72 | 73 | } |
73 | 74 |
|
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) |
75 | 80 | { |
76 | | - for (Transformer transformer : transformers) { |
77 | | - pipeline.add(transformer); |
78 | | - } |
| 81 | + pipeline.add(transformer); |
79 | 82 | return this; |
80 | 83 | } |
81 | 84 |
|
|
0 commit comments