private boolean testCookie( Cookie ck, String name, String value, String path, String domain, int version) throws Exception { StringBuffer sb = new StringBuffer(); boolean pass = true; if (name == "" || name == null) { pass = false; sb.append("Cookie's name is empty"); } else if (!ck.getName().toLowerCase().equals(name)) { pass = false; sb.append("Failed name test. Expect " + name + " got " + ck.getName()); } if (value == "" || value == null) { pass = false; sb.append("Cookie's value is empty"); } else if (!ck.getValue().toLowerCase().equals(value)) { pass = false; sb.append("Failed value test. Expect " + value + " got " + ck.getValue()); } if (ck.getVersion() != version) { pass = false; sb.append("Failed version test. Expect " + version + " got " + ck.getVersion()); } if (path == "" || path == null) { if (ck.getPath() != "" && ck.getPath() != null) { pass = false; sb.append("Failed path test. Expect null String, got " + ck.getPath()); } } else if (ck.getPath() == null || ck.getPath() == "") { pass = false; sb.append("Failed path test. Got null, expecting " + path); } else if (!ck.getPath().toLowerCase().equals(path)) { pass = false; sb.append("Failed path test. Expect " + path + " got " + ck.getPath()); } if (domain == "" || domain == null) { if (ck.getDomain() != "" && ck.getDomain() != null) { pass = false; sb.append("Failed path test. Expect " + domain + " got " + ck.getDomain()); } } else if (!ck.getDomain().toLowerCase().equals(domain)) { pass = false; sb.append("Failed domain test. Expect " + domain + " got " + ck.getDomain()); } if (!pass) { throw new Exception("At least one assertion falied: " + sb.toString()); } return pass; }
private static Cookie toCookie(String one) { try { Cookie c = Cookie.valueOf(one); System.out.println("Cookie name=" + c.getName() + "; value=[" + c.getValue() + "]"); return c; } catch (Exception c) { return new Cookie("", ""); } }
static Map<String, Cookie> extractCookies(MultivaluedMap<String, String> headers) { Map<String, Cookie> cookies = new HashMap<String, Cookie>(); List<String> cookieHeaders = headers.get("Cookie"); if (cookieHeaders == null) return cookies; for (String cookieHeader : cookieHeaders) { for (Cookie cookie : CookieParser.parseCookies(cookieHeader)) { cookies.put(cookie.getName(), cookie); } } return cookies; }
@SuppressWarnings("unchecked") public static Authentication getAuthenticationToken(Message message) { Map<String, List<String>> headers = (Map<String, List<String>>) message.get(Message.PROTOCOL_HEADERS); String token = null; for (Cookie c : extractCookies(headers.get("cookie"))) { if (AUTH_TOKEN.equalsIgnoreCase(c.getName())) { token = c.getValue(); break; } } if (token == null) { return null; } try { return (Authentication) Base64.decodeToObject(token); } catch (Exception e) { return null; } }