hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1398785 - in /httpcomponents/httpcore/trunk/httpcore/src: main/java/org/apache/http/ main/java/org/apache/http/impl/ main/java/org/apache/http/message/ test/java/org/apache/http/message/
Date Tue, 16 Oct 2012 13:30:54 GMT
Author: olegk
Date: Tue Oct 16 13:30:54 2012
New Revision: 1398785

URL: http://svn.apache.org/viewvc?rev=1398785&view=rev
Log:
Cleaned up BasicHttpResponse API

Added:
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicHttpResponse.java
  (with props)
Modified:
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HttpResponse.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/DefaultHttpResponseFactory.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/AbstractHttpMessage.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHttpResponse.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestAbstractMessage.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicMessages.java

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HttpResponse.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HttpResponse.java?rev=1398785&r1=1398784&r2=1398785&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HttpResponse.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HttpResponse.java
Tue Oct 16 13:30:54 2012
@@ -29,6 +29,8 @@ package org.apache.http;
 
 import java.util.Locale;
 
+import org.apache.http.impl.DefaultHttpRequestFactory;
+
 /**
  * After receiving and interpreting a request message, a server responds
  * with an HTTP response message.
@@ -150,7 +152,10 @@ public interface HttpResponse extends Ht
      * It can be changed using {@link #setLocale setLocale}.
      *
      * @return  the locale of this response, never <code>null</code>
+     *
+     * @deprecated (4.3) use {@link DefaultHttpRequestFactory}
      */
+    @Deprecated
     Locale getLocale();
 
     /**
@@ -160,8 +165,9 @@ public interface HttpResponse extends Ht
      *
      * @param loc       the new locale
      *
-     * @see #getLocale getLocale
-     * @see #setStatusCode setStatusCode
+     * @deprecated (4.3) use {@link DefaultHttpRequestFactory}
      */
+    @Deprecated
     void setLocale(Locale loc);
+
 }

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/DefaultHttpResponseFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/DefaultHttpResponseFactory.java?rev=1398785&r1=1398784&r2=1398785&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/DefaultHttpResponseFactory.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/DefaultHttpResponseFactory.java
Tue Oct 16 13:30:54 2012
@@ -73,26 +73,26 @@ public class DefaultHttpResponseFactory 
 
 
     // non-javadoc, see interface HttpResponseFactory
-    public HttpResponse newHttpResponse(final ProtocolVersion ver,
-                                        final int status,
-                                        HttpContext context) {
+    public HttpResponse newHttpResponse(
+            final ProtocolVersion ver,
+            final int status,
+            final HttpContext context) {
         Args.notNull(ver, "HTTP version");
         Locale loc = determineLocale(context);
         String reason   = reasonCatalog.getReason(status, loc);
         StatusLine statusline = new BasicStatusLine(ver, status, reason);
-        return new BasicHttpResponse(statusline, reasonCatalog, loc);
+        return new BasicHttpResponse(statusline);
     }
 
 
     // non-javadoc, see interface HttpResponseFactory
-    public HttpResponse newHttpResponse(final StatusLine statusline,
-                                        HttpContext context) {
+    public HttpResponse newHttpResponse(
+            final StatusLine statusline,
+            final HttpContext context) {
         Args.notNull(statusline, "Status line");
-        final Locale loc = determineLocale(context);
-        return new BasicHttpResponse(statusline, reasonCatalog, loc);
+        return new BasicHttpResponse(statusline);
     }
 
-
     /**
      * Determines the locale of the response.
      * The implementation in this class always returns the default locale.
@@ -105,4 +105,5 @@ public class DefaultHttpResponseFactory 
     protected Locale determineLocale(HttpContext context) {
         return Locale.getDefault();
     }
+
 }

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/AbstractHttpMessage.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/AbstractHttpMessage.java?rev=1398785&r1=1398784&r2=1398785&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/AbstractHttpMessage.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/AbstractHttpMessage.java
Tue Oct 16 13:30:54 2012
@@ -137,7 +137,10 @@ public abstract class AbstractHttpMessag
         return this.headergroup.iterator(name);
     }
 
-    // non-javadoc, see interface HttpMessage
+    /**
+     * @deprecated (4.3) use constructor parameters of configuration API provided by HttpClient
+     */
+    @Deprecated
     public HttpParams getParams() {
         if (this.params == null) {
             this.params = new BasicHttpParams();
@@ -145,7 +148,10 @@ public abstract class AbstractHttpMessag
         return this.params;
     }
 
-    // non-javadoc, see interface HttpMessage
+    /**
+     * @deprecated (4.3) use constructor parameters of configuration API provided by HttpClient
+     */
+    @Deprecated
     public void setParams(final HttpParams params) {
         this.params = Args.notNull(params, "HTTP parameters");
     }

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHttpResponse.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHttpResponse.java?rev=1398785&r1=1398784&r2=1398785&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHttpResponse.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHttpResponse.java
Tue Oct 16 13:30:54 2012
@@ -31,27 +31,33 @@ import java.util.Locale;
 
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
+import org.apache.http.HttpVersion;
 import org.apache.http.ProtocolVersion;
 import org.apache.http.ReasonPhraseCatalog;
 import org.apache.http.StatusLine;
 import org.apache.http.annotation.NotThreadSafe;
+import org.apache.http.impl.DefaultHttpRequestFactory;
+import org.apache.http.impl.DefaultHttpResponseFactory;
 import org.apache.http.util.Args;
 
 /**
  * Basic implementation of {@link HttpResponse}.
  *
+ * @see DefaultHttpResponseFactory
+ *
  * @since 4.0
  */
 @NotThreadSafe
-public class BasicHttpResponse extends AbstractHttpMessage
-    implements HttpResponse {
+public class BasicHttpResponse extends AbstractHttpMessage implements HttpResponse {
 
     private StatusLine          statusline;
+    private ProtocolVersion     ver;
+    private int                 code;
+    private String              reasonPhrase;
     private HttpEntity          entity;
     private ReasonPhraseCatalog reasonCatalog;
     private Locale              locale;
 
-
     /**
      * Creates a new response.
      * This is the constructor to which all others map.
@@ -62,14 +68,20 @@ public class BasicHttpResponse extends A
      *                          reason phrase lookup
      * @param locale            the locale for looking up reason phrases, or
      *                          <code>null</code> for the system locale
+     *
+     * @deprecated (4.3) use {@link DefaultHttpRequestFactory}
      */
+    @Deprecated
     public BasicHttpResponse(final StatusLine statusline,
                              final ReasonPhraseCatalog catalog,
                              final Locale locale) {
         super();
         this.statusline = Args.notNull(statusline, "Status line");
+        this.ver = statusline.getProtocolVersion();
+        this.code = statusline.getStatusCode();
+        this.reasonPhrase = statusline.getReasonPhrase();
         this.reasonCatalog = catalog;
-        this.locale = (locale != null) ? locale : Locale.getDefault();
+        this.locale = locale;
     }
 
     /**
@@ -96,17 +108,30 @@ public class BasicHttpResponse extends A
     public BasicHttpResponse(final ProtocolVersion ver,
                              final int code,
                              final String reason) {
-        this(new BasicStatusLine(ver, code, reason), null, null);
+        super();
+        Args.notNegative(code, "Status code");
+        this.statusline = null;
+        this.ver = ver;
+        this.code = code;
+        this.reasonPhrase = reason;
+        this.reasonCatalog = null;
+        this.locale = null;
     }
 
 
     // non-javadoc, see interface HttpMessage
     public ProtocolVersion getProtocolVersion() {
-        return this.statusline.getProtocolVersion();
+        return this.ver;
     }
 
     // non-javadoc, see interface HttpResponse
     public StatusLine getStatusLine() {
+        if (this.statusline == null) {
+            this.statusline = new BasicStatusLine(
+                    this.ver != null ? this.ver : HttpVersion.HTTP_1_1,
+                    this.code,
+                    this.reasonPhrase != null ? this.reasonPhrase : getReason(this.code));
+        }
         return this.statusline;
     }
 
@@ -115,7 +140,10 @@ public class BasicHttpResponse extends A
         return this.entity;
     }
 
-    // non-javadoc, see interface HttpResponse
+    /**
+     * @deprecated (4.3) use {@link DefaultHttpRequestFactory}
+     */
+    @Deprecated
     public Locale getLocale() {
         return this.locale;
     }
@@ -123,35 +151,41 @@ public class BasicHttpResponse extends A
     // non-javadoc, see interface HttpResponse
     public void setStatusLine(final StatusLine statusline) {
         this.statusline = Args.notNull(statusline, "Status line");
+        this.ver = statusline.getProtocolVersion();
+        this.code = statusline.getStatusCode();
+        this.reasonPhrase = statusline.getReasonPhrase();
     }
 
     // non-javadoc, see interface HttpResponse
     public void setStatusLine(final ProtocolVersion ver, final int code) {
-        // arguments checked in BasicStatusLine constructor
-        this.statusline = new BasicStatusLine(ver, code, getReason(code));
+        Args.notNegative(code, "Status code");
+        this.statusline = null;
+        this.ver = ver;
+        this.code = code;
+        this.reasonPhrase = null;
     }
 
     // non-javadoc, see interface HttpResponse
-    public void setStatusLine(final ProtocolVersion ver, final int code,
-                              final String reason) {
-        // arguments checked in BasicStatusLine constructor
-        this.statusline = new BasicStatusLine(ver, code, reason);
+    public void setStatusLine(
+            final ProtocolVersion ver, final int code, final String reason) {
+        Args.notNegative(code, "Status code");
+        this.statusline = null;
+        this.ver = ver;
+        this.code = code;
+        this.reasonPhrase = reason;
     }
 
     // non-javadoc, see interface HttpResponse
     public void setStatusCode(int code) {
-        // argument checked in BasicStatusLine constructor
-        ProtocolVersion ver = this.statusline.getProtocolVersion();
-        this.statusline = new BasicStatusLine(ver, code, getReason(code));
+        Args.notNegative(code, "Status code");
+        this.statusline = null;
+        this.code = code;
     }
 
     // non-javadoc, see interface HttpResponse
     public void setReasonPhrase(String reason) {
-        Args.check(reason == null || (reason.indexOf('\n') == 0 && reason.indexOf('\r')
== 0), 
-                "Line break in reason phrase.");
-        this.statusline = new BasicStatusLine(this.statusline.getProtocolVersion(),
-                                              this.statusline.getStatusCode(),
-                                              reason);
+        this.statusline = null;
+        this.reasonPhrase = reason;
     }
 
     // non-javadoc, see interface HttpResponse
@@ -159,12 +193,13 @@ public class BasicHttpResponse extends A
         this.entity = entity;
     }
 
-    // non-javadoc, see interface HttpResponse
+    /**
+     * @deprecated (4.3) use {@link DefaultHttpRequestFactory}
+     */
+    @Deprecated
     public void setLocale(Locale locale) {
         this.locale =  Args.notNull(locale, "Locale");
-        final int code = this.statusline.getStatusCode();
-        this.statusline = new BasicStatusLine
-            (this.statusline.getProtocolVersion(), code, getReason(code));
+        this.statusline = null;
     }
 
     /**
@@ -175,15 +210,19 @@ public class BasicHttpResponse extends A
      * @param code      the status code for which to look up the reason
      *
      * @return  the reason phrase, or <code>null</code> if there is none
+     *
+     * @deprecated (4.3) use {@link DefaultHttpRequestFactory}
      */
+    @Deprecated
     protected String getReason(int code) {
-        return (this.reasonCatalog == null) ?
-            null : this.reasonCatalog.getReason(code, this.locale);
+        return this.reasonCatalog != null ? this.reasonCatalog.getReason(code,
+                this.locale != null ? this.locale : Locale.getDefault()) : null;
     }
 
     @Override
     public String toString() {
-        return this.statusline + " " + this.headergroup;
+        StatusLine statusline = getStatusLine();
+        return statusline + " " + this.headergroup;
     }
 
 }

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestAbstractMessage.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestAbstractMessage.java?rev=1398785&r1=1398784&r2=1398785&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestAbstractMessage.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestAbstractMessage.java
Tue Oct 16 13:30:54 2012
@@ -37,7 +37,7 @@ import org.junit.Assert;
 import org.junit.Test;
 
 /**
- * Unit tests for {@link Header}.
+ * Unit tests for {@link AbstractHttpMessage}.
  *
  */
 public class TestAbstractMessage {

Added: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicHttpResponse.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicHttpResponse.java?rev=1398785&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicHttpResponse.java
(added)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicHttpResponse.java
Tue Oct 16 13:30:54 2012
@@ -0,0 +1,101 @@
+/*
+ * ====================================================================
+ * 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.HttpVersion;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Unit tests for {@link BasicHttpResponse}.
+ */
+public class TestBasicHttpResponse {
+
+    @Test
+    public void testBasics() {
+        BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        Assert.assertEquals(HttpVersion.HTTP_1_1, response.getProtocolVersion());
+        Assert.assertEquals(HttpVersion.HTTP_1_1, response.getStatusLine().getProtocolVersion());
+        Assert.assertEquals(200, response.getStatusLine().getStatusCode());
+        Assert.assertEquals("OK", response.getStatusLine().getReasonPhrase());
+    }
+
+    @Test
+    public void testStatusLineMutation() {
+        BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        Assert.assertEquals(HttpVersion.HTTP_1_1, response.getStatusLine().getProtocolVersion());
+        Assert.assertEquals(200, response.getStatusLine().getStatusCode());
+        Assert.assertEquals("OK", response.getStatusLine().getReasonPhrase());
+        response.setReasonPhrase("Kind of OK");
+        Assert.assertEquals(HttpVersion.HTTP_1_1, response.getStatusLine().getProtocolVersion());
+        Assert.assertEquals(200, response.getStatusLine().getStatusCode());
+        Assert.assertEquals("Kind of OK", response.getStatusLine().getReasonPhrase());
+        response.setStatusCode(299);
+        Assert.assertEquals(HttpVersion.HTTP_1_1, response.getStatusLine().getProtocolVersion());
+        Assert.assertEquals(299, response.getStatusLine().getStatusCode());
+        Assert.assertEquals("Kind of OK", response.getStatusLine().getReasonPhrase());
+        response.setStatusLine(HttpVersion.HTTP_1_0, 298);
+        Assert.assertEquals(HttpVersion.HTTP_1_0, response.getStatusLine().getProtocolVersion());
+        Assert.assertEquals(298, response.getStatusLine().getStatusCode());
+        Assert.assertEquals(null, response.getStatusLine().getReasonPhrase());
+        response.setStatusLine(HttpVersion.HTTP_1_1, 200, "OK");
+        Assert.assertEquals(HttpVersion.HTTP_1_1, response.getStatusLine().getProtocolVersion());
+        Assert.assertEquals(200, response.getStatusLine().getStatusCode());
+        Assert.assertEquals("OK", response.getStatusLine().getReasonPhrase());
+        response.setStatusLine(new BasicStatusLine(HttpVersion.HTTP_1_0, 500, "Boom"));
+        Assert.assertEquals(HttpVersion.HTTP_1_0, response.getStatusLine().getProtocolVersion());
+        Assert.assertEquals(500, response.getStatusLine().getStatusCode());
+        Assert.assertEquals("Boom", response.getStatusLine().getReasonPhrase());
+    }
+
+    @Test
+    public void testInvalidStatusCode() {
+        try {
+            new BasicHttpResponse(HttpVersion.HTTP_1_1, -200, "OK");
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+        BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        try {
+            response.setStatusCode(-1);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+        try {
+            response.setStatusLine(HttpVersion.HTTP_1_1, -1);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+        try {
+            response.setStatusLine(HttpVersion.HTTP_1_1, -1, "not ok");
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+    }
+
+}

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

Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicHttpResponse.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

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

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicMessages.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicMessages.java?rev=1398785&r1=1398784&r2=1398785&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicMessages.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicMessages.java
Tue Oct 16 13:30:54 2012
@@ -86,13 +86,6 @@ public class TestBasicMessages {
         }
         response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
         try {
-            response.setStatusLine(null, 200);
-            Assert.fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
-        try {
             response.setStatusLine(null);
             Assert.fail("IllegalArgumentException should have been thrown");
         } catch (IllegalArgumentException ex) {



Mime
View raw message