Example usage for java.net HttpCookie HttpCookie

List of usage examples for java.net HttpCookie HttpCookie

Introduction

In this page you can find the example usage for java.net HttpCookie HttpCookie.

Prototype

public HttpCookie(String name, String value) 

Source Link

Document

Constructs a cookie with a specified name and value.

Usage

From source file:Main.java

@Deprecated
// Deprecated because this uses org.apache.http, which is itself deprecated
public static HttpCookie servletCookieFromApacheCookie(org.apache.http.cookie.Cookie apacheCookie) {
    if (apacheCookie == null) {
        return null;
    }//w  w w  . j  a va 2  s .  c  om

    String name = apacheCookie.getName();
    String value = apacheCookie.getValue();

    HttpCookie cookie = new HttpCookie(name, value);

    value = apacheCookie.getDomain();
    if (value != null) {
        cookie.setDomain(value);
    }
    value = apacheCookie.getPath();
    if (value != null) {
        cookie.setPath(value);
    }
    cookie.setSecure(apacheCookie.isSecure());

    value = apacheCookie.getComment();
    if (value != null) {
        cookie.setComment(value);
    }

    // version
    cookie.setVersion(apacheCookie.getVersion());

    // From the Apache source code, maxAge is converted to expiry date using the following formula
    // if (maxAge >= 0) {
    //     setExpiryDate(new Date(System.currentTimeMillis() + maxAge * 1000L));
    // }
    // Reverse this to get the actual max age

    Date expiryDate = apacheCookie.getExpiryDate();
    if (expiryDate != null) {
        long maxAge = (expiryDate.getTime() - System.currentTimeMillis()) / 1000;
        // we have to lower down, no other option
        cookie.setMaxAge((int) maxAge);
    }

    // return the servlet cookie
    return cookie;
}

From source file:com.github.parisoft.resty.utils.CookieUtils.java

public static String toString(Cookie... cookies) {
    if (isEmpty(cookies)) {
        return null;
    }/* www.  java 2  s  .co  m*/

    final HttpCookie[] httpCookies = new HttpCookie[cookies.length];

    for (int i = 0; i < cookies.length; i++) {
        final Cookie srcCookie = cookies[i];
        final HttpCookie httpCookie = new HttpCookie(srcCookie.getName(), srcCookie.getValue());
        httpCookie.setDomain(srcCookie.getDomain());
        httpCookie.setPath(srcCookie.getPath());
        httpCookie.setVersion(srcCookie.getVersion());

        httpCookies[i] = httpCookie;
    }

    return toString(httpCookies);
}

From source file:keywhiz.cli.JsonCookie.java

public static HttpCookie toHttpCookie(JsonCookie cookieContents) {
    HttpCookie cookie = new HttpCookie(cookieContents.name(), cookieContents.value());
    cookie.setDomain(cookieContents.domain());
    cookie.setPath(cookieContents.path());
    cookie.setSecure(cookieContents.isSecure());
    cookie.setHttpOnly(cookieContents.isHttpOnly());
    cookie.setVersion(1); // Always set version to 1 or important fields will be dropped
    return cookie;
}

From source file:com.github.parisoft.resty.utils.CookieUtils.java

public static String toString(org.apache.http.cookie.Cookie... cookies) {
    if (isEmpty(cookies)) {
        return null;
    }/* www.j  a  v a2s .co m*/

    final HttpCookie[] httpCookies = new HttpCookie[cookies.length];

    for (int i = 0; i < cookies.length; i++) {
        final org.apache.http.cookie.Cookie srcCookie = cookies[i];
        final HttpCookie httpCookie = new HttpCookie(srcCookie.getName(), srcCookie.getValue());
        httpCookie.setComment(srcCookie.getComment());
        httpCookie.setDomain(srcCookie.getDomain());
        httpCookie.setPath(srcCookie.getPath());
        httpCookie.setSecure(srcCookie.isSecure());
        httpCookie.setVersion(srcCookie.getVersion());

        final Date now = new Date();

        if (srcCookie.isExpired(now)) {
            httpCookie.setMaxAge(0);
        } else {
            httpCookie.setMaxAge(
                    TimeUnit.MILLISECONDS.toSeconds(srcCookie.getExpiryDate().getTime() - now.getTime()));
        }

        httpCookies[i] = httpCookie;
    }

    return toString(httpCookies);
}

From source file:com.jaspersoft.android.jaspermobile.network.cookie.CookieMapperTest.java

@Before
public void setUp() throws Exception {
    cookieMapper = new CookieMapper();
    fakeCookie = new HttpCookie("key", "domain");
    fakeCookie.setPath("/path");
    fakeCookie.setDomain("localhost");
    fakeCookie.setVersion(1);//from ww w.  j a v  a2  s  .c  o  m
}

From source file:at.becast.youploader.youtube.data.Cookie.java

public HttpCookie getCookie() {
    final HttpCookie cookie = new HttpCookie(name, value);
    cookie.setComment(comment);/*  w  w  w  .  j a  va 2  s. c om*/
    cookie.setCommentURL(commentUrl);
    cookie.setDiscard(discard);
    cookie.setDomain(domain);
    cookie.setPath(path);
    cookie.setPortlist(portList);
    cookie.setMaxAge(maxAge);
    cookie.setSecure(secure);
    cookie.setVersion(version);
    return cookie;
}

From source file:org.exoplatform.utils.image.CookieAwarePicassoDownloader.java

/**
 * Syncs all cookies from ExoConnectionUtils cookieStore from Apache's
 * HttpClient to HttpURLConnection.//from  ww w . j ava2  s  .co m
 * 
 * @param manager the CookieManager in which to store the retrieved cookies
 */
private void syncCookies(CookieManager manager) {
    CookieStore store = ExoConnectionUtils.cookiesStore;
    if (store == null)
        return;

    for (Cookie cookie : store.getCookies()) {
        HttpCookie c = new HttpCookie(cookie.getName(), cookie.getValue());
        c.setDomain(cookie.getDomain());
        c.setPath(cookie.getPath());
        c.setVersion(cookie.getVersion());
        String url = AccountSetting.getInstance().getDomainName() + "/" + cookie.getPath();
        try {
            manager.getCookieStore().add(new URI(url), c);
        } catch (URISyntaxException e) {
            Log.e(TAG, e.getMessage(), e);
        }
    }
}

From source file:cn.ttyhuo.common.MyApplication.java

public static void getJavaCookieStore(java.net.CookieStore jCookieStore) {
    if (cookieStore == null)
        return;/*from  w w w .j a  v a  2  s .c om*/

    for (Cookie h : cookieStore.getCookies()) {
        HttpCookie newCookie = new HttpCookie(h.getName(), h.getValue());
        newCookie.setVersion(h.getVersion());
        newCookie.setDomain(h.getDomain());
        newCookie.setPath(h.getPath());
        newCookie.setSecure(h.isSecure());
        newCookie.setComment(h.getComment());
        jCookieStore.add(URI.create("http://" + h.getDomain()), newCookie);
    }
}

From source file:CookieAccessor.java

/**
 * Set cookie in cookie store/*w  w w  .ja  v  a2  s . com*/
 */
public void setCookieUsingCookieHandler() {
    try {
        // instantiate CookieManager
        CookieManager manager = new CookieManager();
        CookieHandler.setDefault(manager);
        CookieStore cookieJar = manager.getCookieStore();

        // create cookie
        HttpCookie cookie = new HttpCookie("UserName", "John Doe");

        // add cookie to CookieStore for a particular URL
        URL url = new URL("http://host.example.com");
        cookieJar.add(url.toURI(), cookie);
        System.out.println("Added cookie using cookie handler");
    } catch (Exception e) {
        System.out.println("Unable to set cookie using CookieHandler");
        e.printStackTrace();
    }
}