Skip to content

Commit 2425de3

Browse files
committed
New ResponseBuffer API
1 parent a743cd4 commit 2425de3

14 files changed

Lines changed: 343 additions & 74 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2012 <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.servlet.config.response;
17+
18+
import java.nio.charset.Charset;
19+
20+
import javax.servlet.http.HttpServletResponse;
21+
22+
/**
23+
* Holds contents of the fully completed {@link HttpServletResponse}.
24+
*
25+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
26+
*/
27+
public interface ResponseBuffer
28+
{
29+
/**
30+
* Get the contents of this buffer.
31+
*/
32+
public byte[] getContents();
33+
34+
/**
35+
* Set the contents of this buffer.
36+
*/
37+
public void setContents(byte[] contents);
38+
39+
/**
40+
* Get the {@link Charset} with which response output is encoded.
41+
*/
42+
public Charset getCharset();
43+
44+
/**
45+
* Set the {@link Charset} with which response output will be encoded.
46+
*/
47+
public void setCharset(Charset charset);
48+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2012 <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.servlet.config.response;
17+
18+
import javax.servlet.ServletResponse;
19+
20+
import org.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite;
21+
22+
/**
23+
* An intercepter that operates on the fully buffered {@link ServletResponse#getOutputStream()} before flushing to the
24+
* client, once the control of the application has been returned to Rewrite.
25+
*
26+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
27+
*/
28+
public interface ResponseInterceptor
29+
{
30+
/**
31+
* Perform modifications on the fully buffered {@link ServletResponse#getOutputStream()} contents.
32+
*/
33+
void intercept(HttpServletRewrite event, ResponseBuffer buffer, ResponseInterceptorChain chain);
34+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2012 <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.servlet.config.response;
17+
18+
import org.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite;
19+
20+
/**
21+
* Chain responsible for controlling interception of {@link ResponseInterceptor} instances.
22+
*
23+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
24+
*/
25+
public interface ResponseInterceptorChain
26+
{
27+
/**
28+
* Invoke the next {@link ResponseInterceptor} in the chain. When no more registered {@link ResponseInterceptor}
29+
* instances exist, this method simply returns control back to the calling {@link ResponseInterceptor}.
30+
*/
31+
void next(HttpServletRewrite event, ResponseBuffer buffer, ResponseInterceptorChain chain);
32+
}

‎impl-servlet/src/main/java/org/ocpsoft/rewrite/servlet/config/OutputBuffer.java‎

Lines changed: 0 additions & 20 deletions
This file was deleted.

‎impl-servlet/src/main/java/org/ocpsoft/rewrite/servlet/config/Response.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import javax.servlet.http.HttpServletResponse;
2222

2323
import org.ocpsoft.rewrite.context.EvaluationContext;
24+
import org.ocpsoft.rewrite.servlet.config.response.ResponseInterceptor;
2425
import org.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite;
2526
import org.ocpsoft.rewrite.servlet.impl.HttpRewriteWrappedResponse;
2627

@@ -32,21 +33,21 @@
3233
public abstract class Response extends HttpOperation
3334
{
3435
/**
35-
* Add the given {@link OutputBuffer} instances to the current {@link ServletResponse}. This will activate response
36+
* Add the given {@link ResponseInterceptor} instances to the current {@link ServletResponse}. This will activate response
3637
* buffering on the current {@link ServletRequest} - meaning that generated output will not be sent to the client
37-
* until the entire request has completed and all registered {@link OutputBuffer} instances have been executed.
38+
* until the entire request has completed and all registered {@link ResponseInterceptor} instances have been executed.
3839
*
3940
* @param bufferedResponseToLowercase2
4041
*
4142
* @throws IllegalStateException When output has already been written to the client.
4243
*/
43-
public static Response withOutputBufferedBy(final OutputBuffer... buffers) throws IllegalStateException
44+
public static Response withOutputInterceptedBy(final ResponseInterceptor... buffers) throws IllegalStateException
4445
{
4546
return new Response() {
4647
@Override
4748
public void performHttp(HttpServletRewrite event, EvaluationContext context)
4849
{
49-
for (OutputBuffer buffer : buffers) {
50+
for (ResponseInterceptor buffer : buffers) {
5051
HttpRewriteWrappedResponse.getInstance(event.getRequest()).addBufferStage(buffer);
5152
}
5253
}

‎impl-servlet/src/main/java/org/ocpsoft/rewrite/servlet/config/rule/Join.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected Join(final String pattern)
8181
}
8282

8383
/**
84-
* The outward facing URL path to which this {@link Join} will apply.
84+
* The client-facing URL path to which this {@link Join} will apply.
8585
*/
8686
public static IJoin path(final String pattern)
8787
{
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.ocpsoft.rewrite.servlet.impl;
2+
3+
import javax.servlet.http.HttpServletRequest;
4+
import javax.servlet.http.HttpServletResponse;
5+
6+
import org.ocpsoft.rewrite.servlet.event.BaseRewrite;
7+
import org.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite;
8+
9+
public class HttpBufferRewriteImpl extends BaseRewrite<HttpServletRequest, HttpServletResponse> implements HttpServletRewrite
10+
{
11+
private HttpInboundRewriteImpl delegate;
12+
13+
public HttpBufferRewriteImpl(HttpServletRequest request, HttpServletResponse response)
14+
{
15+
super(request, response);
16+
this.delegate = new HttpInboundRewriteImpl(request, response);
17+
}
18+
19+
@Override
20+
public String getContextPath()
21+
{
22+
return delegate.getContextPath();
23+
}
24+
25+
@Override
26+
public String getRequestPath()
27+
{
28+
return delegate.getRequestPath();
29+
}
30+
31+
@Override
32+
public String getRequestQueryString()
33+
{
34+
return delegate.getRequestQueryString();
35+
}
36+
37+
@Override
38+
public String getRequestQueryStringSeparator()
39+
{
40+
return delegate.getRequestQueryStringSeparator();
41+
}
42+
43+
@Override
44+
public String getURL()
45+
{
46+
return delegate.getURL();
47+
}
48+
}

‎impl-servlet/src/main/java/org/ocpsoft/rewrite/servlet/impl/HttpRewriteWrappedResponse.java‎

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2+
* Copyright 2012 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,13 +13,11 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package org.ocpsoft.rewrite.servlet.impl;
1817

1918
import java.io.ByteArrayInputStream;
2019
import java.io.ByteArrayOutputStream;
2120
import java.io.IOException;
22-
import java.io.InputStream;
2321
import java.io.OutputStreamWriter;
2422
import java.io.PrintWriter;
2523
import java.io.UnsupportedEncodingException;
@@ -41,7 +39,8 @@
4139
import org.ocpsoft.rewrite.event.Rewrite;
4240
import org.ocpsoft.rewrite.exception.RewriteException;
4341
import org.ocpsoft.rewrite.servlet.RewriteLifecycleContext;
44-
import org.ocpsoft.rewrite.servlet.config.OutputBuffer;
42+
import org.ocpsoft.rewrite.servlet.config.response.ResponseBuffer;
43+
import org.ocpsoft.rewrite.servlet.config.response.ResponseInterceptor;
4544
import org.ocpsoft.rewrite.servlet.event.BaseRewrite.Flow;
4645
import org.ocpsoft.rewrite.servlet.http.event.HttpOutboundServletRewrite;
4746
import org.ocpsoft.rewrite.servlet.spi.RewriteLifecycleListener;
@@ -73,25 +72,25 @@ public HttpRewriteWrappedResponse(final HttpServletRequest request, final HttpSe
7372
/*
7473
* Buffering Facilities
7574
*/
76-
private ByteArrayOutputStream stream = new ByteArrayOutputStream();
77-
private PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(stream,
75+
private ByteArrayOutputStream responseOutputBuffer = new ByteArrayOutputStream();
76+
private PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(responseOutputBuffer,
7877
Charset.forName(getCharacterEncoding())), true);
79-
private List<OutputBuffer> bufferedStages = new ArrayList<OutputBuffer>();
78+
private List<ResponseInterceptor> responseInterceptors = new ArrayList<ResponseInterceptor>();
8079
private boolean buffersLocked = false;
8180

8281
public boolean isBufferingActive()
8382
{
84-
return !bufferedStages.isEmpty();
83+
return !responseInterceptors.isEmpty();
8584
}
8685

87-
public void addBufferStage(OutputBuffer stage) throws IllegalStateException
86+
public void addBufferStage(ResponseInterceptor stage) throws IllegalStateException
8887
{
8988
if (areBuffersLocked())
9089
{
9190
throw new IllegalStateException(
9291
"Cannot add output buffers to Response once request processing has been passed to the application.");
9392
}
94-
this.bufferedStages.add(stage);
93+
this.responseInterceptors.add(stage);
9594
}
9695

9796
private boolean areBuffersLocked()
@@ -109,16 +108,18 @@ public void flushBufferedStreams()
109108
if (isBufferingActive())
110109
{
111110
try {
112-
InputStream result = new ByteArrayInputStream(stream.toByteArray());
113-
for (OutputBuffer stage : bufferedStages) {
114-
result = stage.execute(result);
115-
}
116-
Streams.copy(result, super.getOutputStream());
111+
ResponseBuffer buffer = new ResponseBufferImpl(responseOutputBuffer.toByteArray(), Charset.forName(getCharacterEncoding()));
112+
new ResponseInterceptorChainImpl(responseInterceptors).begin(new HttpBufferRewriteImpl(request, this), buffer);
113+
114+
if (!Charset.forName(getCharacterEncoding()).equals(buffer.getCharset()))
115+
setCharacterEncoding(buffer.getCharset().name());
116+
117+
Streams.copy(new ByteArrayInputStream(buffer.getContents()), super.getOutputStream());
117118
if (printWriter != null) {
118119
printWriter.close();
119120
}
120-
if (stream != null) {
121-
stream.close();
121+
if (responseOutputBuffer != null) {
122+
responseOutputBuffer.close();
122123
}
123124
}
124125
catch (IOException e) {
@@ -134,7 +135,7 @@ public String toString()
134135
if (isBufferingActive())
135136
{
136137
try {
137-
return stream.toString(getCharacterEncoding());
138+
return responseOutputBuffer.toString(getCharacterEncoding());
138139
}
139140
catch (UnsupportedEncodingException e) {
140141
throw new RewriteException("Response accepted invalid character encoding " + getCharacterEncoding(), e);
@@ -163,7 +164,7 @@ public PrintWriter getWriter()
163164
public ServletOutputStream getOutputStream()
164165
{
165166
if (isBufferingActive())
166-
return new ByteArrayServletOutputStream(stream);
167+
return new ByteArrayServletOutputStream(responseOutputBuffer);
167168
else
168169
try {
169170
lockBuffers();
@@ -189,7 +190,7 @@ public void setContentLength(int contentLength)
189190
public void flushBuffer() throws IOException
190191
{
191192
if (isBufferingActive())
192-
stream.flush();
193+
responseOutputBuffer.flush();
193194
else
194195
{
195196
lockBuffers();
@@ -450,14 +451,14 @@ public boolean isCommitted()
450451
@Override
451452
public void reset()
452453
{
453-
stream.reset();
454+
responseOutputBuffer.reset();
454455
super.reset();
455456
}
456457

457458
@Override
458459
public void resetBuffer()
459460
{
460-
stream.reset();
461+
responseOutputBuffer.reset();
461462
super.resetBuffer();
462463
}
463464

0 commit comments

Comments
 (0)