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

The following examples show how to use java.net.HttpCookie#setVersion() . 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: 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 2
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 3
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 4
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 5
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 6
Source File: XmlPersistenceRead.java    From rest-client with Apache License 2.0 6 votes vote down vote up
private List<HttpCookie> getCookiesFromCookiesNode(final Element node) 
        throws XMLException {
    List<HttpCookie> out = new ArrayList<>();
    
    for (int i = 0; i < node.getChildElements().size(); i++) {
        Element e = node.getChildElements().get(i);
        if(!"cookie".equals(e.getQualifiedName())) {
            throw new XMLException("<cookies> element should contain only <cookie> elements");
        }
        
        HttpCookie cookie = new HttpCookie(e.getAttributeValue("name"),
                e.getAttributeValue("value"));
        final String cookieVerStr = e.getAttributeValue("version");
        if(StringUtil.isNotEmpty(cookieVerStr)) {
            cookie.setVersion(Integer.parseInt(cookieVerStr));
        }
        else {
            cookie.setVersion(CookieVersion.DEFAULT_VERSION.getIntValue());
        }
        out.add(cookie);
    }
    
    return out;
}
 
Example 7
Source File: AbstractCookiesTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void testCookieWithNullPath() throws Exception {
    FakeSingleCookieStore fscs = new FakeSingleCookieStore();
    CookieManager cm = new CookieManager(fscs, CookiePolicy.ACCEPT_ALL);

    HttpCookie cookie = new HttpCookie("foo", "bar");
    cookie.setDomain("http://www.foo.com");
    cookie.setVersion(0);

    fscs.setNextCookie(cookie);

    Map<String, List<String>> cookieHeaders = cm.get(
            new URI("http://www.foo.com/log/me/in"), Collections.EMPTY_MAP);

    List<String> cookies = cookieHeaders.get("Cookie");
    assertEquals("foo=bar", cookies.get(0));
}
 
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: WebSocketCookieStore.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public List<HttpCookie> get(final URI uri) {
    final List<HttpCookie> cookies = new ArrayList<>();
    try {
        final String urlString = uri.toString().replace("ws://", "http://").replace("wss://", "https://");
        final java.net.URL url = new java.net.URL(urlString);
        for (final Cookie cookie : webClient_.getCookies(url)) {
            final HttpCookie httpCookie = new HttpCookie(cookie.getName(), cookie.getValue());
            httpCookie.setVersion(0);
            cookies.add(httpCookie);
        }
    }
    catch (final Exception e) {
        throw new RuntimeException(e);
    }
    return cookies;
}
 
Example 10
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 11
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());
}
 
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: 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 14
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 15
Source File: WebSocketClient.java    From aliyun-cupid-sdk with Apache License 2.0 5 votes vote down vote up
private void setLoadBalanceKey(ClientUpgradeRequest request) {
    if (SDKConstants.CUPID_INTERACTION_SUB_PROTOCOL_CLIENT.equals(subProtocol)
            && loadBalanceHashKey != null) {
        // add hash_key cookie for load balance
        int cookieVersion = 0;
        String lbKey = loadBalanceHashKey;
        if (loadBalanceHashKey.contains("\"")) {
            cookieVersion = 1;
            lbKey = lbKey.replaceAll("\"", "");
        }
        HttpCookie cookie = new HttpCookie(SDKConstants.CUPID_INTERACTION_COOKIE_HASH_KEY, lbKey);
        cookie.setVersion(cookieVersion);
        request.getCookies().add(cookie);
    }
}
 
Example 16
Source File: RequestUtilsTest.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
@Test
void givenRequestWithoutCookieHeader_whenSetCookie_thenCreatesCookieHeaderAndCookie() {
    doReturn(new Header[] { contentLenght, pragma}).when(request).getAllHeaders();
    ArgumentCaptor<Header[]> argument = ArgumentCaptor.forClass(Header[].class);
    HttpCookie newCookie = new HttpCookie("cookie1", "cookie1");
    newCookie.setVersion(0);

    wrapper.setCookie(newCookie);
    verify(request).setHeaders(argument.capture());
    assertThat(argument.getValue().length, is(3));
    assertThat(Arrays.asList(argument.getValue()), hasItem(hasToString("Cookie: cookie1=cookie1")));
}
 
Example 17
Source File: CookiesTest.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
@Test
void givenRequestWithoutCookieHeader_whenSetCookie_thenCreatesCookieHeaderAndCookie() {
    doReturn(new Header[] {}).when(request).getHeaders(HttpHeaders.COOKIE);
    ArgumentCaptor<Header> argument = ArgumentCaptor.forClass(Header.class);
    HttpCookie newCookie = new HttpCookie("cookie1", "cookie1");
    newCookie.setVersion(0);

    underTest.set(newCookie);
    verify(request).setHeader(argument.capture());
    assertThat(argument.getValue().toString(), is("Cookie: cookie1=cookie1"));
}
 
Example 18
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 19
Source File: JsonCookieTest.java    From keywhiz with Apache License 2.0 5 votes vote down vote up
@Test public void cookiesAlwaysVersion1() throws Exception {
  HttpCookie cookieVersionZero = new HttpCookie("session", "session-contents");
  cookieVersionZero.setPath("/");
  cookieVersionZero.setDomain("localhost");
  cookieVersionZero.setVersion(0);

  HttpCookie convertedCookie = JsonCookie.toHttpCookie(
      JsonCookie.fromHttpCookie(cookieVersionZero));
  assertThat(convertedCookie.getVersion()).isEqualTo(1);
}
 
Example 20
Source File: ResponseSetCookieListDissector.java    From logparser with Apache License 2.0 4 votes vote down vote up
@Override
public void dissect(final Parsable<?> parsable, final String inputname) throws DissectionFailure {
    final ParsedField field = parsable.getParsableField(INPUT_TYPE, inputname);

    final String fieldValue = field.getValue().getString();
    if (fieldValue == null || fieldValue.isEmpty()){
        return; // Nothing to do here
    }

    // This input is a ', ' separated list.
    // But the expires field can contain a ','
    // and HttpCookie.parse(...) doesn't always work :(
    String[] parts = fieldValue.split(SPLIT_BY);

    String previous="";
    for (String part:parts) {
        int expiresIndex = part.toLowerCase().indexOf("expires=");
        if (expiresIndex != -1) {
            if (part.length() - minimalExpiresLength < expiresIndex) {
                previous = part;
                continue;
            }
        }
        String value = part;
        if (!previous.isEmpty()) {
            value = previous+ SPLIT_BY +part;
            previous="";
        }

        List<HttpCookie> cookies = HttpCookie.parse(value);

        for (HttpCookie cookie : cookies) {
            cookie.setVersion(1);
            String cookieName = cookie.getName().toLowerCase();
            if (wantAllCookies || requestedCookies.contains(cookieName)) {
                parsable.addDissection(inputname, "HTTP.SETCOOKIE", cookieName, value);
            }
        }
    }

}