Java Code Examples for org.apache.http.impl.cookie.BasicClientCookie#setExpiryDate()

The following examples show how to use org.apache.http.impl.cookie.BasicClientCookie#setExpiryDate() . 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: Cookie.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new cookie with the specified name and value which applies to the specified domain,
 * the specified path, and expires on the specified date.
 * @param domain the domain to which this cookie applies
 * @param name the cookie name
 * @param value the cookie name
 * @param path the path to which this cookie applies
 * @param expires the date on which this cookie expires
 * @param secure whether or not this cookie is secure (i.e. HTTPS vs HTTP)
 * @param httpOnly whether or not this cookie should be only used for HTTP(S) headers
 */
public Cookie(final String domain, final String name, final String value, final String path, final Date expires,
    final boolean secure, final boolean httpOnly) {
    if (domain == null) {
        throw new IllegalArgumentException("Cookie domain must be specified");
    }

    final BasicClientCookie cookie = new BasicClientCookie(name, value != null ? value : "");
    cookie.setDomain(domain);
    cookie.setPath(path);
    cookie.setExpiryDate(expires);
    cookie.setSecure(secure);
    if (httpOnly) {
        cookie.setAttribute("httponly", "true");
    }
    httpClientCookie_ = cookie;
}
 
Example 2
Source File: Cookie.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new cookie with the specified name and value which applies to the specified domain,
 * the specified path, and expires after the specified amount of time.
 * @param domain the domain to which this cookie applies
 * @param name the cookie name
 * @param value the cookie name
 * @param path the path to which this cookie applies
 * @param maxAge the number of seconds for which this cookie is valid; <tt>-1</tt> indicates that the
 *        cookie should never expire; other negative numbers are not allowed
 * @param secure whether or not this cookie is secure (i.e. HTTPS vs HTTP)
 */
public Cookie(final String domain, final String name, final String value, final String path, final int maxAge,
    final boolean secure) {

    final BasicClientCookie cookie = new BasicClientCookie(name, value != null ? value : "");
    cookie.setDomain(domain);
    cookie.setPath(path);
    cookie.setSecure(secure);

    if (maxAge < -1) {
        throw new IllegalArgumentException("invalid max age:  " + maxAge);
    }
    if (maxAge >= 0) {
        cookie.setExpiryDate(new Date(System.currentTimeMillis() + (maxAge * 1000)));
    }

    httpClientCookie_ = cookie;
}
 
Example 3
Source File: SerializableCookie.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    String name = (String) in.readObject();
    String value = (String) in.readObject();
    clientCookie = new BasicClientCookie(name, value);
    clientCookie.setComment((String) in.readObject());
    clientCookie.setDomain((String) in.readObject());
    clientCookie.setExpiryDate((Date) in.readObject());
    clientCookie.setPath((String) in.readObject());
    clientCookie.setVersion(in.readInt());
    clientCookie.setSecure(in.readBoolean());
}
 
Example 4
Source File: PreferencesCookieStore.java    From android-open-project-demo with Apache License 2.0 5 votes vote down vote up
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    String name = (String) in.readObject();
    String value = (String) in.readObject();
    clientCookie = new BasicClientCookie(name, value);
    clientCookie.setComment((String) in.readObject());
    clientCookie.setDomain((String) in.readObject());
    clientCookie.setExpiryDate((Date) in.readObject());
    clientCookie.setPath((String) in.readObject());
    clientCookie.setVersion(in.readInt());
    clientCookie.setSecure(in.readBoolean());
}
 
Example 5
Source File: SerializableCookie.java    From Android-Basics-Codes with Artistic License 2.0 5 votes vote down vote up
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    String name = (String) in.readObject();
    String value = (String) in.readObject();
    clientCookie = new BasicClientCookie(name, value);
    clientCookie.setComment((String) in.readObject());
    clientCookie.setDomain((String) in.readObject());
    clientCookie.setExpiryDate((Date) in.readObject());
    clientCookie.setPath((String) in.readObject());
    clientCookie.setVersion(in.readInt());
    clientCookie.setSecure(in.readBoolean());
}
 
Example 6
Source File: PreferencesCookieStore.java    From Android-Basics-Codes with Artistic License 2.0 5 votes vote down vote up
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    String name = (String)in.readObject();
    String value = (String)in.readObject();
    clientCookie = new BasicClientCookie(name, value);
    clientCookie.setComment((String)in.readObject());
    clientCookie.setDomain((String)in.readObject());
    clientCookie.setExpiryDate((Date)in.readObject());
    clientCookie.setPath((String)in.readObject());
    clientCookie.setVersion(in.readInt());
    clientCookie.setSecure(in.readBoolean());
}
 
Example 7
Source File: cfHttpConnection.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
private void addCookies() {

		Map<String, List<String>> cookies = httpData.getCookies();
		Iterator<String> keys = cookies.keySet().iterator();
		String domain = "";
		domain = message.getURI().getHost();
		CookieStore cookieStore = new BasicCookieStore();

		HttpContext localContext = new BasicHttpContext();

		// Bind custom cookie store to the local context
		localContext.setAttribute( ClientContext.COOKIE_STORE, cookieStore );
		client.getParams().setParameter( ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY );

		while ( keys.hasNext() ) {
			String nextKey = keys.next();
			List<String> values = cookies.get( nextKey );
			Date date = new Date( 2038, 1, 1 );
			for ( int i = 0; i < values.size(); i++ ) {
				BasicClientCookie cookie = new BasicClientCookie( nextKey, values.get( i ) );
				cookieStore.addCookie( cookie );
				cookie.setVersion( 1 );
				cookie.setDomain( domain );
				cookie.setPath( "/" );
				cookie.setExpiryDate( date );
				cookie.setSecure( false );
			}
		}
		client.setCookieStore( cookieStore );
	}
 
Example 8
Source File: SerializableCookie.java    From bither-desktop-java with Apache License 2.0 5 votes vote down vote up
private void readObject(ObjectInputStream in) throws IOException,
        ClassNotFoundException {
    String name = (String) in.readObject();
    String value = (String) in.readObject();
    clientCookie = new BasicClientCookie(name, value);
    clientCookie.setComment((String) in.readObject());
    clientCookie.setDomain((String) in.readObject());
    clientCookie.setExpiryDate((Date) in.readObject());
    clientCookie.setPath((String) in.readObject());
    clientCookie.setVersion(in.readInt());
    clientCookie.setSecure(in.readBoolean());
}
 
Example 9
Source File: FusionPipelineClient.java    From storm-solr with Apache License 2.0 5 votes vote down vote up
protected synchronized void clearCookieForHost(String sessionHost) throws Exception {
  Cookie sessionCookie = null;
  for (Cookie cookie : cookieStore.getCookies()) {
    String cookieDomain = cookie.getDomain();
    if (cookieDomain != null) {
      if (sessionHost.equals(cookieDomain) ||
        sessionHost.indexOf(cookieDomain) != -1 ||
        cookieDomain.indexOf(sessionHost) != -1)
      {
        sessionCookie = cookie;
        break;
      }
    }
  }

  if (sessionCookie != null) {
    BasicClientCookie httpCookie =
      new BasicClientCookie(sessionCookie.getName(),sessionCookie.getValue());
    httpCookie.setExpiryDate(new Date(0));
    httpCookie.setVersion(1);
    httpCookie.setPath(sessionCookie.getPath());
    httpCookie.setDomain(sessionCookie.getDomain());
    cookieStore.addCookie(httpCookie);
  }

  cookieStore.clearExpired(new Date()); // this should clear the cookie
}
 
Example 10
Source File: SerializableCookie.java    From bither-desktop-java with Apache License 2.0 5 votes vote down vote up
private void readObject(ObjectInputStream in) throws IOException,
        ClassNotFoundException {
    String name = (String) in.readObject();
    String value = (String) in.readObject();
    clientCookie = new BasicClientCookie(name, value);
    clientCookie.setComment((String) in.readObject());
    clientCookie.setDomain((String) in.readObject());
    clientCookie.setExpiryDate((Date) in.readObject());
    clientCookie.setPath((String) in.readObject());
    clientCookie.setVersion(in.readInt());
    clientCookie.setSecure(in.readBoolean());
}
 
Example 11
Source File: SerializableCookie.java    From sealtalk-android with MIT License 5 votes vote down vote up
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    String name = (String) in.readObject();
    String value = (String) in.readObject();
    clientCookie = new BasicClientCookie(name, value);
    clientCookie.setComment((String) in.readObject());
    clientCookie.setDomain((String) in.readObject());
    clientCookie.setExpiryDate((Date) in.readObject());
    clientCookie.setPath((String) in.readObject());
    clientCookie.setVersion(in.readInt());
    clientCookie.setSecure(in.readBoolean());
}
 
Example 12
Source File: PreferencesCookieStore.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    String name = (String) in.readObject();
    String value = (String) in.readObject();
    clientCookie = new BasicClientCookie(name, value);
    clientCookie.setComment((String) in.readObject());
    clientCookie.setDomain((String) in.readObject());
    clientCookie.setExpiryDate((Date) in.readObject());
    clientCookie.setPath((String) in.readObject());
    clientCookie.setVersion(in.readInt());
    clientCookie.setSecure(in.readBoolean());
}
 
Example 13
Source File: CookiesPathTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Test
public void testOldCookieWithNodeInValue() throws IOException {
    String requestURI = OAuthClient.AUTH_SERVER_ROOT + "/realms/foo/account";
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_YEAR, 1);

    // create old cookie with wrong path
    BasicClientCookie wrongCookie = new BasicClientCookie(AuthenticationSessionManager.AUTH_SESSION_ID, AUTH_SESSION_VALUE_NODE);
    wrongCookie.setDomain(AUTH_SERVER_HOST);
    wrongCookie.setPath(OLD_COOKIE_PATH);
    wrongCookie.setExpiryDate(calendar.getTime());

    // obtain new cookies
    CookieStore cookieStore = getCorrectCookies(requestURI);
    cookieStore.addCookie(wrongCookie);

    Assert.assertThat(cookieStore.getCookies(), Matchers.hasSize(3));

    login(requestURI, cookieStore);

    // old cookie has been removed
    // now we have AUTH_SESSION_ID, KEYCLOAK_IDENTITY, KEYCLOAK_SESSION, OAuth_Token_Request_State
    Assert.assertThat(cookieStore.getCookies().stream().map(org.apache.http.cookie.Cookie::getName).collect(Collectors.toList()), 
            Matchers.hasItems("AUTH_SESSION_ID", "KEYCLOAK_IDENTITY", "KEYCLOAK_SESSION"));

    // does each cookie's path end with "/"
    cookieStore.getCookies().stream().filter(c -> !"OAuth_Token_Request_State".equals(c.getName())).map(org.apache.http.cookie.Cookie::getPath).forEach(path ->Assert.assertThat(path, Matchers.endsWith("/")));

    // KEYCLOAK_SESSION should end by AUTH_SESSION_ID value
    String authSessionId = cookieStore.getCookies().stream().filter(c -> "AUTH_SESSION_ID".equals(c.getName())).findFirst().get().getValue();
    String KCSessionId = cookieStore.getCookies().stream().filter(c -> "KEYCLOAK_SESSION".equals(c.getName())).findFirst().get().getValue();
    String KCSessionSuffix = KCSessionId.split("/")[2];
    Assert.assertThat(authSessionId, Matchers.containsString(KCSessionSuffix));
}
 
Example 14
Source File: SerializableCookie.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
private void readObject(ObjectInputStream objectinputstream)
{
    b = new BasicClientCookie((String)objectinputstream.readObject(), (String)objectinputstream.readObject());
    b.setComment((String)objectinputstream.readObject());
    b.setDomain((String)objectinputstream.readObject());
    b.setExpiryDate((Date)objectinputstream.readObject());
    b.setPath((String)objectinputstream.readObject());
    b.setVersion(objectinputstream.readInt());
    b.setSecure(objectinputstream.readBoolean());
}
 
Example 15
Source File: SerializableCookie.java    From bither-android with Apache License 2.0 5 votes vote down vote up
private void readObject(ObjectInputStream in) throws IOException,
		ClassNotFoundException {
	String name = (String) in.readObject();
	String value = (String) in.readObject();
	clientCookie = new BasicClientCookie(name, value);
	clientCookie.setComment((String) in.readObject());
	clientCookie.setDomain((String) in.readObject());
	clientCookie.setExpiryDate((Date) in.readObject());
	clientCookie.setPath((String) in.readObject());
	clientCookie.setVersion(in.readInt());
	clientCookie.setSecure(in.readBoolean());
}
 
Example 16
Source File: MyCookieDBManager.java    From Huochexing12306 with Apache License 2.0 5 votes vote down vote up
public List<Cookie> getAllCookies() {
	List<Cookie> cookies = new ArrayList<Cookie>();

	Cursor cursor = db
			.query(TABLE_NAME, null, null, null, null, null, null);

	for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
		String name = cursor.getString(cursor.getColumnIndex(Column.NAME));
		String value = cursor
				.getString(cursor.getColumnIndex(Column.VALUE));

		BasicClientCookie cookie = new BasicClientCookie(name, value);

		cookie.setComment(cursor.getString(cursor
				.getColumnIndex(Column.COMMENT)));
		cookie.setDomain(cursor.getString(cursor
				.getColumnIndex(Column.DOMAIN)));
		long expireTime = cursor.getLong(cursor
				.getColumnIndex(Column.EXPIRY_DATE));
		if (expireTime != 0) {
			cookie.setExpiryDate(new Date(expireTime));
		}
		cookie.setPath(cursor.getString(cursor.getColumnIndex(Column.PATH)));
		cookie.setSecure(cursor.getInt(cursor.getColumnIndex(Column.SECURE)) == 1);
		cookie.setVersion(cursor.getInt(cursor
				.getColumnIndex(Column.VERSION)));

		cookies.add(cookie);
	}

	cursor.close();

	return cookies;
}
 
Example 17
Source File: FileDownloader.java    From Mastering-Selenium-WebDriver-3.0-Second-Edition with MIT License 5 votes vote down vote up
private BasicCookieStore getWebDriverCookies(Set<Cookie> seleniumCookieSet) {
    BasicCookieStore copyOfWebDriverCookieStore = new BasicCookieStore();
    for (Cookie seleniumCookie : seleniumCookieSet) {
        BasicClientCookie duplicateCookie = new BasicClientCookie(seleniumCookie.getName(), seleniumCookie.getValue());
        duplicateCookie.setDomain(seleniumCookie.getDomain());
        duplicateCookie.setSecure(seleniumCookie.isSecure());
        duplicateCookie.setExpiryDate(seleniumCookie.getExpiry());
        duplicateCookie.setPath(seleniumCookie.getPath());
        copyOfWebDriverCookieStore.addCookie(duplicateCookie);
    }

    return copyOfWebDriverCookieStore;
}
 
Example 18
Source File: FileDownloader.java    From Mastering-Selenium-WebDriver-3.0-Second-Edition with MIT License 5 votes vote down vote up
private BasicCookieStore getWebDriverCookies(Set<Cookie> seleniumCookieSet) {
    BasicCookieStore copyOfWebDriverCookieStore = new BasicCookieStore();
    for (Cookie seleniumCookie : seleniumCookieSet) {
        BasicClientCookie duplicateCookie = new BasicClientCookie(seleniumCookie.getName(), seleniumCookie.getValue());
        duplicateCookie.setDomain(seleniumCookie.getDomain());
        duplicateCookie.setSecure(seleniumCookie.isSecure());
        duplicateCookie.setExpiryDate(seleniumCookie.getExpiry());
        duplicateCookie.setPath(seleniumCookie.getPath());
        copyOfWebDriverCookieStore.addCookie(duplicateCookie);
    }

    return copyOfWebDriverCookieStore;
}
 
Example 19
Source File: CookieConverter.java    From storm-crawler with Apache License 2.0 4 votes vote down vote up
/**
 * Get a list of cookies based on the cookies string taken from response
 * header and the target url.
 * 
 * @param cookiesString
 *            the value of the http header for "Cookie" in the http
 *            response.
 * @param targetURL
 *            the url for which we wish to pass the cookies in the request.
 * @return List off cookies to add to the request.
 */
public static List<Cookie> getCookies(String[] cookiesStrings, URL targetURL) {
    ArrayList<Cookie> list = new ArrayList<Cookie>();

    for (String cs : cookiesStrings) {
        String name = null;
        String value = null;

        String expires = null;
        String domain = null;
        String path = null;

        boolean secure = false;

        String[] tokens = cs.split(";");

        int equals = tokens[0].indexOf("=");
        name = tokens[0].substring(0, equals);
        value = tokens[0].substring(equals + 1);

        for (int i = 1; i < tokens.length; i++) {
            String ti = tokens[i].trim();
            if (ti.equalsIgnoreCase("secure"))
                secure = true;
            if (ti.toLowerCase().startsWith("path=")) {
                path = ti.substring(5);
            }
            if (ti.toLowerCase().startsWith("domain=")) {
                domain = ti.substring(7);
            }
            if (ti.toLowerCase().startsWith("expires=")) {
                expires = ti.substring(8);
            }
        }

        BasicClientCookie cookie = new BasicClientCookie(name, value);

        // check domain
        if (domain != null) {
            cookie.setDomain(domain);

            if (!checkDomainMatchToUrl(domain, targetURL.getHost()))
                continue;
        }

        // check path
        if (path != null) {
            cookie.setPath(path);

            if (!path.equals("") && !path.equals("/")
                    && !targetURL.getPath().startsWith(path))
                continue;
        }

        // check secure
        if (secure) {
            cookie.setSecure(secure);

            if (!targetURL.getProtocol().equalsIgnoreCase("https"))
                continue;
        }

        // check expiration
        if (expires != null) {
            try {
                Date expirationDate = DATE_FORMAT.parse(expires);
                cookie.setExpiryDate(expirationDate);

                // check that it hasn't expired?
                if (cookie.isExpired(new Date()))
                    continue;

                cookie.setExpiryDate(expirationDate);
            } catch (ParseException e) {
                // ignore exceptions
            }
        }

        // attach additional infos to cookie
        list.add(cookie);
    }

    return list;
}
 
Example 20
Source File: RequestParser.java    From Lanmitm with GNU General Public License v2.0 4 votes vote down vote up
public static ArrayList<BasicClientCookie> parseRawCookie(String rawCookie) {
	String[] rawCookieParams = rawCookie.split(";");
	ArrayList<BasicClientCookie> cookies = new ArrayList<BasicClientCookie>();

	for (String rawCookieParam : rawCookieParams) {
		String[] rawCookieNameAndValue = rawCookieParam.split("=");

		if (rawCookieNameAndValue.length != 2)
			continue;

		String cookieName = rawCookieNameAndValue[0].trim();
		String cookieValue = rawCookieNameAndValue[1].trim();
		BasicClientCookie cookie = new BasicClientCookie(cookieName,
				cookieValue);

		for (int i = 1; i < rawCookieParams.length; i++) {
			String rawCookieParamNameAndValue[] = rawCookieParams[i].trim()
					.split("=");
			String paramName = rawCookieParamNameAndValue[0].trim();

			if (paramName.equalsIgnoreCase("secure"))
				cookie.setSecure(true);

			else {
				// attribute not a flag or missing value.
				if (rawCookieParamNameAndValue.length == 2) {
					String paramValue = rawCookieParamNameAndValue[1]
							.trim();
					if (paramName.equalsIgnoreCase("max-age")) {
						long maxAge = Long.parseLong(paramValue);
						Date expiryDate = new Date(
								java.lang.System.currentTimeMillis()
										+ maxAge);
						cookie.setExpiryDate(expiryDate);
					} else if (paramName.equalsIgnoreCase("domain"))
						cookie.setDomain(paramValue);
					else if (paramName.equalsIgnoreCase("path"))
						cookie.setPath(paramValue);
					else if (paramName.equalsIgnoreCase("comment"))
						cookie.setComment(paramValue);
				}
			}
		}
		cookies.add(cookie);
	}
	return cookies;
}