OSDN Git Service

Chome Cookieからのユーザセッション取得処理を移植
authoryukihane <yukihane.feather@gmail.com>
Sat, 17 Sep 2011 04:36:57 +0000 (13:36 +0900)
committeryukihane <yukihane.feather@gmail.com>
Sat, 17 Sep 2011 05:00:26 +0000 (14:00 +0900)
frontend/src/saccubus/net/Cookie.java [new file with mode: 0644]
frontend/src/saccubus/net/CookieWinCrome.java [new file with mode: 0644]

diff --git a/frontend/src/saccubus/net/Cookie.java b/frontend/src/saccubus/net/Cookie.java
new file mode 100644 (file)
index 0000000..41dfbf5
--- /dev/null
@@ -0,0 +1,23 @@
+package saccubus.net;
+
+/**
+ *
+ * @author yuki
+ */
+public abstract class Cookie {
+
+    public enum BrowserType {
+
+        NONE, MSIE, IE6, Firefox3, Firefox, Chrome,
+        Opera, Chromium, Other
+    }
+
+    public static Cookie create(BrowserType type) {
+        if (type == BrowserType.Chrome) {
+            return new CookieWinCrome();
+        }
+        throw new UnsupportedOperationException();
+    }
+
+    public abstract String getUserSessionString();
+}
diff --git a/frontend/src/saccubus/net/CookieWinCrome.java b/frontend/src/saccubus/net/CookieWinCrome.java
new file mode 100644 (file)
index 0000000..5a573d8
--- /dev/null
@@ -0,0 +1,87 @@
+package saccubus.net;
+
+import java.io.File;
+import org.apache.commons.io.FileUtils;
+
+/**
+ *
+ * @author yuki
+ */
+public class CookieWinCrome extends Cookie {
+
+    /** <p>
+     *  Chrome から user_session を取得。エラーが起こった場合、例外を投げずに空文字を返す
+     *  </p>
+     *  @return user_session
+     */
+    @Override
+    public String getUserSessionString() {
+        String user_session = "";
+        File cookie_file;
+        final String googleChrome = "\\Google\\Chrome\\User Data\\Default\\Cookies";
+        try {
+            String local_Appdir = System.getenv("LOCALAPPDATA");
+            if (local_Appdir != null && !local_Appdir.isEmpty()) {
+                // Win7 32bit
+                cookie_file = new File(local_Appdir + googleChrome);
+                if (cookie_file.isFile()) {
+                    final String dataStr = FileUtils.readFileToString(cookie_file, "UTF-8");
+                    user_session = CutUserSession(dataStr, cookie_file.getPath());
+                    if (!user_session.isEmpty()) {
+                        return user_session;
+                    }
+                }
+            }
+
+            String profile_dir = System.getenv("USERPROFILE");
+            if (profile_dir != null && !profile_dir.isEmpty()) {
+                // XP 32bit
+                cookie_file = new File(profile_dir
+                        + "\\Local Settings\\Application Data" + googleChrome);
+                if (cookie_file.isFile()) {
+                    String dataStr = FileUtils.readFileToString(cookie_file, "UTF-8");
+                    user_session = CutUserSession(dataStr, cookie_file.getPath());
+                    return user_session;
+                }
+            }
+            String app_dir = System.getenv("APPDATA");
+            if (app_dir != null && !app_dir.isEmpty()) {
+                // ??? just try
+                cookie_file = new File(app_dir + googleChrome);
+                if (cookie_file.isFile()) {
+                    String dataStr = FileUtils.readFileToString(cookie_file, "UTF-8");
+                    user_session = CutUserSession(dataStr, cookie_file.getPath());
+                    return user_session;
+                }
+            }
+            return user_session;
+        } catch (Exception e) {
+            e.printStackTrace();
+            return user_session;
+        }
+    }
+
+    /// <summary>
+    /// 文字列から user_session_ で始まる文字列を切り出して返す。数字とアンダーバー以外の文字で切れる。
+    /// </summary>
+    /// <param name="str">切り出す対象文字列</param>
+    /// <returns>user_session 文字列。見つからなければ空文字を返す</returns>
+    private String CutUserSession(String str, String filename) {
+        String ret = "";
+        int start = str.indexOf("user_session_");
+        if (start >= 0) {
+            int index = start + "user_session_".length();
+            while (index < str.length() && ('0' <= str.charAt(index) && str.charAt(index) <= '9'
+                    || str.charAt(index) == '_')) {
+                ++index;
+            }
+            ret = str.substring(start, index);
+            // C# の string.SubString( , ) と Java の String.substring( , ) は違うので注意!
+            if (!ret.isEmpty() && !filename.isEmpty()) {
+                System.out.println("Cookie found: " + filename);
+                return ret;
+            }
+        }
+        return "";
+    }
+}