Search in sources :

Example 36 with CookieJar

use of okhttp3.CookieJar in project talk-android by nextcloud.

the class PushUtils method pushRegistrationToServer.

public void pushRegistrationToServer() {
    String token = appPreferences.getPushToken();
    if (!TextUtils.isEmpty(token)) {
        String pushTokenHash = generateSHA512Hash(token).toLowerCase();
        PublicKey devicePublicKey = (PublicKey) readKeyFromFile(true);
        if (devicePublicKey != null) {
            byte[] publicKeyBytes = Base64.encode(devicePublicKey.getEncoded(), Base64.NO_WRAP);
            String publicKey = new String(publicKeyBytes);
            publicKey = publicKey.replaceAll("(.{64})", "$1\n");
            publicKey = "-----BEGIN PUBLIC KEY-----\n" + publicKey + "\n-----END PUBLIC KEY-----\n";
            if (userUtils.anyUserExists()) {
                String providerValue;
                PushConfigurationState accountPushData = null;
                for (Object userEntityObject : userUtils.getUsers()) {
                    UserEntity userEntity = (UserEntity) userEntityObject;
                    providerValue = userEntity.getPushConfigurationState();
                    if (!TextUtils.isEmpty(providerValue)) {
                        try {
                            accountPushData = LoganSquare.parse(providerValue, PushConfigurationState.class);
                        } catch (IOException e) {
                            Log.d(TAG, "Failed to parse account push data");
                            accountPushData = null;
                        }
                    } else {
                        accountPushData = null;
                    }
                    if (accountPushData != null && !accountPushData.getPushToken().equals(token) && !userEntity.getScheduledForDeletion() || TextUtils.isEmpty(providerValue) && !userEntity.getScheduledForDeletion()) {
                        Map<String, String> queryMap = new HashMap<>();
                        queryMap.put("format", "json");
                        queryMap.put("pushTokenHash", pushTokenHash);
                        queryMap.put("devicePublicKey", publicKey);
                        queryMap.put("proxyServer", proxyServer);
                        ncApi = retrofit.newBuilder().client(okHttpClient.newBuilder().cookieJar(new JavaNetCookieJar(new CookieManager())).build()).build().create(NcApi.class);
                        ncApi.registerDeviceForNotificationsWithNextcloud(ApiUtils.getCredentials(userEntity.getUsername(), userEntity.getToken()), ApiUtils.getUrlNextcloudPush(userEntity.getBaseUrl()), queryMap).subscribeOn(Schedulers.newThread()).subscribe(new Observer<PushRegistrationOverall>() {

                            @Override
                            public void onSubscribe(Disposable d) {
                            }

                            @Override
                            public void onNext(PushRegistrationOverall pushRegistrationOverall) {
                                Map<String, String> proxyMap = new HashMap<>();
                                proxyMap.put("pushToken", token);
                                proxyMap.put("deviceIdentifier", pushRegistrationOverall.getOcs().getData().getDeviceIdentifier());
                                proxyMap.put("deviceIdentifierSignature", pushRegistrationOverall.getOcs().getData().getSignature());
                                proxyMap.put("userPublicKey", pushRegistrationOverall.getOcs().getData().getPublicKey());
                                ncApi.registerDeviceForNotificationsWithProxy(ApiUtils.getCredentials(userEntity.getUsername(), userEntity.getToken()), ApiUtils.getUrlPushProxy(), proxyMap).subscribeOn(Schedulers.newThread()).subscribe(new Observer<Void>() {

                                    @Override
                                    public void onSubscribe(Disposable d) {
                                    }

                                    @Override
                                    public void onNext(Void aVoid) {
                                        PushConfigurationState pushConfigurationState = new PushConfigurationState();
                                        pushConfigurationState.setPushToken(token);
                                        pushConfigurationState.setDeviceIdentifier(pushRegistrationOverall.getOcs().getData().getDeviceIdentifier());
                                        pushConfigurationState.setDeviceIdentifierSignature(pushRegistrationOverall.getOcs().getData().getSignature());
                                        pushConfigurationState.setUserPublicKey(pushRegistrationOverall.getOcs().getData().getPublicKey());
                                        pushConfigurationState.setUsesRegularPass(false);
                                        try {
                                            userUtils.createOrUpdateUser(null, null, null, userEntity.getDisplayName(), LoganSquare.serialize(pushConfigurationState), null, null, userEntity.getId(), null).subscribe(new Observer<UserEntity>() {

                                                @Override
                                                public void onSubscribe(Disposable d) {
                                                }

                                                @Override
                                                public void onNext(UserEntity userEntity) {
                                                }

                                                @Override
                                                public void onError(Throwable e) {
                                                }

                                                @Override
                                                public void onComplete() {
                                                }
                                            });
                                        } catch (IOException e) {
                                            Log.e(TAG, "IOException while updating user");
                                        }
                                    }

                                    @Override
                                    public void onError(Throwable e) {
                                    }

                                    @Override
                                    public void onComplete() {
                                    }
                                });
                            }

                            @Override
                            public void onError(Throwable e) {
                            }

                            @Override
                            public void onComplete() {
                            }
                        });
                    }
                }
            }
        }
    }
}
Also used : JavaNetCookieJar(okhttp3.JavaNetCookieJar) Disposable(io.reactivex.disposables.Disposable) HashMap(java.util.HashMap) PublicKey(java.security.PublicKey) IOException(java.io.IOException) UserEntity(com.nextcloud.talk.models.database.UserEntity) PushRegistrationOverall(com.nextcloud.talk.models.json.push.PushRegistrationOverall) PushConfigurationState(com.nextcloud.talk.models.json.push.PushConfigurationState) Observer(io.reactivex.Observer) NcApi(com.nextcloud.talk.api.NcApi) HashMap(java.util.HashMap) Map(java.util.Map) CookieManager(java.net.CookieManager)

Example 37 with CookieJar

use of okhttp3.CookieJar in project apps-android-wikipedia by wikimedia.

the class OkHttpConnectionFactory method createClient.

@NonNull
private static OkHttpClient createClient() {
    SharedPreferenceCookieManager cookieManager = SharedPreferenceCookieManager.getInstance();
    // TODO: consider using okhttp3.CookieJar implementation instead of JavaNetCookieJar wrapper
    CookieJar cookieJar = new JavaNetCookieJar(cookieManager);
    return new OkHttpClient.Builder().cookieJar(cookieJar).cache(NET_CACHE).addInterceptor(new HttpLoggingInterceptor().setLevel(Prefs.getRetrofitLogLevel())).addInterceptor(new UnsuccessfulResponseInterceptor()).addInterceptor(new StatusResponseInterceptor(RbSwitch.INSTANCE)).addNetworkInterceptor(new StripMustRevalidateResponseInterceptor()).addInterceptor(new CommonHeaderRequestInterceptor()).addInterceptor(new DefaultMaxStaleRequestInterceptor()).addInterceptor(new CacheControlRequestInterceptor()).addInterceptor(new OfflineCacheInterceptor(SAVE_CACHE)).addInterceptor(new WikipediaZeroResponseInterceptor(WikipediaApp.getInstance().getWikipediaZeroHandler())).addInterceptor(new TestStubInterceptor()).build();
}
Also used : JavaNetCookieJar(okhttp3.JavaNetCookieJar) OkHttpClient(okhttp3.OkHttpClient) SharedPreferenceCookieManager(org.wikipedia.dataclient.SharedPreferenceCookieManager) JavaNetCookieJar(okhttp3.JavaNetCookieJar) CookieJar(okhttp3.CookieJar) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) NonNull(android.support.annotation.NonNull)

Example 38 with CookieJar

use of okhttp3.CookieJar in project instagram-java-scraper by postaddictme.

the class InstagramFactory method getAuthenticatedInstagramClient.

public static Instagram getAuthenticatedInstagramClient(String login, String password, String userAgent) throws IOException {
    OkHttpClient httpClient = new OkHttpClient.Builder().addInterceptor(new UserAgentInterceptor(userAgent)).addInterceptor(new ErrorInterceptor()).cookieJar(new DefaultCookieJar(new CookieHashSet())).build();
    Instagram client = new Instagram(httpClient);
    client.basePage();
    client.login(login, password);
    client.basePage();
    return client;
}
Also used : CookieHashSet(me.postaddict.instagram.scraper.cookie.CookieHashSet) OkHttpClient(okhttp3.OkHttpClient) ErrorInterceptor(me.postaddict.instagram.scraper.interceptor.ErrorInterceptor) UserAgentInterceptor(me.postaddict.instagram.scraper.interceptor.UserAgentInterceptor) DefaultCookieJar(me.postaddict.instagram.scraper.cookie.DefaultCookieJar)

Aggregations

Test (org.junit.Test)17 MockResponse (okhttp3.mockwebserver.MockResponse)16 OkHttpClient (okhttp3.OkHttpClient)14 CookieManager (java.net.CookieManager)11 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)9 HttpCookie (java.net.HttpCookie)8 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)8 MockWebServer (okhttp3.mockwebserver.MockWebServer)8 Request (okhttp3.Request)6 Response (okhttp3.Response)5 NonNull (android.support.annotation.NonNull)4 IOException (java.io.IOException)4 List (java.util.List)4 CookieHashSet (me.postaddict.instagram.scraper.cookie.CookieHashSet)4 DefaultCookieJar (me.postaddict.instagram.scraper.cookie.DefaultCookieJar)4 ErrorInterceptor (me.postaddict.instagram.scraper.interceptor.ErrorInterceptor)4 UserAgentInterceptor (me.postaddict.instagram.scraper.interceptor.UserAgentInterceptor)4 Cookie (okhttp3.Cookie)4 JavaNetCookieJar (okhttp3.JavaNetCookieJar)4 ClearableCookieJar (com.franmontiel.persistentcookiejar.ClearableCookieJar)3