From 56a4696adb7d1c0146748b510391836ab4f160ca Mon Sep 17 00:00:00 2001 From: yukihane Date: Sat, 17 Sep 2011 21:19:41 +0900 Subject: [PATCH] =?utf8?q?Cookie=E9=96=A2=E4=BF=82=E6=83=85=E5=A0=B1?= =?utf8?q?=E3=82=92=E3=82=B3=E3=83=B3=E3=83=95=E3=82=A3=E3=82=B0=E3=81=8B?= =?utf8?q?=E3=82=89=E5=8F=96=E5=BE=97=E3=81=97=E3=83=97=E3=83=AD=E3=83=95?= =?utf8?q?=E3=82=A1=E3=82=A4=E3=83=AB=E3=82=92=E4=BD=9C=E6=88=90=E3=81=99?= =?utf8?q?=E3=82=8B=E5=87=A6=E7=90=86=E3=81=AE=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../saccubus/worker/impl/download/Download.java | 13 ++-- .../src/saccubus/worker/profile/LoginProfile.java | 15 ++++- frontend/src/yukihane/inqubus/config/Config.java | 71 ++++++++++++++++++++++ .../inqubus/config/ConfigLoginProfile.java | 22 +++++++ .../saccubus/prompt/DownloadProfileImpl.java | 3 +- 5 files changed, 117 insertions(+), 7 deletions(-) diff --git a/frontend/src/saccubus/worker/impl/download/Download.java b/frontend/src/saccubus/worker/impl/download/Download.java index 8a67234..898b7db 100644 --- a/frontend/src/saccubus/worker/impl/download/Download.java +++ b/frontend/src/saccubus/worker/impl/download/Download.java @@ -102,7 +102,7 @@ public class Download extends Worker { final File file = new File(profile.getCommentProfile().getDir(), name); final EnumSet commentSet = EnumSet.of(DownloadCommentType.OWNER); - if(profile.getCommentProfile().isDisablePerMinComment()) { + if (profile.getCommentProfile().isDisablePerMinComment()) { commentSet.add(DownloadCommentType.COMMENT_OLD); } else { commentSet.add(DownloadCommentType.COMMENT); @@ -149,10 +149,15 @@ public class Download extends Worker { */ private NicoHttpClient createClientAndLogin() throws IOException, InterruptedException { final NicoHttpClient client = createClient(profile.getProxyProfile()); + if (profile.getLoginProfile().needsLogin()) { - final boolean hasLogin = client.login(profile.getLoginProfile().getMail(), profile.getLoginProfile().getPassword()); - if (!hasLogin) { - throw new IOException("login fail"); + final boolean hasLogin = client.login(profile.getLoginProfile().getMail(), profile.getLoginProfile(). + getPassword()); + if (!hasLogin) { + throw new IOException("login fail"); + } + } else { + client.addCookie(profile.getLoginProfile().getCookies()); } return client; diff --git a/frontend/src/saccubus/worker/profile/LoginProfile.java b/frontend/src/saccubus/worker/profile/LoginProfile.java index 7f37bb5..3079e8d 100644 --- a/frontend/src/saccubus/worker/profile/LoginProfile.java +++ b/frontend/src/saccubus/worker/profile/LoginProfile.java @@ -1,14 +1,25 @@ package saccubus.worker.profile; +import java.util.Map; + /** * ニコニコ動画サービスへのログイン情報を保持するためのプロファイルです. * @author yuki */ public interface LoginProfile { + /** @return ログイン情報を使用してログインする場合はtrue, cookieを使用する場合はfalse. */ + boolean needsLogin(); + /** @return ログインID(メールアドレス). */ - public String getMail(); + String getMail(); /** @return ログインパスワード. */ - public String getPassword(); + String getPassword(); + + /** + * @return 追加のCookie. + * {@link #needsLogin()} がfalseの場合, ここにユーザセッション情報を含める必要があります. + */ + Map getCookies(); } diff --git a/frontend/src/yukihane/inqubus/config/Config.java b/frontend/src/yukihane/inqubus/config/Config.java index 9499567..1b94606 100644 --- a/frontend/src/yukihane/inqubus/config/Config.java +++ b/frontend/src/yukihane/inqubus/config/Config.java @@ -5,10 +5,54 @@ import java.util.ArrayList; import java.util.List; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.XMLConfiguration; +import saccubus.net.Cookie.BrowserType; public enum Config { INSTANCE; + + public enum CookieBrowser { + + MSIE { + + @Override + public BrowserType toBrowserType() { + return BrowserType.MSIE; + } + }, FIREFOX { + + @Override + public BrowserType toBrowserType() { + return BrowserType.FIREFOX; + } + }, CHROME { + + @Override + public BrowserType toBrowserType() { + return BrowserType.CHROME; + } + }, CHROMIUM { + + @Override + public BrowserType toBrowserType() { + return BrowserType.CHROMIUM; + } + }, OPERA { + + @Override + public BrowserType toBrowserType() { + return BrowserType.OPERA; + } + }, OTHER { + + @Override + public BrowserType toBrowserType() { + return BrowserType.OTHER; + } + }; + + public abstract BrowserType toBrowserType(); + } private final XMLConfiguration config = new XMLConfiguration(); void clear() { @@ -18,6 +62,15 @@ public enum Config { /* * ネットワーク - アカウント */ + private static final String LOGIN_NEED = "network.login"; + + public boolean getNeedsLogin() { + return config.getBoolean(LOGIN_NEED, true); + } + + public void setNeedsLogin(boolean s) { + config.setProperty(LOGIN_NEED, s); + } private static final String ACCOUNT_ID = "network.account.id"; public String getId() { @@ -36,7 +89,25 @@ public enum Config { public void setPassword(String s) { config.setProperty(ACCOUNT_PASSWORD, s); } + private static final String COOKIE_BROWSER = "network.cookie.browser"; + + public CookieBrowser getCookieBrowser() { + final String str = config.getString(COOKIE_BROWSER, CookieBrowser.MSIE.name()); + return CookieBrowser.valueOf(str); + } + public void setCookieBrowser(CookieBrowser s) { + config.setProperty(COOKIE_BROWSER, s.name()); + } + private static final String COOKIE_DIR = "network.cookie.dir"; + + public String getCookieDir() { + return config.getString(COOKIE_DIR, ""); + } + + public void setCookieDir(String s) { + config.setProperty(COOKIE_DIR, s); + } /* * ネットワーク - プロキシ */ diff --git a/frontend/src/yukihane/inqubus/config/ConfigLoginProfile.java b/frontend/src/yukihane/inqubus/config/ConfigLoginProfile.java index 29cdbf2..8b4191c 100644 --- a/frontend/src/yukihane/inqubus/config/ConfigLoginProfile.java +++ b/frontend/src/yukihane/inqubus/config/ConfigLoginProfile.java @@ -1,7 +1,11 @@ package yukihane.inqubus.config; +import java.util.HashMap; +import java.util.Map; import org.apache.commons.lang.builder.ToStringBuilder; +import saccubus.net.Cookie; import saccubus.worker.profile.LoginProfile; +import yukihane.inqubus.config.Config.CookieBrowser; /** * コンフィグに設定された値を基にしたLoginProfile実装. @@ -9,13 +13,26 @@ import saccubus.worker.profile.LoginProfile; */ public class ConfigLoginProfile implements LoginProfile { + private final boolean login; private final String mail; private final String password; + private final Map cookies; public ConfigLoginProfile() { final Config p = Config.INSTANCE; + this.login = p.getNeedsLogin(); this.mail = p.getId(); this.password = p.getPassword(); + + final CookieBrowser cookieBrowser = p.getCookieBrowser(); + final String cookieDir = p.getCookieDir(); + final Cookie cookie = Cookie.create(cookieBrowser.toBrowserType(), cookieDir); + this.cookies = new HashMap<>(cookie.get()); + } + + @Override + public boolean needsLogin() { + return this.login; } @Override @@ -29,6 +46,11 @@ public class ConfigLoginProfile implements LoginProfile { } @Override + public Map getCookies() { + return new HashMap<>(this.cookies); + } + + @Override public String toString() { return ToStringBuilder.reflectionToString(this); } diff --git a/frontend/src/yukihane/inqubus/saccubus/prompt/DownloadProfileImpl.java b/frontend/src/yukihane/inqubus/saccubus/prompt/DownloadProfileImpl.java index 85bd525..4e32de2 100644 --- a/frontend/src/yukihane/inqubus/saccubus/prompt/DownloadProfileImpl.java +++ b/frontend/src/yukihane/inqubus/saccubus/prompt/DownloadProfileImpl.java @@ -4,6 +4,7 @@ import saccubus.worker.profile.CommentProfile; import saccubus.worker.profile.LoginProfile; import yukihane.inqubus.config.ConfigCommentProfile; import yukihane.inqubus.config.ConfigDownloadProfile; +import yukihane.inqubus.config.ConfigLoginProfile; /** * さきゅばすのコマンドプロンプト仕様にあわせるためのDownloadProfile実装 @@ -15,7 +16,7 @@ class DownloadProfileImpl extends ConfigDownloadProfile { private final CommentProfile commentProfile; DownloadProfileImpl(final String mail, final String pass, final long time) { - this.loginProfile = new LoginProfile() { + this.loginProfile = new ConfigLoginProfile() { @Override public String getMail() { -- 2.11.0