Java Code Examples for org.apache.http.cookie.Cookie#getExpiryDate()

The following examples show how to use org.apache.http.cookie.Cookie#getExpiryDate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebFragment.java    From BigApp_Discuz_Android with Apache License 2.0 8 votes vote down vote up
@Override
    public void setCookieFromCookieStore(Context context, String url, List<Cookie> cks) {
        CookieUtils.syncCookie(context);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);

        if (!ListUtils.isNullOrContainEmpty(cks)) {
            for (int i = 0; i < cks.size(); i++) {
                Cookie cookie = cks.get(i);
                String cookieStr = cookie.getName() + "=" + cookie.getValue() + ";"
                        + "expiry=" + cookie.getExpiryDate() + ";"
                        + "domain=" + cookie.getDomain() + ";"
                        + "path=/";
//                ZogUtils.printError(WebFragment.class, "set cookie string:" + cookieStr);
                cookieManager.setCookie(url, cookieStr);//cookieStr是在HttpClient中获得的cookie

            }
        }
    }
 
Example 2
Source File: HC4ExchangeFormAuthenticator.java    From davmail with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Authenticated httpClientAdapter (with cookies).
 *
 * @return http client
 */
public org.apache.commons.httpclient.HttpClient getHttpClient() throws DavMailException {
    org.apache.commons.httpclient.HttpClient oldHttpClient;
    oldHttpClient = DavGatewayHttpClientFacade.getInstance(url);
    DavGatewayHttpClientFacade.setCredentials(oldHttpClient, username, password);
    DavGatewayHttpClientFacade.createMultiThreadedHttpConnectionManager(oldHttpClient);

    for (Cookie cookie : httpClientAdapter.getCookies()) {
        org.apache.commons.httpclient.Cookie oldCookie = new org.apache.commons.httpclient.Cookie(
                cookie.getDomain(),
                cookie.getName(),
                cookie.getValue(),
                cookie.getPath(),
                cookie.getExpiryDate(),
                cookie.isSecure());
        oldCookie.setPathAttributeSpecified(cookie.getPath() != null);
        oldHttpClient.getState().addCookie(oldCookie);
    }

    return oldHttpClient;
}
 
Example 3
Source File: MyCookieDBManager.java    From Huochexing12306 with Apache License 2.0 6 votes vote down vote up
public void saveCookie(Cookie cookie) {
	L.d("saveCookie:" + cookie);
	if (cookie == null) {
		return;
	}
	db.delete(TABLE_NAME, Column.NAME + " = ? ",
			new String[] { cookie.getName() });
	ContentValues values = new ContentValues();
	values.put(Column.VALUE, cookie.getValue());
	values.put(Column.NAME, cookie.getName());
	values.put(Column.COMMENT, cookie.getComment());
	values.put(Column.DOMAIN, cookie.getDomain());
	if (cookie.getExpiryDate() != null) {
		values.put(Column.EXPIRY_DATE, cookie.getExpiryDate().getTime());
	}
	values.put(Column.PATH, cookie.getPath());
	values.put(Column.SECURE, cookie.isSecure() ? 1 : 0);
	values.put(Column.VERSION, cookie.getVersion());

	db.insert(TABLE_NAME, null, values);
}
 
Example 4
Source File: CookieUtils.java    From BigApp_Discuz_Android with Apache License 2.0 6 votes vote down vote up
public static void setCookieFromCookieStore(Context context, String url) {
    syncCookie(context);

    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    List<Cookie> cookies = getCookies(context);
    Log.e("cookies", "cookies.size:" + cookies.size());
    if (!cookies.isEmpty()) {
        for (int i = 0; i < cookies.size(); i++) {
            Cookie cookie = cookies.get(i);
            String cookieStr = cookie.getName() + "=" + cookie.getValue() + ";"
                    + "expiry=" + cookie.getExpiryDate() + ";"
                    + "domain=" + cookie.getDomain() + ";"
                    + "path=/";

            cookieManager.setCookie(url, cookieStr);//cookies是在HttpClient中获得的cookie

        }
    }

}
 
Example 5
Source File: ClanBaseUtils.java    From BigApp_Discuz_Android with Apache License 2.0 6 votes vote down vote up
public static void printCookieStore(CookieStore cookieStore) {
    List<Cookie> cookies = cookieStore.getCookies();
    Log.e("APP", "========================================== start cookies.size:" + cookies.size());
    if (!cookies.isEmpty()) {
        for (int i = 0; i < cookies.size(); i++) {
            Cookie ck = cookies.get(i);
            String ckstr = ck.getName() + "=" + ck.getValue() + ";"
                    + "expiry=" + ck.getExpiryDate() + ";"
                    + "domain=" + ck.getDomain() + ";"
                    + "path=/";

            Log.v("APP", "cookieStr:" + ckstr);
        }
    }
    Log.e("APP", "========================================== end cookies.size:" + cookies.size());
}
 
Example 6
Source File: PreferencesCookieStore.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
@Override
public boolean clearExpired(Date date) {
    boolean clearedAny = false;
    SharedPreferences.Editor editor = cookiePrefs.edit();

    for (ConcurrentHashMap.Entry<String, Cookie> entry : cookies.entrySet()) {
        String name = entry.getKey();
        Cookie cookie = entry.getValue();
        if (cookie.getExpiryDate() == null || cookie.isExpired(date)) {
            // Remove the cookie by name
            cookies.remove(name);

            // Clear cookies from persistent store
            editor.remove(COOKIE_NAME_PREFIX + name);

            // We've cleared at least one
            clearedAny = true;
        }
    }

    // Update names in persistent store
    if (clearedAny) {
        editor.putString(COOKIE_NAME_STORE, TextUtils.join(",", cookies.keySet()));
    }
    editor.commit();

    return clearedAny;
}
 
Example 7
Source File: ImplicitFlowTest.java    From io with Apache License 2.0 5 votes vote down vote up
private Map<String, Object> getSessionMap() {
    Map<String, Object> sessionMap = new HashMap<String, Object>();
    String sessionId = null;
    Date maxAge = null;
    for (Cookie cookie : cookies) {
        if (SESSION_ID.equals(cookie.getName())) {
            sessionId = cookie.getValue();
            maxAge = cookie.getExpiryDate();
        }
    }
    sessionMap.put(SESSION_ID, sessionId);
    sessionMap.put(MAX_AGE, maxAge);

    return sessionMap;
}
 
Example 8
Source File: DefaultCookieManager.java    From esigate with Apache License 2.0 5 votes vote down vote up
private String toString(Cookie cookie) {
    StringBuilder result = new StringBuilder(Parameters.SMALL_BUFFER_SIZE);
    result.append(cookie.getName());
    result.append("=");
    result.append(cookie.getValue());
    if (cookie.getDomain() != null) {
        result.append(";domain=");
        result.append(cookie.getDomain());
    }
    if (cookie.getPath() != null) {
        result.append(";path=");
        result.append(cookie.getPath());
    }
    if (cookie.getExpiryDate() != null) {
        result.append(";expires=");
        result.append(cookie.getExpiryDate());
    }
    if (cookie.getCommentURL() != null) {
        result.append(";comment=");
        result.append(cookie.getComment());
    }
    if (cookie.getCommentURL() != null) {
        result.append(";comment=");
        result.append(cookie.getCommentURL());
    }
    return result.toString();
}
 
Example 9
Source File: PreferencesCookieStore.java    From android-open-project-demo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean clearExpired(Date date) {
    boolean clearedAny = false;
    SharedPreferences.Editor editor = cookiePrefs.edit();

    for (ConcurrentHashMap.Entry<String, Cookie> entry : cookies.entrySet()) {
        String name = entry.getKey();
        Cookie cookie = entry.getValue();
        if (cookie.getExpiryDate() == null || cookie.isExpired(date)) {
            // Remove the cookie by name
            cookies.remove(name);

            // Clear cookies from persistent store
            editor.remove(COOKIE_NAME_PREFIX + name);

            // We've cleared at least one
            clearedAny = true;
        }
    }

    // Update names in persistent store
    if (clearedAny) {
        editor.putString(COOKIE_NAME_STORE, TextUtils.join(",", cookies.keySet()));
    }
    editor.commit();

    return clearedAny;
}
 
Example 10
Source File: ApacheHttpClient.java    From karate with MIT License 4 votes vote down vote up
@Override
protected HttpResponse makeHttpRequest(HttpEntity entity, ScenarioContext context) {
    if (entity != null) {
        requestBuilder.setEntity(entity);
        requestBuilder.setHeader(entity.getContentType());
    }
    HttpUriRequest httpRequest = requestBuilder.build();
    CloseableHttpClient client = clientBuilder.build();
    BasicHttpContext httpContext = new BasicHttpContext();
    httpContext.setAttribute(URI_CONTEXT_KEY, getRequestUri());
    CloseableHttpResponse httpResponse;
    byte[] bytes;
    try {
        httpResponse = client.execute(httpRequest, httpContext);
        HttpEntity responseEntity = httpResponse.getEntity();
        if (responseEntity == null || responseEntity.getContent() == null) {
            bytes = new byte[0];
        } else {
            InputStream is = responseEntity.getContent();
            bytes = FileUtils.toBytes(is);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    HttpRequest actualRequest = context.getPrevRequest();
    HttpResponse response = new HttpResponse(actualRequest.getStartTime(), actualRequest.getEndTime());
    response.setUri(getRequestUri());
    response.setBody(bytes);
    response.setStatus(httpResponse.getStatusLine().getStatusCode());
    for (Cookie c : cookieStore.getCookies()) {
        com.intuit.karate.http.Cookie cookie = new com.intuit.karate.http.Cookie(c.getName(), c.getValue());
        cookie.put(DOMAIN, c.getDomain());
        cookie.put(PATH, c.getPath());
        if (c.getExpiryDate() != null) {
            cookie.put(EXPIRES, c.getExpiryDate().getTime() + "");
        }
        cookie.put(PERSISTENT, c.isPersistent() + "");
        cookie.put(SECURE, c.isSecure() + "");
        response.addCookie(cookie);
    }
    cookieStore.clear(); // we rely on the StepDefs for cookie 'persistence'
    for (Header header : httpResponse.getAllHeaders()) {
        response.addHeader(header.getName(), header.getValue());
    }
    return response;
}
 
Example 11
Source File: CookieUtil.java    From esigate with Apache License 2.0 4 votes vote down vote up
/**
 * Utility method to transform a Cookie into a Set-Cookie Header.
 * 
 * @param cookie
 *            the {@link Cookie} to format
 * 
 * @return the value of the Set-Cookie header
 */
public static String encodeCookie(Cookie cookie) {
    int maxAge = -1;
    if (cookie.getExpiryDate() != null) {
        maxAge = (int) ((cookie.getExpiryDate().getTime() - System.currentTimeMillis()) / ONE_SECOND);
        // According to Cookie class specification, a negative value
        // would be considered as no value. That is not what we want!
        if (maxAge < 0) {
            maxAge = 0;
        }
    }

    String cookieName = cookie.getName();
    String cookieValue = cookie.getValue();

    StringBuilder buffer = new StringBuilder();

    // Copied from org.apache.http.impl.cookie.BrowserCompatSpec.formatCookies(List<Cookie>)
    if (cookie.getVersion() > 0 && !isQuoteEnclosed(cookieValue)) {
        buffer.append(BasicHeaderValueFormatter.INSTANCE.formatHeaderElement(null,
                new BasicHeaderElement(cookieName, cookieValue), false).toString());
    } else {
        // Netscape style cookies do not support quoted values
        buffer.append(cookieName);
        buffer.append("=");
        if (cookieValue != null) {
            buffer.append(cookieValue);
        }
    }
    // End copy

    appendAttribute(buffer, "Comment", cookie.getComment());
    appendAttribute(buffer, "Domain", cookie.getDomain());
    appendAttribute(buffer, "Max-Age", maxAge);
    appendAttribute(buffer, "Path", cookie.getPath());
    if (cookie.isSecure()) {
        appendAttribute(buffer, "Secure");
    }
    if (((BasicClientCookie) cookie).containsAttribute(HTTP_ONLY_ATTR)) {
        appendAttribute(buffer, "HttpOnly");
    }
    appendAttribute(buffer, "Version", cookie.getVersion());

    return (buffer.toString());
}