OSDN Git Service

Opera Cookie処理クラス.
authoryukihane <yukihane.feather@gmail.com>
Sat, 17 Sep 2011 09:00:52 +0000 (18:00 +0900)
committeryukihane <yukihane.feather@gmail.com>
Sat, 17 Sep 2011 09:00:52 +0000 (18:00 +0900)
frontend/src/saccubus/net/BrowserInfo.java
frontend/src/saccubus/net/CookieWinOpera.java [new file with mode: 0644]

index 52436c2..65ae534 100644 (file)
@@ -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;
-    }
-
-    /// <summary>
-    /// IE6 から user_session を取得
-    /// </summary>
-    /// <param name="url">サイト(ニコニコ動画)のURL</param>
-    /// <returns>user_session</returns>
-    private String GetUserSessionFromIE6(String url) {
-        return CutUserSession(GetCookieFromIE6(url), "");
-    }
-
-    /// <summary>
-    /// IE6 からクッキーを取得
-    /// </summary>
-    /// <param name="url">取得するクッキーに関連づけられたURL</param>
-    /// <returns>クッキー文字列</returns>
-    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 {
         }
     }
 
-    /** <p>
-     *  Opera から user_session を取得。エラーが起こった場合、例外を投げずに空文字を返す
-     *  </p>
-     *  @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 "";
-        }
-    }
-
-    /// <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 "";
-    }
 }
diff --git a/frontend/src/saccubus/net/CookieWinOpera.java b/frontend/src/saccubus/net/CookieWinOpera.java
new file mode 100644 (file)
index 0000000..7a0325b
--- /dev/null
@@ -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);
+    }
+}