hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1334434 - in /httpcomponents/httpcore/trunk: RELEASE_NOTES.txt httpcore/src/main/java/org/apache/http/entity/StringEntity.java httpcore/src/test/java/org/apache/http/entity/TestStringEntity.java
Date Sat, 05 May 2012 15:48:01 GMT
Author: olegk
Date: Sat May  5 15:48:00 2012
New Revision: 1334434

URL: http://svn.apache.org/viewvc?rev=1334434&view=rev
Log:
Fixed NPE in StringEntity constructor thrown if ContentType#getCharset is null

Modified:
    httpcomponents/httpcore/trunk/RELEASE_NOTES.txt
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/StringEntity.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestStringEntity.java

Modified: httpcomponents/httpcore/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/RELEASE_NOTES.txt?rev=1334434&r1=1334433&r2=1334434&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpcore/trunk/RELEASE_NOTES.txt Sat May  5 15:48:00 2012
@@ -1,3 +1,9 @@
+Changes since 4.2
+-------------------
+
+* Fixed NPE in StringEntity constructor thrown if ContentType#getCharset is null.
+  Contributed by Oleg Kalnichevski <olegk at apache.org>
+
 Release 4.2
 -------------------
 

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/StringEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/StringEntity.java?rev=1334434&r1=1334433&r2=1334434&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/StringEntity.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/StringEntity.java
Sat May  5 15:48:00 2012
@@ -66,6 +66,9 @@ public class StringEntity extends Abstra
             throw new IllegalArgumentException("Source string may not be null");
         }
         Charset charset = contentType != null ? contentType.getCharset() : null;
+        if (charset == null) {
+            charset = HTTP.DEF_CONTENT_CHARSET;
+        }
         try {
             this.content = string.getBytes(charset.name());
         } catch (UnsupportedEncodingException ex) {

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestStringEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestStringEntity.java?rev=1334434&r1=1334433&r2=1334434&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestStringEntity.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestStringEntity.java
Sat May  5 15:48:00 2012
@@ -28,8 +28,10 @@
 package org.apache.http.entity;
 
 import java.io.ByteArrayOutputStream;
+import java.nio.charset.Charset;
 
 import org.apache.http.Consts;
+import org.apache.http.util.EntityUtils;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -77,6 +79,37 @@ public class TestStringEntity {
                 httpentity.getContentType().getValue());
     }
 
+    private static String constructString(int [] unicodeChars) {
+        StringBuilder buffer = new StringBuilder();
+        if (unicodeChars != null) {
+            for (int i = 0; i < unicodeChars.length; i++) {
+                buffer.append((char)unicodeChars[i]);
+            }
+        }
+        return buffer.toString();
+    }
+
+    static final int SWISS_GERMAN_HELLO [] = {
+            0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
+        };
+
+    @Test
+    public void testNullCharset() throws Exception {
+        String s = constructString(SWISS_GERMAN_HELLO);
+        StringEntity httpentity = new StringEntity(s, ContentType.create("text/plain", (Charset)
null));
+        Assert.assertNotNull(httpentity.getContentType());
+        Assert.assertEquals("text/plain", httpentity.getContentType().getValue());
+        Assert.assertEquals(s, EntityUtils.toString(httpentity));
+        httpentity = new StringEntity(s, (Charset) null);
+        Assert.assertNotNull(httpentity.getContentType());
+        Assert.assertEquals("text/plain", httpentity.getContentType().getValue());
+        Assert.assertEquals(s, EntityUtils.toString(httpentity));
+        httpentity = new StringEntity(s, (String) null);
+        Assert.assertNotNull(httpentity.getContentType());
+        Assert.assertEquals("text/plain", httpentity.getContentType().getValue());
+        Assert.assertEquals(s, EntityUtils.toString(httpentity));
+    }
+
     @Test
     public void testWriteTo() throws Exception {
         String s = "Message content";



Mime
View raw message