Reduce code duplication in connectors: Move addFilter() and addInputFilter() to base...
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 21 Jun 2010 11:27:18 +0000 (11:27 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 21 Jun 2010 11:27:18 +0000 (11:27 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@956533 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/coyote/http11/AbstractHttp11Processor.java
java/org/apache/coyote/http11/Http11AprProcessor.java
java/org/apache/coyote/http11/Http11NioProcessor.java
java/org/apache/coyote/http11/Http11Processor.java

index ced5b18..0dcd435 100644 (file)
@@ -30,7 +30,7 @@ import org.apache.tomcat.util.buf.ByteChunk;
 import org.apache.tomcat.util.buf.MessageBytes;
 import org.apache.tomcat.util.res.StringManager;
 
-public class AbstractHttp11Processor {
+public abstract class AbstractHttp11Processor {
 
     /**
      * Logger.
@@ -720,4 +720,69 @@ public class AbstractHttp11Processor {
     }
 
 
+    /**
+     * Exposes input buffer to super class to allow better code re-use.
+     * @return  The input buffer used by the processor. 
+     */
+    protected abstract AbstractInputBuffer getInputBuffer();
+
+    /**
+     * Exposes output buffer to super class to allow better code re-use.
+     * @return  The output buffer used by the processor. 
+     */
+    protected abstract AbstractOutputBuffer getOutputBuffer();
+
+    /**
+     * Add input or output filter.
+     *
+     * @param className class name of the filter
+     */
+    protected void addFilter(String className) {
+        try {
+            Class<?> clazz = Class.forName(className);
+            Object obj = clazz.newInstance();
+            if (obj instanceof InputFilter) {
+                getInputBuffer().addFilter((InputFilter) obj);
+            } else if (obj instanceof OutputFilter) {
+                getOutputBuffer().addFilter((OutputFilter) obj);
+            } else {
+                log.warn(sm.getString("http11processor.filter.unknown",
+                        className));
+            }
+        } catch (Exception e) {
+            log.error(sm.getString(
+                    "http11processor.filter.error", className), e);
+        }
+    }
+
+
+    /**
+     * Add an input filter to the current request.
+     *
+     * @return false if the encoding was not found (which would mean it is
+     * unsupported)
+     */
+    protected boolean addInputFilter(InputFilter[] inputFilters,
+                                     String encodingName) {
+        if (encodingName.equals("identity")) {
+            // Skip
+        } else if (encodingName.equals("chunked")) {
+            getInputBuffer().addActiveFilter
+                (inputFilters[Constants.CHUNKED_FILTER]);
+            contentDelimitation = true;
+        } else {
+            for (int i = 2; i < inputFilters.length; i++) {
+                if (inputFilters[i].getEncodingName()
+                    .toString().equals(encodingName)) {
+                    getInputBuffer().addActiveFilter(inputFilters[i]);
+                    return true;
+                }
+            }
+            return false;
+        }
+        return true;
+    }
+
+
+
 }
index aebec8a..764ba9e 100644 (file)
@@ -151,28 +151,6 @@ public class Http11AprProcessor extends AbstractHttp11Processor implements Actio
 
 
     /**
-     * Add input or output filter.
-     *
-     * @param className class name of the filter
-     */
-    protected void addFilter(String className) {
-        try {
-            Class<?> clazz = Class.forName(className);
-            Object obj = clazz.newInstance();
-            if (obj instanceof InputFilter) {
-                inputBuffer.addFilter((InputFilter) obj);
-            } else if (obj instanceof OutputFilter) {
-                outputBuffer.addFilter((OutputFilter) obj);
-            } else {
-                log.warn(sm.getString("http11processor.filter.unknown", className));
-            }
-        } catch (Exception e) {
-            log.error(sm.getString("http11processor.filter.error", className), e);
-        }
-    }
-
-
-    /**
      * Process pipelined HTTP requests using the specified input and output
      * streams.
      *
@@ -1196,33 +1174,14 @@ public class Http11AprProcessor extends AbstractHttp11Processor implements Actio
         outputBuffer.addFilter(new GzipOutputFilter());
 
     }
-
-
-    /**
-     * Add an input filter to the current request.
-     *
-     * @return false if the encoding was not found (which would mean it is
-     * unsupported)
-     */
-    protected boolean addInputFilter(InputFilter[] inputFilters,
-                                     String encodingName) {
-        if (encodingName.equals("identity")) {
-            // Skip
-        } else if (encodingName.equals("chunked")) {
-            inputBuffer.addActiveFilter
-                (inputFilters[Constants.CHUNKED_FILTER]);
-            contentDelimitation = true;
-        } else {
-            for (int i = 2; i < inputFilters.length; i++) {
-                if (inputFilters[i].getEncodingName()
-                    .toString().equals(encodingName)) {
-                    inputBuffer.addActiveFilter(inputFilters[i]);
-                    return true;
-                }
-            }
-            return false;
-        }
-        return true;
+    
+    @Override
+    protected AbstractInputBuffer getInputBuffer() {
+        return inputBuffer;
     }
 
+    @Override
+    protected AbstractOutputBuffer getOutputBuffer() {
+        return outputBuffer;
+    }
 }
index 992f98d..fc36109 100644 (file)
@@ -16,7 +16,6 @@
  */
 
 package org.apache.coyote.http11;
-
 import java.io.IOException;
 import java.io.InterruptedIOException;
 import java.net.InetAddress;
@@ -161,28 +160,6 @@ public class Http11NioProcessor extends AbstractHttp11Processor implements Actio
 
 
     /**
-     * Add input or output filter.
-     *
-     * @param className class name of the filter
-     */
-    protected void addFilter(String className) {
-        try {
-            Class<?> clazz = Class.forName(className);
-            Object obj = clazz.newInstance();
-            if (obj instanceof InputFilter) {
-                inputBuffer.addFilter((InputFilter) obj);
-            } else if (obj instanceof OutputFilter) {
-                outputBuffer.addFilter((OutputFilter) obj);
-            } else {
-                log.warn(sm.getString("http11processor.filter.unknown", className));
-            }
-        } catch (Exception e) {
-            log.error(sm.getString("http11processor.filter.error", className), e);
-        }
-    }
-
-
-    /**
      * Process pipelined HTTP requests using the specified input and output
      * streams.
      *
@@ -1251,34 +1228,6 @@ public class Http11NioProcessor extends AbstractHttp11Processor implements Actio
 
 
     /**
-     * Add an input filter to the current request.
-     *
-     * @return false if the encoding was not found (which would mean it is
-     * unsupported)
-     */
-    protected boolean addInputFilter(InputFilter[] inputFilters,
-                                     String encodingName) {
-        if (encodingName.equals("identity")) {
-            // Skip
-        } else if (encodingName.equals("chunked")) {
-            inputBuffer.addActiveFilter
-                (inputFilters[Constants.CHUNKED_FILTER]);
-            contentDelimitation = true;
-        } else {
-            for (int i = 2; i < inputFilters.length; i++) {
-                if (inputFilters[i].getEncodingName()
-                    .toString().equals(encodingName)) {
-                    inputBuffer.addActiveFilter(inputFilters[i]);
-                    return true;
-                }
-            }
-            return false;
-        }
-        return true;
-    }
-
-
-    /**
      * Specialized utility method: find a sequence of lower case bytes inside
      * a ByteChunk.
      */
@@ -1323,6 +1272,16 @@ public class Http11NioProcessor extends AbstractHttp11Processor implements Actio
                status == 501 /* SC_NOT_IMPLEMENTED */;
     }
      
+    @Override
+    protected AbstractInputBuffer getInputBuffer() {
+        return inputBuffer;
+    }
+
+    @Override
+    protected AbstractOutputBuffer getOutputBuffer() {
+        return outputBuffer;
+    }
+
     /**
      * Set the SSL information for this HTTP connection.
      */
index 9576929..98f5124 100644 (file)
@@ -1001,55 +1001,6 @@ public class Http11Processor extends AbstractHttp11Processor implements ActionHo
 
 
     /**
-     * Add an input filter to the current request.
-     *
-     * @return false if the encoding was not found (which would mean it is
-     * unsupported)
-     */
-    protected boolean addInputFilter(InputFilter[] inputFilters,
-                                     String encodingName) {
-        if (encodingName.equals("identity")) {
-            // Skip
-        } else if (encodingName.equals("chunked")) {
-            inputBuffer.addActiveFilter
-                (inputFilters[Constants.CHUNKED_FILTER]);
-            contentDelimitation = true;
-        } else {
-            for (int i = 2; i < inputFilters.length; i++) {
-                if (inputFilters[i].getEncodingName()
-                    .toString().equals(encodingName)) {
-                    inputBuffer.addActiveFilter(inputFilters[i]);
-                    return true;
-                }
-            }
-            return false;
-        }
-        return true;
-    }
-
-
-    /**
-     * Add input or output filter.
-     *
-     * @param className class name of the filter
-     */
-    protected void addFilter(String className) {
-        try {
-            Class<?> clazz = Class.forName(className);
-            Object obj = clazz.newInstance();
-            if (obj instanceof InputFilter) {
-                inputBuffer.addFilter((InputFilter) obj);
-            } else if (obj instanceof OutputFilter) {
-                outputBuffer.addFilter((OutputFilter) obj);
-            } else {
-                log.warn(sm.getString("http11processor.filter.unknown", className));
-            }
-        } catch (Exception e) {
-            log.error(sm.getString("http11processor.filter.error", className), e);
-        }
-    }
-
-    /**
      * Parse host.
      */
     protected void parseHost(MessageBytes valueMB) {
@@ -1124,6 +1075,16 @@ public class Http11Processor extends AbstractHttp11Processor implements ActionHo
 
     }
 
+    @Override
+    protected AbstractInputBuffer getInputBuffer() {
+        return inputBuffer;
+    }
+
+    @Override
+    protected AbstractOutputBuffer getOutputBuffer() {
+        return outputBuffer;
+    }
+
     /**
      * Set the socket buffer flag.
      * @Override