From 9164a892dd9371e67bfc6cac50973b4588cd861d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Manuel=20Carrasco=20Mo=C3=B1ino?= Date: Tue, 21 Jan 2014 07:34:14 +0000 Subject: [PATCH] managing cookies --- .../gwt/query/client/plugins/ajax/Ajax.java | 6 +- .../google/gwt/query/vm/AjaxTransportJre.java | 7 + .../google/gwt/query/vm/CookieManager.java | 217 ++++++++++++++++++ .../google/gwt/query/vm/JsonFactoryJre.java | 2 +- .../com/google/gwt/query/vm/ResponseJre.java | 6 +- 5 files changed, 234 insertions(+), 4 deletions(-) create mode 100644 gwtquery-core/src/main/java/com/google/gwt/query/vm/CookieManager.java diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java index 6591fc0a..f516fc6a 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java @@ -167,8 +167,8 @@ public class Ajax extends GQuery { } }, new Function() { public Object f(Object...args) { - Throwable exception = getArgument(0, Throwable.class); - Request request = getArgument(0, Request.class); + Throwable exception = arguments(0); + Request request = arguments(1); String msg = String.valueOf(exception); return new Object[]{null, msg, request, null, exception}; } @@ -212,6 +212,8 @@ public class Ajax extends GQuery { settings.setDataString(dataString); settings.setContentType(contentType); } + + System.out.println(settings.getDataString()); if ("GET".equals(settings.getType()) && settings.getDataString() != null) { url += (url.contains("?") ? "&" : "?") + settings.getDataString(); diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/vm/AjaxTransportJre.java b/gwtquery-core/src/main/java/com/google/gwt/query/vm/AjaxTransportJre.java index 0433738b..91df6353 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/vm/AjaxTransportJre.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/vm/AjaxTransportJre.java @@ -26,6 +26,7 @@ import com.google.gwt.user.server.Base64Utils; public class AjaxTransportJre implements AjaxTransport { private static String localDomain = null; + private static CookieManager cookieManager = CookieManager.getInstance(); public static void enableCORS(String domain) { localDomain = domain; @@ -95,6 +96,8 @@ public class AjaxTransportJre implements AjaxTransport { if (s.getUsername() != null && s.getPassword() != null) { c.setRequestProperty ("Authorization", "Basic " + Base64Utils.toBase64((s.getUsername() + ":" + s.getPassword()).getBytes())); } + System.err.println("SET COOK"); + cookieManager.setCookies(c); boolean isCORS = cors && localDomain != null && !s.getUrl().contains(localDomain); if (isCORS) { @@ -141,6 +144,7 @@ public class AjaxTransportJre implements AjaxTransport { if (isCORS && !localDomain.equals(c.getHeaderField("Access-Control-Allow-Origin"))) { code = 0; } + System.err.println(c.getResponseCode()); BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream())); String inputLine; @@ -150,6 +154,9 @@ public class AjaxTransportJre implements AjaxTransport { } in.close(); + cookieManager.storeCookies(c); + return new ResponseJre(code, c.getResponseMessage(), response.toString(), c.getHeaderFields()); } + } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/vm/CookieManager.java b/gwtquery-core/src/main/java/com/google/gwt/query/vm/CookieManager.java new file mode 100644 index 00000000..e33620b6 --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/vm/CookieManager.java @@ -0,0 +1,217 @@ +package com.google.gwt.query.vm; + +import java.io.IOException; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLEncoder; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.StringTokenizer; + +/** + * @author Ian Brown spam@hccp.org + * http://www.hccp.org/java-net-cookie-how-to.html + */ +public class CookieManager { + + private Map>> store = new HashMap>>(); + + private static final String SET_COOKIE = "Set-Cookie"; + private static final String COOKIE_VALUE_DELIMITER = ";"; + private static final String PATH = "path"; + private static final String EXPIRES = "expires"; + private static final String DATE_FORMAT = "EEE, dd-MMM-yyyy hh:mm:ss z"; + private static final String SET_COOKIE_SEPARATOR = "; "; + private static final String COOKIE = "Cookie"; + + private static final char NAME_VALUE_SEPARATOR = '='; + private static final char DOT = '.'; + + private DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);; + + private static CookieManager cookieManager = new CookieManager(); + + public static CookieManager getInstance() { + return cookieManager; + } + + public void removeDomainCookies(String domain) { + store.remove(domain); + } + + public void removeDomainCookie(String domain, String... cookies) { + Map> domainStore = store.get(domain); + if (domainStore != null) { + for (String cookie: cookies) { + domainStore.remove(cookie); + } + } + } + + public void setDomcainCookieProp(String host, String name, String prop, String value) { + String domain = getDomainFromHost(host); + Map> domainStore = store.get(domain); + if (domainStore == null) { + domainStore = new HashMap>(); + store.put(domain, domainStore); + } + Map cookie = domainStore.get(name); + if (cookie == null) { + cookie = new HashMap(); + domainStore.put(name, cookie); + } + if (value == null) { + cookie.remove(prop); + } else { + cookie.put(prop, value); + } + } + + /** + * Retrieves and stores cookies returned by the host on the other side of the the open + * java.net.URLConnection. + * + * The connection MUST have been opened using the connect() method or a IOException will be + * thrown. + * + * @param conn a java.net.URLConnection - must be open, or IOException will be thrown + * @throws java.io.IOException Thrown if conn is not open. + */ + public void storeCookies(URLConnection conn) throws IOException { + // let's determine the domain from where these cookies are being sent + String domain = getDomainFromHost(conn.getURL().getHost()); + + Map> domainStore; // this is where we will store cookies for this domain + + // now let's check the store to see if we have an entry for this domain + if (store.containsKey(domain)) { + // we do, so lets retrieve it from the store + domainStore = store.get(domain); + } else { + // we don't, so let's create it and put it in the store + domainStore = new HashMap>(); + store.put(domain, domainStore); + } + + // OK, now we are ready to get the cookies out of the URLConnection + + String headerName = null; + for (int i = 1; (headerName = conn.getHeaderFieldKey(i)) != null; i++) { + if (headerName.equalsIgnoreCase(SET_COOKIE)) { + Map cookie = new HashMap(); + StringTokenizer st = new StringTokenizer(conn.getHeaderField(i), COOKIE_VALUE_DELIMITER); + + // the specification dictates that the first name/value pair + // in the string is the cookie name and value, so let's handle + // them as a special case: + + if (st.hasMoreTokens()) { + String token = st.nextToken(); + String name = token.substring(0, token.indexOf(NAME_VALUE_SEPARATOR)); + String value = token.substring(token.indexOf(NAME_VALUE_SEPARATOR) + 1, token.length()); + domainStore.put(name, cookie); + cookie.put(name, value); + } + + while (st.hasMoreTokens()) { + String token = st.nextToken().toLowerCase(); + int idx = token.indexOf(NAME_VALUE_SEPARATOR); + if (idx > 0 && idx < token.length() -1) { + cookie.put(token.substring(0, idx).toLowerCase(), token.substring(idx + 1, token.length())); + } + } + } + } + } + + /** + * Prior to opening a URLConnection, calling this method will set all unexpired cookies that match + * the path or subpaths for thi underlying URL + * + * The connection MUST NOT have been opened method or an IOException will be thrown. + * + * @param conn a java.net.URLConnection - must NOT be open, or IOException will be thrown + * @throws java.io.IOException Thrown if conn has already been opened. + */ + public void setCookies(URLConnection conn) throws IOException { + + // let's determine the domain and path to retrieve the appropriate cookies + URL url = conn.getURL(); + String domain = getDomainFromHost(url.getHost()); + if (domain.equals("localhost")) { + domain = "linkedin.com"; + } + String path = url.getPath(); + + Map> domainStore = store.get(domain); + if (domainStore == null) + return; + StringBuffer cookieStringBuffer = new StringBuffer(); + + Iterator cookieNames = domainStore.keySet().iterator(); + while (cookieNames.hasNext()) { + String cookieName = cookieNames.next(); + Map cookie = domainStore.get(cookieName); + // check cookie to ensure path matches and cookie is not expired + // if all is cool, add cookie to header string + if (comparePaths((String) cookie.get(PATH), path) + && isNotExpired(cookie.get(EXPIRES))) { + cookieStringBuffer.append(cookieName); + cookieStringBuffer.append("="); + cookieStringBuffer.append(cookie.get(cookieName)); + if (cookieNames.hasNext()) + cookieStringBuffer.append(SET_COOKIE_SEPARATOR); + } + } + try { + conn.setRequestProperty(COOKIE, cookieStringBuffer.toString()); + System.err.println(">>> SET cookie: " + COOKIE + " " + cookieStringBuffer.toString()); + } catch (java.lang.IllegalStateException ise) { + IOException ioe = + new IOException( + "Illegal State! Cookies cannot be set on a URLConnection that is already connected. " + + "Only call setCookies(java.net.URLConnection) AFTER calling java.net.URLConnection.connect()."); + throw ioe; + } + } + + private String getDomainFromHost(String host) { + if (host.indexOf(DOT) != host.lastIndexOf(DOT)) { + return host.substring(host.indexOf(DOT) + 1); + } else { + return host; + } + } + + private boolean isNotExpired(String cookieExpires) { + if (cookieExpires == null) + return true; + Date now = new Date(); + try { + return (now.compareTo(dateFormat.parse(cookieExpires))) <= 0; + } catch (java.text.ParseException pe) { + pe.printStackTrace(); + return false; + } + } + + private boolean comparePaths(String cookiePath, String targetPath) { + if (cookiePath == null) { + return true; + } else if (cookiePath.equals("/")) { + return true; + } else if (targetPath.regionMatches(0, cookiePath, 0, cookiePath.length())) { + return true; + } else { + return false; + } + } + + public String toString() { + return store.toString(); + } +} diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java b/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java index 05c2bff0..e40feca0 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java @@ -256,7 +256,7 @@ public class JsonFactoryJre implements JsonFactory { if (p != null) { ret += k + "=" + p.toString(); } else if (s != null) { - if (!s.isEmpty() && !"null".equalsIgnoreCase(s)) { + if (!"null".equalsIgnoreCase(s)) { ret += k + "=" + s; } } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/vm/ResponseJre.java b/gwtquery-core/src/main/java/com/google/gwt/query/vm/ResponseJre.java index 36aafd37..207484a9 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/vm/ResponseJre.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/vm/ResponseJre.java @@ -52,7 +52,11 @@ public class ResponseJre extends Response { return ret; } public String getHeadersAsString() { - return null; + String ret = ""; + for (Header h : getHeaders()) { + ret += h.getName() + ":" + h.getValue() + "\n"; + } + return ret; } public int getStatusCode() { return status; -- 2.39.5