OSDN Git Service

Chromium Cookie処理クラス実装
[coroid/inqubus.git] / frontend / src / saccubus / net / Cookie.java
1 package saccubus.net;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.regex.Matcher;
6 import java.util.regex.Pattern;
7 import org.apache.commons.io.FileUtils;
8 import org.apache.commons.lang.StringUtils;
9
10 /**
11  *
12  * @author yuki
13  */
14 public abstract class Cookie {
15
16     private static final Pattern USER_SESSION_PATTERN = Pattern.compile("user_session_[\\d_]+");
17
18     public enum BrowserType {
19
20         MSIE, FIREFOX, CHROME, CHROMIUM, OPERA, OTHER
21     }
22
23     public static Cookie create(BrowserType type, String dir) {
24         switch (type) {
25             case MSIE:
26                 return new CookieWinMsIe();
27             case FIREFOX:
28                 return new CookieWinFirefox4();
29             case CHROME:
30                 return new CookieWinCrome();
31             case CHROMIUM:
32                 return new CookieWinChromium();
33             case OPERA:
34                 return new CookieWinOpera();
35             default:
36                 final File f = new File(dir);
37                 return new CookieDefault(f);
38         }
39     }
40
41     public abstract String getUserSessionString() throws IOException;
42
43     /**
44      * クッキーファイルを見つけて user_session を返す.
45      * @param cookieFileOrDirs cookieが保存されたディレクトリの候補, あるいはcookieファイルの候補.
46      * @return ユーザセッション文字列. 無ければnull.
47      */
48     protected final String getUserSession(String charsetName, File... cookieFileOrDirs) throws IOException {
49         for (File file : cookieFileOrDirs) {
50             if (file.isDirectory()) {
51                 File[] files = file.listFiles();
52                 for (File cookieFile : files) {
53                     if (cookieFile.isFile()) {
54                         final String userSession = cutUserSession(cookieFile, charsetName);
55                         if (StringUtils.isNotEmpty(userSession)) {
56                             return userSession;
57                         }
58                     }
59                 }
60             } else if (file.isFile()) {
61                 final String userSession = cutUserSession(file, charsetName);
62                 if (StringUtils.isNotEmpty(userSession)) {
63                     return userSession;
64                 }
65             }
66         }
67
68         return null;
69     }
70
71     /**
72      * 文字列から user_session_ で始まる文字列を切り出して返す。数字とアンダーバー以外の文字で切れる。
73      * @param cookieStr 切り出す対象文字列
74      * @return user_session 文字列。見つからなければnull。
75      */
76     protected final String getUserSession(final String cookieStr) {
77         final Matcher mather = USER_SESSION_PATTERN.matcher(cookieStr);
78         if (mather.lookingAt()) {
79             return mather.group(1);
80         }
81         return null;
82     }
83
84     private String cutUserSession(File cookieFile, String charsetName) throws IOException {
85         final String str = FileUtils.readFileToString(cookieFile, charsetName);
86         return getUserSession(str);
87     }
88 }