Java Code Examples for javax.servlet.http.Cookie#getVersion()

The following examples show how to use javax.servlet.http.Cookie#getVersion() . 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: TestCookies.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private void test(boolean useRfc6265, String header, Cookie... expected) {
    MimeHeaders mimeHeaders = new MimeHeaders();
    ServerCookies serverCookies = new ServerCookies(4);
    CookieProcessor cookieProcessor;

    if (useRfc6265) {
        cookieProcessor = new Rfc6265CookieProcessor();
    } else {
        cookieProcessor = new LegacyCookieProcessor();
    }
    MessageBytes cookieHeaderValue = mimeHeaders.addValue("Cookie");
    byte[] bytes = header.getBytes(StandardCharsets.UTF_8);
    cookieHeaderValue.setBytes(bytes, 0, bytes.length);
    cookieProcessor.parseCookieHeader(mimeHeaders, serverCookies);
    Assert.assertEquals(expected.length, serverCookies.getCookieCount());
    for (int i = 0; i < expected.length; i++) {
        Cookie cookie = expected[i];
        ServerCookie actual = serverCookies.getCookie(i);
        Assert.assertEquals(cookie.getVersion(), actual.getVersion());
        Assert.assertEquals(cookie.getName(), actual.getName().toString());
        actual.getValue().getByteChunk().setCharset(StandardCharsets.UTF_8);
        Assert.assertEquals(cookie.getValue(),
                org.apache.tomcat.util.http.parser.Cookie.unescapeCookieValueRfc2109(
                        actual.getValue().toString()));
        if (cookie.getVersion() == 1) {
            Assert.assertEquals(cookie.getDomain(), actual.getDomain().toString());
            Assert.assertEquals(cookie.getPath(), actual.getPath().toString());
        }
    }
}
 
Example 2
Source File: HttpServletResponseImpl.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
public void addCookie(final Cookie cookie) {
    if (insideInclude) {
        return;
    }
    final ServletCookieAdaptor servletCookieAdaptor = new ServletCookieAdaptor(cookie);
    if (cookie.getVersion() == 0) {
        servletCookieAdaptor.setVersion(servletContext.getDeployment().getDeploymentInfo().getDefaultCookieVersion());
    }
    exchange.setResponseCookie(servletCookieAdaptor);
}
 
Example 3
Source File: HttpServletResponseImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void addCookie(final Cookie cookie) {
    if (insideInclude) {
        return;
    }
    final ServletCookieAdaptor servletCookieAdaptor = new ServletCookieAdaptor(cookie);
    if (cookie.getVersion() == 0) {
        servletCookieAdaptor.setVersion(servletContext.getDeployment().getDeploymentInfo().getDefaultCookieVersion());
    }
    exchange.setResponseCookie(servletCookieAdaptor);
}
 
Example 4
Source File: CookieSerializer.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@SuppressWarnings("RedundantIfStatement")
static boolean equals(final Cookie thisCookie, final Cookie thatCookie) {

   if (thisCookie.getMaxAge() != thatCookie.getMaxAge()) {
      return false;
   }
   if (thisCookie.getSecure() != thatCookie.getSecure()) {
      return false;
   }
   if (thisCookie.getVersion() != thatCookie.getVersion()) {
      return false;
   }
   if (thisCookie.getName() != null ? !thisCookie.getName().equals(
           thatCookie.getName()) : thatCookie.getName() != null) {
      return false;
   }
   if (thisCookie.getValue() != null ? !thisCookie.getValue().equals(
           thatCookie.getValue()) : thatCookie.getValue() != null) {
      return false;
   }
   if (thisCookie.getComment() != null ? !thisCookie.getComment().equals(
           thatCookie.getComment()) : thatCookie.getComment() != null) {
      return false;
   }
   if (thisCookie.getDomain() != null ? !thisCookie.getDomain().equals(
           thatCookie.getDomain()) : thatCookie.getDomain() != null) {
      return false;
   }
   if (thisCookie.getPath() != null ? !thisCookie.getPath().equals(
           thatCookie.getPath()) : thatCookie.getPath() != null) {
      return false;
   }
   return true;
}
 
Example 5
Source File: LegacyCookieProcessor.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public String generateHeader(Cookie cookie) {
    /*
     * The spec allows some latitude on when to send the version attribute
     * with a Set-Cookie header. To be nice to clients, we'll make sure the
     * version attribute is first. That means checking the various things
     * that can cause us to switch to a v1 cookie first.
     *
     * Note that by checking for tokens we will also throw an exception if a
     * control character is encountered.
     */
    int version = cookie.getVersion();
    String value = cookie.getValue();
    String path = cookie.getPath();
    String domain = cookie.getDomain();
    String comment = cookie.getComment();

    if (version == 0) {
        // Check for the things that require a v1 cookie
        if (needsQuotes(value, 0) || comment != null || needsQuotes(path, 0) || needsQuotes(domain, 0)) {
            version = 1;
        }
    }

    // Now build the cookie header
    StringBuffer buf = new StringBuffer(); // can't use StringBuilder due to DateFormat

    // Just use the name supplied in the Cookie
    buf.append(cookie.getName());
    buf.append("=");

    // Value
    maybeQuote(buf, value, version);

    // Add version 1 specific information
    if (version == 1) {
        // Version=1 ... required
        buf.append ("; Version=1");

        // Comment=comment
        if (comment != null) {
            buf.append ("; Comment=");
            maybeQuote(buf, comment, version);
        }
    }

    // Add domain information, if present
    if (domain != null) {
        buf.append("; Domain=");
        maybeQuote(buf, domain, version);
    }

    // Max-Age=secs ... or use old "Expires" format
    int maxAge = cookie.getMaxAge();
    if (maxAge >= 0) {
        if (version > 0) {
            buf.append ("; Max-Age=");
            buf.append (maxAge);
        }
        // IE6, IE7 and possibly other browsers don't understand Max-Age.
        // They do understand Expires, even with V1 cookies!
        if (version == 0 || getAlwaysAddExpires()) {
            // Wdy, DD-Mon-YY HH:MM:SS GMT ( Expires Netscape format )
            buf.append ("; Expires=");
            // To expire immediately we need to set the time in past
            if (maxAge == 0) {
                buf.append( ANCIENT_DATE );
            } else {
                COOKIE_DATE_FORMAT.get().format(
                        new Date(System.currentTimeMillis() + maxAge * 1000L),
                        buf,
                        new FieldPosition(0));
            }
        }
    }

    // Path=path
    if (path!=null) {
        buf.append ("; Path=");
        maybeQuote(buf, path, version);
    }

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

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

    SameSiteCookies sameSiteCookiesValue = getSameSiteCookies();

    if (!sameSiteCookiesValue.equals(SameSiteCookies.UNSET)) {
        buf.append("; SameSite=");
        buf.append(sameSiteCookiesValue.getValue());
    }

    return buf.toString();
}
 
Example 6
Source File: RequestUtil.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Encode a cookie as per RFC 2109. The resulting string can be used as the value for a <code>Set-Cookie</code> header.
 * 
 * @param cookie
 *          The cookie to encode.
 * @return A string following RFC 2109.
 */
public static String encodeCookie(Cookie cookie) {

	StringBuilder buf = new StringBuilder(cookie.getName());
	buf.append("=");
	buf.append(cookie.getValue());

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

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

	long age = cookie.getMaxAge();
	if (cookie.getMaxAge() >= 0) {
		buf.append("; Max-Age=\"");
		buf.append(age);
		buf.append("\"");
	}

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

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

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

	return (buf.toString());
}
 
Example 7
Source File: RequestUtil.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
 * Encode a cookie as per RFC 2109. The resulting string can be used as the value for a <code>Set-Cookie</code> header.
 * 
 * @param cookie
 *            The cookie to encode.
 * @return A string following RFC 2109.
 */
public static String encodeCookie(Cookie cookie) {

    StringBuilder buf = new StringBuilder(cookie.getName());
    buf.append("=");
    buf.append(cookie.getValue());

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

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

    if (cookie.getMaxAge() >= 0) {
        buf.append("; Max-Age=\"");
        buf.append(cookie.getMaxAge());
        buf.append("\"");
    }

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

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

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

    return (buf.toString());
}
 
Example 8
Source File: RequestUtil.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
 * Encode a cookie as per RFC 2109. The resulting string can be used as the value for a <code>Set-Cookie</code> header.
 * 
 * @param cookie
 *            The cookie to encode.
 * @return A string following RFC 2109.
 */
public static String encodeCookie(Cookie cookie) {

    StringBuilder buf = new StringBuilder(cookie.getName());
    buf.append("=");
    buf.append(cookie.getValue());

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

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

    if (cookie.getMaxAge() >= 0) {
        buf.append("; Max-Age=\"");
        buf.append(cookie.getMaxAge());
        buf.append("\"");
    }

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

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

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

    return (buf.toString());
}