From ee63117832da894cd7625a2ff9b6cf60b471a3ad Mon Sep 17 00:00:00 2001 From: yukihane Date: Sat, 17 Sep 2011 18:00:52 +0900 Subject: [PATCH] =?utf8?q?Opera=20Cookie=E5=87=A6=E7=90=86=E3=82=AF?= =?utf8?q?=E3=83=A9=E3=82=B9.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- frontend/src/saccubus/net/BrowserInfo.java | 119 -------------------------- frontend/src/saccubus/net/CookieWinOpera.java | 29 +++++++ 2 files changed, 29 insertions(+), 119 deletions(-) create mode 100644 frontend/src/saccubus/net/CookieWinOpera.java diff --git a/frontend/src/saccubus/net/BrowserInfo.java b/frontend/src/saccubus/net/BrowserInfo.java index 52436c2..65ae534 100644 --- a/frontend/src/saccubus/net/BrowserInfo.java +++ b/frontend/src/saccubus/net/BrowserInfo.java @@ -20,76 +20,6 @@ import java.util.Arrays; * */ public class BrowserInfo { - - public enum BrowserCookieKind { - - NONE, MSIE, IE6, Firefox3, Firefox, Chrome, - Opera, Chromium, Other,} - private BrowserCookieKind validBrowser; - - public String getBrowserName() { - if (validBrowser == BrowserCookieKind.NONE) { - return "さきゅばす"; - } else if (validBrowser == BrowserCookieKind.MSIE) { - return "Internet Exploror"; - } else { - return validBrowser.toString(); - } - } - - public BrowserInfo() { - validBrowser = BrowserCookieKind.NONE; - } - private static final String NICOVIDEO_URL = "http://www.nicovideo.jp"; - - /** - * - * @param browserKind - * @return - */ - public String getUserSession(BrowserCookieKind browserKind) { - String user_session = ""; - switch (browserKind) { - case IE6: - user_session = GetUserSessionFromIE6(NICOVIDEO_URL); - break; - case Chromium: - user_session = GetUserSesionChromium(); - break; - case Opera: - user_session = GetUserSessionOpera(); - break; - } - if (!user_session.isEmpty()) { - validBrowser = browserKind; - } - return user_session; - } - - /// - /// IE6 から user_session を取得 - /// - /// サイト(ニコニコ動画)のURL - /// user_session - private String GetUserSessionFromIE6(String url) { - return CutUserSession(GetCookieFromIE6(url), ""); - } - - /// - /// IE6 からクッキーを取得 - /// - /// 取得するクッキーに関連づけられたURL - /// クッキー文字列 - private String GetCookieFromIE6(String url) { - int size = 4096; - byte[] dummy = new byte[size]; - Arrays.fill(dummy, (byte) ' '); - StringBuilder buff = new StringBuilder(new String(dummy)); - int[] ref_size = new int[1]; - ref_size[0] = size; - //InternetGetCookie(url, null, buff, /*ref*/ ref_size); - return buff.toString().replace(';', ','); - } /* * [DllImport("wininet.dll")] * private extern static bool InternetGetCookie(string url, string name, StringBuilder data, ref uint size); @@ -136,53 +66,4 @@ public class BrowserInfo { } } - /**

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

- * @return user_session - */ - private String GetUserSessionOpera() { - String user_session = ""; - String cookie_file = ""; - try { - String app_dir = System.getenv("APPDATA"); - if (app_dir != null && !app_dir.isEmpty()) { - // Win7/XP 32bit - cookie_file = app_dir + "\\Opera\\Opera\\cookies4.dat"; - if (Path.isFile(cookie_file)) { - String dataStr = Path.ReadAllText(cookie_file, "UTF-8"); - user_session = CutUserSession(dataStr, cookie_file); - return user_session; - } - } - return ""; - } catch (Exception e) { - e.printStackTrace(); - return ""; - } - } - - /// - /// 文字列から 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 ""; - } } diff --git a/frontend/src/saccubus/net/CookieWinOpera.java b/frontend/src/saccubus/net/CookieWinOpera.java new file mode 100644 index 0000000..7a0325b --- /dev/null +++ b/frontend/src/saccubus/net/CookieWinOpera.java @@ -0,0 +1,29 @@ +package saccubus.net; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import org.apache.commons.lang.StringUtils; + +/** + * Windows Opera用. + * @author yuki + */ +public class CookieWinOpera extends Cookie { + + /** + * Opera から user_session を取得。 + * @return ユーザセッション文字列. + * @throws IOException 取得失敗. + */ + @Override + public String getUserSessionString() throws IOException { + final String appData = System.getenv("APPDATA"); + if (StringUtils.isEmpty(appData)) { + throw new IOException("APPDATA not defined"); + } + + final File cookieFile = new File(appData + "\\Opera\\Opera\\cookies4.dat"); + return getUserSession("UTF-8", cookieFile); + } +} -- 2.11.0