Java Code Examples for javax.ws.rs.core.Cookie#DEFAULT_VERSION

The following examples show how to use javax.ws.rs.core.Cookie#DEFAULT_VERSION . 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: FormWebUiAuthenticationFilter.java    From presto with Apache License 2.0 5 votes vote down vote up
private NewCookie createAuthenticationCookie(String userName, boolean secure)
{
    String jwt = jwtGenerator.apply(userName);
    return new NewCookie(
            PRESTO_UI_COOKIE,
            jwt,
            "/ui",
            null,
            Cookie.DEFAULT_VERSION,
            null,
            NewCookie.DEFAULT_MAX_AGE,
            null,
            secure,
            true);
}
 
Example 2
Source File: FormWebUiAuthenticationFilter.java    From presto with Apache License 2.0 5 votes vote down vote up
public static NewCookie getDeleteCookie(boolean secure)
{
    return new NewCookie(
            PRESTO_UI_COOKIE,
            "delete",
            "/ui",
            null,
            Cookie.DEFAULT_VERSION,
            null,
            0,
            null,
            secure,
            true);
}
 
Example 3
Source File: CookieHeaderProvider.java    From msf4j with Apache License 2.0 5 votes vote down vote up
@Override
public String toString(Cookie cookie) {
    StringBuilder sb = new StringBuilder();

    if (cookie.getVersion() != Cookie.DEFAULT_VERSION) {
        sb.append(VERSION).append('=').append(cookie.getVersion()).append(';');
    }
    sb.append(cookie.getName()).append('=').append(cookie.getValue());
    if (cookie.getPath() != null) {
        sb.append(';').append(PATH).append('=').append(cookie.getPath());
    }
    if (cookie.getDomain() != null) {
        sb.append(';').append(DOMAIN).append('=').append(cookie.getDomain());
    }
    if (cookie instanceof NewCookie) {
        NewCookie newCookie = (NewCookie) cookie;
        if (newCookie.getMaxAge() != NewCookie.DEFAULT_MAX_AGE) {
            sb.append(';').append(MAX_AGE).append('=').append(newCookie.getMaxAge());
        }
        if (newCookie.getComment() != null) {
            sb.append(';').append(COMMENT).append('=').append(newCookie.getComment());
        }
        if (newCookie.getExpiry() != null) {
            //All HTTP date/time stamps MUST be represented in Greenwich Mean Time (GMT)
            dateFormat.setTimeZone(TimeZone.getTimeZone(GMT_TIMEZONE));
            sb.append(';').append(EXPIRES).append('=').append(dateFormat.format(newCookie.getExpiry()));
        }
        if (newCookie.isSecure()) {
            sb.append(';').append(SECURE);
        }
        if (newCookie.isHttpOnly()) {
            sb.append(';').append(HTTP_ONLY);
        }
    }
    return sb.toString();
}
 
Example 4
Source File: ClientSideState.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("JdkObsolete")
public NewCookie persist(final String key, final String path, final Object value) {
    final Date expiry = Date.from(ZonedDateTime.now(ZoneOffset.UTC).plusSeconds(timeout).toInstant());
    return new NewCookie(key, protect(value), path, null, Cookie.DEFAULT_VERSION, null, timeout, expiry, true, false);
}
 
Example 5
Source File: NewCookieHeaderProvider.java    From cxf with Apache License 2.0 4 votes vote down vote up
public NewCookie fromString(String c) {

        if (c == null) {
            throw new IllegalArgumentException("SetCookie value can not be null");
        }

        String name = null;
        String value = null;
        String path = null;
        String domain = null;
        String comment = null;
        int maxAge = NewCookie.DEFAULT_MAX_AGE;
        boolean isSecure = false;
        Date expires = null;
        boolean httpOnly = false;
        int version = Cookie.DEFAULT_VERSION;

        String[] tokens = c.split(";");
        for (String token : tokens) {
            String theToken = token.trim();

            int sepIndex = theToken.indexOf('=');
            String paramName = sepIndex != -1 ? theToken.substring(0, sepIndex) : theToken;
            String paramValue = null;

            if (sepIndex == theToken.length() - 1) {
                paramValue = "";
            } else if (sepIndex != -1) {
                paramValue = theToken.substring(sepIndex + 1);
            }

            if (paramValue != null) {
                paramValue = stripQuotes(paramValue);
            }

            if (paramName.equalsIgnoreCase(MAX_AGE)) {
                maxAge = Integer.parseInt(paramValue);
            } else if (paramName.equalsIgnoreCase(PATH)) {
                path = paramValue;
            } else if (paramName.equalsIgnoreCase(DOMAIN)) {
                domain = paramValue;
            } else if (paramName.equalsIgnoreCase(COMMENT)) {
                comment = paramValue;
            } else if (paramName.equalsIgnoreCase(SECURE)) {
                isSecure = true;
            } else if (paramName.equalsIgnoreCase(EXPIRES)) {
                expires = HttpUtils.getHttpDate(paramValue);
            } else if (paramName.equalsIgnoreCase(HTTP_ONLY)) {
                httpOnly = true;
            } else if (paramName.equalsIgnoreCase(VERSION)) {
                version = Integer.parseInt(paramValue);
            } else if (paramValue != null) {
                name = paramName;
                value = paramValue;
            }
        }

        if (name == null || value == null) {
            throw new IllegalArgumentException("Set-Cookie is malformed : " + c);
        }

        return new NewCookie(name, value, path, domain, version, comment, maxAge, expires, isSecure, httpOnly);
    }
 
Example 6
Source File: HeaderHelper.java    From everrest with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Parses cookie header string and create collection of cookie from it.
 *
 * @param cookiesString
 *         the cookie string.
 * @return collection of Cookie.
 */
public static List<Cookie> parseCookies(String cookiesString) {
    final List<Cookie> cookies = new ArrayList<>();

    int p = 0;
    int n;
    CookieBuilder cookieBuilder = null;
    int version = Cookie.DEFAULT_VERSION;

    while (p < cookiesString.length()) {
        n = findCookieParameterSeparator(cookiesString, p);

        String pair = cookiesString.substring(p, n);

        String name;
        String value = "";

        int eq = scan(pair, '=');
        if (charAtIs(pair, eq, '=')) {
            name = pair.substring(0, eq).trim();
            value = pair.substring(eq + 1).trim();
            if (value.length() > 1 && value.startsWith("\"") && value.endsWith("\"")) {
                value = value.substring(1, value.length() - 1);
            }
        } else {
            name = pair.trim();
        }

        // Name of parameter not start from '$', then it is cookie name and value.
        // In header string name/value pair (name without '$') SHOULD be before
        // '$Path' and '$Domain' parameters, but '$Version' goes before name/value pair.
        if (doesNotContain(name, '$')) {
            if (cookieBuilder != null) {
                cookies.add(cookieBuilder.build());
            }
            cookieBuilder = aCookie().withName(name).withValue(value);
            // version was kept before http://www.ietf.org/rfc/rfc2109.txt section 4.4
            cookieBuilder.withVersion(version);
        } else if ("$Version".equalsIgnoreCase(name)) {
            version = Integer.valueOf(value);
        } else if (cookieBuilder != null && "$Path".equalsIgnoreCase(name)) {
            cookieBuilder.withPath(value);
        } else if (cookieBuilder != null && "$Domain".equalsIgnoreCase(name)) {
            cookieBuilder.withDomain(value);
        }

        p = n + 1;
    }

    if (cookieBuilder != null) {
        cookies.add(cookieBuilder.build());
    }

    return cookies;
}