rwaldhoff    01/08/15 15:55:05

  Modified:    httpclient/src/java/org/apache/commons/httpclient Tag:
                        rlwrefactoring HttpMethod.java HttpMethodBase.java
               httpclient/src/java/org/apache/commons/httpclient/methods
                        Tag: rlwrefactoring PostMethod.java
               httpclient/src/test/org/apache/commons/httpclient Tag:
                        rlwrefactoring TestWebappParameters.java
  Log:
  Pushing setParameter/addParameter/etc. down to POST method (the only place where it 
really makes sense).  This is driven by the need to clear the query string in some 
places (like when processing redirects) but without necessarily knowing if the params 
contribute to the query string or the request body.
  
  Adding setQueryString(NameValuePair[]), etc. methods to replace some (the important 
part, i hope) of that functionality.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.5.2.6   +17 -45    
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethod.java
  
  Index: HttpMethod.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethod.java,v
  retrieving revision 1.5.2.5
  retrieving revision 1.5.2.6
  diff -u -r1.5.2.5 -r1.5.2.6
  --- HttpMethod.java   2001/08/14 20:22:52     1.5.2.5
  +++ HttpMethod.java   2001/08/15 22:55:04     1.5.2.6
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethod.java,v
 1.5.2.5 2001/08/14 20:22:52 rwaldhoff Exp $
  - * $Revision: 1.5.2.5 $
  - * $Date: 2001/08/14 20:22:52 $
  + * $Header: 
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethod.java,v
 1.5.2.6 2001/08/15 22:55:04 rwaldhoff Exp $
  + * $Revision: 1.5.2.6 $
  + * $Date: 2001/08/15 22:55:04 $
    * ====================================================================
    * Copyright (C) The Apache Software Foundation. All rights reserved.
    *
  @@ -105,53 +105,25 @@
       public void setFollowRedirects(boolean followRedirects);
   
       /**
  -     * Set the specified request (query string) parameter,
  -     * overwriting any previous value associated with the
  -     * given <i>parameterName</i>.
  -     *
  -     * @param parameterName the name of the parameter, which MUST NOT
  -     *                      be <tt>null</tt>
  -     * @param parameterValue the name of the parameter, which MAY
  -     *                       be <tt>null</tt>
  -     */
  -    public void setParameter(String parameterName, String parameterValue);
  -
  -    /**
  -     * Set the specified request (query string) parameter,
  -     * NOT overwriting any previous value associated with
  -     * the given <i>parameterName</i>.
  -     *
  -     * @param parameterName the name of the parameter, which MUST NOT
  -     *                      be <tt>null</tt>
  -     * @param parameterValue the name of the parameter, which MAY
  -     *                       be <tt>null</tt>
  -     */
  -    public void addParameter(String parameterName, String parameterValue);
  -
  -    /**
  -     * Remove all request parameters associated with
  -     * the given <i>parameterName</i>.
  -     *
  -     * @param parameterName the name of the parameter, which MUST NOT
  -     *                      be <tt>null</tt>
  -     */
  -    public void removeParameter(String parameterName);
  -
  -    /**
  -     * Remove a single instance of the specified name-value pair
  -     * from my request parameters
  -     *
  -     * @param parameterName the name of the parameter, which MUST NOT
  -     *                      be <tt>null</tt>
  -     * @param parameterValue the name of the parameter, which MAY
  -     *                       be <tt>null</tt>
  +     * Set my query string.
        */
  -    public void removeParameter(String parameterName, String parameterValue);
  +    public void setQueryString(String queryString);
   
       /**
        * Set my query string.
        */
  -    public void setQueryString(String queryString);
  +    public void setQueryString(NameValuePair[] params);
  +
  +    /**
  +     * Set my query string.
  +     * @param params A {@link Collection} of {@link NameValuePair}s
  +     */
  +    public void setQueryString(Collection params);
  +
  +    /**
  +     * Clear query string.
  +     */
  +    public void clearQueryString();
   
       /**
        * Get my query string.
  
  
  
  1.10.2.16 +46 -118   
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.10.2.15
  retrieving revision 1.10.2.16
  diff -u -r1.10.2.15 -r1.10.2.16
  --- HttpMethodBase.java       2001/08/14 23:06:17     1.10.2.15
  +++ HttpMethodBase.java       2001/08/15 22:55:04     1.10.2.16
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v
 1.10.2.15 2001/08/14 23:06:17 rwaldhoff Exp $
  - * $Revision: 1.10.2.15 $
  - * $Date: 2001/08/14 23:06:17 $
  + * $Header: 
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v
 1.10.2.16 2001/08/15 22:55:04 rwaldhoff Exp $
  + * $Revision: 1.10.2.16 $
  + * $Date: 2001/08/15 22:55:04 $
    * ====================================================================
    * Copyright (C) The Apache Software Foundation. All rights reserved.
    *
  @@ -67,7 +67,7 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Remy Maucherat</a>
    * @author Rodney Waldhoff
  - * @version $Id: HttpMethodBase.java,v 1.10.2.15 2001/08/14 23:06:17 rwaldhoff Exp $
  + * @version $Id: HttpMethodBase.java,v 1.10.2.16 2001/08/15 22:55:04 rwaldhoff Exp $
    */
   public abstract class HttpMethodBase implements HttpMethod {
   
  @@ -187,94 +187,58 @@
       }
   
       /**
  -     * Set the specified request (for GET requests, query string)
  -     * parameter, overwriting any previous value associated with the
  -     * given <i>parameterName</i>.
  -     *
  -     * @param parameterName the name of the parameter, which MUST NOT
  -     *                      be <tt>null</tt>
  -     * @param parameterValue the name of the parameter, which MAY
  -     *                       be <tt>null</tt>
  -     */
  -    public void setParameter(String parameterName, String parameterValue) {
  -        parameters.put(parameterName,parameterValue);
  +     * Set my query string.
  +     */
  +    public void setQueryString(String queryString) {
  +        this.queryString = queryString;
       }
   
  -    /**
  -     * Set the specified request (for GET requests, query string)
  -     * parameter, NOT overwriting any previous value associated
  -     * with the given <i>parameterName</i>.
  -     *
  -     * @param parameterName the name of the parameter, which MUST NOT
  -     *                      be <tt>null</tt>
  -     * @param parameterValue the name of the parameter, which MAY
  -     *                       be <tt>null</tt>
  -     */
  -    public void addParameter(String parameterName, String parameterValue) {
  -        Object old = parameters.put(parameterName,parameterValue);
  -        if(null != old) {
  -            List v = null;
  -            if(old instanceof String) {
  -                v = new ArrayList();
  -                v.add(old);
  -            } else if(old instanceof List) {
  -                v = (List)old;
  +    public void setQueryString(NameValuePair[] params) {
  +        StringBuffer buf = new StringBuffer();
  +        boolean needAmp = false;
  +        for(int i=0;i<params.length;i++) {
  +            if(needAmp) {
  +                buf.append("&");
               } else {
  -                throw new ClassCastException("Didn't expect to find " +
  -                                             old.getClass().getName() +
  -                                             " as parameter value for \"" +
  -                                             parameterName + "\"");
  +                needAmp = true;
               }
  -            v.add(parameterValue);
  -            parameters.put(parameterName,v);
  +            if(null != params[i].getName()) {
  +                buf.append(URLEncoder.encode(params[i].getName()));
  +            }
  +            if(null != params[i].getValue()) {
  +                buf.append("=");
  +                buf.append(URLEncoder.encode(params[i].getValue()));
  +            }
           }
  +        queryString = buf.toString();
       }
   
  -    /**
  -     * Remove all request parameters associated with
  -     * the given <i>parameterName</i>.
  -     *
  -     * @param parameterName the name of the parameter, which MUST NOT
  -     *                      be <tt>null</tt>
  -     */
  -    public void removeParameter(String paramName) {
  -        parameters.remove(paramName);
  -    }
  -
  -    /**
  -     * Remove a single instance of the specified name-value pair
  -     * from my request parameters
  -     *
  -     * @param parameterName the name of the parameter, which MUST NOT
  -     *                      be <tt>null</tt>
  -     * @param parameterValue the name of the parameter, which MAY
  -     *                       be <tt>null</tt>
  -     */
  -    public void removeParameter(String paramName, String paramValue) {
  -        Object old = parameters.get(paramName);
  -        if(null != old) {
  -            if(paramValue.equals(old)) {
  -                parameters.remove(paramName);
  -            } else if(old instanceof List) {
  -                List list = (List)old;
  -                if(list.remove(paramValue)) {
  -                    if(list.isEmpty()) {
  -                        parameters.remove(paramName);
  -                    } else if(list.size() == 1) {
  -                        parameters.put(paramName,list.get(0));
  -                    } else {
  -                        parameters.put(paramValue,list);
  -                    }
  +    public void setQueryString(Collection params) {
  +        StringBuffer buf = new StringBuffer();
  +        Iterator it = params.iterator();
  +        boolean needAmp = false;
  +        while(it.hasNext()) {
  +            NameValuePair pair = (NameValuePair)it.next();
  +            if(null != pair) {
  +                if(needAmp) {
  +                    buf.append("&");
  +                } else {
  +                    needAmp = true;
  +                }
  +                if(null != pair.getName()) {
  +                    buf.append(URLEncoder.encode(pair.getName()));
  +                }
  +                if(null != pair.getValue()) {
  +                    buf.append("=");
  +                    buf.append(URLEncoder.encode(pair.getValue()));
                   }
               }
           }
  +        queryString = buf.toString();
       }
   
  -    /**
  -     * Set my query string.
  -     */
  -    public void setQueryString(String queryString) {
  -        this.queryString = queryString;
  +    public void clearQueryString() {
  +        this.queryString = null;
       }
   
       /**
  @@ -592,7 +556,7 @@
        */
       protected void writeRequestLine(State state, HttpConnection conn) throws 
IOException, HttpException {
           log.debug("HttpMethodBase.writeRequestLine(State,HttpConnection)");
  -        String requestLine = HttpMethodBase.generateRequestLine(conn, 
getName(),getPath(),queryString,parameters,(http11 ? "HTTP/1.1" : "HTTP/1.0"));
  +        String requestLine = HttpMethodBase.generateRequestLine(conn, 
getName(),getPath(),getQueryString(),(http11 ? "HTTP/1.1" : "HTTP/1.0"));
           conn.print(requestLine);
       }
   
  @@ -1004,7 +968,6 @@
       public void recycle() {
           path = null;
           followRedirects = false;
  -        parameters.clear();
           queryString = null;
           requestHeaders.clear();
           responseHeaders.clear();
  @@ -1049,14 +1012,13 @@
           }
       }
   
  -
       // ------------------------------------------------- Static Utility Methods
   
       /**
        * Generate an HTTP/S request line according to
        * the specified attributes.
        */
  -    protected static String generateRequestLine(HttpConnection connection, String 
name, String path, String queryString, HashMap queryParams, String protocol) {
  +    protected static String generateRequestLine(HttpConnection connection, String 
name, String path, String queryString, String protocol) {
           boolean addedQmark = false;
           StringBuffer buf = new StringBuffer();
           buf.append(path);
  @@ -1068,39 +1030,6 @@
               addedQmark = true;
           }
   
  -        if(null != queryParams) {
  -            Iterator it = queryParams.keySet().iterator();
  -            while(it.hasNext()) {
  -                if(!addedQmark) {
  -                    buf.append("?");
  -                    addedQmark = true;
  -                } else {
  -                    buf.append("&");
  -                }
  -                Object pname = it.next();
  -                Object pvalue = queryParams.get(pname);
  -                if(pvalue instanceof List) {
  -                    Iterator it2 = ((List)(pvalue)).iterator();
  -                    while(it2.hasNext()) {
  -                        buf.append(URLEncoder.encode(String.valueOf(pname)));
  -                        Object value2 = it2.next();
  -                        if(null != value2) {
  -                            buf.append("=");
  -                            buf.append(URLEncoder.encode(String.valueOf(value2)));
  -                        }
  -                        if(it2.hasNext()) {
  -                            buf.append("&");
  -                        }
  -                    }
  -                } else {
  -                    buf.append(URLEncoder.encode(String.valueOf(pname)));
  -                    if(null != pvalue) {
  -                        buf.append("=");
  -                        buf.append(URLEncoder.encode(String.valueOf(pvalue)));
  -                    }
  -                }
  -            }
  -        }
           if(!connection.isProxied()) {
               return (name + " " + buf.toString() + " " + protocol + "\r\n");
           } else {
  @@ -1116,7 +1045,6 @@
   
       protected String path = null;
       protected boolean followRedirects = false;
  -    protected HashMap parameters = new HashMap();
       protected String queryString = null;
       protected HashMap requestHeaders = new HashMap();
       protected HashMap responseHeaders = new HashMap();
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.3.2.6   +40 -19    
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.3.2.5
  retrieving revision 1.3.2.6
  diff -u -r1.3.2.5 -r1.3.2.6
  --- PostMethod.java   2001/08/13 23:35:29     1.3.2.5
  +++ PostMethod.java   2001/08/15 22:55:04     1.3.2.6
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/PostMethod.java,v
 1.3.2.5 2001/08/13 23:35:29 rwaldhoff Exp $
  - * $Revision: 1.3.2.5 $
  - * $Date: 2001/08/13 23:35:29 $
  + * $Header: 
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/PostMethod.java,v
 1.3.2.6 2001/08/15 22:55:04 rwaldhoff Exp $
  + * $Revision: 1.3.2.6 $
  + * $Date: 2001/08/15 22:55:04 $
    * ====================================================================
    * Copyright (C) The Apache Software Foundation. All rights reserved.
    *
  @@ -111,7 +111,7 @@
           if(null != requestBody) {
               throw new IllegalStateException("Request body already generated.");
           }
  -        super.setParameter(parameterName,parameterValue);
  +        parameters.put(parameterName,parameterValue);
       }
   
       /**
  @@ -125,7 +125,23 @@
           if(null != requestBody) {
               throw new IllegalStateException("Request body already generated.");
           }
  -        super.addParameter(parameterName,parameterValue);
  +        Object old = parameters.put(parameterName,parameterValue);
  +        if(null != old) {
  +            List v = null;
  +            if(old instanceof String) {
  +                v = new ArrayList();
  +                v.add(old);
  +            } else if(old instanceof List) {
  +                v = (List)old;
  +            } else {
  +                throw new ClassCastException("Didn't expect to find " +
  +                                             old.getClass().getName() +
  +                                             " as parameter value for \"" +
  +                                             parameterName + "\"");
  +            }
  +            v.add(parameterValue);
  +            parameters.put(parameterName,v);
  +        }
       }
   
       /**
  @@ -139,7 +155,7 @@
           if(null != requestBody) {
               throw new IllegalStateException("Request body already generated.");
           }
  -        super.removeParameter(paramName);
  +        parameters.remove(paramName);
       }
   
       /**
  @@ -153,19 +169,23 @@
           if(null != requestBody) {
               throw new IllegalStateException("Request body already generated.");
           }
  -        super.removeParameter(paramName,paramValue);
  -    }
  -
  -    /**
  -     * Override method of {@link HttpMethodBase}
  -     * to not send request parameters
  -     * as part of the query string.
  -     */
  -    protected void writeRequestLine(State state, HttpConnection conn) throws 
IOException, HttpException {
  -        // don't send parameters on query string
  -        log.debug("PostMethod.writeRequestLine(State,HttpConnection)");
  -        String requestLine = HttpMethodBase.generateRequestLine(conn, 
getName(),getPath(),queryString,null,(http11 ? "HTTP/1.1" : "HTTP/1.0"));
  -        conn.print(requestLine);
  +        Object old = parameters.get(paramName);
  +        if(null != old) {
  +            if(paramValue.equals(old)) {
  +                parameters.remove(paramName);
  +            } else if(old instanceof List) {
  +                List list = (List)old;
  +                if(list.remove(paramValue)) {
  +                    if(list.isEmpty()) {
  +                        parameters.remove(paramName);
  +                    } else if(list.size() == 1) {
  +                        parameters.put(paramName,list.get(0));
  +                    } else {
  +                        parameters.put(paramValue,list);
  +                    }
  +                }
  +            }
  +        }
       }
   
       /**
  @@ -244,4 +264,5 @@
       }
   
       protected String requestBody = null;
  +    protected HashMap parameters = new HashMap();
   }
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.2   +21 -20    
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/Attic/TestWebappParameters.java
  
  Index: TestWebappParameters.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/Attic/TestWebappParameters.java,v
  retrieving revision 1.1.2.1
  retrieving revision 1.1.2.2
  diff -u -r1.1.2.1 -r1.1.2.2
  --- TestWebappParameters.java 2001/08/13 16:02:57     1.1.2.1
  +++ TestWebappParameters.java 2001/08/15 22:55:05     1.1.2.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/Attic/TestWebappParameters.java,v
 1.1.2.1 2001/08/13 16:02:57 rwaldhoff Exp $
  - * $Revision: 1.1.2.1 $
  - * $Date: 2001/08/13 16:02:57 $
  + * $Header: 
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/Attic/TestWebappParameters.java,v
 1.1.2.2 2001/08/15 22:55:05 rwaldhoff Exp $
  + * $Revision: 1.1.2.2 $
  + * $Date: 2001/08/15 22:55:05 $
    * ====================================================================
    * Copyright (C) The Apache Software Foundation. All rights reserved.
    *
  @@ -32,7 +32,7 @@
    * "httpclient.test.webappContext" property.
    *
    * @author Rodney Waldhoff
  - * @version $Id: TestWebappParameters.java,v 1.1.2.1 2001/08/13 16:02:57 rwaldhoff 
Exp $
  + * @version $Id: TestWebappParameters.java,v 1.1.2.2 2001/08/15 22:55:05 rwaldhoff 
Exp $
    */
   public class TestWebappParameters extends TestCase {
   
  @@ -118,7 +118,7 @@
           HttpClient client = new HttpClient();
           client.startSession(host, port);
           GetMethod method = new GetMethod("/" + context + "/params");
  -        method.addParameter("param-one","param-value");
  +        method.setQueryString(new NameValuePair[] { new 
NameValuePair("param-one","param-value") });
           method.setUseDisk(false);
           try {
               client.executeMethod(method);
  @@ -139,9 +139,11 @@
           HttpClient client = new HttpClient();
           client.startSession(host, port);
           GetMethod method = new GetMethod("/" + context + "/params");
  -        method.addParameter("param-one","param-value");
  -        method.addParameter("param-two","param-value2");
  -        method.addParameter("special-chars",":/?~.");
  +        method.setQueryString(new NameValuePair[] {
  +                                new NameValuePair("param-one","param-value"),
  +                                new NameValuePair("param-two","param-value2"),
  +                                new NameValuePair("special-chars",":/?~.")
  +                              });
           method.setUseDisk(false);
           try {
               client.executeMethod(method);
  @@ -164,7 +166,7 @@
           HttpClient client = new HttpClient();
           client.startSession(host, port);
           GetMethod method = new GetMethod("/" + context + "/params");
  -        method.addParameter("param-without-value",null);
  +        method.setQueryString(new NameValuePair[] { new 
NameValuePair("param-without-value",null) });
           method.setUseDisk(false);
           try {
               client.executeMethod(method);
  @@ -185,8 +187,10 @@
           HttpClient client = new HttpClient();
           client.startSession(host, port);
           GetMethod method = new GetMethod("/" + context + "/params");
  -        method.addParameter("foo","one");
  -        method.addParameter("foo","two");
  +        method.setQueryString(new NameValuePair[] {
  +                                  new NameValuePair("foo","one"),
  +                                  new NameValuePair("foo","two")
  +                             });
           method.setUseDisk(false);
           try {
               client.executeMethod(method);
  @@ -200,18 +204,15 @@
           assert(method.getDataAsString().indexOf("name=\"foo\";value=\"two\"") >= 0);
       }
   
  -    /**
  -     * Test that {@link GetMethod#addParameter(java.lang.String,java.lang.String)}
  -     * and {@link GetMethod#setQueryString(java.lang.String)} combine
  -     * properly.
  -     */
  -    public void testGetMethodParameterAndQueryString() throws Exception {
  +    public void testGetMethodOverwriteQueryString() throws Exception {
           HttpClient client = new HttpClient();
           client.startSession(host, port);
           GetMethod method = new GetMethod("/" + context + "/params");
           method.setQueryString("query=string");
  -        method.addParameter("param","eter");
  -        method.addParameter("para","meter");
  +        method.setQueryString(new NameValuePair[] {
  +                                  new NameValuePair("param","eter"),
  +                                  new NameValuePair("para","meter")
  +                             });
           method.setUseDisk(false);
           try {
               client.executeMethod(method);
  @@ -221,7 +222,7 @@
           }
           assert(method.getDataAsString().indexOf("<title>Param Servlet: 
GET</title>") >= 0);
           assertEquals(200,method.getStatusCode());
  -        assert(method.getDataAsString().indexOf("name=\"query\";value=\"string\"") 
>= 0);
  +        assert(method.getDataAsString().indexOf("name=\"query\";value=\"string\"") 
== -1);
           assert(method.getDataAsString().indexOf("name=\"param\";value=\"eter\"") >= 
0);
           assert(method.getDataAsString().indexOf("name=\"para\";value=\"meter\"") >= 
0);
       }
  
  
  

Reply via email to