OSDN Git Service

MSIE cookie処理
[coroid/inqubus.git] / frontend / src / saccubus / net / CookieWinMsIe.java
1 package saccubus.net;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.ArrayList;
6 import java.util.List;
7 import org.apache.commons.io.FileUtils;
8 import org.apache.commons.lang.StringUtils;
9
10 /**
11  * Microsoft Internete Explorer用cookie.
12  * @author yuki
13  */
14 public class CookieWinMsIe extends Cookie {
15
16     /** <p>
17      *  IE7/IE8/IE9 から user_session を取得。<br/>
18      *  エラーが起こった場合、例外を投げずに空文字を返す
19      *  </p>
20      *  @return user_session
21      */
22     @Override
23     public String getUserSessionString() throws IOException {
24
25         final String userProfile = System.getenv("USERPROFILE");
26         if (StringUtils.isEmpty(userProfile)) {
27             throw new IOException("USERPROFILE not found");
28         }
29
30         final List<File> searchDirs = new ArrayList<>();
31         // http://msdn.microsoft.com/en-us/library/aa385326(v=VS.85).aspx
32         searchDirs.add(new File(userProfile + "\\AppData\\Roaming\\Microsoft\\Windows\\Cookies\\Low\\"));
33         searchDirs.add(new File(userProfile + "\\AppData\\Roaming\\Microsoft\\Windows\\Cookies\\"));
34         // これは何だ?
35         searchDirs.add(new File(userProfile + "\\Cookies\\"));
36
37         return getUserSession(searchDirs);
38     }
39
40     /**
41      * dir_name ディレクトリから MSIE のクッキーを見つけて user_session を返す
42      * @param cookieDirs cookie保存ディレクトリの候補.
43      * @return ユーザセッション文字列. 無ければnull.
44      */
45     private String getUserSession(List<File> cookieDirs) throws IOException {
46         for (File dir : cookieDirs) {
47             if (dir.isDirectory()) {
48                 File[] files = dir.listFiles();
49                 for (File fullname : files) {
50                     final String cookie = FileUtils.readFileToString(fullname, "MS932");
51                     final String userSession = cutUserSession(cookie);
52                     if (StringUtils.isNotEmpty(userSession)) {
53                         return userSession;
54                     }
55                 }
56             }
57         }
58
59         throw new IOException();
60     }
61 }