You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by di...@apache.org on 2005/06/14 03:45:34 UTC

cvs commit: ws-axis/java/src/org/apache/axis/transport/http CommonsHTTPSender.java HTTPSender.java

dims        2005/06/13 18:45:34

  Modified:    java/src/org/apache/axis/transport/http
                        CommonsHTTPSender.java HTTPSender.java
  Log:
  Fix for AXIS-895 - Axis doesn't maintain more than one cookie with http
  from  Gerry Gao
  
  Revision  Changes    Path
  1.35      +49 -14    ws-axis/java/src/org/apache/axis/transport/http/CommonsHTTPSender.java
  
  Index: CommonsHTTPSender.java
  ===================================================================
  RCS file: /home/cvs/ws-axis/java/src/org/apache/axis/transport/http/CommonsHTTPSender.java,v
  retrieving revision 1.34
  retrieving revision 1.35
  diff -u -r1.34 -r1.35
  --- CommonsHTTPSender.java	10 Jun 2005 06:30:44 -0000	1.34
  +++ CommonsHTTPSender.java	14 Jun 2005 01:45:34 -0000	1.35
  @@ -256,25 +256,14 @@
               // handle cookies (if any)
               if (msgContext.getMaintainSession()) {
                   Header[] headers = method.getResponseHeaders();
  -                ArrayList cookies = new ArrayList();
  -                ArrayList cookies2 = new ArrayList();
  +
                   for (int i = 0; i < headers.length; i++) {
                       if (headers[i].getName().equalsIgnoreCase(HTTPConstants.HEADER_SET_COOKIE)) {
  -                        cookies.add(cleanupCookie(headers[i].getValue()));
  +                        handleCookie(HTTPConstants.HEADER_COOKIE, headers[i].getValue(), msgContext);
                       } else if (headers[i].getName().equalsIgnoreCase(HTTPConstants.HEADER_SET_COOKIE2)) {
  -                        cookies2.add(cleanupCookie(headers[i].getValue()));
  +                        handleCookie(HTTPConstants.HEADER_COOKIE2, headers[i].getValue(), msgContext);
                       }
                   }
  -                if(cookies.size()==1) {
  -                    msgContext.setProperty(HTTPConstants.HEADER_COOKIE, cookies.get(0));
  -                } else if (cookies.size() > 1) {
  -                    msgContext.setProperty(HTTPConstants.HEADER_COOKIE, cookies.toArray(new String[cookies.size()]));
  -                }
  -                if(cookies2.size()==1) {
  -                    msgContext.setProperty(HTTPConstants.HEADER_COOKIE2, cookies2.get(0));
  -                } else if (cookies2.size() > 1) {
  -                    msgContext.setProperty(HTTPConstants.HEADER_COOKIE2, cookies2.toArray(new String[cookies2.size()]));
  -                }
               }
   
               // always release the connection back to the pool if 
  @@ -295,6 +284,52 @@
       }
   
       /**
  +     * little helper function for cookies. fills up the message context with
  +     * a string or an array of strings (if there are more than one Set-Cookie)
  +     *
  +     * @param cookieName
  +     * @param setCookieName
  +     * @param cookie
  +     * @param msgContext
  +     */
  +    public void handleCookie(String cookieName, String cookie,
  +            MessageContext msgContext) {
  +        
  +        cookie = cleanupCookie(cookie);
  +        int keyIndex = cookie.indexOf("=");
  +        String key = cookie.substring(0, keyIndex);
  +        
  +        ArrayList cookies = new ArrayList();
  +        Object oldCookies = msgContext.getProperty(cookieName);
  +        boolean alreadyExist = false;
  +        if(oldCookies != null) {
  +            if(oldCookies instanceof String[]) {
  +                String[] oldCookiesArray = (String[])oldCookies;
  +                for(int i = 0; i < oldCookiesArray.length; i++) {
  +                    String anOldCookie = oldCookiesArray[i];
  +                    if (anOldCookie.indexOf(key) == 0) { // same cookie key
  +                        anOldCookie = cookie;             // update to new one
  +                        alreadyExist = true;
  +                    }
  +                    cookies.add(anOldCookie);
  +                }
  +            } else {
  +                cookies.add((String)oldCookies);
  +            }
  +        }
  +        
  +        if (!alreadyExist) {
  +            cookies.add(cookie);
  +        }
  +        
  +        if(cookies.size()==1) {
  +            msgContext.setProperty(cookieName, cookies.get(0));
  +        } else if (cookies.size() > 1) {
  +            msgContext.setProperty(cookieName, cookies.toArray(new String[cookies.size()]));
  +        }
  +    }
  +    
  +    /**
        * Add cookies from message context
        *
        * @param msgContext
  
  
  
  1.131     +38 -12    ws-axis/java/src/org/apache/axis/transport/http/HTTPSender.java
  
  Index: HTTPSender.java
  ===================================================================
  RCS file: /home/cvs/ws-axis/java/src/org/apache/axis/transport/http/HTTPSender.java,v
  retrieving revision 1.130
  retrieving revision 1.131
  diff -u -r1.130 -r1.131
  --- HTTPSender.java	8 Jun 2005 17:36:59 -0000	1.130
  +++ HTTPSender.java	14 Jun 2005 01:45:34 -0000	1.131
  @@ -648,11 +648,9 @@
                       if (msgContext.getMaintainSession()) {
                           final String nameLowerCase = name.toLowerCase();
                           if (nameLowerCase.equalsIgnoreCase(HTTPConstants.HEADER_SET_COOKIE)) {
  -                            handleCookie(HTTPConstants.HEADER_COOKIE,
  -                                    HTTPConstants.HEADER_SET_COOKIE, value, msgContext);
  +                            handleCookie(HTTPConstants.HEADER_COOKIE, null, value, msgContext);
                           } else if (nameLowerCase.equalsIgnoreCase(HTTPConstants.HEADER_SET_COOKIE2)) {
  -                            handleCookie(HTTPConstants.HEADER_COOKIE2,
  -                                    HTTPConstants.HEADER_SET_COOKIE2, value, msgContext);
  +                            handleCookie(HTTPConstants.HEADER_COOKIE2, null, value, msgContext);
                           } else {
                               headers.put(name.toLowerCase(), value);
                           }
  @@ -812,28 +810,56 @@
        */
       public void handleCookie(String cookieName, String setCookieName,
                                String cookie, MessageContext msgContext) {
  +
  +        cookie = cleanupCookie(cookie);
  +        int keyIndex = cookie.indexOf("=");
  +        String key = cookie.substring(0, keyIndex);
  +        
           ArrayList cookies = new ArrayList();
           Object oldCookies = msgContext.getProperty(cookieName);
  +        boolean alreadyExist = false;
           if(oldCookies != null) {
               if(oldCookies instanceof String[]) {
  -                cookies.addAll(java.util.Arrays.asList((String[])oldCookies));
  +                
  +                String[] oldCookiesArray = (String[])oldCookies;
  +                for(int i = 0; i < oldCookiesArray.length; i++) {
  +                    String anOldCookie = oldCookiesArray[i];
  +                    if (anOldCookie.indexOf(key) == 0) { // same cookie key
  +                        anOldCookie = cookie;             // update to new one
  +                        alreadyExist = true;
  +                    }
  +                    cookies.add(anOldCookie);
  +                }
               } else {
                   cookies.add((String)oldCookies);
               }
           }
  -        cookie = cookie.trim();
  -        // chop after first ; a la Apache SOAP (see HTTPUtils.java there)
  -        int index = cookie.indexOf(';');
  -        if (index != -1) {
  -            cookie = cookie.substring(0, index);
  -        }
  -        if(cookies.indexOf(cookie)==-1) {
  +        
  +        if (!alreadyExist) {
               cookies.add(cookie);
           }
  +        
           if(cookies.size()==1) {
               msgContext.setProperty(cookieName, cookies.get(0));
           } else if (cookies.size() > 1) {
               msgContext.setProperty(cookieName, cookies.toArray(new String[cookies.size()]));
           }
       }
  +    
  +    /**
  +     * cleanup the cookie value.
  +     *
  +     * @param cookie initial cookie value
  +     *
  +     * @return a cleaned up cookie value.
  +     */
  +    private String cleanupCookie(String cookie) {
  +        cookie = cookie.trim();
  +        // chop after first ; a la Apache SOAP (see HTTPUtils.java there)
  +        int index = cookie.indexOf(';');
  +        if (index != -1) {
  +            cookie = cookie.substring(0, index);
  +        }
  +        return cookie;
  +    }
   }