OSDN Git Service

cookieインスタンス生成
[coroid/inqubus.git] / frontend / src / saccubus / net / Cookie.java
1 package saccubus.net;
2
3 import java.io.IOException;
4 import java.util.regex.Matcher;
5 import java.util.regex.Pattern;
6
7 /**
8  *
9  * @author yuki
10  */
11 public abstract class Cookie {
12
13     private static final Pattern USER_SESSION_PATTERN = Pattern.compile("user_session_[\\d_]+");
14
15     public enum BrowserType {
16
17         NONE, MSIE, IE6, FIREFOX3, FIREFOX, CHROME,
18         OPERA, CHROMIUM, OTHER
19     }
20
21     public static Cookie create(BrowserType type) {
22         switch (type) {
23             case CHROME:
24                 return new CookieWinCrome();
25             case FIREFOX:
26                 return new CookieWinFirefox4();
27             default:
28                 throw new UnsupportedOperationException();
29         }
30     }
31
32     public abstract String getUserSessionString() throws IOException;
33
34     /**
35      * 文字列から user_session_ で始まる文字列を切り出して返す。数字とアンダーバー以外の文字で切れる。
36      * @param str 切り出す対象文字列
37      * @return user_session 文字列。見つからなければnull。
38      */
39     protected String cutUserSession(String str) {
40         final Matcher mather = USER_SESSION_PATTERN.matcher(str);
41         if (mather.lookingAt()) {
42             return mather.group(1);
43         }
44         return null;
45     }
46 }