You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cactus-dev@jakarta.apache.org by cm...@apache.org on 2003/05/06 10:07:10 UTC

cvs commit: jakarta-cactus/framework/src/java/share/org/apache/cactus/client/connector/http HttpClientConnectionHelper.java AbstractConnectionHelper.java

cmlenz      2003/05/06 01:07:10

  Modified:    samples/servlet/src/test-cactus/share/org/apache/cactus/sample/unit
                        Tag: CACTUS_14_ANT_BRANCH TestCookie.java
               framework/src/java/share/org/apache/cactus/client/connector/http
                        Tag: CACTUS_14_ANT_BRANCH
                        HttpClientConnectionHelper.java
                        AbstractConnectionHelper.java
  Log:
  Port fixes from HEAD
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.3.2.1   +3 -2      jakarta-cactus/samples/servlet/src/test-cactus/share/org/apache/cactus/sample/unit/TestCookie.java
  
  Index: TestCookie.java
  ===================================================================
  RCS file: /home/cvs/jakarta-cactus/samples/servlet/src/test-cactus/share/org/apache/cactus/sample/unit/TestCookie.java,v
  retrieving revision 1.3
  retrieving revision 1.3.2.1
  diff -u -r1.3 -r1.3.2.1
  --- TestCookie.java	17 Mar 2003 19:30:32 -0000	1.3
  +++ TestCookie.java	6 May 2003 08:07:10 -0000	1.3.2.1
  @@ -88,12 +88,13 @@
       {
           javax.servlet.http.Cookie[] cookies = request.getCookies();
   
  +        assertNotNull("No cookies in request", cookies);
  +
           for (int i = 0; i < cookies.length; i++)
           {
               if (cookies[i].getName().equals("testcookie"))
               {
                   assertEquals("user&pwd", cookies[i].getValue());
  -
                   return;
               }
           }
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.3.2.1   +7 -9      jakarta-cactus/framework/src/java/share/org/apache/cactus/client/connector/http/HttpClientConnectionHelper.java
  
  Index: HttpClientConnectionHelper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/client/connector/http/HttpClientConnectionHelper.java,v
  retrieving revision 1.3
  retrieving revision 1.3.2.1
  diff -u -r1.3 -r1.3.2.1
  --- HttpClientConnectionHelper.java	19 Mar 2003 17:08:35 -0000	1.3
  +++ HttpClientConnectionHelper.java	6 May 2003 08:07:10 -0000	1.3.2.1
  @@ -71,6 +71,7 @@
   import org.apache.cactus.util.UrlUtil;
   import org.apache.commons.httpclient.HostConfiguration;
   import org.apache.commons.httpclient.HttpClient;
  +import org.apache.commons.httpclient.HttpState;
   import org.apache.commons.httpclient.NameValuePair;
   import org.apache.commons.httpclient.methods.GetMethod;
   import org.apache.commons.httpclient.methods.PostMethod;
  @@ -151,14 +152,6 @@
           // Add the other header fields
           addHeaders(theRequest);
   
  -        // Add the cookies
  -        String cookieString = getCookieString(theRequest, url);
  -
  -        if (cookieString != null)
  -        {
  -            this.method.addRequestHeader("Cookie", cookieString);
  -        }
  -
           // Add the POST parameters if no user data has been specified (user data
           // overried post parameters)
           if (theRequest.getUserData() != null)
  @@ -170,11 +163,16 @@
               addParametersPost(theRequest);
           }
   
  +        // Add the cookies
  +        HttpState state = new HttpState();
  +        state.addCookies(createHttpClientCookies(theRequest, url));
  +
           // Open the connection and get the result
           HttpClient client = new HttpClient();
           HostConfiguration hostConfiguration = new HostConfiguration();
           hostConfiguration.setHost(url.getHost(), url.getPort(),
               Protocol.getProtocol(url.getProtocol()));
  +        client.setState(state);
           client.executeMethod(hostConfiguration, this.method);
   
           // Wrap the HttpClient method in a java.net.HttpURLConnection object
  
  
  
  1.2.2.1   +78 -41    jakarta-cactus/framework/src/java/share/org/apache/cactus/client/connector/http/AbstractConnectionHelper.java
  
  Index: AbstractConnectionHelper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/client/connector/http/AbstractConnectionHelper.java,v
  retrieving revision 1.2
  retrieving revision 1.2.2.1
  diff -u -r1.2 -r1.2.2.1
  --- AbstractConnectionHelper.java	19 Mar 2003 17:31:54 -0000	1.2
  +++ AbstractConnectionHelper.java	6 May 2003 08:07:10 -0000	1.2.2.1
  @@ -176,46 +176,7 @@
           {
               // transform the Cactus cookies into HttpClient cookies
               org.apache.commons.httpclient.Cookie[] httpclientCookies = 
  -                new org.apache.commons.httpclient.Cookie[cookies.size()];
  -
  -            for (int i = 0; i < cookies.size(); i++)
  -            {
  -                Cookie cactusCookie = (Cookie) cookies.elementAt(i);
  -
  -                // If no domain has been specified, use a default one
  -                String domain;
  -
  -                if (cactusCookie.getDomain() == null)
  -                {
  -                    domain = Cookie.getCookieDomain(theRequest, 
  -                        theUrl.getHost());
  -                }
  -                else
  -                {
  -                    domain = cactusCookie.getDomain();
  -                }
  -
  -                // If not path has been specified , use a default one
  -                String path;
  -
  -                if (cactusCookie.getPath() == null)
  -                {
  -                    path = Cookie.getCookiePath(theRequest, theUrl.getFile());
  -                }
  -                else
  -                {
  -                    path = cactusCookie.getPath();
  -                }
  -
  -                httpclientCookies[i] = new org.apache.commons.httpclient.Cookie(
  -                    domain, cactusCookie.getName(), cactusCookie.getValue());
  -
  -                httpclientCookies[i].setComment(cactusCookie.getComment());
  -                httpclientCookies[i].setExpiryDate(
  -                    cactusCookie.getExpiryDate());
  -                httpclientCookies[i].setPath(path);
  -                httpclientCookies[i].setSecure(cactusCookie.isSecure());
  -            }
  +                createHttpClientCookies(theRequest, theUrl);
   
               // and create the cookie header to send
               Header cookieHeader = createCookieHeader(
  @@ -230,6 +191,81 @@
       }
   
       /**
  +     * Transforms an array of Cactus cookies into an array of Commons-HttpClient
  +     * cookies, using information from the request and URL.
  +     * 
  +     * @param theRequest The request
  +     * @param theUrl The URL
  +     * @param theCactusCookies The array of Cactus Cookies
  +     * @return The array of HttpClient cookies
  +     */
  +    protected org.apache.commons.httpclient.Cookie[] createHttpClientCookies(
  +        WebRequest theRequest, URL theUrl)
  +    {
  +        Vector cactusCookies = theRequest.getCookies();
  +        
  +        // transform the Cactus cookies into HttpClient cookies
  +        org.apache.commons.httpclient.Cookie[] httpclientCookies = 
  +            new org.apache.commons.httpclient.Cookie[cactusCookies.size()];
  +
  +        for (int i = 0; i < cactusCookies.size(); i++)
  +        {
  +            Cookie cactusCookie = (Cookie) cactusCookies.elementAt(i);
  +            httpclientCookies[i] =
  +                createHttpClientCookie(theRequest, theUrl, cactusCookie);
  +        }
  +
  +        return httpclientCookies;
  +    }
  +
  +    /**
  +     * Create a Commons-HttpClient cookie from a Cactus cookie, with information
  +     * from the web request and the URL.
  +     * 
  +     * @param theRequest The request
  +     * @param theUrl The URL
  +     * @param theCactusCookie The Cactus Cookie object
  +     * @return The HttpClient cookie
  +     */
  +    protected org.apache.commons.httpclient.Cookie createHttpClientCookie(
  +        WebRequest theRequest, URL theUrl, Cookie theCactusCookie)
  +    {
  +        // If no domain has been specified, use a default one
  +        String domain;
  +        if (theCactusCookie.getDomain() == null)
  +        {
  +            domain = Cookie.getCookieDomain(theRequest, theUrl.getHost());
  +        }
  +        else
  +        {
  +            domain = theCactusCookie.getDomain();
  +        }
  +
  +        // If not path has been specified , use a default one
  +        String path;
  +        if (theCactusCookie.getPath() == null)
  +        {
  +            path = Cookie.getCookiePath(theRequest, theUrl.getFile());
  +        }
  +        else
  +        {
  +            path = theCactusCookie.getPath();
  +        }
  +
  +        // Assemble the HttpClient cookie
  +        org.apache.commons.httpclient.Cookie httpclientCookie =
  +            new org.apache.commons.httpclient.Cookie(domain,
  +                theCactusCookie.getName(), theCactusCookie.getValue());
  +        httpclientCookie.setComment(theCactusCookie.getComment());
  +        httpclientCookie.setExpiryDate(
  +            theCactusCookie.getExpiryDate());
  +        httpclientCookie.setPath(path);
  +        httpclientCookie.setSecure(theCactusCookie.isSecure());
  +        
  +        return httpclientCookie;
  +    }
  +
  +    /**
        * Create a HttpClient {@link Header} for cookies that matches
        * the domain and path.
        * 
  @@ -275,4 +311,5 @@
           
           return cookieHeader;
       }
  +
   }
  
  
  

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