You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2005/03/13 20:49:00 UTC

svn commit: r157351 - in jakarta/httpclient/trunk/http-common/src: java/org/apache/http/StatusLine.java test/org/apache/http/TestAll.java test/org/apache/http/TestStatusLine.java

Author: olegk
Date: Sun Mar 13 11:48:58 2005
New Revision: 157351

URL: http://svn.apache.org/viewcvs?view=rev&rev=157351
Log:
Post SVN COPY refactoring

Modified:
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/StatusLine.java
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestAll.java
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestStatusLine.java

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/StatusLine.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/StatusLine.java?view=diff&r1=157350&r2=157351
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/StatusLine.java (original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/StatusLine.java Sun Mar 13 11:48:58 2005
@@ -1,5 +1,5 @@
 /*
- * $Header: $
+ * $HeadURL$
  * $Revision$
  * $Date$
  *
@@ -58,11 +58,8 @@
 
     // ----------------------------------------------------- Instance Variables
 
-    /** The original Status-Line. */
-    private final String statusLine;
-
     /** The HTTP-Version. */
-    private final String httpVersion;
+    private final HttpVersion httpVersion;
 
     /** The Status-Code. */
     private final int statusCode;
@@ -70,17 +67,38 @@
     /** The Reason-Phrase. */
     private final String reasonPhrase;
 
-
     // ----------------------------------------------------------- Constructors
-
     /**
-     * Default constructor.
+     * Default constructor
+     */
+    public StatusLine(final HttpVersion httpVersion, int statusCode, final String reasonPhrase) {
+        super();
+        if (httpVersion == null) {
+            throw new IllegalArgumentException("HTTP version may not be null");
+        }
+        if (statusCode < 0) {
+            throw new IllegalArgumentException("Status code may not be negative");
+        }
+        this.httpVersion = httpVersion;
+        this.statusCode = statusCode;
+        this.reasonPhrase = reasonPhrase;
+    }
+    /**
+     * Parses the status line returned from the HTTP server.
      *
-     * @param statusLine the status line returned from the HTTP server
+     * @param statusLine the status line to be parsed
+     * 
      * @throws HttpException if the status line is invalid
+     * 
+     * @since 4.0 
      */
-    public StatusLine(final String statusLine) throws HttpException {
-
+    public static StatusLine parse(final String statusLine) throws HttpException {
+        if (statusLine == null) {
+            throw new IllegalArgumentException("Status line string may not be null");
+        }
+        HttpVersion httpVersion = null;
+        int statusCode = 0;
+        String reasonPhrase = null;
         int length = statusLine.length();
         int at = 0;
         int start = 0;
@@ -100,7 +118,7 @@
                         "Unable to parse HTTP-Version from the status line: '"
                         + statusLine + "'");
             }
-            this.httpVersion = (statusLine.substring(start, at)).toUpperCase();
+            httpVersion = HttpVersion.parse(statusLine.substring(start, at));
 
             //advance through spaces
             while (statusLine.charAt(at) == ' ') {
@@ -113,7 +131,7 @@
                 to = length;
             }
             try {
-                this.statusCode = Integer.parseInt(statusLine.substring(at, to));
+                statusCode = Integer.parseInt(statusLine.substring(at, to));
             } catch (NumberFormatException e) {
                 throw new ProtocolException(
                     "Unable to parse status code from status line: '" 
@@ -122,47 +140,49 @@
             //handle the Reason-Phrase
             at = to + 1;
             if (at < length) {
-                this.reasonPhrase = statusLine.substring(at).trim();
+                reasonPhrase = statusLine.substring(at).trim();
             } else {
-                this.reasonPhrase = "";
+                reasonPhrase = "";
             }
         } catch (StringIndexOutOfBoundsException e) {
             throw new HttpException("Status-Line '" + statusLine + "' is not valid"); 
         }
-        //save the original Status-Line
-        this.statusLine = new String(statusLine);
+        return new StatusLine(httpVersion, statusCode, reasonPhrase);
     }
 
-
     // --------------------------------------------------------- Public Methods
 
     /**
      * @return the Status-Code
      */
     public final int getStatusCode() {
-        return statusCode;
+        return this.statusCode;
     }
 
     /**
      * @return the HTTP-Version
      */
-    public final String getHttpVersion() {
-        return httpVersion;
+    public final HttpVersion getHttpVersion() {
+        return this.httpVersion;
     }
 
     /**
      * @return the Reason-Phrase
      */
     public final String getReasonPhrase() {
-        return reasonPhrase;
+        return this.reasonPhrase;
     }
 
-    /**
-     * Return a string representation of this object.
-     * @return a string represenation of this object.
-     */
     public final String toString() {
-        return statusLine;
+        StringBuffer buffer = new StringBuffer();
+        buffer.append(this.httpVersion);
+        buffer.append(' ');
+        buffer.append(this.statusCode);
+        if (this.reasonPhrase != null && !this.reasonPhrase.equals("")) {
+            buffer.append(' ');
+            buffer.append(this.reasonPhrase);
+        }
+        return buffer.toString();
     }
 
     /**

Modified: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestAll.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestAll.java?view=diff&r1=157350&r2=157351
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestAll.java (original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestAll.java Sun Mar 13 11:48:58 2005
@@ -47,6 +47,7 @@
         suite.addTest(TestHeaderGroup.suite());
         suite.addTest(TestHttpStatus.suite());
         suite.addTest(TestHttpVersion.suite());
+        suite.addTest(TestStatusLine.suite());
         return suite;
     }
 

Modified: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestStatusLine.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestStatusLine.java?view=diff&r1=157350&r2=157351
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestStatusLine.java (original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestStatusLine.java Sun Mar 13 11:48:58 2005
@@ -1,5 +1,5 @@
 /*
- * $Header: $
+ * $HeadURL$
  * $Revision$
  * $Date$
  * ====================================================================
@@ -34,6 +34,7 @@
  * Simple tests for {@link StatusLine}.
  *
  * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
+ * 
  * @version $Id$
  */
 public class TestStatusLine extends TestCase {
@@ -74,73 +75,76 @@
 
     public void testSuccess() throws Exception {
         //typical status line
-        statusLine = new StatusLine("HTTP/1.1 200 OK");
+        statusLine = StatusLine.parse("HTTP/1.1 200 OK");
         assertEquals("HTTP/1.1 200 OK", statusLine.toString());
-        assertEquals("HTTP/1.1", statusLine.getHttpVersion());
+        assertEquals(HttpVersion.HTTP_1_1, statusLine.getHttpVersion());
         assertEquals(200, statusLine.getStatusCode());
         assertEquals("OK", statusLine.getReasonPhrase());
 
         //status line with multi word reason phrase
-        statusLine = new StatusLine("HTTP/1.1 404 Not Found");
+        statusLine = StatusLine.parse("HTTP/1.1 404 Not Found");
         assertEquals(404, statusLine.getStatusCode());
         assertEquals("Not Found", statusLine.getReasonPhrase());
 
         //reason phrase can be anyting
-        statusLine = new StatusLine("HTTP/1.1 404 Non Trouve");
+        statusLine = StatusLine.parse("HTTP/1.1 404 Non Trouve");
         assertEquals("Non Trouve", statusLine.getReasonPhrase());
 
         //its ok to end with a \n\r
-        statusLine = new StatusLine("HTTP/1.1 404 Not Found\r\n");
+        statusLine = StatusLine.parse("HTTP/1.1 404 Not Found\r\n");
         assertEquals("Not Found", statusLine.getReasonPhrase());
 
         //this is valid according to the Status-Line BNF
-        statusLine = new StatusLine("HTTP/1.1 200 ");
+        statusLine = StatusLine.parse("HTTP/1.1 200 ");
         assertEquals(200, statusLine.getStatusCode());
         assertEquals("", statusLine.getReasonPhrase());
 
         //this is not strictly valid, but is lienent
-        statusLine = new StatusLine("HTTP/1.1 200");
+        statusLine = StatusLine.parse("HTTP/1.1 200");
         assertEquals(200, statusLine.getStatusCode());
         assertEquals("", statusLine.getReasonPhrase());
 
         //this is not strictly valid, but is lienent
-        statusLine = new StatusLine("HTTP/1.1     200 OK");
+        statusLine = StatusLine.parse("HTTP/1.1     200 OK");
         assertEquals(200, statusLine.getStatusCode());
         assertEquals("OK", statusLine.getReasonPhrase());
 
         //this is not strictly valid, but is lienent
-        statusLine = new StatusLine("\rHTTP/1.1 200 OK");
+        statusLine = StatusLine.parse("\rHTTP/1.1 200 OK");
         assertEquals(200, statusLine.getStatusCode());
         assertEquals("OK", statusLine.getReasonPhrase());
-        assertEquals("HTTP/1.1", statusLine.getHttpVersion());
+        assertEquals(HttpVersion.HTTP_1_1, statusLine.getHttpVersion());
 
         //this is not strictly valid, but is lienent
-        statusLine = new StatusLine("  HTTP/1.1 200 OK");
+        statusLine = StatusLine.parse("  HTTP/1.1 200 OK");
         assertEquals(200, statusLine.getStatusCode());
         assertEquals("OK", statusLine.getReasonPhrase());
-        assertEquals("HTTP/1.1", statusLine.getHttpVersion());
+        assertEquals(HttpVersion.HTTP_1_1, statusLine.getHttpVersion());
     }
 
     public void testFailure() throws Exception {
         try {
-            statusLine = new StatusLine(null);
-            fail();
-        } catch (NullPointerException e) { /* expected */ }
-
-        try {
-            statusLine = new StatusLine("xxx 200 OK");
+            statusLine = StatusLine.parse("xxx 200 OK");
             fail();
         } catch (HttpException e) { /* expected */ }
 
         try {
-            statusLine = new StatusLine("HTTP/1.1 xxx OK");
+            statusLine = StatusLine.parse("HTTP/1.1 xxx OK");
             fail();
         } catch (HttpException e) { /* expected */ }
 
         try {
-            statusLine = new StatusLine("HTTP/1.1    ");
+            statusLine = StatusLine.parse("HTTP/1.1    ");
             fail();
         } catch (HttpException e) { /* expected */ }
     }
 
+    public void testNullInput() throws Exception {
+        try {
+            statusLine = StatusLine.parse(null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException e) { /* expected */ }
+
+    }
+    
 }