|
// requires StringUtils -> better way? |
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import java.io.IOException; |
|
import java.net.CookieManager; |
|
import java.net.CookiePolicy; |
|
import java.net.CookieStore; |
|
import java.net.URI; |
|
import java.util.Arrays; |
|
import java.util.HashMap; |
|
import java.util.List; |
|
import java.util.Map; |
|
|
|
// http://stackoverflow.com/questions/18057624/two-way-sync-for-cookies-between-httpurlconnection-java-net-cookiemanager-and |
|
|
|
public class WebkitCookieManagerProxy extends CookieManager |
|
{ |
|
private android.webkit.CookieManager webkitCookieManager; |
|
|
|
public WebkitCookieManagerProxy() |
|
{ |
|
this(null, null); |
|
} |
|
|
|
public WebkitCookieManagerProxy(CookieStore store, CookiePolicy cookiePolicy) |
|
{ |
|
super(null, cookiePolicy); |
|
|
|
this.webkitCookieManager = android.webkit.CookieManager.getInstance(); |
|
} |
|
|
|
@Override |
|
public void put(URI uri, Map<String, List<String>> responseHeaders) throws IOException |
|
{ |
|
// make sure our args are valid |
|
if ((uri == null) || (responseHeaders == null)) return; |
|
|
|
// save our url once |
|
String url = uri.toString(); |
|
|
|
// go over the headers |
|
for (String headerKey : responseHeaders.keySet()) |
|
{ |
|
// ignore headers which aren't cookie related |
|
if ((headerKey == null) || !(headerKey.equalsIgnoreCase("Set-Cookie2") || headerKey.equalsIgnoreCase("Set-Cookie"))) continue; |
|
|
|
// process each of the headers |
|
for (String headerValue : responseHeaders.get(headerKey)) |
|
{ |
|
this.webkitCookieManager.setCookie(url, headerValue); |
|
} |
|
} |
|
} |
|
|
|
@Override |
|
public Map<String, List<String>> get(URI uri, Map<String, List<String>> requestHeaders) throws IOException |
|
{ |
|
// make sure our args are valid |
|
if ((uri == null) || (requestHeaders == null)) throw new IllegalArgumentException("Argument is null"); |
|
|
|
// save our url once |
|
String url = uri.toString(); |
|
|
|
// prepare our response |
|
Map<String, List<String>> res = new java.util.HashMap<String, List<String>>(); |
|
|
|
// get the cookie |
|
String cookie = this.webkitCookieManager.getCookie(url); |
|
|
|
// return it |
|
if (cookie != null) res.put("Cookie", Arrays.asList(cookie)); |
|
return res; |
|
} |
|
|
|
public String get(String url, String cookieName) { |
|
String cookieValues = this.webkitCookieManager.getCookie(url); |
|
if (cookieValues != null) { |
|
String[] cookies = cookieValues.split(";"); |
|
// Put all Cookies in a HashMap with cookieKey & cookieToken |
|
HashMap<String, String> cookieMap = new HashMap<String, String>(); |
|
for (String cookie : cookies) { |
|
String[] cs = cookie.split("="); |
|
cookieMap.put(cs[0].trim(), StringUtils.strip(cs[1].trim(), "\"")); |
|
} |
|
if (cookieMap.containsKey(cookieName)) { |
|
String cookieValue = cookieMap.get(cookieName); |
|
return cookieValue; |
|
} |
|
} |
|
return null; |
|
} |
|
|
|
public void clearAllCookies() { |
|
this.webkitCookieManager.removeAllCookie(); |
|
} |
|
|
|
@Override |
|
public CookieStore getCookieStore() |
|
{ |
|
// we don't want anyone to work with this cookie store directly |
|
throw new UnsupportedOperationException(); |
|
} |
|
} |