If you think the Android project khandroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright (C) 2012-2014 Ognyan Bankov
*/*www.java2s.com*/
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/package com.github.khandroid.http.functionality;
import java.util.Date;
import java.util.List;
import khandroid.ext.apache.http.client.CookieStore;
import khandroid.ext.apache.http.cookie.Cookie;
import khandroid.ext.apache.http.impl.client.AbstractHttpClient;
import khandroid.ext.apache.http.impl.client.BasicCookieStore;
publicclass HttpFunctionalityWCookiesImpl extends HttpFunctionalityImpl implements
HttpFunctionalityWCookies {
private CookieStore mCookies;
public HttpFunctionalityWCookiesImpl(AbstractHttpClient httpClient) {
super(httpClient);
mCookies = new BasicCookieStore();
httpClient.setCookieStore(mCookies);
}
public HttpFunctionalityWCookiesImpl(AbstractHttpClient httpClient, CookieStore cookies) {
super(httpClient);
if (cookies != null) {
mCookies = cookies;
httpClient.setCookieStore(mCookies);
} else {
thrownew IllegalArgumentException("Parameter cookies is null");
}
}
@Override
publicboolean cookieExists(Cookie cookie) {
boolean ret = false;
List<Cookie> l = mCookies.getCookies();
for (Cookie c : l) {
if (c.equals(cookie)) {
ret = true;
break;
}
}
return ret;
}
/**
* Adds new cookie or replaces existing
*
* @param cookie
*/
@Override
publicvoid setCookie(Cookie cookie) {
mCookies.addCookie(cookie);
}
@Override
publicboolean cookieExists(String name, String domain, String path) {
boolean ret = false;
List<Cookie> l = mCookies.getCookies();
for (Cookie c : l) {
if (c.getName().equals(name) && c.getDomain().equals(domain)
&& c.getPath().equals(path)) {
ret = true;
break;
}
}
return ret;
}
public Cookie getCookie(String name, String domain, String path) {
Cookie ret = null;
List<Cookie> l = getHttpClient().getCookieStore().getCookies();
for (Cookie c : l) {
if (c.getName().equals(name) && c.getDomain().equals(domain)
&& c.getPath().equals(path)) {
ret = c;
break;
}
}
return ret;
}
/**
* Returns first cookie with the given name
*
* Usually you work with one and the same site so all of the cookies have same domain and path.
* In such case it is save to retrieve cookie just by name.
* However, if you work with several sites/paths this method is not safe because it will return the first cookie
* with the given name which may not be what you want.
*
* @param name
* @return Value of the cookie or <code>null</code> if not found
*/public Cookie getCookie(String name) {
Cookie ret = null;
List<Cookie> l = getHttpClient().getCookieStore().getCookies();
for (Cookie c : l) {
if (c.getName().equals(name)) {
ret = c;
break;
}
}
return ret;
}
public String getCookieValue(String name, String domain, String path) {
String ret = null;
Cookie c = getCookie(name, domain, path);
if (c != null) {
ret = c.getValue();
}
return ret;
}
/**
* Not safe. @see #getCookie(String)
*/public String getCookieValue(String name) {
String ret = null;
Cookie c = getCookie(name);
if (c != null) {
ret = c.getValue();
}
return ret;
}
@Override
public AbstractHttpClient getHttpClient() {
return ((AbstractHttpClient) super.getHttpClient());
}
@Override
publicvoid clearCookies() {
mCookies.clear();
}
@Override
publicvoid clearExpiredCookies(Date date) {
mCookies.clearExpired(date);
}
}