Coverage Report - org.mule.providers.http.CookieHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
CookieHelper
12%
3/24
6%
1/16
3
 
 1  
 /*
 2  
  * $Id: CookieHelper.java 7963 2007-08-21 08:53:15Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.providers.http;
 12  
 
 13  
 import java.util.ArrayList;
 14  
 import java.util.List;
 15  
 
 16  
 import org.apache.commons.httpclient.Cookie;
 17  
 import org.apache.commons.httpclient.Header;
 18  
 import org.apache.commons.httpclient.HeaderElement;
 19  
 import org.apache.commons.httpclient.NameValuePair;
 20  
 import org.apache.commons.httpclient.cookie.CookiePolicy;
 21  
 import org.apache.commons.httpclient.cookie.CookieSpec;
 22  
 import org.apache.commons.httpclient.cookie.MalformedCookieException;
 23  
 import org.apache.commons.httpclient.cookie.NetscapeDraftSpec;
 24  
 import org.apache.commons.httpclient.cookie.RFC2109Spec;
 25  
 import org.apache.commons.logging.Log;
 26  
 import org.apache.commons.logging.LogFactory;
 27  
 
 28  
 /**
 29  
  * Helper functions for parsing cookie headers.
 30  
  * 
 31  
  */
 32  
 public class CookieHelper
 33  
 {
 34  
 
 35  
     /**
 36  
      * logger used by this class
 37  
      */
 38  4
     protected static final Log logger = LogFactory.getLog(CookieHelper.class);
 39  
 
 40  
     /**
 41  
      * Do not instantiate.
 42  
      */
 43  
     private CookieHelper ()
 44  0
     {
 45  
         // no op
 46  0
     }
 47  
 
 48  
     public static CookieSpec getCookieSpec(String spec)
 49  
     {
 50  0
         if (spec != null && spec.equalsIgnoreCase(HttpConnector.COOKIE_SPEC_NETSCAPE))
 51  
         {
 52  0
             return new NetscapeDraftSpec();
 53  
         }
 54  
         else
 55  
         {
 56  0
             return new RFC2109Spec();
 57  
         }
 58  
     }
 59  
 
 60  
     public static String getCookiePolicy(String spec)
 61  
     {
 62  12
         if (spec != null && spec.equalsIgnoreCase(HttpConnector.COOKIE_SPEC_NETSCAPE))
 63  
         {
 64  0
             return CookiePolicy.NETSCAPE;
 65  
         }
 66  
         else
 67  
         {
 68  12
             return CookiePolicy.RFC_2109;
 69  
         }
 70  
     }
 71  
 
 72  
     public static Cookie[] parseCookies(Header header, String spec) throws MalformedCookieException
 73  
     {
 74  0
         List cookies = new ArrayList();
 75  0
         CookieSpec cookieSpec = getCookieSpec(spec);
 76  0
         HeaderElement[] headerElements = header.getElements();
 77  
 
 78  0
         for (int j = 0; j < headerElements.length; j++)
 79  
         {
 80  0
             HeaderElement headerElement = headerElements[j];
 81  0
             NameValuePair[] headerElementParameters = headerElement.getParameters();
 82  0
             Cookie cookie = new Cookie();
 83  
 
 84  0
             for (int k = 0; k < headerElementParameters.length; k++)
 85  
             {
 86  0
                 NameValuePair nameValuePair = headerElementParameters[k];
 87  0
                 cookieSpec.parseAttribute(nameValuePair, cookie);
 88  
             }
 89  
 
 90  0
             if (cookie.isExpired())
 91  
             {
 92  0
                 if (logger.isDebugEnabled())
 93  
                 {
 94  0
                     logger.debug("Cookie: " + cookie.toString() + " has expired, not adding it.");
 95  
                 }
 96  
             }
 97  
             else
 98  
             {
 99  0
                 cookies.add(cookie);
 100  
             }
 101  
         }
 102  
 
 103  0
         return (Cookie[])cookies.toArray(new Cookie[cookies.size()]);
 104  
     }
 105  
 
 106  
 }