1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.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 | |
|
30 | |
|
31 | |
|
32 | |
public class CookieHelper |
33 | |
{ |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 0 | protected static final Log logger = LogFactory.getLog(CookieHelper.class); |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
private CookieHelper() |
44 | 0 | { |
45 | |
|
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 | 0 | if (spec != null && spec.equalsIgnoreCase(HttpConnector.COOKIE_SPEC_NETSCAPE)) |
63 | |
{ |
64 | 0 | return CookiePolicy.NETSCAPE; |
65 | |
} |
66 | |
else |
67 | |
{ |
68 | 0 | 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 | |
} |