View Javadoc

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