package saccubus.net;
+import java.io.File;
+import java.io.IOException;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang.StringUtils;
+
/**
*
* @author yuki
*/
public abstract class Cookie {
+ private static final Pattern USER_SESSION_PATTERN = Pattern.compile("user_session_[\\d_]+");
+
public enum BrowserType {
- NONE, MSIE, IE6, Firefox3, Firefox, Chrome,
- Opera, Chromium, Other
+ MSIE, FIREFOX, CHROME, CHROMIUM, OPERA, OTHER
}
- public static Cookie create(BrowserType type) {
- if (type == BrowserType.Chrome) {
- return new CookieWinCrome();
+ public static Cookie create(BrowserType type, String dir) {
+ switch (type) {
+ case MSIE:
+ return new CookieWinMsIe();
+ case FIREFOX:
+ return new CookieWinFirefox4();
+ case CHROME:
+ return new CookieWinCrome();
+ case CHROMIUM:
+ return new CookieWinChromium();
+ case OPERA:
+ return new CookieWinOpera();
+ default:
+ final File f = new File(dir);
+ return new CookieDefault(f);
}
- throw new UnsupportedOperationException();
}
- public abstract String getUserSessionString();
+ public abstract String getUserSessionString() throws IOException;
+
+ /**
+ * クッキーファイルを見つけて user_session を返す.
+ * @param cookieFileOrDirs cookieが保存されたディレクトリの候補, あるいはcookieファイルの候補.
+ * @return ユーザセッション文字列. 無ければnull.
+ */
+ protected final String getUserSession(String charsetName, File... cookieFileOrDirs) throws IOException {
+ for (File file : cookieFileOrDirs) {
+ if (file.isDirectory()) {
+ File[] files = file.listFiles();
+ for (File cookieFile : files) {
+ if (cookieFile.isFile()) {
+ final String userSession = cutUserSession(cookieFile, charsetName);
+ if (StringUtils.isNotEmpty(userSession)) {
+ return userSession;
+ }
+ }
+ }
+ } else if (file.isFile()) {
+ final String userSession = cutUserSession(file, charsetName);
+ if (StringUtils.isNotEmpty(userSession)) {
+ return userSession;
+ }
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * 文字列から user_session_ で始まる文字列を切り出して返す。数字とアンダーバー以外の文字で切れる。
+ * @param cookieStr 切り出す対象文字列
+ * @return user_session 文字列。見つからなければnull。
+ */
+ protected final String getUserSession(final String cookieStr) {
+ final Matcher mather = USER_SESSION_PATTERN.matcher(cookieStr);
+ if (mather.lookingAt()) {
+ return mather.group(1);
+ }
+ return null;
+ }
+
+ private String cutUserSession(File cookieFile, String charsetName) throws IOException {
+ final String str = FileUtils.readFileToString(cookieFile, charsetName);
+ return getUserSession(str);
+ }
}