@Test public void testParseNewCookie() { RuntimeDelegate rd = RuntimeDelegate.getInstance(); HeaderDelegate<Cookie> cookieHeaderDelegate = rd.createHeaderDelegate(Cookie.class); if (cookieHeaderDelegate == null) { fail("Cookie header delegate is not regestered in RuntimeDelegateImpl"); } Cookie expectedNewCookie = new Cookie("MyCookie", "MyCookieValue", ".", "mydomain", 1); String cookieToParse = "$Version=1; MyCookie=MyCookieValue; $Path=.; $Domain=mydomain, SecondCookie=Value"; Cookie parsedNewCookie = cookieHeaderDelegate.fromString(cookieToParse); assertEquals(expectedNewCookie, parsedNewCookie); expectedNewCookie = new Cookie("MyCookie", "", ".", "mydomain", 1); cookieToParse = "$Version=1; MyCookie=\"\"; $Path=.; $Domain=mydomain;"; parsedNewCookie = cookieHeaderDelegate.fromString(cookieToParse); assertEquals(expectedNewCookie, parsedNewCookie); try { cookieHeaderDelegate.fromString("Invalid Cookie"); fail("Cookie is null - IllegalArgumentException must be thrown"); } catch (IllegalArgumentException e) { } try { cookieHeaderDelegate.fromString("MyCookieName;Version=1"); fail("Cookie is invalid - IllegalArgumentException must be thrown"); } catch (IllegalArgumentException e) { } try { cookieHeaderDelegate.fromString(null); fail("Cookie is null - IllegalArgumentException must be thrown"); } catch (IllegalArgumentException e) { } }
public Cookie fromString(String cookie) throws IllegalArgumentException { if (cookie == null) { throw new IllegalArgumentException(Messages.getMessage("headerIsNull", "Cookie")); } String tokens[] = cookie.split("[;,]"); if (tokens.length <= 0) { throw new IllegalArgumentException(Messages.getMessage("invalidCookie", cookie)); } ModifiableCookie firstCookie = null; int cookieNum = 0; boolean versionSet = false; for (String token : tokens) { String[] subTokens = token.trim().split("=", 2); String name = subTokens.length > 0 ? subTokens[0] : null; String value = subTokens.length > 1 ? subTokens[1] : null; if (value != null && value.startsWith("\"") && value.endsWith("\"") && value.length() > 1) { value = value.substring(1, value.length() - 1); } if (!name.startsWith("$")) { cookieNum++; if (cookieNum > 1) { break; } if (firstCookie == null) { firstCookie = new ModifiableCookie(); } firstCookie.name = name; firstCookie.value = value; } else if (name.startsWith(VERSION)) { if (firstCookie == null) { firstCookie = new ModifiableCookie(); } else { if (versionSet) { throw new IllegalArgumentException( Messages.getMessage("cookieCannotContainAddlVers", cookie)); } } versionSet = true; firstCookie.version = Integer.parseInt(value); } else if (name.startsWith(PATH) && cookie != null) { if (firstCookie == null) { throw new IllegalArgumentException(Messages.getMessage("cookieMustStartWithVersion", cookie)); } firstCookie.path = value; } else if (name.startsWith(DOMAIN) && cookie != null) { if (firstCookie == null) { throw new IllegalArgumentException(Messages.getMessage("cookieMustStartWithVersion", cookie)); } firstCookie.domain = value; } } if (cookieNum == 0) { throw new IllegalArgumentException(Messages.getMessage("cookieDoesNotContainNAMEVALUE", cookie)); } if (firstCookie != null) { return validateAndBuildCookie(firstCookie, cookie); } else { throw new IllegalArgumentException(Messages.getMessage("failedToParseCookie", cookie)); } }
CookieHeaderDelegate implements HeaderDelegate<Cookie> { public Cookie fromString(String cookie) throws IllegalArgumentException { if (cookie == null) { throw new IllegalArgumentException(Messages.getMessage("headerIsNull", "Cookie")); } String tokens[] = cookie.split("[;,]"); if (tokens.length <= 0) { throw new IllegalArgumentException(Messages.getMessage("invalidCookie", cookie)); } ModifiableCookie firstCookie = null; int cookieNum = 0; boolean versionSet = false; for (String token : tokens) { String[] subTokens = token.trim().split("=", 2); String name = subTokens.length > 0 ? subTokens[0] : null; String value = subTokens.length > 1 ? subTokens[1] : null; if (value != null && value.startsWith("\"") && value.endsWith("\"") && value.length() > 1) { value = value.substring(1, value.length() - 1); } if (!name.startsWith("$")) { cookieNum++; if (cookieNum > 1) { break; } if (firstCookie == null) { firstCookie = new ModifiableCookie(); } firstCookie.name = name; firstCookie.value = value; } else if (name.startsWith(VERSION)) { if (firstCookie == null) { firstCookie = new ModifiableCookie(); } else { if (versionSet) { throw new IllegalArgumentException( Messages.getMessage("cookieCannotContainAddlVers", cookie)); } } versionSet = true; firstCookie.version = Integer.parseInt(value); } else if (name.startsWith(PATH) && cookie != null) { if (firstCookie == null) { throw new IllegalArgumentException(Messages.getMessage("cookieMustStartWithVersion", cookie)); } firstCookie.path = value; } else if (name.startsWith(DOMAIN) && cookie != null) { if (firstCookie == null) { throw new IllegalArgumentException(Messages.getMessage("cookieMustStartWithVersion", cookie)); } firstCookie.domain = value; } } if (cookieNum == 0) { throw new IllegalArgumentException(Messages.getMessage("cookieDoesNotContainNAMEVALUE", cookie)); } if (firstCookie != null) { return validateAndBuildCookie(firstCookie, cookie); } else { throw new IllegalArgumentException(Messages.getMessage("failedToParseCookie", cookie)); } } }
CookieHeaderDelegate implements HeaderDelegate<Cookie> { public Cookie fromString(String cookie) throws IllegalArgumentException { if (cookie == null) { throw new IllegalArgumentException(Messages.getMessage("headerIsNull", "Cookie")); } String tokens[] = cookie.split("[;,]"); if (tokens.length <= 0) { throw new IllegalArgumentException(Messages.getMessage("invalidCookie", cookie)); } ModifiableCookie firstCookie = null; int cookieNum = 0; boolean versionSet = false; for (String token : tokens) { String[] subTokens = token.trim().split("=", 2); String name = subTokens.length > 0 ? subTokens[0] : null; String value = subTokens.length > 1 ? subTokens[1] : null; if (value != null && value.startsWith("\"") && value.endsWith("\"") && value.length() > 1) { value = value.substring(1, value.length() - 1); } if (!name.startsWith("$")) { cookieNum++; if (cookieNum > 1) { break; } if (firstCookie == null) { firstCookie = new ModifiableCookie(); } firstCookie.name = name; firstCookie.value = value; } else if (name.startsWith(VERSION)) { if (firstCookie == null) { firstCookie = new ModifiableCookie(); } else { if (versionSet) { throw new IllegalArgumentException( Messages.getMessage("cookieCannotContainAddlVers", cookie)); } } versionSet = true; firstCookie.version = Integer.parseInt(value); } else if (name.startsWith(PATH) && cookie != null) { if (firstCookie == null) { throw new IllegalArgumentException(Messages.getMessage("cookieMustStartWithVersion", cookie)); } firstCookie.path = value; } else if (name.startsWith(DOMAIN) && cookie != null) { if (firstCookie == null) { throw new IllegalArgumentException(Messages.getMessage("cookieMustStartWithVersion", cookie)); } firstCookie.domain = value; } } if (cookieNum == 0) { throw new IllegalArgumentException(Messages.getMessage("cookieDoesNotContainNAMEVALUE", cookie)); } if (firstCookie != null) { return validateAndBuildCookie(firstCookie, cookie); } else { throw new IllegalArgumentException(Messages.getMessage("failedToParseCookie", cookie)); } }  }
CookieHeaderDelegate implements HeaderDelegate<Cookie> { public Cookie fromString(String cookie) throws IllegalArgumentException { if (cookie == null) { throw new IllegalArgumentException(Messages.getMessage("headerIsNull", "Cookie")); } String tokens[] = cookie.split("[;,]"); if (tokens.length <= 0) { throw new IllegalArgumentException(Messages.getMessage("invalidCookie", cookie)); } ModifiableCookie firstCookie = null; int cookieNum = 0; boolean versionSet = false; for (String token : tokens) { String[] subTokens = token.trim().split("=", 2); String name = subTokens.length > 0 ? subTokens[0] : null; String value = subTokens.length > 1 ? subTokens[1] : null; if (value != null && value.startsWith("\"") && value.endsWith("\"") && value.length() > 1) { value = value.substring(1, value.length() - 1); } if (!name.startsWith("$")) { cookieNum++; if (cookieNum > 1) { break; } if (firstCookie == null) { firstCookie = new ModifiableCookie(); } firstCookie.name = name; firstCookie.value = value; } else if (name.startsWith(VERSION)) { if (firstCookie == null) { firstCookie = new ModifiableCookie(); } else { if (versionSet) { throw new IllegalArgumentException( Messages.getMessage("cookieCannotContainAddlVers", cookie)); } } versionSet = true; firstCookie.version = Integer.parseInt(value); } else if (name.startsWith(PATH) && cookie != null) { if (firstCookie == null) { throw new IllegalArgumentException(Messages.getMessage("cookieMustStartWithVersion", cookie)); } firstCookie.path = value; } else if (name.startsWith(DOMAIN) && cookie != null) { if (firstCookie == null) { throw new IllegalArgumentException(Messages.getMessage("cookieMustStartWithVersion", cookie)); } firstCookie.domain = value; } } if (cookieNum == 0) { throw new IllegalArgumentException(Messages.getMessage("cookieDoesNotContainNAMEVALUE", cookie)); } if (firstCookie != null) { return validateAndBuildCookie(firstCookie, cookie); } else { throw new IllegalArgumentException(Messages.getMessage("failedToParseCookie", cookie)); } }  Cookie fromString(String cookie); String toString(Cookie cookie); }
CookieHeaderDelegate implements HeaderDelegate<Cookie> { public Cookie fromString(String cookie) throws IllegalArgumentException { if (cookie == null) { throw new IllegalArgumentException(Messages.getMessage("headerIsNull", "Cookie")); } String tokens[] = cookie.split("[;,]"); if (tokens.length <= 0) { throw new IllegalArgumentException(Messages.getMessage("invalidCookie", cookie)); } ModifiableCookie firstCookie = null; int cookieNum = 0; boolean versionSet = false; for (String token : tokens) { String[] subTokens = token.trim().split("=", 2); String name = subTokens.length > 0 ? subTokens[0] : null; String value = subTokens.length > 1 ? subTokens[1] : null; if (value != null && value.startsWith("\"") && value.endsWith("\"") && value.length() > 1) { value = value.substring(1, value.length() - 1); } if (!name.startsWith("$")) { cookieNum++; if (cookieNum > 1) { break; } if (firstCookie == null) { firstCookie = new ModifiableCookie(); } firstCookie.name = name; firstCookie.value = value; } else if (name.startsWith(VERSION)) { if (firstCookie == null) { firstCookie = new ModifiableCookie(); } else { if (versionSet) { throw new IllegalArgumentException( Messages.getMessage("cookieCannotContainAddlVers", cookie)); } } versionSet = true; firstCookie.version = Integer.parseInt(value); } else if (name.startsWith(PATH) && cookie != null) { if (firstCookie == null) { throw new IllegalArgumentException(Messages.getMessage("cookieMustStartWithVersion", cookie)); } firstCookie.path = value; } else if (name.startsWith(DOMAIN) && cookie != null) { if (firstCookie == null) { throw new IllegalArgumentException(Messages.getMessage("cookieMustStartWithVersion", cookie)); } firstCookie.domain = value; } } if (cookieNum == 0) { throw new IllegalArgumentException(Messages.getMessage("cookieDoesNotContainNAMEVALUE", cookie)); } if (firstCookie != null) { return validateAndBuildCookie(firstCookie, cookie); } else { throw new IllegalArgumentException(Messages.getMessage("failedToParseCookie", cookie)); } }  Cookie fromString(String cookie); String toString(Cookie cookie);  }