ContentGrabbingJi/ContentGrabbingJi-pixiv/src/main/java/net/lamgc/pixiv/login/ObjectInputStreamLogin.java
LamGC 2bea395cf7
[Add] Pixiv 添加一组与 Pixiv 登录会话相关的类;
[Add] PixivLogin, PixivLoginException 初版的登录器接口;
[Add] PixivSession, BasicPixivSession Pixiv 帐号登录会话抽象类和一个默认基本实现;
[Add] ObjectInputStreamLogin 通过反序列化 CacheStore 进行登录的登录器;
2020-10-07 14:14:16 +08:00

63 lines
2.1 KiB
Java

/*
* Copyright (C) 2020 LamGC
*
* ContentGrabbingJi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ContentGrabbingJi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.lamgc.pixiv.login;
import net.lamgc.pixiv.*;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.util.concurrent.atomic.AtomicReference;
/**
* 读入对象进行登录.
* @author LamGC
*/
public class ObjectInputStreamLogin implements PixivLogin {
private final HttpClient httpClient;
private final ObjectInputStream inputStream;
private final AtomicReference<PixivSession> sessionCache = new AtomicReference<>();
public ObjectInputStreamLogin(HttpClient httpClient, InputStream cookieInput) throws IOException {
this.httpClient = httpClient;
this.inputStream = new ObjectInputStream(cookieInput);
}
@Override
public synchronized PixivSession login() throws PixivLoginException {
if (sessionCache.get() == null) {
CookieStore cookieStore;
try {
cookieStore = (CookieStore) inputStream.readObject();
} catch (Exception e) {
throw new PixivLoginException("An exception occurred while trying to read in a Java serialized object", e);
}
PixivSession session = new BasicPixivSession(cookieStore, this.httpClient);
sessionCache.set(session);
return session;
} else {
return sessionCache.get();
}
}
}