Example usage for java.net HttpCookie setDomain

List of usage examples for java.net HttpCookie setDomain

Introduction

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

Prototype

public void setDomain(String pattern) 

Source Link

Document

Specifies the domain within which this cookie should be presented.

Usage

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: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;
    }// ww  w  .j a v a2s  . co m

    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;
    }/*from   w  w  w .jav  a  2  s.  com*/

    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:com.github.parisoft.resty.utils.CookieUtils.java

public static String toString(org.apache.http.cookie.Cookie... cookies) {
    if (isEmpty(cookies)) {
        return null;
    }//  w  ww.  j  a va 2  s.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:interactivespaces.service.web.server.internal.netty.NettyHttpRequest.java

public static HttpCookie convertFromNettyCookie(Cookie cookie) {
    HttpCookie httpCookie = new HttpCookie(cookie.getName(), cookie.getValue());
    httpCookie.setComment(cookie.getComment());
    httpCookie.setDomain(cookie.getDomain());
    httpCookie.setMaxAge(cookie.getMaxAge());
    httpCookie.setPath(cookie.getPath());
    httpCookie.setPortlist(createPortString(cookie.getPorts()));
    httpCookie.setVersion(cookie.getVersion());
    httpCookie.setSecure(cookie.isSecure());
    httpCookie.setDiscard(cookie.isDiscard());

    return httpCookie;
}

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

public static void getJavaCookieStore(java.net.CookieStore jCookieStore) {
    if (cookieStore == null)
        return;//  w ww . jav a 2 s.c o m

    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:in.rab.ordboken.CookieSerializer.java

public void loadFromString(String json) {
    try {//w  w  w .  j  av a 2 s  . c  o  m
        JSONObject obj = new JSONObject(json);
        JSONArray cookieStrings = obj.getJSONArray("cookies");
        URI uri = new URI("http://" + mDomain);

        for (int i = 0; i < cookieStrings.length(); i++) {
            String cookieString = cookieStrings.getString(i);
            HttpCookie cookie = HttpCookie.parse(cookieString).get(0);

            cookie.setPath("/");
            cookie.setDomain(mDomain);

            mCookieStore.add(uri, cookie);
        }
    } catch (Exception e) {
        // No big deal if this fails.
    }
}

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

/**
 * Syncs all cookies from ExoConnectionUtils cookieStore from Apache's
 * HttpClient to HttpURLConnection./*  ww w. j a v a 2s .  c  om*/
 * 
 * @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:at.becast.youploader.youtube.data.Cookie.java

public HttpCookie getCookie() {
    final HttpCookie cookie = new HttpCookie(name, value);
    cookie.setComment(comment);//from w ww  .  j  a  v  a 2s . c  o  m
    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.nextlets.erc.defaults.http.ERCHttpInvokerImpl.java

private HttpCookie toHttpCookie(Cookie cookie) {
    HttpCookie httpCookie = new HttpCookie(cookie.getName(), cookie.getValue());
    httpCookie.setComment(cookie.getComment());
    httpCookie.setDomain(cookie.getDomain());
    if (cookie.getExpiryDate() != null) {
        httpCookie.setMaxAge(cookie.getExpiryDate().getTime() / 1000);
    }/*  ww w . jav  a 2s. c o m*/
    httpCookie.setPath(cookie.getPath());
    httpCookie.setSecure(cookie.isSecure());
    httpCookie.setVersion(cookie.getVersion());
    return httpCookie;
}