You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2010/06/21 13:25:47 UTC

svn commit: r956532 - /tomcat/trunk/java/org/apache/coyote/http11/InternalAprOutputBuffer.java

Author: markt
Date: Mon Jun 21 11:25:46 2010
New Revision: 956532

URL: http://svn.apache.org/viewvc?rev=956532&view=rev
Log:
Reduce code duplication in connectors: Use AbstractOutputBuffer with APR/native connector

Modified:
    tomcat/trunk/java/org/apache/coyote/http11/InternalAprOutputBuffer.java

Modified: tomcat/trunk/java/org/apache/coyote/http11/InternalAprOutputBuffer.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/coyote/http11/InternalAprOutputBuffer.java?rev=956532&r1=956531&r2=956532&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/coyote/http11/InternalAprOutputBuffer.java (original)
+++ tomcat/trunk/java/org/apache/coyote/http11/InternalAprOutputBuffer.java Mon Jun 21 11:25:46 2010
@@ -22,11 +22,7 @@ import java.nio.ByteBuffer;
 
 import org.apache.tomcat.jni.Socket;
 import org.apache.tomcat.util.buf.ByteChunk;
-import org.apache.tomcat.util.buf.CharChunk;
-import org.apache.tomcat.util.buf.MessageBytes;
 import org.apache.tomcat.util.http.HttpMessages;
-import org.apache.tomcat.util.http.MimeHeaders;
-import org.apache.tomcat.util.res.StringManager;
 
 import org.apache.coyote.ActionCode;
 import org.apache.coyote.OutputBuffer;
@@ -37,8 +33,7 @@ import org.apache.coyote.Response;
  * 
  * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
  */
-public class InternalAprOutputBuffer 
-    implements OutputBuffer {
+public class InternalAprOutputBuffer extends AbstractOutputBuffer {
 
 
     // -------------------------------------------------------------- Constants
@@ -85,87 +80,16 @@ public class InternalAprOutputBuffer 
     }
 
 
-    // -------------------------------------------------------------- Variables
-
-
-    /**
-     * The string manager for this package.
-     */
-    protected static final StringManager sm =
-        StringManager.getManager(Constants.Package);
-
-
     // ----------------------------------------------------- Instance Variables
 
 
     /**
-     * Associated Coyote response.
-     */
-    protected Response response;
-
-
-    /**
-     * Headers of the associated request.
-     */
-    protected MimeHeaders headers;
-
-
-    /**
-     * Committed flag.
-     */
-    protected boolean committed;
-
-
-    /**
-     * Finished flag.
-     */
-    protected boolean finished;
-
-
-    /**
-     * Pointer to the current write buffer.
-     */
-    protected byte[] buf;
-
-
-    /**
-     * Position in the buffer.
-     */
-    protected int pos;
-
-
-    /**
      * Underlying socket.
      */
     protected long socket;
 
 
     /**
-     * Underlying output buffer.
-     */
-    protected OutputBuffer outputStreamOutputBuffer;
-
-
-    /**
-     * Filter library.
-     * Note: Filter[0] is always the "chunked" filter.
-     */
-    protected OutputFilter[] filterLibrary;
-
-
-    /**
-     * Active filter (which is actually the top of the pipeline).
-     */
-    protected OutputFilter[] activeFilters;
-
-
-    /**
-     * Index of the last active filter.
-     */
-    protected int lastActiveFilter;
-
-
-    /**
      * Direct byte buffer used for writing.
      */
     protected ByteBuffer bbuf = null;
@@ -191,67 +115,6 @@ public class InternalAprOutputBuffer 
     }
 
 
-    /**
-     * Add an output filter to the filter library.
-     */
-    public void addFilter(OutputFilter filter) {
-
-        OutputFilter[] newFilterLibrary = 
-            new OutputFilter[filterLibrary.length + 1];
-        for (int i = 0; i < filterLibrary.length; i++) {
-            newFilterLibrary[i] = filterLibrary[i];
-        }
-        newFilterLibrary[filterLibrary.length] = filter;
-        filterLibrary = newFilterLibrary;
-
-        activeFilters = new OutputFilter[filterLibrary.length];
-
-    }
-
-
-    /**
-     * Get filters.
-     */
-    public OutputFilter[] getFilters() {
-
-        return filterLibrary;
-
-    }
-
-
-    /**
-     * Clear filters.
-     */
-    public void clearFilters() {
-
-        filterLibrary = new OutputFilter[0];
-        lastActiveFilter = -1;
-
-    }
-
-
-    /**
-     * Add an output filter to the filter library.
-     */
-    public void addActiveFilter(OutputFilter filter) {
-
-        if (lastActiveFilter == -1) {
-            filter.setBuffer(outputStreamOutputBuffer);
-        } else {
-            for (int i = 0; i <= lastActiveFilter; i++) {
-                if (activeFilters[i] == filter)
-                    return;
-            }
-            filter.setBuffer(activeFilters[lastActiveFilter]);
-        }
-
-        activeFilters[++lastActiveFilter] = filter;
-
-        filter.setResponse(response);
-
-    }
-
-
     // --------------------------------------------------------- Public Methods
 
 
@@ -260,37 +123,14 @@ public class InternalAprOutputBuffer 
      * 
      * @throws IOException an underlying I/O error occurred
      */
+    @Override
     public void flush()
         throws IOException {
 
-        if (!committed) {
-
-            // Send the connector a request for commit. The connector should
-            // then validate the headers, send them (using sendHeader) and 
-            // set the filters accordingly.
-            response.action(ActionCode.ACTION_COMMIT, null);
-
-        }
+        super.flush();
 
         // Flush the current buffer
         flushBuffer();
-
-    }
-
-
-    /**
-     * Reset current response.
-     * 
-     * @throws IllegalStateException if the response has already been committed
-     */
-    public void reset() {
-
-        if (committed)
-            throw new IllegalStateException(/*FIXME:Put an error message*/);
-
-        // Recycle Request object
-        response.recycle();
-
     }
 
 
@@ -298,43 +138,12 @@ public class InternalAprOutputBuffer 
      * Recycle the output buffer. This should be called when closing the 
      * connection.
      */
+    @Override
     public void recycle() {
 
-        // Recycle Request object
-        response.recycle();
+        super.recycle();
+        
         bbuf.clear();
-
-        socket = 0;
-        pos = 0;
-        lastActiveFilter = -1;
-        committed = false;
-        finished = false;
-
-    }
-
-
-    /**
-     * End processing of current HTTP request.
-     * Note: All bytes of the current request should have been already 
-     * consumed. This method only resets all the pointers so that we are ready
-     * to parse the next HTTP request.
-     */
-    public void nextRequest() {
-
-        // Recycle Request object
-        response.recycle();
-
-        // Recycle filters
-        for (int i = 0; i <= lastActiveFilter; i++) {
-            activeFilters[i].recycle();
-        }
-
-        // Reset pointers
-        pos = 0;
-        lastActiveFilter = -1;
-        committed = false;
-        finished = false;
-
     }
 
 
@@ -343,6 +152,7 @@ public class InternalAprOutputBuffer 
      * 
      * @throws IOException an underlying I/O error occurred
      */
+    @Override
     public void endRequest()
         throws IOException {
 
@@ -374,6 +184,7 @@ public class InternalAprOutputBuffer 
     /**
      * Send an acknowledgment.
      */
+    @Override
     public void sendAck()
         throws IOException {
 
@@ -385,147 +196,6 @@ public class InternalAprOutputBuffer 
     }
 
 
-    /**
-     * Send the response status line.
-     */
-    public void sendStatus() {
-
-        // Write protocol name
-        write(Constants.HTTP_11_BYTES);
-        buf[pos++] = Constants.SP;
-
-        // Write status code
-        int status = response.getStatus();
-        switch (status) {
-        case 200:
-            write(Constants._200_BYTES);
-            break;
-        case 400:
-            write(Constants._400_BYTES);
-            break;
-        case 404:
-            write(Constants._404_BYTES);
-            break;
-        default:
-            write(status);
-        }
-
-        buf[pos++] = Constants.SP;
-
-        // Write message
-        String message = null;
-        if (org.apache.coyote.Constants.USE_CUSTOM_STATUS_MSG_IN_HEADER &&
-                HttpMessages.isSafeInHttpHeader(response.getMessage())) {
-            message = response.getMessage();
-        }
-        if (message == null) {
-            write(HttpMessages.getMessage(status));
-        } else {
-            write(message);
-        }
-
-        // End the response status line
-        buf[pos++] = Constants.CR;
-        buf[pos++] = Constants.LF;
-
-    }
-
-
-    /**
-     * Send a header.
-     * 
-     * @param name Header name
-     * @param value Header value
-     */
-    public void sendHeader(MessageBytes name, MessageBytes value) {
-
-        write(name);
-        buf[pos++] = Constants.COLON;
-        buf[pos++] = Constants.SP;
-        write(value);
-        buf[pos++] = Constants.CR;
-        buf[pos++] = Constants.LF;
-
-    }
-
-
-    /**
-     * Send a header.
-     * 
-     * @param name Header name
-     * @param value Header value
-     */
-    public void sendHeader(ByteChunk name, ByteChunk value) {
-
-        write(name);
-        buf[pos++] = Constants.COLON;
-        buf[pos++] = Constants.SP;
-        write(value);
-        buf[pos++] = Constants.CR;
-        buf[pos++] = Constants.LF;
-
-    }
-
-
-    /**
-     * Send a header.
-     * 
-     * @param name Header name
-     * @param value Header value
-     */
-    public void sendHeader(String name, String value) {
-
-        write(name);
-        buf[pos++] = Constants.COLON;
-        buf[pos++] = Constants.SP;
-        write(value);
-        buf[pos++] = Constants.CR;
-        buf[pos++] = Constants.LF;
-
-    }
-
-
-    /**
-     * End the header block.
-     */
-    public void endHeaders() {
-
-        buf[pos++] = Constants.CR;
-        buf[pos++] = Constants.LF;
-
-    }
-
-
-    // --------------------------------------------------- OutputBuffer Methods
-
-
-    /**
-     * Write the contents of a byte chunk.
-     * 
-     * @param chunk byte chunk
-     * @return number of bytes written
-     * @throws IOException an underlying I/O error occurred
-     */
-    public int doWrite(ByteChunk chunk, Response res) 
-        throws IOException {
-
-        if (!committed) {
-
-            // Send the connector a request for commit. The connector should
-            // then validate the headers, send them (using sendHeaders) and 
-            // set the filters accordingly.
-            response.action(ActionCode.ACTION_COMMIT, null);
-
-        }
-
-        if (lastActiveFilter == -1)
-            return outputStreamOutputBuffer.doWrite(chunk, res);
-        else
-            return activeFilters[lastActiveFilter].doWrite(chunk, res);
-
-    }
-
-
     // ------------------------------------------------------ Protected Methods
 
 
@@ -534,8 +204,8 @@ public class InternalAprOutputBuffer 
      * 
      * @throws IOException an underlying I/O error occurred
      */
-    protected void commit()
-        throws IOException {
+    @Override
+    protected void commit() throws IOException {
 
         // The response is now committed
         committed = true;
@@ -549,134 +219,6 @@ public class InternalAprOutputBuffer 
     }
 
 
-    /**
-     * This method will write the contents of the specified message bytes 
-     * buffer to the output stream, without filtering. This method is meant to
-     * be used to write the response header.
-     * 
-     * @param mb data to be written
-     */
-    protected void write(MessageBytes mb) {
-
-        if (mb.getType() == MessageBytes.T_BYTES) {
-            ByteChunk bc = mb.getByteChunk();
-            write(bc);
-        } else if (mb.getType() == MessageBytes.T_CHARS) {
-            CharChunk cc = mb.getCharChunk();
-            write(cc);
-        } else {
-            write(mb.toString());
-        }
-
-    }
-
-
-    /**
-     * This method will write the contents of the specified message bytes 
-     * buffer to the output stream, without filtering. This method is meant to
-     * be used to write the response header.
-     * 
-     * @param bc data to be written
-     */
-    protected void write(ByteChunk bc) {
-
-        // Writing the byte chunk to the output buffer
-        int length = bc.getLength();
-        System.arraycopy(bc.getBytes(), bc.getStart(), buf, pos, length);
-        pos = pos + length;
-
-    }
-
-
-    /**
-     * This method will write the contents of the specified char 
-     * buffer to the output stream, without filtering. This method is meant to
-     * be used to write the response header.
-     * 
-     * @param cc data to be written
-     */
-    protected void write(CharChunk cc) {
-
-        int start = cc.getStart();
-        int end = cc.getEnd();
-        char[] cbuf = cc.getBuffer();
-        for (int i = start; i < end; i++) {
-            char c = cbuf[i];
-            // Note:  This is clearly incorrect for many strings,
-            // but is the only consistent approach within the current
-            // servlet framework.  It must suffice until servlet output
-            // streams properly encode their output.
-            if ((c <= 31) && (c != 9)) {
-                c = ' ';
-            } else if (c == 127) {
-                c = ' ';
-            }
-            buf[pos++] = (byte) c;
-        }
-
-    }
-
-
-    /**
-     * This method will write the contents of the specified byte 
-     * buffer to the output stream, without filtering. This method is meant to
-     * be used to write the response header.
-     * 
-     * @param b data to be written
-     */
-    public void write(byte[] b) {
-
-        // Writing the byte chunk to the output buffer
-        System.arraycopy(b, 0, buf, pos, b.length);
-        pos = pos + b.length;
-
-    }
-
-
-    /**
-     * This method will write the contents of the specified String to the 
-     * output stream, without filtering. This method is meant to be used to 
-     * write the response header.
-     * 
-     * @param s data to be written
-     */
-    protected void write(String s) {
-
-        if (s == null)
-            return;
-
-        // From the Tomcat 3.3 HTTP/1.0 connector
-        int len = s.length();
-        for (int i = 0; i < len; i++) {
-            char c = s.charAt (i);
-            // Note:  This is clearly incorrect for many strings,
-            // but is the only consistent approach within the current
-            // servlet framework.  It must suffice until servlet output
-            // streams properly encode their output.
-            if ((c <= 31) && (c != 9)) {
-                c = ' ';
-            } else if (c == 127) {
-                c = ' ';
-            }
-            buf[pos++] = (byte) c;
-        }
-
-    }
-
-
-    /**
-     * This method will print the specified integer to the output stream, 
-     * without filtering. This method is meant to be used to write the 
-     * response header.
-     * 
-     * @param i data to be written
-     */
-    protected void write(int i) {
-
-        write(String.valueOf(i));
-
-    }
-
 
     /**
      * Callback to write data from the buffer.



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org