1 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 ; 26 import java.util.List ; 27 28 34 public class CookieHelper 35 { 36 37 40 protected static final Log logger = LogFactory.getLog(CookieHelper.class); 41 42 public static CookieSpec getCookieSpec(String 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 getCookiePolicy(String 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 spec) throws MalformedCookieException 67 { 68 List cookies = new ArrayList (); 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 |