Browse Source

Use new GetMethod, drop apache one

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
tobiasKaminsky 4 years ago
parent
commit
f97a86df68

+ 6 - 8
src/main/java/com/nextcloud/client/network/ClientFactory.java

@@ -28,21 +28,19 @@ import android.net.Uri;
 
 import com.nextcloud.client.account.User;
 import com.nextcloud.common.NextcloudClient;
+import com.nextcloud.common.PlainClient;
 import com.owncloud.android.lib.common.OwnCloudClient;
 import com.owncloud.android.lib.common.accounts.AccountUtils;
 
-import org.apache.commons.httpclient.HttpClient;
-
 import java.io.IOException;
 
 public interface ClientFactory {
 
     /**
-     * This exception wraps all possible errors thrown by trigger-happy
-     * OwnCloudClient constructor, making try-catch blocks manageable.
-     *
-     * This is a temporary refactoring measure, until a better
-     * error handling method can be procured.
+     * This exception wraps all possible errors thrown by trigger-happy OwnCloudClient constructor, making try-catch
+     * blocks manageable.
+     * <p>
+     * This is a temporary refactoring measure, until a better error handling method can be procured.
      */
     @Deprecated
     class CreationException extends Exception {
@@ -74,5 +72,5 @@ public interface ClientFactory {
 
     OwnCloudClient create(Uri uri, boolean followRedirects);
 
-    HttpClient createPlainClient();
+    PlainClient createPlainClient();
 }

+ 3 - 2
src/main/java/com/nextcloud/client/network/ClientFactoryImpl.java

@@ -29,6 +29,7 @@ import android.net.Uri;
 
 import com.nextcloud.client.account.User;
 import com.nextcloud.common.NextcloudClient;
+import com.nextcloud.common.PlainClient;
 import com.owncloud.android.lib.common.OwnCloudClient;
 import com.owncloud.android.lib.common.OwnCloudClientFactory;
 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
@@ -89,7 +90,7 @@ class ClientFactoryImpl implements ClientFactory {
     }
 
     @Override
-    public PlainHttpClient createPlainClient() {
-        return new PlainHttpClient();
+    public PlainClient createPlainClient() {
+        return new PlainClient(context);
     }
 }

+ 17 - 32
src/main/java/com/nextcloud/client/network/ConnectivityServiceImpl.java

@@ -26,44 +26,36 @@ import android.net.NetworkInfo;
 
 import com.nextcloud.client.account.Server;
 import com.nextcloud.client.account.UserAccountManager;
-import com.nextcloud.client.logger.Logger;
+import com.nextcloud.common.PlainClient;
+import com.nextcloud.operations.GetMethod;
 
-import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.HttpStatus;
-import org.apache.commons.httpclient.methods.GetMethod;
-
-import java.io.IOException;
 
 import androidx.core.net.ConnectivityManagerCompat;
 import kotlin.jvm.functions.Function1;
 
 class ConnectivityServiceImpl implements ConnectivityService {
 
-    private final static String TAG = ConnectivityServiceImpl.class.getName();
-
     private final ConnectivityManager platformConnectivityManager;
     private final UserAccountManager accountManager;
     private final ClientFactory clientFactory;
     private final GetRequestBuilder requestBuilder;
-    private final Logger logger;
 
     static class GetRequestBuilder implements Function1<String, GetMethod> {
         @Override
         public GetMethod invoke(String url) {
-            return new GetMethod(url);
+            return new GetMethod(url, false);
         }
     }
 
     ConnectivityServiceImpl(ConnectivityManager platformConnectivityManager,
                             UserAccountManager accountManager,
                             ClientFactory clientFactory,
-                            GetRequestBuilder requestBuilder,
-                            Logger logger) {
+                            GetRequestBuilder requestBuilder) {
         this.platformConnectivityManager = platformConnectivityManager;
         this.accountManager = accountManager;
         this.clientFactory = clientFactory;
         this.requestBuilder = requestBuilder;
-        this.logger = logger;
     }
 
     @Override
@@ -71,33 +63,26 @@ class ConnectivityServiceImpl implements ConnectivityService {
         Connectivity c = getConnectivity();
         if (c.isConnected() && c.isWifi()) {
 
-            GetMethod get = null;
-            try {
-                Server server = accountManager.getUser().getServer();
-                String baseServerAddress = server.getUri().toString();
-                if (baseServerAddress.isEmpty()) {
-                    return true;
-                }
+            GetMethod get;
+            Server server = accountManager.getUser().getServer();
+            String baseServerAddress = server.getUri().toString();
+            if (baseServerAddress.isEmpty()) {
+                return true;
+            }
 
-                get = requestBuilder.invoke(baseServerAddress + "/index.php/204");
-                HttpClient client = clientFactory.createPlainClient();
+            get = requestBuilder.invoke(baseServerAddress + "/index.php/204");
+            PlainClient client = clientFactory.createPlainClient();
 
-                int status = client.executeMethod(get);
+            int status = get.execute(client);
 
-                return !(status == HttpStatus.SC_NO_CONTENT &&
-                    (get.getResponseContentLength() == -1 || get.getResponseContentLength() == 0));
-            } catch (IOException e) {
-                logger.e(TAG, "Error checking internet connection", e);
-            } finally {
-                if (get != null) {
-                    get.releaseConnection();
-                }
+            if (get != null) {
+                get.releaseConnection();
             }
+
+            return !(status == HttpStatus.SC_NO_CONTENT && (get.getResponseBodyAsString().length() == 0));
         } else {
             return !getConnectivity().isConnected();
         }
-
-        return true;
     }
 
     @Override

+ 3 - 5
src/main/java/com/nextcloud/client/network/NetworkModule.java

@@ -24,7 +24,6 @@ import android.content.Context;
 import android.net.ConnectivityManager;
 
 import com.nextcloud.client.account.UserAccountManager;
-import com.nextcloud.client.logger.Logger;
 
 import javax.inject.Singleton;
 
@@ -37,13 +36,12 @@ public class NetworkModule {
     @Provides
     ConnectivityService connectivityService(ConnectivityManager connectivityManager,
                                             UserAccountManager accountManager,
-                                            ClientFactory clientFactory,
-                                            Logger logger) {
+                                            ClientFactory clientFactory) {
         return new ConnectivityServiceImpl(connectivityManager,
                                            accountManager,
                                            clientFactory,
-                                           new ConnectivityServiceImpl.GetRequestBuilder(),
-                                           logger);
+                                           new ConnectivityServiceImpl.GetRequestBuilder()
+        );
     }
 
     @Provides

+ 0 - 42
src/main/java/com/nextcloud/client/network/PlainHttpClient.java

@@ -1,42 +0,0 @@
-/*
- *
- *  * Nextcloud Android client application
- *  *
- *  * @author Tobias Kaminsky
- *  * Copyright (C) 2019 Tobias Kaminsky
- *  * Copyright (C) 2019 Nextcloud GmbH
- *  *
- *  * This program 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.
- *  *
- *  * This program 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 com.nextcloud.client.network;
-
-import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
-
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.HttpMethod;
-import org.apache.commons.httpclient.params.HttpMethodParams;
-
-import java.io.IOException;
-
-public class PlainHttpClient extends HttpClient {
-
-    @Override
-    public int executeMethod(HttpMethod method) throws IOException {
-        method.getParams().setParameter(HttpMethodParams.USER_AGENT, OwnCloudClientManagerFactory.getUserAgent());
-
-        return super.executeMethod(method);
-    }
-}

+ 1 - 2
src/test/java/com/nextcloud/client/network/ConnectivityServiceTest.kt

@@ -108,8 +108,7 @@ class ConnectivityServiceTest {
                 platformConnectivityManager,
                 accountManager,
                 clientFactory,
-                requestBuilder,
-                logger
+                requestBuilder
             )
 
             whenever(platformConnectivityManager.activeNetworkInfo).thenReturn(networkInfo)