commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/test/org/apache/commons/httpclient TestMethodCharEncoding.java
Date Sat, 03 Jul 2004 14:27:03 GMT
olegk       2004/07/03 07:27:03

  Modified:    httpclient/src/java/org/apache/commons/httpclient/methods
                        EntityEnclosingMethod.java StringRequestEntity.java
               httpclient/src/test/org/apache/commons/httpclient
                        TestMethodCharEncoding.java
  Log:
  PR #29883 (StringRequestEntity.getContentLength wrong for multibyte chars)
  
  Contributed by Oleg Kalnichevski
  Reviewed by Michael Becke
  
  Revision  Changes    Path
  1.39      +16 -8     jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java
  
  Index: EntityEnclosingMethod.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java,v
  retrieving revision 1.38
  retrieving revision 1.39
  diff -u -r1.38 -r1.39
  --- EntityEnclosingMethod.java	13 Jun 2004 20:22:19 -0000	1.38
  +++ EntityEnclosingMethod.java	3 Jul 2004 14:27:03 -0000	1.39
  @@ -32,6 +32,7 @@
   import java.io.IOException;
   import java.io.InputStream;
   import java.io.OutputStream;
  +import java.io.UnsupportedEncodingException;
   
   import org.apache.commons.httpclient.ChunkedOutputStream;
   import org.apache.commons.httpclient.Header;
  @@ -176,10 +177,17 @@
                   requestContentLength);
               this.requestStream = null;
           } else if (this.requestString != null) {
  -            this.requestEntity = new StringRequestEntity(
  -                requestString,
  -                null,
  -                getRequestCharSet());
  +            String charset = getRequestCharSet(); 
  +            try {
  +                this.requestEntity = new StringRequestEntity(
  +                        requestString, null, charset);
  +            } catch (UnsupportedEncodingException e) {
  +                if (LOG.isWarnEnabled()) {
  +                    LOG.warn(charset + " not supported");
  +                }
  +                this.requestEntity = new StringRequestEntity(
  +                        requestString);
  +            }
           }
   
           return this.requestEntity;
  
  
  
  1.3       +33 -18    jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/StringRequestEntity.java
  
  Index: StringRequestEntity.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/StringRequestEntity.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- StringRequestEntity.java	13 May 2004 02:26:08 -0000	1.2
  +++ StringRequestEntity.java	3 Jul 2004 14:27:03 -0000	1.3
  @@ -32,8 +32,7 @@
   
   import java.io.IOException;
   import java.io.OutputStream;
  -import java.io.OutputStreamWriter;
  -import java.io.Writer;
  +import java.io.UnsupportedEncodingException;
   
   import org.apache.commons.httpclient.HeaderElement;
   import org.apache.commons.httpclient.NameValuePair;
  @@ -46,7 +45,7 @@
   public class StringRequestEntity implements RequestEntity {
   
       /** The content */
  -    private String content;
  +    private byte[] content;
       
       /** The charset */
       private String charset;
  @@ -61,7 +60,13 @@
        * @param content The content to set.
        */
       public StringRequestEntity(String content) {
  -        this(content, null, null);
  +        super();
  +        if (content == null) {
  +            throw new IllegalArgumentException("The content cannot be null");
  +        }
  +        this.contentType = null;
  +        this.charset = null;
  +        this.content = content.getBytes();
       }
   
       /**
  @@ -75,13 +80,13 @@
        *   content to bytes.  If the content type does not contain a charset and charset
is not null,
        *   then the charset will be appended to the content type.
        */
  -    public StringRequestEntity(String content, String contentType, String charset) {
  +    public StringRequestEntity(String content, String contentType, String charset) 
  +        throws UnsupportedEncodingException {
           super();
           if (content == null) {
               throw new IllegalArgumentException("The content cannot be null");
           }
           
  -        this.content = content;
           this.contentType = contentType;
           this.charset = charset;
           
  @@ -103,6 +108,11 @@
                   this.contentType = contentType + "; charset=" + charset; 
               }
           }
  +        if (this.charset != null) {
  +            this.content = content.getBytes(this.charset);
  +        } else {
  +            this.content = content.getBytes();
  +        }
       }
   
       /* (non-Javadoc)
  @@ -123,28 +133,33 @@
        * @see org.apache.commons.httpclient.RequestEntity#writeRequest(java.io.OutputStream)
        */
       public void writeRequest(OutputStream out) throws IOException {
  -        Writer writer = null;
  -        if (this.charset != null) {
  -            writer = new OutputStreamWriter(out, this.charset); 
  -        } else {
  -            writer = new OutputStreamWriter(out); 
  +        if (out == null) {
  +            throw new IllegalArgumentException("Output stream may not be null");
           }
  -        writer.write(content);
  -        writer.flush();
  +        out.write(this.content);
  +        out.flush();
       }
   
       /**
        * @return The length of the content.
        */
       public long getContentLength() {
  -        return content.length();
  +        return this.content.length;
       }
   
       /**
        * @return Returns the content.
        */
       public String getContent() {
  -        return this.content;
  +        if (this.charset != null) {
  +            try {
  +                return new String(this.content, this.charset);
  +            } catch (UnsupportedEncodingException e) {
  +                return new String(this.content);
  +            }
  +        } else {
  +            return new String(this.content);
  +        }
       }
   
       /**
  
  
  
  1.10      +9 -0      jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodCharEncoding.java
  
  Index: TestMethodCharEncoding.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodCharEncoding.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- TestMethodCharEncoding.java	12 May 2004 20:43:54 -0000	1.9
  +++ TestMethodCharEncoding.java	3 Jul 2004 14:27:03 -0000	1.10
  @@ -277,4 +277,13 @@
           assertEquals(ch_msg, params.get("ch"));
       }
       
  +    public void testRequestEntityLength() throws IOException {
  +        String s = constructString(SWISS_GERMAN_STUFF_UNICODE);
  +        RequestEntity requestentity =
  +            new StringRequestEntity(s, "text/plain", CHARSET_UTF8);
  +        assertEquals(
  +                s.getBytes(CHARSET_UTF8).length, 
  +                requestentity.getContentLength()); 
  +    }
  +    
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message