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

The following examples show how to use java.net.HttpCookie#setSecure() . 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: NMBApplication.java    From Nimingban with Apache License 2.0 6 votes vote down vote up
public static void updateCookies(Context context) {
    SimpleCookieStore cookieStore = NMBApplication.getSimpleCookieStore(context);

    URL url = ACSite.getInstance().getSiteUrl();
    HttpCookieWithId hcwi = cookieStore.getCookie(url, "userId");
    if (hcwi != null) {
        HttpCookie oldCookie = hcwi.httpCookie;
        cookieStore.remove(url, oldCookie);

        HttpCookie newCookie = new HttpCookie("userhash", oldCookie.getValue());
        newCookie.setComment(oldCookie.getComment());
        newCookie.setCommentURL(oldCookie.getCommentURL());
        newCookie.setDiscard(oldCookie.getDiscard());
        newCookie.setDomain(oldCookie.getDomain());
        newCookie.setMaxAge(oldCookie.getMaxAge());
        newCookie.setPath(oldCookie.getPath());
        newCookie.setPortlist(oldCookie.getPortlist());
        newCookie.setSecure(oldCookie.getSecure());
        newCookie.setVersion(oldCookie.getVersion());

        cookieStore.add(url, newCookie);
    }
}
 
Example 2
Source File: AbstractCookiesTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * CookieStoreImpl has a strict requirement on HttpCookie.equals() to enable replacement of
 * cookies with the same name.
 */
public void testCookieEquality() throws Exception {
    HttpCookie baseCookie = createCookie("theme", "light", "a.com", "/");

    // None of the attributes immediately below should affect equality otherwise CookieStoreImpl
    // eviction will not work as intended.
    HttpCookie valueCookie = createCookie("theme", "light", "a.com", "/");
    valueCookie.setValue("dark");
    valueCookie.setPortlist("1234");
    valueCookie.setSecure(true);
    valueCookie.setComment("comment");
    valueCookie.setCommentURL("commentURL");
    valueCookie.setDiscard(true);
    valueCookie.setMaxAge(12345L);
    valueCookie.setVersion(1);
    assertEquals(baseCookie, valueCookie);

    // Changing any of the 3 main identity attributes should render cookies unequal.
    assertNotEquals(createCookie("theme2", "light", "a.com", "/"), baseCookie);
    assertNotEquals(createCookie("theme", "light", "b.com", "/"), baseCookie);
    assertNotEquals(createCookie("theme", "light", "a.com", "/path"), baseCookie);
}
 
Example 3
Source File: SerializableHttpCookie.java    From 4pdaClient-plus with Apache License 2.0 6 votes vote down vote up
private void readObject(ObjectInputStream in) throws IOException,
        ClassNotFoundException {
    String name = (String) in.readObject();
    String value = (String) in.readObject();
    cookie = new HttpCookie(name, value);
    cookie.setComment((String) in.readObject());
    cookie.setCommentURL((String) in.readObject());
    cookie.setDomain((String) in.readObject());
    cookie.setMaxAge(in.readLong());
    cookie.setPath((String) in.readObject());
    cookie.setPortlist((String) in.readObject());
    cookie.setVersion(in.readInt());
    cookie.setSecure(in.readBoolean());
    cookie.setDiscard(in.readBoolean());
    setHttpOnly(in.readBoolean());
}
 
Example 4
Source File: SerializableHttpCookie.java    From DaVinci with Apache License 2.0 6 votes vote down vote up
private void readObject(ObjectInputStream in) throws IOException,
        ClassNotFoundException {
    String name = (String) in.readObject();
    String value = (String) in.readObject();
    cookie = new HttpCookie(name, value);
    cookie.setComment((String) in.readObject());
    cookie.setCommentURL((String) in.readObject());
    cookie.setDomain((String) in.readObject());
    cookie.setMaxAge(in.readLong());
    cookie.setPath((String) in.readObject());
    cookie.setPortlist((String) in.readObject());
    cookie.setVersion(in.readInt());
    cookie.setSecure(in.readBoolean());
    cookie.setDiscard(in.readBoolean());
    setHttpOnly(in.readBoolean());
}
 
Example 5
Source File: CookieEntity.java    From NoHttp with Apache License 2.0 6 votes vote down vote up
/**
 * Into {@link HttpCookie}.
 *
 * @return {@link HttpCookie}.
 */
public HttpCookie toHttpCookie() {
    HttpCookie cookie = new HttpCookie(name, value);
    cookie.setComment(comment);
    cookie.setCommentURL(commentURL);
    cookie.setDiscard(discard);
    cookie.setDomain(domain);
    if (expiry == -1L)
        cookie.setMaxAge(-1L);
    else
        cookie.setMaxAge((expiry - System.currentTimeMillis()) / 1000L);
    cookie.setPath(path);
    cookie.setPortlist(portList);
    cookie.setSecure(secure);
    cookie.setVersion(version);
    return cookie;
}
 
Example 6
Source File: HttpResultCoder.java    From redkale with Apache License 2.0 6 votes vote down vote up
public static List<HttpCookie> getCookieList(ByteBuffer buffer) {
    int len = buffer.getChar();
    if (len == 0) return null;
    final List<HttpCookie> list = new ArrayList<>(len);
    for (int i = 0; i < len; i++) {
        HttpCookie cookie = new HttpCookie(getShortString(buffer), getShortString(buffer));
        cookie.setDomain(getShortString(buffer));
        cookie.setPath(getShortString(buffer));
        cookie.setPortlist(getShortString(buffer));
        cookie.setMaxAge(buffer.getLong());
        cookie.setSecure(buffer.get() == 1);
        cookie.setHttpOnly(buffer.get() == 1);
        list.add(cookie);
    }
    return list;
}
 
Example 7
Source File: TransportableHttpCookie.java    From Nimingban with Apache License 2.0 6 votes vote down vote up
@Nullable
public HttpCookie to() {
    if (name == null || value == null) {
        return null;
    }

    HttpCookie cookie = new HttpCookie(name, value);
    cookie.setComment(comment);
    cookie.setCommentURL(commentURL);
    cookie.setDiscard(discard);
    cookie.setDomain(domain);
    cookie.setMaxAge(maxAge);
    cookie.setPath(path);
    cookie.setPortlist(portList);
    cookie.setSecure(secure);
    cookie.setVersion(version);

    return cookie;
}
 
Example 8
Source File: SerializableCookie.java    From httplite with Apache License 2.0 6 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 HttpCookie(name, value);

    clientCookie.setComment((String) in.readObject());
    clientCookie.setCommentURL((String) in.readObject());
    clientCookie.setDomain((String) in.readObject());
    clientCookie.setPath((String) in.readObject());

    clientCookie.setMaxAge(in.readLong());
    clientCookie.setVersion(in.readInt());

    clientCookie.setSecure(in.readBoolean());
    clientCookie.setDiscard(in.readBoolean());
}
 
Example 9
Source File: SerializableHttpCookie.java    From AndroidStudyDemo with GNU General Public License v2.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();
    mClientCookie = new HttpCookie(name, value);
    mClientCookie.setComment((String) in.readObject());
    mClientCookie.setCommentURL((String) in.readObject());
    mClientCookie.setDomain((String) in.readObject());
    mClientCookie.setMaxAge(in.readLong());
    mClientCookie.setPath((String) in.readObject());
    mClientCookie.setPortlist((String) in.readObject());
    mClientCookie.setVersion(in.readInt());
    mClientCookie.setSecure(in.readBoolean());
    mClientCookie.setDiscard(in.readBoolean());
}
 
Example 10
Source File: CookieDBJar.java    From Nimingban with Apache License 2.0 5 votes vote down vote up
private HttpCookie cookie2HttpCookie(Cookie cookie) {
    HttpCookie httpCookie = new HttpCookie(cookie.name(), cookie.value());
    if (cookie.expiresAt() < System.currentTimeMillis()) {
        httpCookie.setMaxAge(-100L);
    } else {
        httpCookie.setMaxAge((cookie.expiresAt() - System.currentTimeMillis()) / 1000);
    }
    httpCookie.setDomain(cookie.domain());
    httpCookie.setPath(cookie.path());
    httpCookie.setSecure(cookie.secure());
    return httpCookie;
}
 
Example 11
Source File: ZosmfScheme.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
private void createCookie(Cookies cookies, String name, String token) {
    HttpCookie jwtCookie = new HttpCookie(name, token);
    jwtCookie.setSecure(true);
    jwtCookie.setHttpOnly(true);
    jwtCookie.setVersion(0);
    cookies.set(jwtCookie);
}
 
Example 12
Source File: SerializableHttpCookie.java    From ratebeer with GNU General Public License v3.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();
	cookie = new HttpCookie(name, value);
	cookie.setComment((String) in.readObject());
	cookie.setCommentURL((String) in.readObject());
	cookie.setDomain((String) in.readObject());
	cookie.setMaxAge(in.readLong());
	cookie.setPath((String) in.readObject());
	cookie.setPortlist((String) in.readObject());
	cookie.setVersion(in.readInt());
	cookie.setSecure(in.readBoolean());
	cookie.setDiscard(in.readBoolean());
	setHttpOnly(in.readBoolean());
}
 
Example 13
Source File: JsonCookie.java    From keywhiz with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 14
Source File: SerializableHttpCookie.java    From mattermost-android-classic 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 HttpCookie(name, value);
    clientCookie.setComment((String) in.readObject());
    clientCookie.setCommentURL((String) in.readObject());
    clientCookie.setDomain((String) in.readObject());
    clientCookie.setMaxAge(in.readLong());
    clientCookie.setPath((String) in.readObject());
    clientCookie.setPortlist((String) in.readObject());
    clientCookie.setVersion(in.readInt());
    clientCookie.setSecure(in.readBoolean());
    clientCookie.setDiscard(in.readBoolean());
}
 
Example 15
Source File: SerializableHttpCookie.java    From NewsMe 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 HttpCookie(name, value);
    clientCookie.setComment((String) in.readObject());
    clientCookie.setCommentURL((String) in.readObject());
    clientCookie.setDomain((String) in.readObject());
    clientCookie.setMaxAge(in.readLong());
    clientCookie.setPath((String) in.readObject());
    clientCookie.setPortlist((String) in.readObject());
    clientCookie.setVersion(in.readInt());
    clientCookie.setSecure(in.readBoolean());
    clientCookie.setDiscard(in.readBoolean());
}
 
Example 16
Source File: SerializableHttpCookie.java    From mimi-reader 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();
        cookie = new HttpCookie(name, value);
        cookie.setComment((String) in.readObject());
        cookie.setCommentURL((String) in.readObject());
        cookie.setDomain((String) in.readObject());
        cookie.setMaxAge(in.readLong());
        cookie.setPath((String) in.readObject());
        cookie.setPortlist((String) in.readObject());
        cookie.setVersion(in.readInt());
        cookie.setSecure(in.readBoolean());
        cookie.setDiscard(in.readBoolean());
//        setHttpOnly(in.readBoolean());
    }
 
Example 17
Source File: AbstractCookiesTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testCookieStoreGet() throws Exception {
    CookieStore cookieStore = new CookieManager(createCookieStore(), null).getCookieStore();
    HttpCookie cookiePort1 = createCookie("a1", "android", "a.com", "/path1");
    HttpCookie cookiePort2 = createCookie("a2", "android", "a.com", "/path2");
    HttpCookie secureCookie = createCookie("a3", "android", "a.com", "/path3");
    secureCookie.setSecure(true);
    HttpCookie notSecureCookie = createCookie("a4", "android", "a.com", "/path4");

    HttpCookie bCookie = createCookie("b1", "android", "b.com", "/path5");

    cookieStore.add(new URI("http://a.com:443/path1"), cookiePort1);
    cookieStore.add(new URI("http://a.com:8080/path2"), cookiePort2);
    cookieStore.add(new URI("https://a.com:443/path3"), secureCookie);
    cookieStore.add(new URI("https://a.com:443/path4"), notSecureCookie);
    cookieStore.add(new URI("https://b.com:8080/path5"), bCookie);

    List<HttpCookie> expectedStoreCookies = new ArrayList<>();
    expectedStoreCookies.add(cookiePort1);
    expectedStoreCookies.add(cookiePort2);
    expectedStoreCookies.add(secureCookie);
    expectedStoreCookies.add(notSecureCookie);

    // The default CookieStore implementation on Android is currently responsible for matching
    // the host/domain but not handling other cookie rules: it ignores the scheme (e.g. "secure"
    // checks), port and path.
    // The tests below fail on the RI. It looks like in the RI it is CookieStoreImpl that is
    // enforcing "secure" checks.
    assertEquals(expectedStoreCookies, cookieStore.get(new URI("http://a.com:443/anypath")));
    assertEquals(expectedStoreCookies, cookieStore.get(new URI("http://a.com:8080/anypath")));
    assertEquals(expectedStoreCookies, cookieStore.get(new URI("https://a.com/anypath")));
    assertEquals(expectedStoreCookies, cookieStore.get(new URI("http://a.com/anypath")));
}
 
Example 18
Source File: AbstractCookiesTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testCookieStoreGetWithSecure() throws Exception {
    CookieStore cookieStore = new CookieManager(createCookieStore(), null).getCookieStore();
    HttpCookie cookie = createCookie("theme", "light", "a.com", "/path");
    cookie.setSecure(true);
    cookieStore.add(new URI("https://a.com/path"), cookie);

    // CookieStoreImpl on Android ignores the "Secure" attribute. The RI implements the secure
    // check in CookieStoreImpl. For safety / app compatibility, if this is changed Android
    // should probably implement it in both places.
    assertEquals(1, cookieStore.get(new URI("http://a.com/path")).size());
    assertEquals(1, cookieStore.get(new URI("https://a.com/path")).size());
}
 
Example 19
Source File: SerializableHttpCookie.java    From Social 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 HttpCookie(name, value);
    clientCookie.setComment((String) in.readObject());
    clientCookie.setCommentURL((String) in.readObject());
    clientCookie.setDomain((String) in.readObject());
    clientCookie.setMaxAge(in.readLong());
    clientCookie.setPath((String) in.readObject());
    clientCookie.setPortlist((String) in.readObject());
    clientCookie.setVersion(in.readInt());
    clientCookie.setSecure(in.readBoolean());
    clientCookie.setDiscard(in.readBoolean());
}