KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > http > CookieHelper


1 /*
2  * $Id: CookieHelper.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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 org.apache.commons.httpclient.Cookie;
14 import org.apache.commons.httpclient.Header;
15 import org.apache.commons.httpclient.HeaderElement;
16 import org.apache.commons.httpclient.NameValuePair;
17 import org.apache.commons.httpclient.cookie.CookiePolicy;
18 import org.apache.commons.httpclient.cookie.CookieSpec;
19 import org.apache.commons.httpclient.cookie.MalformedCookieException;
20 import org.apache.commons.httpclient.cookie.NetscapeDraftSpec;
21 import org.apache.commons.httpclient.cookie.RFC2109Spec;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27
28 /**
29  * Helper functions for parsing cookie headers
30  *
31  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
32  * @version $Revision: 3798 $
33  */

34 public class CookieHelper
35 {
36
37     /**
38      * logger used by this class
39      */

40     protected static final Log logger = LogFactory.getLog(CookieHelper.class);
41
42     public static CookieSpec getCookieSpec(String JavaDoc spec)
43     {
44         if (spec != null && spec.equalsIgnoreCase(HttpConnector.COOKIE_SPEC_NETSCAPE))
45         {
46             return new NetscapeDraftSpec();
47         }
48         else
49         {
50             return new RFC2109Spec();
51         }
52     }
53
54     public static String JavaDoc getCookiePolicy(String JavaDoc spec)
55     {
56         if (spec != null && spec.equalsIgnoreCase(HttpConnector.COOKIE_SPEC_NETSCAPE))
57         {
58             return CookiePolicy.NETSCAPE;
59         }
60         else
61         {
62             return CookiePolicy.RFC_2109;
63         }
64     }
65
66     public static Cookie[] parseCookies(Header header, String JavaDoc spec) throws MalformedCookieException
67     {
68         List JavaDoc cookies = new ArrayList JavaDoc();
69         CookieSpec cookieSpec = getCookieSpec(spec);
70         HeaderElement[] headerElements = header.getElements();
71
72         for (int j = 0; j < headerElements.length; j++)
73         {
74             HeaderElement headerElement = headerElements[j];
75             NameValuePair[] headerElementParameters = headerElement.getParameters();
76             Cookie cookie = new Cookie();
77
78             for (int k = 0; k < headerElementParameters.length; k++)
79             {
80                 NameValuePair nameValuePair = headerElementParameters[k];
81                 cookieSpec.parseAttribute(nameValuePair, cookie);
82             }
83
84             if (cookie.isExpired())
85             {
86                 if (logger.isDebugEnabled())
87                 {
88                     logger.debug("Cookie: " + cookie.toString() + " has expired, not adding it.");
89                 }
90             }
91             else
92             {
93                 cookies.add(cookie);
94             }
95         }
96
97         return (Cookie[])cookies.toArray(new Cookie[cookies.size()]);
98     }
99
100 }
101
Popular Tags