Example usage for java.net HttpCookie getDomain

List of usage examples for java.net HttpCookie getDomain

Introduction

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

Prototype

public String getDomain() 

Source Link

Document

Returns the domain name set for this cookie.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    CookieManager cm = new CookieManager();
    cm.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(cm);

    new URL("http://google.com").openConnection().getContent();

    List<HttpCookie> cookies = cm.getCookieStore().getCookies();
    for (HttpCookie cookie : cookies) {
        System.out.println(cookie.getDomain());
        System.out.println();//w ww  .j  a  v a 2  s.  c  om
    }
}

From source file:keywhiz.cli.JsonCookie.java

public static JsonCookie fromHttpCookie(HttpCookie cookie) {
    return JsonCookie.create(cookie.getName(), cookie.getValue(), cookie.getDomain(), cookie.getPath(),
            cookie.getSecure(), cookie.isHttpOnly());
}

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

public static String toString(HttpCookie... cookies) {
    if (isEmpty(cookies)) {
        return null;
    }//from w w w .ja v a2s .  c  o  m

    final StringBuilder cookieBuilder = new StringBuilder();

    for (HttpCookie cookie : cookies) {
        cookieBuilder.append(cookie.getName()).append("=").append(cookie.getValue());

        if (cookie.getComment() != null) {
            cookieBuilder.append(";").append("Comment=").append(cookie.getComment());
        }

        if (cookie.getDomain() != null) {
            cookieBuilder.append(";").append("Domain=").append(cookie.getDomain());
        }

        if (cookie.getPath() != null) {
            cookieBuilder.append(";").append("Path=").append(cookie.getPath());
        }

        if (cookie.getSecure()) {
            cookieBuilder.append(";").append("Secure");
        }

        if (cookie.isHttpOnly()) {
            cookieBuilder.append(";").append("HttpOnly");
        }

        if (cookie.getVersion() > 0) {
            cookieBuilder.append(";").append("Version=").append(cookie.getVersion());
        }

        if (cookie.hasExpired()) {
            cookieBuilder.append(";").append("Max-Age=0");
        } else {
            cookieBuilder.append(";").append("Max-Age=").append(cookie.getMaxAge());
        }

        cookieBuilder.append(", ");
    }

    return cookieBuilder.deleteCharAt(cookieBuilder.length() - 1).toString();
}

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

public static void setUpPersistentCookieStore() {
    if (context == null)
        return;//from  ww w .j  av  a 2  s .c o  m
    cookieStore = new PersistentCookieStore(context);

    CookieStore httpCookieStore = HttpRequestUtil.cookieManager.getCookieStore();
    for (HttpCookie h : httpCookieStore.getCookies()) {
        BasicClientCookie newCookie = new BasicClientCookie(h.getName(), h.getValue());
        newCookie.setVersion(h.getVersion());
        newCookie.setDomain(h.getDomain());
        newCookie.setPath(h.getPath());
        newCookie.setSecure(h.getSecure());
        newCookie.setComment(h.getComment());
        cookieStore.addCookie(newCookie);
    }
}

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

public Cookie(final HttpCookie cookie) {
    name = cookie.getName();/*w w w .  j a  va2s .co m*/
    value = cookie.getValue();
    comment = cookie.getComment();
    commentUrl = cookie.getCommentURL();
    domain = cookie.getDomain();
    discard = cookie.getDiscard();
    maxAge = cookie.getMaxAge();
    path = cookie.getPath();
    portList = cookie.getPortlist();
    secure = cookie.getSecure();
    version = cookie.getVersion();
}

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

@Nullable
public org.apache.http.cookie.Cookie toApacheCookie(@Nullable HttpCookie cookie) {
    if (cookie == null) {
        return null;
    }/*from  w  ww. j a  v  a 2s  . c  om*/

    BasicClientCookie clientCookie = new BasicClientCookie(cookie.getName(), cookie.getValue());
    clientCookie.setDomain(cookie.getDomain());
    clientCookie.setPath(cookie.getPath());
    clientCookie.setVersion(cookie.getVersion());

    Date expiryDate = new Date(new Date().getTime() + cookie.getMaxAge());
    clientCookie.setExpiryDate(expiryDate);

    return clientCookie;
}

From source file:com.subgraph.vega.ui.scanner.ScanExecutor.java

private List<Cookie> getCookieList(List<String> cookieStringList, URI uri) {
    if (cookieStringList.size() != 0) {
        ArrayList<Cookie> cookieList = new ArrayList<Cookie>(cookieStringList.size());
        for (String cookieString : cookieStringList) {
            List<HttpCookie> parseList = HttpCookie.parse(cookieString);
            for (HttpCookie cookie : parseList) {
                BasicClientCookie cp = new BasicClientCookie(cookie.getName(), cookie.getValue());
                cp.setComment(cookie.getComment());
                if (cookie.getDomain() != null) {
                    cp.setDomain(cookie.getDomain());
                } else {
                    // just set it to the target host for now - may need something slightly less specific
                    cp.setDomain(uri.getHost());
                }//w  ww . ja  v a 2 s  .  c  o m
                long maxAge = cookie.getMaxAge();
                if (maxAge > 0) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.add(Calendar.SECOND, (int) maxAge);
                    cp.setExpiryDate(calendar.getTime());
                }
                cp.setPath(cookie.getPath());
                cp.setSecure(cookie.getSecure());
                cp.setVersion(cookie.getVersion());
                cookieList.add(cp);
            }
        }
        return cookieList;
    }
    return null;
}

From source file:ai.eve.volley.stack.HurlStack.java

@Override
public HttpResponse performRequest(Request<?> request) throws IOException, AuthFailureError {
    HashMap<String, String> map = new HashMap<String, String>();
    if (!TextUtils.isEmpty(mUserAgent)) {
        map.put(HTTP.USER_AGENT, mUserAgent);
    }/*from w  ww . j av  a 2  s. c om*/
    if (!TextUtils.isEmpty(mSignInfo)) {
        map.put("SIGN", ESecurity.Encrypt(mSignInfo));
    }
    map.putAll(request.getHeaders());

    URL parsedUrl = new URL(request.getUrl());
    HttpURLConnection connection = openConnection(parsedUrl, request);

    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }
    if (EApplication.getCookies() != null) {
        ELog.I("cookie", EApplication.getCookies().toString());
        connection.addRequestProperty("Cookie", EApplication.getReqCookies());
    } else {
        ELog.I("cookie", "null");
    }
    setConnectionParametersForRequest(connection, request);
    int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        // -1 is returned by getResponseCode() if the response code could not be retrieved.
        // Signal to the caller that something was wrong with the connection.
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }

    StatusLine responseStatus = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1),
            connection.getResponseCode(), connection.getResponseMessage());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromConnection(connection));
    for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
        if (header.getKey() != null) {
            if (header.getKey().equalsIgnoreCase("Set-Cookie")) {
                List<String> cookies = header.getValue();
                HashMap<String, HttpCookie> cookieMap = new HashMap<String, HttpCookie>();
                for (String string : cookies) {
                    List<HttpCookie> cookie = HttpCookie.parse(string);
                    for (HttpCookie httpCookie : cookie) {
                        if (httpCookie.getDomain() != null && httpCookie.getPath() != null) {
                            cookieMap.put(httpCookie.getName() + httpCookie.getDomain() + httpCookie.getPath(),
                                    httpCookie);
                        } else if (httpCookie.getDomain() == null && httpCookie.getPath() != null) {
                            cookieMap.put(httpCookie.getName() + httpCookie.getPath(), httpCookie);
                        } else if (httpCookie.getDomain() != null && httpCookie.getPath() == null) {
                            cookieMap.put(httpCookie.getName() + httpCookie.getDomain(), httpCookie);
                        } else {
                            cookieMap.put(httpCookie.getName(), httpCookie);
                        }
                    }
                }

                EApplication.setCookies(cookieMap);
                if (EApplication.getCookies() != null) {
                    ELog.I("?cookie", EApplication.getCookies().toString());
                }
            } else {
                Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
                response.addHeader(h);
            }
        }
    }

    return response;
}

From source file:org.nextlets.erc.defaults.http.ERCHttpInvokerImpl.java

private Cookie toCookie(URI uri, HttpCookie httpCookie) {
    SetCookie cookie = new BasicClientCookie(httpCookie.getName(), httpCookie.getValue());
    cookie.setComment(httpCookie.getComment());
    if (httpCookie.getDomain() != null) {
        cookie.setDomain(httpCookie.getDomain());
    } else {//from   w ww .j a  va2 s  . c om
        cookie.setDomain(uri.getHost());
    }
    if (httpCookie.getMaxAge() != -1) {
        cookie.setExpiryDate(new Date(httpCookie.getMaxAge() * 1000));
    }
    if (httpCookie.getPath() != null) {
        cookie.setPath(httpCookie.getPath());
    } else {
        cookie.setPath(uri.getPath());
    }
    cookie.setSecure(httpCookie.getSecure());
    cookie.setVersion(httpCookie.getVersion());
    return cookie;
}

From source file:com.nominanuda.web.http.ServletHelper.java

public Cookie servletCookie(HttpCookie c) {
    Cookie _c = new Cookie(c.getName(), c.getValue());
    if (c.getComment() != null) {
        _c.setComment(c.getComment());/*from   w  ww .j  av a  2 s. c o  m*/
    }
    if (c.getDomain() != null) {
        _c.setDomain(c.getDomain());
    }
    if (c.getPath() != null) {
        _c.setPath(c.getPath());
    }
    _c.setSecure(c.getSecure());
    _c.setVersion(c.getVersion());
    _c.setHttpOnly(c.getDiscard());
    _c.setMaxAge((int) c.getMaxAge());
    return _c;
}