Added OBJavaNetCookieJar, and overriding the cookie jar
This commit is contained in:
parent
412991129d
commit
4165d7451f
|
@ -2,15 +2,25 @@ package org.skolplattformen.app;
|
|||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
|
||||
import com.facebook.react.PackageList;
|
||||
import com.facebook.react.ReactApplication;
|
||||
import com.facebook.react.ReactInstanceManager;
|
||||
import com.facebook.react.ReactNativeHost;
|
||||
import com.facebook.react.ReactPackage;
|
||||
import com.facebook.react.modules.network.ForwardingCookieHandler;
|
||||
import com.facebook.react.modules.network.OkHttpClientProvider;
|
||||
import com.facebook.react.modules.network.ReactCookieJarContainer;
|
||||
import com.facebook.soloader.SoLoader;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.CookieHandler;
|
||||
import java.util.List;
|
||||
|
||||
import okhttp3.CookieJar;
|
||||
import okhttp3.JavaNetCookieJar;
|
||||
import okhttp3.OkHttpClient;
|
||||
|
||||
public class MainApplication extends Application implements ReactApplication {
|
||||
|
||||
private final ReactNativeHost mReactNativeHost =
|
||||
|
@ -45,9 +55,28 @@ public class MainApplication extends Application implements ReactApplication {
|
|||
super.onCreate();
|
||||
SoLoader.init(this, /* native exopackage */ false);
|
||||
initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
|
||||
changeCookieManager();
|
||||
}
|
||||
|
||||
/**
|
||||
private void changeCookieManager() {
|
||||
OkHttpClientProvider.setOkHttpClientFactory(() -> {
|
||||
OkHttpClient.Builder builder = OkHttpClientProvider.createClientBuilder(
|
||||
mReactNativeHost.getReactInstanceManager().getCurrentReactContext());
|
||||
|
||||
|
||||
builder.cookieJar(new ReactCookieJarContainer() {
|
||||
|
||||
@Override
|
||||
public void setCookieJar(CookieJar cookieJar) {
|
||||
super.setCookieJar(new OBJavaNetCookieJar(new ForwardingCookieHandler(mReactNativeHost.getReactInstanceManager().getCurrentReactContext())));
|
||||
}
|
||||
});
|
||||
return builder.build();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Loads Flipper in React Native templates. Call this in the onCreate method with something like
|
||||
* initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
|
||||
*
|
||||
|
@ -77,4 +106,6 @@ public class MainApplication extends Application implements ReactApplication {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,98 @@
|
|||
|
||||
package org.skolplattformen.app;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.CookieHandler;
|
||||
import java.net.HttpCookie;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import okhttp3.Cookie;
|
||||
import okhttp3.CookieJar;
|
||||
import okhttp3.HttpUrl;
|
||||
import okhttp3.internal.platform.Platform;
|
||||
|
||||
import static okhttp3.internal.Util.delimiterOffset;
|
||||
import static okhttp3.internal.Util.trimSubstring;
|
||||
import static okhttp3.internal.platform.Platform.WARN;
|
||||
|
||||
|
||||
final class OBJavaNetCookieJar implements CookieJar {
|
||||
private final CookieHandler cookieHandler;
|
||||
|
||||
public OBJavaNetCookieJar(CookieHandler cookieHandler) {
|
||||
this.cookieHandler = cookieHandler;
|
||||
}
|
||||
|
||||
@Override public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
|
||||
if (cookieHandler != null) {
|
||||
List<String> cookieStrings = new ArrayList<>();
|
||||
for (Cookie cookie : cookies) {
|
||||
cookieStrings.add(cookie.toString());
|
||||
}
|
||||
Map<String, List<String>> multimap = Collections.singletonMap("Set-Cookie", cookieStrings);
|
||||
try {
|
||||
cookieHandler.put(url.uri(), multimap);
|
||||
} catch (IOException e) {
|
||||
Platform.get().log(WARN, "Saving cookies failed for " + url.resolve("/..."), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override public List<Cookie> loadForRequest(HttpUrl url) {
|
||||
// The RI passes all headers. We don't have 'em, so we don't pass 'em!
|
||||
Map<String, List<String>> headers = Collections.emptyMap();
|
||||
Map<String, List<String>> cookieHeaders;
|
||||
try {
|
||||
cookieHeaders = cookieHandler.get(url.uri(), headers);
|
||||
} catch (IOException e) {
|
||||
Platform.get().log(WARN, "Loading cookies failed for " + url.resolve("/..."), e);
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
List<Cookie> cookies = null;
|
||||
for (Map.Entry<String, List<String>> entry : cookieHeaders.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
if (("Cookie".equalsIgnoreCase(key) || "Cookie2".equalsIgnoreCase(key))
|
||||
&& !entry.getValue().isEmpty()) {
|
||||
for (String header : entry.getValue()) {
|
||||
if (cookies == null) cookies = new ArrayList<>();
|
||||
cookies.addAll(decodeHeaderAsJavaNetCookies(url, header));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return cookies != null
|
||||
? Collections.unmodifiableList(cookies)
|
||||
: Collections.emptyList();
|
||||
}
|
||||
|
||||
private List<Cookie> decodeHeaderAsJavaNetCookies(HttpUrl url, String header) {
|
||||
List<Cookie> result = new ArrayList<>();
|
||||
for (int pos = 0, limit = header.length(), pairEnd; pos < limit; pos = pairEnd + 1) {
|
||||
pairEnd = delimiterOffset(header, pos, limit, ";");
|
||||
int equalsSign = delimiterOffset(header, pos, pairEnd, '=');
|
||||
String name = trimSubstring(header, pos, equalsSign);
|
||||
if (name.startsWith("$")) continue;
|
||||
|
||||
// We have either name=value or just a name.
|
||||
String value = equalsSign < pairEnd
|
||||
? trimSubstring(header, equalsSign + 1, pairEnd)
|
||||
: "";
|
||||
|
||||
// If the value is "quoted", drop the quotes.
|
||||
if (value.startsWith("\"") && value.endsWith("\"")) {
|
||||
value = value.substring(1, value.length() - 1);
|
||||
}
|
||||
|
||||
result.add(new Cookie.Builder()
|
||||
.name(name)
|
||||
.value(value)
|
||||
.domain(url.host())
|
||||
.build());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue