hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rola...@apache.org
Subject svn commit: r569673 - in /jakarta/httpcomponents/httpcore/trunk: module-main/src/main/java/org/apache/http/impl/io/ module-main/src/main/java/org/apache/http/message/ module-main/src/test/java/org/apache/http/message/ module-nio/src/main/java/org/apach...
Date Sat, 25 Aug 2007 13:58:52 GMT
Author: rolandw
Date: Sat Aug 25 06:58:51 2007
New Revision: 569673

URL: http://svn.apache.org/viewvc?rev=569673&view=rev
Log:
reversed argument order in LineFormatter, removed static formatting from BasicHeader, adjusted
test cases

Added:
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicLineFormatter.java
  (with props)
Modified:
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/AbstractMessageWriter.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpRequestWriter.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpResponseWriter.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeader.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicLineFormatter.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/LineFormatter.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestAllMessage.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHeader.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/AbstractMessageWriter.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/HttpRequestWriter.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/HttpResponseWriter.java

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/AbstractMessageWriter.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/AbstractMessageWriter.java?rev=569673&r1=569672&r2=569673&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/AbstractMessageWriter.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/AbstractMessageWriter.java
Sat Aug 25 06:58:51 2007
@@ -76,7 +76,7 @@
         for (Iterator it = message.headerIterator(); it.hasNext(); ) {
             Header header = (Header) it.next();
             this.sessionBuffer.writeLine
-                (lineFormatter.formatHeader(header, this.lineBuf));
+                (lineFormatter.formatHeader(this.lineBuf, header));
         }
         this.lineBuf.clear();
         this.sessionBuffer.writeLine(this.lineBuf);

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpRequestWriter.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpRequestWriter.java?rev=569673&r1=569672&r2=569673&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpRequestWriter.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpRequestWriter.java
Sat Aug 25 06:58:51 2007
@@ -52,7 +52,7 @@
         throws IOException {
 
         final CharArrayBuffer buffer = lineFormatter.formatRequestLine
-            (((HttpRequest) message).getRequestLine(), this.lineBuf);
+            (this.lineBuf, ((HttpRequest) message).getRequestLine());
         this.sessionBuffer.writeLine(buffer);
     }
 

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpResponseWriter.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpResponseWriter.java?rev=569673&r1=569672&r2=569673&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpResponseWriter.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/HttpResponseWriter.java
Sat Aug 25 06:58:51 2007
@@ -52,7 +52,7 @@
         throws IOException {
 
         final CharArrayBuffer buffer = lineFormatter.formatStatusLine
-            (((HttpResponse) message).getStatusLine(), this.lineBuf);
+            (this.lineBuf, ((HttpResponse) message).getStatusLine());
         this.sessionBuffer.writeLine(buffer);
     }
 

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeader.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeader.java?rev=569673&r1=569672&r2=569673&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeader.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeader.java
Sat Aug 25 06:58:51 2007
@@ -115,13 +115,8 @@
      * @return a string
      */
     public String toString() {
-        CharArrayBuffer buffer = new CharArrayBuffer(32);
-        buffer.append(this.name);
-        buffer.append(": ");
-        if (this.value != null) {
-            buffer.append(this.value);
-        }
-        return buffer.toString();
+        // no need for non-default formatting in toString()
+        return BasicLineFormatter.DEFAULT.formatHeader(null, this).toString();
     }
 
     /**
@@ -140,37 +135,6 @@
         } else {
             return new HeaderElement[] {}; 
         }
-    }
-
-    /**
-     * Formats a Header into a header line. The <code>header</code> is
-     * directly appended to <code>buffer</code>; no newline characters are
-     * inserted (folding).
-     * 
-     * @param buffer the buffer to append to
-     * @param header the header to format
-     */
-    /*public@@@*/ static void format(final CharArrayBuffer buffer, final Header header) {
-        if (buffer == null) {
-            throw new IllegalArgumentException("String buffer may not be null");
-        }
-        if (header == null) {
-            throw new IllegalArgumentException("Header may not be null");
-        }
-        buffer.append(header.getName());
-        buffer.append(": ");
-        if (header.getValue() != null) {
-            buffer.append(header.getValue());
-        }
-    }
- 
-    /**
-     * @see #format(CharArrayBuffer, Header)
-     */
-    /*public@@@*/ static String format(final Header header) {
-        CharArrayBuffer buffer = new CharArrayBuffer(32);
-        format(buffer, header);
-        return buffer.toString();
     }
 
 }

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicLineFormatter.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicLineFormatter.java?rev=569673&r1=569672&r2=569673&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicLineFormatter.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicLineFormatter.java
Sat Aug 25 06:58:51 2007
@@ -91,8 +91,8 @@
 
 
     // non-javadoc, see interface LineFormatter
-    public CharArrayBuffer formatRequestLine(RequestLine reqline,
-                                             CharArrayBuffer buffer) {
+    public CharArrayBuffer formatRequestLine(CharArrayBuffer buffer,
+                                             RequestLine reqline) {
 
         CharArrayBuffer result = initBuffer(buffer);
         BasicRequestLine.format(result, reqline); //@@@ move code here
@@ -102,18 +102,35 @@
 
 
     // non-javadoc, see interface LineFormatter
-    public CharArrayBuffer formatStatusLine(StatusLine statline,
-                                            CharArrayBuffer buffer) {
+    public CharArrayBuffer formatStatusLine(CharArrayBuffer buffer,
+                                            StatusLine statline) {
         CharArrayBuffer result = initBuffer(buffer);
         BasicStatusLine.format(result, statline); //@@@ move code here
         return result;
     }
 
 
+    /**
+     * Formats a header.
+     *
+     * @param header            the header to format
+     * @param formatter         the formatter to use, or
+     *                          <code>null</code> for the
+     *                          {@link #DEFAULT default}
+     *
+     * @return  the formatted header
+     */
+    public final static String formatHeader(final Header header,
+                                            LineFormatter formatter) {
+        if (formatter == null)
+            formatter = BasicLineFormatter.DEFAULT;
+        return formatter.formatHeader(null, header).toString();
+    }
+
 
     // non-javadoc, see interface LineFormatter
-    public CharArrayBuffer formatHeader(Header header,
-                                        CharArrayBuffer buffer) {
+    public CharArrayBuffer formatHeader(CharArrayBuffer buffer,
+                                        Header header) {
         if (header == null) {
             throw new IllegalArgumentException
                 ("Header must not be null.");
@@ -125,11 +142,38 @@
             result = ((BufferedHeader)header).getBuffer();
         } else {
             result = initBuffer(buffer);
-            BasicHeader.format(result, header); //@@@ move code here
+            doFormatHeader(result, header);
         }
         return result;
 
     } // formatHeader
+
+
+    /**
+     * Actually formats a header.
+     * Called from {@link #formatHeader}.
+     *
+     * @param buffer    the empty buffer into which to format,
+     *                  never <code>null</code>
+     * @param header    the header to format, never <code>null</code>
+     */
+    protected void doFormatHeader(CharArrayBuffer buffer,
+                                  Header header) {
+        final String name = header.getName();
+        final String value = header.getValue();
+
+        int len = name.length() + 2;
+        if (value != null) {
+            len += value.length();
+        }
+        buffer.ensureCapacity(len);
+
+        buffer.append(name);
+        buffer.append(": ");
+        if (value != null) {
+            buffer.append(value);
+        }
+    }
 
 
 } // class BasicLineFormatter

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/LineFormatter.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/LineFormatter.java?rev=569673&r1=569672&r2=569673&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/LineFormatter.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/LineFormatter.java
Sat Aug 25 06:58:51 2007
@@ -74,33 +74,32 @@
     /**
      * Formats a request line.
      *
-     * @param reqline   the request line to format
      * @param buffer    a buffer available for formatting, or
      *                  <code>null</code>.
      *                  The buffer will be cleared before use.
+     * @param reqline   the request line to format
      *
      * @return  the formatted request line
      */
-    CharArrayBuffer formatRequestLine(RequestLine reqline,
-                                      CharArrayBuffer buffer) 
+    CharArrayBuffer formatRequestLine(CharArrayBuffer buffer,
+                                      RequestLine reqline) 
         ;
 
 
     /**
      * Formats a status line.
      *
-     * @param statline  the status line to format
      * @param buffer    a buffer available for formatting, or
      *                  <code>null</code>.
      *                  The buffer will be cleared before use.
-     * @param buffer    a buffer holding the line to parse
+     * @param statline  the status line to format
      *
      * @return  the formatted status line
      *
      * @throws ParseException        in case of a parse error
      */
-    CharArrayBuffer formatStatusLine(StatusLine statline,
-                                     CharArrayBuffer buffer) 
+    CharArrayBuffer formatStatusLine(CharArrayBuffer buffer,
+                                     StatusLine statline) 
         ;
 
 
@@ -113,17 +112,18 @@
      * <br/>
      * See the class comment for details about the buffer argument.
      *
-     * @param header    the header to format
      * @param buffer    a buffer available for formatting, or
      *                  <code>null</code>.
      *                  The buffer will be cleared before use.
+     * @param header    the header to format
      *
      * @return  a buffer holding the formatted header, never <code>null</code>.
      *          The returned buffer may be different from the argument buffer.
      *
      * @throws ParseException        in case of a parse error
      */
-    CharArrayBuffer formatHeader(Header header, CharArrayBuffer buffer)
+    CharArrayBuffer formatHeader(CharArrayBuffer buffer,
+                                 Header header)
         ;
 
 }

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestAllMessage.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestAllMessage.java?rev=569673&r1=569672&r2=569673&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestAllMessage.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestAllMessage.java
Sat Aug 25 06:58:51 2007
@@ -48,6 +48,7 @@
         suite.addTest(TestRequestLine.suite());
         suite.addTest(TestHttpVersion.suite());
         suite.addTest(TestBasicLineParser.suite());
+        suite.addTest(TestBasicLineFormatter.suite());
         suite.addTest(TestAbstractMessage.suite());
         suite.addTest(TestBasicMessages.suite());
 

Added: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicLineFormatter.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicLineFormatter.java?rev=569673&view=auto
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicLineFormatter.java
(added)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicLineFormatter.java
Sat Aug 25 06:58:51 2007
@@ -0,0 +1,94 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.message;
+
+import org.apache.http.Header;
+import org.apache.http.RequestLine;
+import org.apache.http.StatusLine;
+import org.apache.http.util.CharArrayBuffer;
+
+import junit.framework.*;
+
+/**
+ * Tests for {@link BasicLineFormatter}.
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ *
+ * @version $Revision$
+ */
+public class TestBasicLineFormatter extends TestCase {
+
+    // ------------------------------------------------------------ Constructor
+    public TestBasicLineFormatter(String testName) {
+        super(testName);
+    }
+
+    // ------------------------------------------------------------------- Main
+    public static void main(String args[]) {
+        String[] testCaseName = { TestBasicLineFormatter.class.getName() };
+        junit.textui.TestRunner.main(testCaseName);
+    }
+
+    // ------------------------------------------------------- TestCase Methods
+
+    public static Test suite() {
+        return new TestSuite(TestBasicLineFormatter.class);
+    }
+
+
+
+    public void testHeaderFormatting() throws Exception {
+        Header header1 = new BasicHeader("name", "value");
+        String s = BasicLineFormatter.formatHeader(header1, null);
+        assertEquals("name: value", s);
+        Header header2 = new BasicHeader("name", null);
+        s = BasicLineFormatter.formatHeader(header2, null); 
+        assertEquals("name: ", s);
+    }
+    
+    public void testHeaderFormattingInvalidInput() throws Exception {
+        try {
+            BasicLineFormatter.formatHeader
+                (null, BasicLineFormatter.DEFAULT);
+            fail("IllegalArgumentException should habe been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+        try {
+            BasicLineFormatter.DEFAULT.formatHeader
+                (new CharArrayBuffer(10), null);
+            fail("IllegalArgumentException should habe been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+    }
+     
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicLineFormatter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicLineFormatter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicLineFormatter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHeader.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHeader.java?rev=569673&r1=569672&r2=569673&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHeader.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHeader.java
Sat Aug 25 06:58:51 2007
@@ -105,31 +105,7 @@
         elements = header.getElements();
         assertNotNull(elements); 
         assertEquals(0, elements.length); 
-    }    
- 
-    public void testHeaderFormatting() throws Exception {
-        Header header1 = new BasicHeader("name", "value");
-        String s = BasicHeader.format(header1); 
-        assertEquals("name: value", s);
-        Header header2 = new BasicHeader("name", null);
-        s = BasicHeader.format(header2); 
-        assertEquals("name: ", s);
     }
-    
-    public void testHeaderFormattingInvalidInput() throws Exception {
-        try {
-            BasicHeader.format(null, new BasicHeader("name", "value"));
-            fail("IllegalArgumentException should habe been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicHeader.format(new CharArrayBuffer(10), (Header) null);
-            fail("IllegalArgumentException should habe been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-    }
-        
+
 }
 

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/AbstractMessageWriter.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/AbstractMessageWriter.java?rev=569673&r1=569672&r2=569673&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/AbstractMessageWriter.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/AbstractMessageWriter.java
Sat Aug 25 06:58:51 2007
@@ -78,7 +78,7 @@
         for (Iterator it = message.headerIterator(); it.hasNext(); ) {
             Header header = (Header) it.next();
             this.sessionBuffer.writeLine
-                (lineFormatter.formatHeader(header, this.lineBuf));
+                (lineFormatter.formatHeader(this.lineBuf, header));
         }
         this.lineBuf.clear();
         this.sessionBuffer.writeLine(this.lineBuf);

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/HttpRequestWriter.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/HttpRequestWriter.java?rev=569673&r1=569672&r2=569673&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/HttpRequestWriter.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/HttpRequestWriter.java
Sat Aug 25 06:58:51 2007
@@ -52,7 +52,7 @@
         throws IOException {
 
         final CharArrayBuffer buffer = lineFormatter.formatRequestLine
-            (((HttpRequest) message).getRequestLine(), this.lineBuf);
+            (this.lineBuf, ((HttpRequest) message).getRequestLine());
         this.sessionBuffer.writeLine(buffer);
     }
 

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/HttpResponseWriter.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/HttpResponseWriter.java?rev=569673&r1=569672&r2=569673&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/HttpResponseWriter.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/HttpResponseWriter.java
Sat Aug 25 06:58:51 2007
@@ -52,7 +52,7 @@
         throws IOException {
 
         final CharArrayBuffer buffer = lineFormatter.formatStatusLine
-            (((HttpResponse) message).getStatusLine(), this.lineBuf);
+            (this.lineBuf, ((HttpResponse) message).getStatusLine());
         this.sessionBuffer.writeLine(buffer);
     }
 



Mime
View raw message