Skip to content

Commit 9317d34

Browse files
committed
Support for multiple transformers
1 parent dc2613d commit 9317d34

6 files changed

Lines changed: 322 additions & 11 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.transform;
17+
18+
import java.io.ByteArrayInputStream;
19+
import java.io.ByteArrayOutputStream;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.io.OutputStream;
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
26+
import org.apache.commons.io.IOUtils;
27+
28+
class Pipeline implements Transformer
29+
{
30+
31+
private List<Transformer> pipeline = new ArrayList<Transformer>();
32+
33+
public void add(Transformer transformer)
34+
{
35+
pipeline.add(transformer);
36+
}
37+
38+
@Override
39+
public void transform(InputStream input, OutputStream output) throws IOException
40+
{
41+
42+
// just do a copy if no transformers have been added
43+
if (pipeline.isEmpty()) {
44+
IOUtils.copy(input, output);
45+
}
46+
47+
// delegate if there is only one transformer
48+
else if (pipeline.size() == 1) {
49+
pipeline.get(0).transform(input, output);
50+
}
51+
52+
// multiple transformers
53+
else {
54+
55+
// prepar for the first transformation step
56+
InputStream in = input;
57+
OutputStream out = new ByteArrayOutputStream();
58+
59+
for (int i = 0; i < pipeline.size(); i++) {
60+
61+
// run transformation
62+
pipeline.get(i).transform(in, out);
63+
64+
// if it isn't the last transformation step -> prepare next one
65+
if (i < pipeline.size() - 1) {
66+
67+
// prepare input for next iteration
68+
ByteArrayOutputStream lastOutput = (ByteArrayOutputStream) out;
69+
in = new ByteArrayInputStream(lastOutput.toByteArray());
70+
71+
// if the next transform step is the last one, write directly to the output
72+
if (i == pipeline.size() - 2) {
73+
out = output;
74+
}
75+
76+
// other steps write into the last byte array again
77+
else {
78+
lastOutput.reset();
79+
}
80+
81+
}
82+
83+
}
84+
85+
}
86+
87+
}
88+
89+
}

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

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ public class Transform implements Rule
3737

3838
private final Condition condition;
3939

40-
private ResourceResolver resourceResolver = WebResourceResolver.identity();
41-
42-
private Transformer transformer;
40+
private ResourceResolver resolver = WebResourceResolver.identity();
41+
42+
private Pipeline pipeline = new Pipeline();
4343

4444
public static Transform request(Condition condition)
4545
{
@@ -48,7 +48,7 @@ public static Transform request(Condition condition)
4848

4949
public static Transform request(String fileType)
5050
{
51-
return request(Path.matches("{something}"+fileType).where("something").matches(".*"));
51+
return request(Path.matches("{something}" + fileType).where("something").matches(".*"));
5252
}
5353

5454
public Transform(Condition condition)
@@ -69,14 +69,44 @@ public Transform apply(Class<? extends Transformer> transformerType)
6969
}
7070
}
7171

72-
public Transform apply(Transformer transformer)
72+
public Transform apply(Class<? extends Transformer> first, Class<? extends Transformer> second,
73+
Class<? extends Transformer> third)
74+
{
75+
apply(first);
76+
apply(second);
77+
apply(third);
78+
return this;
79+
}
80+
81+
public Transform apply(Class<? extends Transformer> first, Class<? extends Transformer> second)
7382
{
74-
this.transformer = transformer;
83+
apply(first);
84+
apply(second);
7585
return this;
7686
}
77-
78-
public Transform resolvedBy(ResourceResolver resourceResolver) {
79-
this.resourceResolver = resourceResolver;
87+
88+
/*
89+
* invoking this one will create compiler warnings :(
90+
*/
91+
public Transform apply(Class<? extends Transformer>... transformerTypes)
92+
{
93+
for (Class<? extends Transformer> transformerType : transformerTypes) {
94+
apply(transformerType);
95+
}
96+
return this;
97+
}
98+
99+
public Transform apply(Transformer... transformers)
100+
{
101+
for (Transformer transformer : transformers) {
102+
pipeline.add(transformer);
103+
}
104+
return this;
105+
}
106+
107+
public Transform resolvedBy(ResourceResolver resourceResolver)
108+
{
109+
this.resolver = resourceResolver;
80110
return this;
81111
}
82112

@@ -109,7 +139,7 @@ public void perform(Rewrite event, EvaluationContext context)
109139
HttpInboundServletRewrite inboundRewrite = (HttpInboundServletRewrite) event;
110140

111141
// try to load the underlying resource
112-
InputStream inputStream = resourceResolver.getResource(event, context);
142+
InputStream inputStream = resolver.getResource(event, context);
113143

114144
// proceed only if requested resource has been found
115145
if (inputStream != null) {
@@ -119,7 +149,7 @@ public void perform(Rewrite event, EvaluationContext context)
119149

120150
// run the rendering process
121151
HttpServletResponse response = inboundRewrite.getResponse();
122-
transformer.transform(inputStream, response.getOutputStream());
152+
pipeline.transform(inputStream, response.getOutputStream());
123153
response.flushBuffer();
124154

125155
// the application doesn't need to process the request anymore
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.transform;
17+
18+
public class FooBarTransformer extends StringTransformer
19+
{
20+
21+
@Override
22+
public String transform(String input)
23+
{
24+
return input.replace("foo", "bar");
25+
}
26+
27+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.transform;
17+
18+
import static org.junit.Assert.assertEquals;
19+
20+
import org.apache.http.client.methods.HttpGet;
21+
import org.jboss.arquillian.container.test.api.Deployment;
22+
import org.jboss.arquillian.junit.Arquillian;
23+
import org.jboss.shrinkwrap.api.asset.StringAsset;
24+
import org.jboss.shrinkwrap.api.spec.WebArchive;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.ocpsoft.rewrite.config.ConfigurationProvider;
28+
import org.ocpsoft.rewrite.test.HttpAction;
29+
import org.ocpsoft.rewrite.test.RewriteTest;
30+
import org.ocpsoft.rewrite.transform.less.Less;
31+
32+
/**
33+
*
34+
* Integration test for {@link Less}.
35+
*
36+
* @author Christian Kaltepoth
37+
*
38+
*/
39+
@RunWith(Arquillian.class)
40+
public class TransformPipelineTest extends RewriteTest
41+
{
42+
@Deployment(testable = false)
43+
public static WebArchive getDeployment()
44+
{
45+
return RewriteTest.getDeployment()
46+
.addAsWebResource(new StringAsset("this is foo"), "test.txt")
47+
.addAsServiceProvider(ConfigurationProvider.class, TransformPipelineTestProvider.class);
48+
}
49+
50+
@Test
51+
public void testPipelineWithoutTransformer() throws Exception
52+
{
53+
HttpAction<HttpGet> action = get("/test.none");
54+
assertEquals(200, action.getResponse().getStatusLine().getStatusCode());
55+
assertEquals("this is foo", action.getResponseContent());
56+
}
57+
58+
@Test
59+
public void testPipelineWithOneTransformer() throws Exception
60+
{
61+
HttpAction<HttpGet> action = get("/test.one");
62+
assertEquals(200, action.getResponse().getStatusLine().getStatusCode());
63+
assertEquals("this is bar", action.getResponseContent());
64+
}
65+
66+
@Test
67+
public void testPipelineWithTwoTransformers() throws Exception
68+
{
69+
HttpAction<HttpGet> action = get("/test.two");
70+
assertEquals(200, action.getResponse().getStatusLine().getStatusCode());
71+
assertEquals("THIS IS BAR", action.getResponseContent());
72+
}
73+
74+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.transform;
17+
18+
import javax.servlet.ServletContext;
19+
20+
import org.ocpsoft.rewrite.config.Configuration;
21+
import org.ocpsoft.rewrite.config.ConfigurationBuilder;
22+
import org.ocpsoft.rewrite.config.ConfigurationProvider;
23+
import org.ocpsoft.rewrite.servlet.config.HttpConfigurationProvider;
24+
import org.ocpsoft.rewrite.transform.resolve.WebResourceResolver;
25+
26+
/**
27+
*
28+
* {@link ConfigurationProvider} for {@link TransformPipelineTest}.
29+
*
30+
* @author Christian Kaltepoth
31+
*
32+
*/
33+
public class TransformPipelineTestProvider extends HttpConfigurationProvider
34+
{
35+
@Override
36+
public int priority()
37+
{
38+
return 0;
39+
}
40+
41+
@Override
42+
public Configuration getConfiguration(final ServletContext context)
43+
{
44+
return ConfigurationBuilder
45+
.begin()
46+
47+
// no transformer added
48+
.addRule(Transform.request(".none")
49+
.resolvedBy(WebResourceResolver.fileType(".txt")))
50+
51+
// one single transformer
52+
.addRule(Transform.request(".one")
53+
.resolvedBy(WebResourceResolver.fileType(".txt"))
54+
.apply(FooBarTransformer.class))
55+
56+
// multiple transformers
57+
.addRule(Transform.request(".two")
58+
.resolvedBy(WebResourceResolver.fileType(".txt"))
59+
.apply(FooBarTransformer.class, UppercaseTransformer.class))
60+
61+
;
62+
}
63+
64+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.transform;
17+
18+
public class UppercaseTransformer extends StringTransformer
19+
{
20+
21+
@Override
22+
public String transform(String input)
23+
{
24+
return input.toUpperCase();
25+
}
26+
27+
}

0 commit comments

Comments
 (0)