From: yukihane Date: Sat, 17 Sep 2011 04:36:57 +0000 (+0900) Subject: Chome Cookieからのユーザセッション取得処理を移植 X-Git-Tag: rel20110922_ver2.1.0~21^2~29 X-Git-Url: http://git.sourceforge.jp/view?p=coroid%2Finqubus.git;a=commitdiff_plain;h=d15a2cc710a7b9b9d69daf340980805799628b02;hp=e2a361cfa61718eec642896a6fc6a52995e845cc Chome Cookieからのユーザセッション取得処理を移植 --- diff --git a/frontend/src/saccubus/net/Cookie.java b/frontend/src/saccubus/net/Cookie.java new file mode 100644 index 0000000..41dfbf5 --- /dev/null +++ b/frontend/src/saccubus/net/Cookie.java @@ -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 index 0000000..5a573d8 --- /dev/null +++ b/frontend/src/saccubus/net/CookieWinCrome.java @@ -0,0 +1,87 @@ +package saccubus.net; + +import java.io.File; +import org.apache.commons.io.FileUtils; + +/** + * + * @author yuki + */ +public class CookieWinCrome extends Cookie { + + /**

+ * Chrome から user_session を取得。エラーが起こった場合、例外を投げずに空文字を返す + *

+ * @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; + } + } + + /// + /// 文字列から user_session_ で始まる文字列を切り出して返す。数字とアンダーバー以外の文字で切れる。 + /// + /// 切り出す対象文字列 + /// user_session 文字列。見つからなければ空文字を返す + 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 ""; + } +}