OSDN Git Service

リファクタリング. 共通メソッドのプルアップ.
[coroid/inqubus.git] / frontend / src / saccubus / net / Cookie.java
index ccfa485..03fc962 100644 (file)
@@ -1,8 +1,12 @@
 package saccubus.net;
 
+import java.io.File;
 import java.io.IOException;
+import java.util.List;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang.StringUtils;
 
 /**
  *
@@ -24,6 +28,8 @@ public abstract class Cookie {
                 return new CookieWinCrome();
             case FIREFOX:
                 return new CookieWinFirefox4();
+            case MSIE:
+                return new CookieWinMsIe();
             default:
                 throw new UnsupportedOperationException();
         }
@@ -36,11 +42,35 @@ public abstract class Cookie {
      * @param str 切り出す対象文字列
      * @return user_session 文字列。見つからなければnull。
      */
-    protected String cutUserSession(String str) {
+    protected final String cutUserSession(String str) {
         final Matcher mather = USER_SESSION_PATTERN.matcher(str);
         if (mather.lookingAt()) {
             return mather.group(1);
         }
         return null;
     }
+
+    /**
+     * cookieDirs ディレクトリからクッキーを見つけて user_session を返す
+     * @param cookieDirs cookie保存ディレクトリの候補.
+     * @return ユーザセッション文字列. 無ければnull.
+     */
+    protected final String getUserSessionFromDir(List<File> cookieDirs, String charsetName) throws IOException {
+        for (File dir : cookieDirs) {
+            if (dir.isDirectory()) {
+                File[] files = dir.listFiles();
+                for (File cookieFile : files) {
+                    if (cookieFile.isFile()) {
+                        final String cookie = FileUtils.readFileToString(cookieFile, charsetName);
+                        final String userSession = cutUserSession(cookie);
+                        if (StringUtils.isNotEmpty(userSession)) {
+                            return userSession;
+                        }
+                    }
+                }
+            }
+        }
+
+        return null;
+    }
 }