Java Code Examples for java.net.HttpCookie#getPath()

The following examples show how to use java.net.HttpCookie#getPath() . 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: CookieManager.java    From Kalle with Apache License 2.0 6 votes vote down vote up
/**
 * Cookie for the specified URI to save, where path and port will be verified.
 *
 * @param uri        uri.
 * @param cookieList all you want to save the Cookie, does not meet the rules will not be saved.
 */
public void add(URI uri, List<String> cookieList) {
    for (String cookieValue : cookieList) {
        List<HttpCookie> cookies = HttpCookie.parse(cookieValue);
        for (HttpCookie cookie : cookies) {
            if (cookie.getPath() == null) {
                String path = normalizePath(uri.getPath());
                cookie.setPath(path);
            } else if (!pathMatches(uri, cookie)) {
                continue;
            }

            if (cookie.getDomain() == null) cookie.setDomain(uri.getHost());

            String portList = cookie.getPortlist();
            int port = getPort(uri);
            if (TextUtils.isEmpty(portList) || containsPort(portList, port)) {
                cookieJar.add(uri, cookie);
            }
        }
    }
}
 
Example 2
Source File: DBCookieStore.java    From Kalle with Apache License 2.0 6 votes vote down vote up
@Override
public void remove(HttpCookie httpCookie) {
    mLock.lock();
    try {
        Where.Builder whereBuilder = Where.newBuilder().add(NAME, Where.Options.EQUAL, httpCookie.getName());

        String domain = httpCookie.getDomain();
        if (!TextUtils.isEmpty(domain)) whereBuilder.and(DOMAIN, Where.Options.EQUAL, domain);

        String path = httpCookie.getPath();
        if (!TextUtils.isEmpty(path)) {
            if (path.length() > 1 && path.endsWith("/")) {
                path = path.substring(0, path.length() - 1);
            }
            whereBuilder.and(PATH, Where.Options.EQUAL, path);
        }
        mCookieDao.delete(whereBuilder.build().toString());
    } finally {
        mLock.unlock();
    }
}
 
Example 3
Source File: CookieDBJar.java    From Nimingban with Apache License 2.0 6 votes vote down vote up
private Cookie httpCookie2Cookie(URL url, HttpCookie httpCookie) {
    String domain = httpCookie.getDomain();
    String path = httpCookie.getPath();
    if (TextUtils.isEmpty(domain)) {
        domain = url.getHost();
    }
    if (TextUtils.isEmpty(path)) {
        path = url.getPath();
    }

    Cookie.Builder builder = new Cookie.Builder()
            .name(httpCookie.getName())
            .value(httpCookie.getValue())
            .expiresAt(System.currentTimeMillis() + (httpCookie.getMaxAge() * 1000))
            .domain(domain)
            .path(path);
    if (httpCookie.getSecure()) {
        builder.secure();
    }
    return builder.build();
}
 
Example 4
Source File: TransportableHttpCookie.java    From Nimingban with Apache License 2.0 6 votes vote down vote up
public static List<TransportableHttpCookie> from(URL url, List<HttpCookieWithId> list) {
    List<TransportableHttpCookie> result = new ArrayList<>(list.size());
    for (HttpCookieWithId hcwi : list) {
        HttpCookie cookie = hcwi.httpCookie;
        TransportableHttpCookie thc = new TransportableHttpCookie();
        thc.name = cookie.getName();
        thc.value = cookie.getValue();
        thc.comment = cookie.getComment();
        thc.commentURL = cookie.getCommentURL();
        thc.discard = cookie.getDiscard();
        thc.domain = cookie.getDomain();
        thc.maxAge = cookie.getMaxAge();
        thc.path = cookie.getPath();
        thc.portList = cookie.getPortlist();
        thc.secure = cookie.getSecure();
        thc.version = cookie.getVersion();
        thc.url = url.toString();
        result.add(thc);
    }
    return result;
}
 
Example 5
Source File: CookieEntity.java    From NoHttp with Apache License 2.0 6 votes vote down vote up
/**
 * Cookie building database entities.
 *
 * @param uri    cookie corresponding uri.
 * @param cookie cookie.
 */
public CookieEntity(URI uri, HttpCookie cookie) {
    this.uri = uri == null ? null : uri.toString();
    this.name = cookie.getName();
    this.value = cookie.getValue();
    this.comment = cookie.getComment();
    this.commentURL = cookie.getCommentURL();
    this.discard = cookie.getDiscard();
    this.domain = cookie.getDomain();
    long maxAge = cookie.getMaxAge();
    if (maxAge != -1 && maxAge > 0) {
        this.expiry = (maxAge * 1000L) + System.currentTimeMillis();
        if (this.expiry < 0L) // 溢出
            this.expiry = HeaderUtils.getMaxExpiryMillis();
    } else
        this.expiry = -1L;

    this.path = cookie.getPath();
    if (!TextUtils.isEmpty(path) && path.length() > 1 && path.endsWith("/")) {
        this.path = path.substring(0, path.length() - 1);
    }
    this.portList = cookie.getPortlist();
    this.secure = cookie.getSecure();
    this.version = cookie.getVersion();
}
 
Example 6
Source File: DBCookieStore.java    From NoHttp with Apache License 2.0 6 votes vote down vote up
@Override
public boolean remove(URI uri, HttpCookie httpCookie) {
    checkInitialization();

    mLock.lock();
    try {
        if (httpCookie == null || !isEnable()) return true;
        if (mCookieStoreListener != null) mCookieStoreListener.onRemoveCookie(uri, httpCookie);
        Where where = new Where(CookieSQLHelper.NAME, Options.EQUAL, httpCookie.getName());

        String domain = httpCookie.getDomain();
        if (!TextUtils.isEmpty(domain)) where.and(CookieSQLHelper.DOMAIN, Options.EQUAL, domain);

        String path = httpCookie.getPath();
        if (!TextUtils.isEmpty(path)) {
            if (path.length() > 1 && path.endsWith("/")) {
                path = path.substring(0, path.length() - 1);
            }
            where.and(CookieSQLHelper.PATH, Options.EQUAL, path);
        }
        return mCookieEntityDao.delete(where.toString());
    } finally {
        mLock.unlock();
    }
}
 
Example 7
Source File: Cookie.java    From Kalle with Apache License 2.0 5 votes vote down vote up
public static Cookie toCookie(String url, HttpCookie httpCookie) {
    Cookie cookie = new Cookie();
    cookie.setUrl(url);
    cookie.setName(httpCookie.getName());
    cookie.setValue(httpCookie.getValue());
    cookie.setComment(httpCookie.getComment());
    cookie.setCommentURL(httpCookie.getCommentURL());
    cookie.setDiscard(httpCookie.getDiscard());
    cookie.setDomain(httpCookie.getDomain());
    long maxAge = httpCookie.getMaxAge();
    if (maxAge > 0) {
        long expiry = (maxAge * 1000L) + System.currentTimeMillis();
        if (expiry < 0L) {
            expiry = System.currentTimeMillis() + 100L * 365L * 24L * 60L * 60L * 1000L;
        }
        cookie.setExpiry(expiry);
    } else if (maxAge < 0) {
        cookie.setExpiry(-1);
    } else {
        cookie.setExpiry(0);
    }

    String path = httpCookie.getPath();
    if (!TextUtils.isEmpty(path) && path.length() > 1 && path.endsWith("/")) {
        path = path.substring(0, path.length() - 1);
    }
    cookie.setPath(path);
    cookie.setPortList(httpCookie.getPortlist());
    cookie.setSecure(httpCookie.getSecure());
    cookie.setVersion(httpCookie.getVersion());
    return cookie;
}
 
Example 8
Source File: CookieAdapter.java    From SimpleProject with MIT License 5 votes vote down vote up
public CookieAdapter(HttpCookie cookie) {
	this.name = cookie.getName();
	this.value = cookie.getValue();
	this.domain = cookie.getDomain();
	this.path = cookie.getPath();
	this.secure = cookie.getSecure();
}
 
Example 9
Source File: TestAuthenticationFilter.java    From hadoop with Apache License 2.0 5 votes vote down vote up
private static void parseCookieMap(String cookieHeader, HashMap<String,
        String> cookieMap) {
  List<HttpCookie> cookies = HttpCookie.parse(cookieHeader);
  for (HttpCookie cookie : cookies) {
    if (AuthenticatedURL.AUTH_COOKIE.equals(cookie.getName())) {
      cookieMap.put(cookie.getName(), cookie.getValue());
      if (cookie.getPath() != null) {
        cookieMap.put("Path", cookie.getPath());
      }
      if (cookie.getDomain() != null) {
        cookieMap.put("Domain", cookie.getDomain());
      }
    }
  }
}
 
Example 10
Source File: TestAuthenticationFilter.java    From big-c with Apache License 2.0 5 votes vote down vote up
private static void parseCookieMap(String cookieHeader, HashMap<String,
        String> cookieMap) {
  List<HttpCookie> cookies = HttpCookie.parse(cookieHeader);
  for (HttpCookie cookie : cookies) {
    if (AuthenticatedURL.AUTH_COOKIE.equals(cookie.getName())) {
      cookieMap.put(cookie.getName(), cookie.getValue());
      if (cookie.getPath() != null) {
        cookieMap.put("Path", cookie.getPath());
      }
      if (cookie.getDomain() != null) {
        cookieMap.put("Domain", cookie.getDomain());
      }
    }
  }
}
 
Example 11
Source File: TestAuthenticationFilter.java    From registry with Apache License 2.0 5 votes vote down vote up
private static void parseCookieMap(String cookieHeader, HashMap<String,
        String> cookieMap) {
    List<HttpCookie> cookies = HttpCookie.parse(cookieHeader);
    for (HttpCookie cookie : cookies) {
        if (AuthenticatedURL.AUTH_COOKIE.equals(cookie.getName())) {
            cookieMap.put(cookie.getName(), cookie.getValue());
            if (cookie.getPath() != null) {
                cookieMap.put("Path", cookie.getPath());
            }
            if (cookie.getDomain() != null) {
                cookieMap.put("Domain", cookie.getDomain());
            }
        }
    }
}
 
Example 12
Source File: UriBuilder.java    From http-builder-ng with Apache License 2.0 5 votes vote down vote up
public URI forCookie(final HttpCookie cookie) throws URISyntaxException {
    final String scheme = traverse(this, UriBuilder::getParent, UriBuilder::getScheme, Traverser::notNull);
    final Integer port = traverse(this, UriBuilder::getParent, UriBuilder::getPort, notValue(DEFAULT_PORT));
    final String host = traverse(this, UriBuilder::getParent, UriBuilder::getHost, Traverser::notNull);
    final String path = cookie.getPath();
    final String query = null;
    final String fragment = null;
    final String userInfo = null;

    return new URI(scheme, userInfo, host, (port == null ? -1 : port), path, query, fragment);
}
 
Example 13
Source File: NonBlockingCookieStore.java    From http-builder-ng with Apache License 2.0 4 votes vote down vote up
public DomainKey(final HttpCookie cookie) {
    super(cookie.getName());
    this.domain = cookie.getDomain();
    this.path = cookie.getPath();
}