01: // Copyright 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.internal.services;
16:
17: import java.io.BufferedInputStream;
18: import java.io.IOException;
19: import java.io.InputStream;
20: import java.io.OutputStream;
21:
22: import org.apache.tapestry.StreamResponse;
23: import org.apache.tapestry.internal.TapestryInternalUtils;
24: import org.apache.tapestry.runtime.Component;
25: import org.apache.tapestry.services.ActionResponseGenerator;
26: import org.apache.tapestry.services.ComponentEventResultProcessor;
27: import org.apache.tapestry.services.Response;
28:
29: public class StreamResponseResultProcessor implements
30: ComponentEventResultProcessor<StreamResponse> {
31: private static final int BUFFER_SIZE = 5000;
32:
33: public ActionResponseGenerator processComponentEvent(
34: final StreamResponse streamResponse, Component component,
35: final String methodDescripion) {
36: return new ActionResponseGenerator() {
37: public void sendClientResponse(Response response)
38: throws IOException {
39: OutputStream os = null;
40: InputStream is = null;
41:
42: streamResponse.prepareResponse(response);
43: try {
44: is = new BufferedInputStream(streamResponse
45: .getStream());
46:
47: os = response.getOutputStream(streamResponse
48: .getContentType());
49:
50: byte[] buffer = new byte[BUFFER_SIZE];
51:
52: while (true) {
53: int length = is.read(buffer, 0, buffer.length);
54: if (length < 0)
55: break;
56:
57: os.write(buffer, 0, length);
58: }
59:
60: os.close();
61: os = null;
62:
63: is.close();
64: is = null;
65: } finally {
66: TapestryInternalUtils.close(is);
67: TapestryInternalUtils.close(os);
68: }
69: }
70:
71: };
72: }
73:
74: }
|