HeaderHelper { public static NewCookie parseNewCookie(String newCookieString) { int p = 0; int n = findCookieParameterSeparator(newCookieString, p); int separator = -1; if (n > 0 && n < newCookieString.length()) { separator = newCookieString.charAt(n); } NewCookieBuilder newCookieBuilder = null; while (p < newCookieString.length()) { String pair = newCookieString.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(); } if (newCookieBuilder == null) { newCookieBuilder = aNewCookie().withName(name).withValue(value).withVersion(Cookie.DEFAULT_VERSION); } else { if (name.equalsIgnoreCase("version")) { newCookieBuilder.withVersion(Integer.parseInt(value)); } else if (name.equalsIgnoreCase("domain")) { newCookieBuilder.withDomain(value); } else if (name.equalsIgnoreCase("path")) { newCookieBuilder.withPath(value); } else if (name.equalsIgnoreCase("secure")) { newCookieBuilder.withSecure(true); } else if (name.equalsIgnoreCase("HttpOnly")) { newCookieBuilder.withHttpOnly(true); } else if (name.equalsIgnoreCase("Max-Age")) { newCookieBuilder.withMaxAge(Integer.parseInt(value)); } else if (name.equalsIgnoreCase("expires")) { try { newCookieBuilder.withExpiry(parseDateHeader(value)); } catch (IllegalArgumentException ignored) { ignored.printStackTrace(); } } else if (name.equalsIgnoreCase("comment")) { newCookieBuilder.withComment(value); } } if (separator == -1) { break; } p = n + 1; n = scan(newCookieString, p, (char)separator); } if (newCookieBuilder == null) { return null; } return newCookieBuilder.build(); } private  HeaderHelper(); static List<AcceptMediaType> createAcceptMediaTypeList(String header); static List<AcceptLanguage> createAcceptedLanguageList(String header); static List<AcceptToken> createAcceptedCharsetList(String header); static List<AcceptToken> createAcceptedEncodingList(String header); static List<Cookie> parseCookies(String cookiesString); static NewCookie parseNewCookie(String newCookieString); static Date parseDateHeader(String header); static String formatDate(Date date); static long getContentLengthLong(MultivaluedMap<String, String> httpHeaders); @SuppressWarnings({"unchecked"}) static String getHeaderAsString(Object o); static String convertToString(Collection<String> collection); static final Comparator<QualityValue> QUALITY_VALUE_COMPARATOR; }
@UseDataProvider("forParsesNewCookies") @Test public void parsesNewCookies(String cookieHeader, NewCookie expectedNewCookie) { NewCookie newCookie = HeaderHelper.parseNewCookie(cookieHeader); assertNotNull(newCookie); assertEquals(expectedNewCookie.getName(), newCookie.getName()); assertEquals(expectedNewCookie.getValue(), newCookie.getValue()); if (expectedNewCookie.getExpiry() != null) { assertTrue(String.format("Expiry dates are not equal. Expected %s, actual %s", expectedNewCookie.getExpiry(), newCookie.getExpiry()), Math.abs(expectedNewCookie.getExpiry().getTime() - newCookie.getExpiry().getTime()) < 1000); } assertEquals(expectedNewCookie.getMaxAge(), newCookie.getMaxAge()); assertEquals(expectedNewCookie.getPath(), newCookie.getPath()); assertEquals(expectedNewCookie.getDomain(), newCookie.getDomain()); assertEquals(expectedNewCookie.getComment(), newCookie.getComment()); assertEquals(expectedNewCookie.getVersion(), newCookie.getVersion()); assertEquals(expectedNewCookie.isSecure(), newCookie.isSecure()); assertEquals(expectedNewCookie.isHttpOnly(), newCookie.isHttpOnly()); }