commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From oglu...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods PostMethod.java
Date Mon, 28 Oct 2002 14:20:31 GMT
oglueck     2002/10/28 06:20:31

  Modified:    httpclient/src/java/org/apache/commons/httpclient
                        HeaderElement.java HttpMethodBase.java
               httpclient/src/java/org/apache/commons/httpclient/methods
                        PostMethod.java
  Log:
  fixed encoding for PostMethod response string (contrib. by Oleg Kalnichevski)
  
  Revision  Changes    Path
  1.12      +30 -4     jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HeaderElement.java
  
  Index: HeaderElement.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HeaderElement.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- HeaderElement.java	7 Aug 2002 03:04:01 -0000	1.11
  +++ HeaderElement.java	28 Oct 2002 14:20:30 -0000	1.12
  @@ -413,5 +413,31 @@
           return pair;
       }
   
  +
  +    /**
  +     * Returns parameter with the given name, if found. Otherwise null 
  +     * is returned
  +     *
  +     * @return NameValuePair parameter with the given name
  +     */
  +
  +    public NameValuePair getParameterByName(String name) {
  +        if (name == null) {
  +            throw new NullPointerException("Name is null");
  +        } 
  +        NameValuePair found = null;
  +        if (this.parameters != null) {
  +            for(int i = 0; i < this.parameters.length; i ++) {
  +                NameValuePair current = this.parameters[ i ];
  +                if (current.getName().equalsIgnoreCase(name)) {
  +                    found = current;
  +                    break;
  +                }
  +            }
  +        }
  +        return found;
  +    }
  +
  +
   }
   
  
  
  
  1.74      +67 -7     jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java
  
  Index: HttpMethodBase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v
  retrieving revision 1.73
  retrieving revision 1.74
  diff -u -r1.73 -r1.74
  --- HttpMethodBase.java	26 Oct 2002 15:38:18 -0000	1.73
  +++ HttpMethodBase.java	28 Oct 2002 14:20:30 -0000	1.74
  @@ -64,7 +64,9 @@
   import java.io.ByteArrayInputStream;
   import java.io.ByteArrayOutputStream;
   import java.io.IOException;
  +import java.io.UnsupportedEncodingException;
   import java.io.InputStream;
  +import java.io.InputStreamReader;
   import java.io.OutputStream;
   import java.net.URL;
   import java.util.Date;
  @@ -235,6 +237,9 @@
        */
       private int maxRetries = 3;
   
  +    /** Default content encoding chatset */
  +    protected static final String DEFAULT_CHARSET = "US-ASCII";
  +
   
       //~ Constructors иииииииииииииииииииииииииииииииииииииииииииииииииииииииииии
   
  @@ -595,9 +600,21 @@
        *         <tt>null</tt>.
        */
       public String getResponseBodyAsString() {
  -        //FIXME: Take into account any character encoding information and
  -        //use the correct (rather than default) encoding.
  -        return responseAvailable() ? new String(getResponseBody()) : null;
  +        if (!responseAvailable()) {
  +            return null;
  +        }
  +        String body;
  +        try {
  +            body = new String(getResponseBody(), getResponseCharSet());
  +        }
  +        catch(UnsupportedEncodingException e) {
  +            if (log.isWarnEnabled()) {
  +                log.warn("Unsupported request body charset: " + e.getMessage());
  +            }
  +            body = new String(getResponseBody());
  +        }
  +
  +        return body;
       }
   
   
  @@ -2167,4 +2184,47 @@
               readResponse(state, connection);
           }
       }
  +
  +
  +    protected static String getContentCharSet(Header contentheader)
  +    {
  +        log.trace("enter getContentCharSet( Header contentheader )");
  +        String charset = null;
  +        if (contentheader != null){
  +            try {
  +                HeaderElement values[] = contentheader.getValues();
  +                // I expect only one header element to be there
  +                // No more. no less
  +                if (values.length == 1) {
  +                    NameValuePair param = values[0].getParameterByName("charset");
  +                    if (param != null) {
  +                        // If I get anything "funny" UnsupportedEncondingException will
result
  +                        charset = param.getValue();
  +                    }
  +                }
  +            }
  +            catch(HttpException e){
  +                log.error(e);
  +            }
  +        }
  +        if (charset == null) {
  +            if (log.isDebugEnabled()) {
  +                log.debug("Default charset used: " + DEFAULT_CHARSET);
  +            }
  +            charset = DEFAULT_CHARSET;
  +        }
  +        return charset;
  +    }
  +
  +
  +    public String getRequestCharSet() {
  +        return getContentCharSet(getRequestHeader("Content-Type"));
  +    }
  +
  +
  +    public String getResponseCharSet() {
  +        return getContentCharSet(getResponseHeader("Content-Type"));
  +    }
  +
  +
   }
  
  
  
  1.26      +21 -8     jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/PostMethod.java
  
  Index: PostMethod.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/PostMethod.java,v
  retrieving revision 1.25
  retrieving revision 1.26
  diff -u -r1.25 -r1.26
  --- PostMethod.java	26 Oct 2002 15:38:18 -0000	1.25
  +++ PostMethod.java	28 Oct 2002 14:20:30 -0000	1.26
  @@ -64,6 +64,7 @@
   import java.io.ByteArrayInputStream;
   import java.io.ByteArrayOutputStream;
   import java.io.IOException;
  +import java.io.UnsupportedEncodingException;
   import java.io.InputStream;
   import java.io.OutputStream;
   import java.util.Iterator;
  @@ -331,14 +332,11 @@
        * altered until I am {@link #recycle recycled}.
        * </p>
        * 
  -     * @param body DOCUMENT ME!
  +     * @param body Request content as a string
        * 
        * @throws IllegalStateException if request params have been added
        * 
        * @since 2.0
  -     * @deprecated This method converts characters to bytes in a platform
  -     *             dependent encoding. Use {@link
  -     *             #setRequestBody(java.io.InputStream)} instead.
        */
       public void setRequestBody(String body) {
           log.trace("enter PostMethod.setRequestBody(String)");
  @@ -348,7 +346,22 @@
                   "Request parameters have already been added.");
           }
   
  -        requestBody = new ByteArrayInputStream(body.getBytes());
  +        if (body == null) {
  +            this.requestBody = null;
  +            return;
  +        }
  +
  +        byte[] tmp = null;
  +        try {
  +            tmp = body.getBytes(getRequestCharSet());
  +        }
  +        catch(UnsupportedEncodingException e) {
  +            if (log.isWarnEnabled()) {
  +                log.warn("Unsupported request body charset: " + e.getMessage());
  +            }
  +            tmp = body.getBytes();
  +        }
  +        this.requestBody = new ByteArrayInputStream(tmp);
       }
   
       /**
  
  
  

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


Mime
View raw message