*/
- /**
- * dir_name ディレクトリから MSIE のクッキーを見つけて user_session を返す
- * @param dir_name
- * @return
- */
- private String GetUserSessionFromDirectory(String dir_name) {
- String user_session = "";
- try {
- if (Path.isDirectory(dir_name)) {
- String[] files = Path.GetFiles(dir_name);
- for (String fullname : files) {
- user_session = CutUserSession(Path.ReadAllText(fullname, "MS932"), fullname);
- if (!user_session.isEmpty()) {
- return user_session;
- }
-
- /* obsolate after WindowsUpdate Aug 2011
- String name = Path.GetFileName(fullname);
- if (name.indexOf("nicovideo") >= 0 && name.indexOf("www") < 0)
- {
- user_session = CutUserSession(Path.ReadAllText(fullname, "MS932"), "");
- if (!user_session.isEmpty()){
- System.out.println("Found cookie in " + fullname.replace("\\", "/"));
- return user_session;
- }
- }
- */
- }
- return "";
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return "";
- }
-
/** <p>
* Chromium から user_session を取得。エラーが起こった場合、例外を投げずに空文字を返す
* </p>
package saccubus.net;
+import java.io.File;
import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang.StringUtils;
/**
- *
+ * Microsoft Internete Explorer用cookie.
* @author yuki
*/
public class CookieWinMsIe extends Cookie {
*/
@Override
public String getUserSessionString() throws IOException {
- String user_session = "";
- String profile_dir = System.getenv("USERPROFILE");
- if (profile_dir == null || profile_dir.isEmpty()) {
- return "";
+ final String userProfile = System.getenv("USERPROFILE");
+ if (StringUtils.isEmpty(userProfile)) {
+ throw new IOException("USERPROFILE not found");
}
- String search_dir = profile_dir + "\\AppData\\Roaming\\Microsoft\\Windows\\Cookies\\Low\\";
- user_session = GetUserSessionFromDirectory(search_dir);
- if (user_session.isEmpty()) {
- search_dir = profile_dir + "\\AppData\\Roaming\\Microsoft\\Windows\\Cookies\\";
- user_session = GetUserSessionFromDirectory(search_dir);
- }
- if (user_session.isEmpty()) {
- search_dir = profile_dir + "\\Cookies\\";
- user_session = GetUserSessionFromDirectory(search_dir);
+
+ final List<File> searchDirs = new ArrayList<>();
+ // http://msdn.microsoft.com/en-us/library/aa385326(v=VS.85).aspx
+ searchDirs.add(new File(userProfile + "\\AppData\\Roaming\\Microsoft\\Windows\\Cookies\\Low\\"));
+ searchDirs.add(new File(userProfile + "\\AppData\\Roaming\\Microsoft\\Windows\\Cookies\\"));
+ // これは何だ?
+ searchDirs.add(new File(userProfile + "\\Cookies\\"));
+
+ return getUserSession(searchDirs);
+ }
+
+ /**
+ * dir_name ディレクトリから MSIE のクッキーを見つけて user_session を返す
+ * @param cookieDirs cookie保存ディレクトリの候補.
+ * @return ユーザセッション文字列. 無ければnull.
+ */
+ private String getUserSession(List<File> cookieDirs) throws IOException {
+ for (File dir : cookieDirs) {
+ if (dir.isDirectory()) {
+ File[] files = dir.listFiles();
+ for (File fullname : files) {
+ final String cookie = FileUtils.readFileToString(fullname, "MS932");
+ final String userSession = cutUserSession(cookie);
+ if (StringUtils.isNotEmpty(userSession)) {
+ return userSession;
+ }
+ }
+ }
}
- return user_session;
+
+ throw new IOException();
}
}