01: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
02: * This code is licensed under the GPL 2.0 license, availible at the root
03: * application directory.
04: */
05: package org.vfny.geoserver.servlets;
06:
07: import org.geoserver.ows.DispatcherOutputStream;
08: import org.geoserver.ows.ServiceStrategy;
09: import java.io.ByteArrayOutputStream;
10: import java.io.IOException;
11: import java.io.OutputStream;
12: import javax.servlet.http.HttpServletResponse;
13:
14: /**
15: * A safe Service strategy that buffers output until writeTo completes.
16: *
17: * <p>
18: * This strategy wastes memory, for saftey. It represents a middle ground
19: * between SpeedStrategy and FileStrategy
20: * </p>
21: *
22: * @author jgarnett
23: */
24: public class BufferStrategy implements ServiceStrategy {
25: public String getId() {
26: return "BUFFER";
27: }
28:
29: /** DOCUMENT ME! */
30: ByteArrayOutputStream buffer = null;
31:
32: /**
33: * Provides a ByteArrayOutputStream for writeTo.
34: *
35: * @param response Response being processed.
36: *
37: * @return A ByteArrayOutputStream for writeTo opperation.
38: *
39: * @throws IOException DOCUMENT ME!
40: */
41: public DispatcherOutputStream getDestination(
42: HttpServletResponse response) throws IOException {
43: buffer = new ByteArrayOutputStream(1024 * 1024);
44:
45: return new DispatcherOutputStream(buffer);
46: }
47:
48: /**
49: * Copies Buffer to Response output output stream.
50: *
51: * @throws IOException If the response outputt stream is unavailable.
52: */
53: public void flush(HttpServletResponse response) throws IOException {
54: if ((buffer == null) || (response == null)) {
55: return; // should we throw an Exception here
56: }
57:
58: OutputStream out = response.getOutputStream();
59: buffer.writeTo(out);
60:
61: buffer = null;
62: }
63:
64: /**
65: * Clears the buffer with out writing anything out to response.
66: *
67: * @see org.geoserver.ows.ServiceStrategy#abort()
68: */
69: public void abort() {
70: buffer = null;
71: }
72:
73: public Object clone() throws CloneNotSupportedException {
74: return new BufferStrategy();
75: }
76: }
|