Cookies are now persisted in the config dir
This commit is contained in:
parent
b73627c0fb
commit
75351cedb5
|
@ -101,4 +101,8 @@ public class Config {
|
|||
public boolean isServerMode() {
|
||||
return Objects.equals(System.getProperty("ctbrec.server.mode"), "1");
|
||||
}
|
||||
|
||||
public File getConfigDir() {
|
||||
return configDir;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,64 @@
|
|||
package ctbrec.io;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import com.squareup.moshi.JsonAdapter;
|
||||
import com.squareup.moshi.JsonReader;
|
||||
import com.squareup.moshi.JsonReader.Token;
|
||||
import com.squareup.moshi.JsonWriter;
|
||||
|
||||
import ctbrec.io.HttpClient.CookieContainer;
|
||||
import okhttp3.Cookie;
|
||||
|
||||
public class CookieContainerJsonAdapter extends JsonAdapter<CookieContainer> {
|
||||
|
||||
private CookieJsonAdapter cookieAdapter = new CookieJsonAdapter();
|
||||
|
||||
@Override
|
||||
public CookieContainer fromJson(JsonReader reader) throws IOException {
|
||||
CookieContainer cookies = new CookieContainer();
|
||||
reader.beginArray();
|
||||
while(reader.hasNext()) {
|
||||
reader.beginObject();
|
||||
reader.nextName(); // "domain"
|
||||
String domain = reader.nextString();
|
||||
reader.nextName(); // "cookies"
|
||||
reader.beginArray();
|
||||
List<Cookie> cookieList = new ArrayList<>();
|
||||
while(reader.hasNext()) {
|
||||
Token token = reader.peek();
|
||||
if(token == Token.END_ARRAY) {
|
||||
break;
|
||||
}
|
||||
Cookie cookie = cookieAdapter.fromJson(reader);
|
||||
cookieList.add(cookie);
|
||||
}
|
||||
reader.endArray();
|
||||
reader.endObject();
|
||||
cookies.put(domain, cookieList);
|
||||
}
|
||||
reader.endArray();
|
||||
return cookies;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toJson(JsonWriter writer, CookieContainer cookieContainer) throws IOException {
|
||||
writer.beginArray();
|
||||
for (Entry<String, List<Cookie>> entry : cookieContainer.entrySet()) {
|
||||
writer.beginObject();
|
||||
writer.name("domain").value(entry.getKey());
|
||||
writer.name("cookies");
|
||||
writer.beginArray();
|
||||
for (Cookie cookie : entry.getValue()) {
|
||||
cookieAdapter.toJson(writer, cookie);
|
||||
}
|
||||
writer.endArray();
|
||||
writer.endObject();
|
||||
}
|
||||
writer.endArray();
|
||||
}
|
||||
|
||||
}
|
|
@ -13,6 +13,7 @@ public class CookieJsonAdapter extends JsonAdapter<Cookie> {
|
|||
|
||||
@Override
|
||||
public Cookie fromJson(JsonReader reader) throws IOException {
|
||||
reader.beginObject();
|
||||
Builder builder = new Cookie.Builder();
|
||||
// domain
|
||||
reader.nextName();
|
||||
|
@ -59,6 +60,7 @@ public class CookieJsonAdapter extends JsonAdapter<Cookie> {
|
|||
reader.nextName();
|
||||
builder.value(reader.nextString());
|
||||
|
||||
reader.endObject();
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
|
|
|
@ -1,10 +1,16 @@
|
|||
package ctbrec.io;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.Authenticator;
|
||||
import java.net.PasswordAuthentication;
|
||||
import java.nio.file.Files;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
|
@ -105,6 +111,7 @@ public abstract class HttpClient {
|
|||
|
||||
public void reconfigure() {
|
||||
loadProxySettings();
|
||||
loadCookies();
|
||||
Builder builder = new OkHttpClient.Builder()
|
||||
.cookieJar(cookieJar)
|
||||
.connectTimeout(Config.getInstance().getSettings().httpTimeout, TimeUnit.MILLISECONDS)
|
||||
|
@ -132,16 +139,54 @@ public abstract class HttpClient {
|
|||
|
||||
private void persistCookies() {
|
||||
try {
|
||||
Map<String, List<Cookie>> cookies = cookieJar.getCookies();
|
||||
Moshi moshi = new Moshi.Builder().add(Cookie.class, new CookieJsonAdapter()).build();
|
||||
@SuppressWarnings("rawtypes")
|
||||
JsonAdapter<Map> adapter = moshi.adapter(Map.class).indent(" ");
|
||||
CookieContainer cookies = new CookieContainer();
|
||||
cookies.putAll(cookieJar.getCookies());
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.add(CookieContainer.class, new CookieContainerJsonAdapter())
|
||||
.build();
|
||||
JsonAdapter<CookieContainer> adapter = moshi.adapter(CookieContainer.class).indent(" ");
|
||||
String json = adapter.toJson(cookies);
|
||||
|
||||
File cookieFile = new File(Config.getInstance().getConfigDir(), "cookies-" + name + ".json");
|
||||
try(FileOutputStream fout = new FileOutputStream(cookieFile)) {
|
||||
fout.write(json.getBytes("utf-8"));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.error("Couldn't persist cookies for {}", name, e);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
private void loadCookies() {
|
||||
try {
|
||||
File cookieFile = new File(Config.getInstance().getConfigDir(), "cookies-" + name + ".json");
|
||||
if(!cookieFile.exists()) {
|
||||
return;
|
||||
}
|
||||
byte[] jsonBytes = Files.readAllBytes(cookieFile.toPath());
|
||||
String json = new String(jsonBytes, "utf-8");
|
||||
|
||||
Map<String, List<Cookie>> cookies = cookieJar.getCookies();
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.add(CookieContainer.class, new CookieContainerJsonAdapter())
|
||||
.build();
|
||||
JsonAdapter<CookieContainer> adapter = moshi.adapter(CookieContainer.class).indent(" ");
|
||||
CookieContainer fromJson = adapter.fromJson(json);
|
||||
Set entries = fromJson.entrySet();
|
||||
for (Object _entry : entries) {
|
||||
Entry entry = (Entry) _entry;
|
||||
cookies.put((String)entry.getKey(), (List<Cookie>)entry.getValue());
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
LOG.error("Couldn't load cookies for {}", name, e);
|
||||
}
|
||||
}
|
||||
|
||||
public static class CookieContainer extends HashMap<String, List<Cookie>> {
|
||||
|
||||
}
|
||||
|
||||
private okhttp3.Authenticator createHttpProxyAuthenticator(String username, String password) {
|
||||
return new okhttp3.Authenticator() {
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue