001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.vfny.geoserver.servlets;
006:
007: import org.geoserver.ows.DispatcherOutputStream;
008: import org.geoserver.ows.ServiceStrategy;
009: import org.vfny.geoserver.util.PartialBufferedOutputStream2;
010: import java.io.IOException;
011: import java.io.OutputStream;
012: import java.util.logging.Logger;
013: import javax.servlet.http.HttpServletResponse;
014:
015: /**
016: * <b>PartialBufferStrategy</b><br> Oct 19, 2005<br>
017: * <b>Purpose:</b><br>
018: * This strategy will buffer the response before it starts streaming it to the
019: * user. This will allow for errors to be caught early so a proper error
020: * message can be sent to the user. Right now it buffers the first 20KB,
021: * enough for a full getCapabilities document.
022: *
023: * @author Brent Owens (The Open Planning Project)
024: * @version
025: */
026: public class PartialBufferStrategy2 implements ServiceStrategy {
027: /** Class logger */
028: protected static Logger LOGGER = org.geotools.util.logging.Logging
029: .getLogger("org.vfny.geoserver.servlets");
030: public static final int DEFAULT_BUFFER_SIZE = 50;
031: private PartialBufferedOutputStream2 out = null;
032: private int bufferSize;
033:
034: public String getId() {
035: return "PARTIAL-BUFFER2";
036: }
037:
038: public void setBufferSize(int bufferSize) {
039: this .bufferSize = bufferSize;
040: }
041:
042: private int bufferedSize() {
043: if (bufferSize > 0) {
044: return bufferSize;
045: }
046:
047: return DEFAULT_BUFFER_SIZE;
048: }
049:
050: /*
051: * (non-Javadoc)
052: *
053: * @see org.vfny.geoserver.servlets.AbstractService.ServiceStrategy#getDestination(javax.servlet.http.HttpServletResponse)
054: */
055: public DispatcherOutputStream getDestination(
056: HttpServletResponse response) throws IOException {
057: out = new PartialBufferedOutputStream2(response, bufferedSize());
058:
059: return new DispatcherOutputStream(out);
060: }
061:
062: /*
063: * (non-Javadoc)
064: *
065: * @see org.vfny.geoserver.servlets.AbstractService.ServiceStrategy#flush()
066: */
067: public void flush(HttpServletResponse response) throws IOException {
068: if (out != null) {
069: out.forceFlush();
070: out = null;
071: }
072: }
073:
074: /*
075: * (non-Javadoc)
076: *
077: * @see org.vfny.geoserver.servlets.AbstractService.ServiceStrategy#abort()
078: */
079: public void abort() {
080: if (out != null) {
081: try {
082: if (out.abort()) {
083: LOGGER
084: .info("OutputStream was successfully aborted.");
085: } else {
086: LOGGER
087: .warning("OutputStream could not be aborted in time. An error has occurred and could not be sent to the user.");
088: }
089: } catch (IOException e) {
090: LOGGER.warning("Error aborting OutputStream");
091: e.printStackTrace();
092: }
093: }
094: }
095:
096: public Object clone() throws CloneNotSupportedException {
097: PartialBufferStrategy2 clone = new PartialBufferStrategy2();
098: clone.bufferSize = bufferSize;
099:
100: return clone;
101: }
102: }
|