From 33fd1037a3de3809391cc0484e1c3955a3c17225 Mon Sep 17 00:00:00 2001 From: davigonz Date: Tue, 2 Oct 2018 10:53:52 +0200 Subject: [PATCH] Update sample client to use new library --- .../lib/sampleclient/MainActivity.java | 181 ++++++----------- .../lib/common/OwnCloudClientFactory.java | 8 - .../common/OwnCloudClientManagerFactory.java | 3 +- .../android/lib/common/http/HttpClient.java | 20 +- .../lib/common/http/HttpConstants.java | 10 + .../SelfSignedConfidentSslSocketFactory.java | 185 ------------------ .../lib/test_project/TestActivity.java | 185 ++++++++---------- .../lib/test_project/test/CopyFileTest.java | 1 - .../test/GetCapabilitiesTest.java | 1 - .../lib/test_project/test/GetShareesTest.java | 1 - .../lib/test_project/test/MoveFileTest.java | 1 - .../test_project/test/OwnCloudClientTest.java | 1 - .../test/SingleSessionManagerTest.java | 1 - .../test/UpdatePrivateShareTest.java | 1 - .../test/UpdatePublicShareTest.java | 1 - 15 files changed, 158 insertions(+), 442 deletions(-) delete mode 100644 test_client/src/com/owncloud/android/lib/test_project/SelfSignedConfidentSslSocketFactory.java diff --git a/sample_client/src/com/owncloud/android/lib/sampleclient/MainActivity.java b/sample_client/src/com/owncloud/android/lib/sampleclient/MainActivity.java index 72e61460..7f105d2c 100644 --- a/sample_client/src/com/owncloud/android/lib/sampleclient/MainActivity.java +++ b/sample_client/src/com/owncloud/android/lib/sampleclient/MainActivity.java @@ -24,7 +24,10 @@ package com.owncloud.android.lib.sampleclient; +import android.annotation.SuppressLint; import android.app.Activity; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; import android.content.res.AssetManager; import android.graphics.drawable.BitmapDrawable; import android.net.Uri; @@ -38,22 +41,19 @@ import android.widget.Toast; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.OwnCloudClientFactory; +import com.owncloud.android.lib.common.OwnCloudClientManagerFactory; +import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory; import com.owncloud.android.lib.common.network.OnDatatransferProgressListener; import com.owncloud.android.lib.common.operations.OnRemoteOperationListener; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; -import com.owncloud.android.lib.refactor.OCContext; -import com.owncloud.android.lib.refactor.account.OCAccount; -import com.owncloud.android.lib.refactor.authentication.credentials.OwnCloudCredentialsFactory; -import com.owncloud.android.lib.refactor.operations.files.DownloadRemoteFileOperation; -import com.owncloud.android.lib.refactor.operations.files.PropfindOperation; -import com.owncloud.android.lib.refactor.operations.files.RemoveRemoteFileOperation; -import com.owncloud.android.lib.refactor.operations.files.UploadRemoteFileOperation; +import com.owncloud.android.lib.common.utils.Log_OC; +import com.owncloud.android.lib.resources.files.DownloadRemoteFileOperation; import com.owncloud.android.lib.resources.files.FileUtils; import com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation; import com.owncloud.android.lib.resources.files.RemoteFile; - -import at.bitfire.dav4android.DavResource; +import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation; +import com.owncloud.android.lib.resources.files.UploadRemoteFileOperation; import java.io.File; import java.io.FileOutputStream; @@ -63,13 +63,14 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import static android.content.ContentValues.TAG; + public class MainActivity extends Activity implements OnRemoteOperationListener, OnDatatransferProgressListener { private static String LOG_TAG = MainActivity.class.getCanonicalName(); private Handler mHandler; private OwnCloudClient mClient; - private OCContext mOCContext; private FilesArrayAdapter mFilesAdapter; private View mFrame; @@ -82,15 +83,16 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, mHandler = new Handler(); final Uri serverUri = Uri.parse(getString(R.string.server_base_url)); + + OwnCloudClientManagerFactory.setUserAgent(getUserAgent()); mClient = OwnCloudClientFactory.createOwnCloudClient(serverUri, this, true); - - OCAccount ocAccount = new OCAccount(serverUri, + mClient.setCredentials( OwnCloudCredentialsFactory.newBasicCredentials( - getString(R.string.username), - getString(R.string.password))); - - mOCContext = new OCContext(ocAccount, getString(R.string.user_agent)); + getString(R.string.username), + getString(R.string.password) + ) + ); mFilesAdapter = new FilesArrayAdapter(this, R.layout.file_in_list); ((ListView)findViewById(R.id.list_view)).setAdapter(mFilesAdapter); @@ -153,32 +155,11 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, } private void startRefresh() { - - final PropfindOperation propfindOperation = new PropfindOperation(mOCContext, FileUtils.PATH_SEPARATOR); - final Handler handler = new Handler(); - new Thread(() -> { - final PropfindOperation.Result result = propfindOperation.exec(); - final List remoteFiles = new ArrayList<>(); - if(!result.isSuccess()) { - handler.post(() -> - Toast.makeText(this, result.getLogMessage(), Toast.LENGTH_LONG).show()); - return; - } -// for(DavResource el : result.getData().getMembers()) { -// remoteFiles.add(new RemoteFile(el)); -// } - handler.post(() -> { - Toast.makeText(this, result.getData().getMembers().toString(), Toast.LENGTH_LONG).show(); - mFilesAdapter.clear(); - mFilesAdapter.addAll(remoteFiles); - }); - }).start(); -// ReadRemoteFolderOperation refreshOperation = new ReadRemoteFolderOperation(FileUtils.PATH_SEPARATOR); -// refreshOperation.onExecute(mClient, this, mHandler); + ReadRemoteFolderOperation refreshOperation = new ReadRemoteFolderOperation(FileUtils.PATH_SEPARATOR); + refreshOperation.execute(mClient, this, mHandler); } private void startUpload() { - File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path)); File fileToUpload = upFolder.listFiles()[0]; String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName(); @@ -188,29 +169,10 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, Long timeStampLong = fileToUpload.lastModified()/1000; String timeStamp = timeStampLong.toString(); - final UploadRemoteFileOperation uploadRemoteFileOperation = new UploadRemoteFileOperation( - mOCContext, - fileToUpload.getAbsolutePath(), - remotePath, - mimeType, - timeStamp - ); - final Handler handler = new Handler(); - - new Thread(() -> { - final UploadRemoteFileOperation.Result result = uploadRemoteFileOperation.exec(); - if (!result.isSuccess()) { - handler.post(() -> - Toast.makeText(this, result.getLogMessage(), Toast.LENGTH_LONG).show()); - return; - } - handler.post(() -> - Toast.makeText(this, "Upload successful", Toast.LENGTH_LONG).show()); - }).start(); - -// UploadRemoteFileOperation uploadOperation = new UploadRemoteFileOperation(fileToUpload.getAbsolutePath(), remotePath, mimeType, timeStamp); -// uploadOperation.addDatatransferProgressListener(this); -// uploadOperation.onExecute(mClient, this, mHandler); + UploadRemoteFileOperation uploadOperation = new UploadRemoteFileOperation(fileToUpload.getAbsolutePath(), + remotePath, mimeType, timeStamp); + uploadOperation.addDatatransferProgressListener(this); + uploadOperation.execute(mClient, this, mHandler); } private void startRemoteDeletion() { @@ -218,26 +180,8 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, File fileToUpload = upFolder.listFiles()[0]; String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName(); - final RemoveRemoteFileOperation removeRemoteFileOperation = new RemoveRemoteFileOperation( - mOCContext, - remotePath - ); - final Handler handler = new Handler(); - - new Thread(() -> { - final RemoveRemoteFileOperation.Result result = removeRemoteFileOperation.exec(); - if (!result.isSuccess()) { - handler.post(() -> - Toast.makeText(this, result.getLogMessage(), Toast.LENGTH_LONG).show()); - return; - } - handler.post(() -> - Toast.makeText(this, "Delete successful", Toast.LENGTH_LONG).show()); - }).start(); - - -// RemoveRemoteFileOperation removeOperation = new RemoveRemoteFileOperation(remotePath); -// removeOperation.onExecute(mClient, this, mHandler); + RemoveRemoteFileOperation removeOperation = new RemoveRemoteFileOperation(remotePath); + removeOperation.execute(mClient, this, mHandler); } private void startDownload() { @@ -247,31 +191,12 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, File fileToUpload = upFolder.listFiles()[0]; String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName(); - final DownloadRemoteFileOperation downloadRemoteFileOperation = new DownloadRemoteFileOperation( - mOCContext, - remotePath, - downFolder.getAbsolutePath() - ); - final Handler handler = new Handler(); - - new Thread(() -> { - final DownloadRemoteFileOperation.Result result = downloadRemoteFileOperation.exec(); - if (!result.isSuccess()) { - handler.post(() -> - Toast.makeText(this, result.getLogMessage(), Toast.LENGTH_LONG).show()); - return; - } - handler.post(() -> - Toast.makeText(this, "Download successful, pending file creation", - Toast.LENGTH_LONG).show()); - }).start(); - -// DownloadRemoteFileOperation downloadOperation = new DownloadRemoteFileOperation(remotePath, downFolder.getAbsolutePath()); -// downloadOperation.addDatatransferProgressListener(this); -// downloadOperation.onExecute(mClient, this, mHandler); + DownloadRemoteFileOperation downloadOperation = new DownloadRemoteFileOperation(remotePath, + downFolder.getAbsolutePath()); + downloadOperation.addDatatransferProgressListener(this); + downloadOperation.execute(mClient, this, mHandler); } - - @SuppressWarnings("deprecation") + private void startLocalDeletion() { File downFolder = new File(getCacheDir(), getString(R.string.download_folder_path)); File downloadedFile = downFolder.listFiles()[0]; @@ -295,11 +220,11 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, } else if (operation instanceof com.owncloud.android.lib.resources.files.UploadRemoteFileOperation) { onSuccessfulUpload((com.owncloud.android.lib.resources.files.UploadRemoteFileOperation)operation, result); -// } else if (operation instanceof RemoveRemoteFileOperation ) { -// onSuccessfulRemoteDeletion((RemoveRemoteFileOperation)operation, result); -// -// } else if (operation instanceof DownloadRemoteFileOperation ) { -// onSuccessfulDownload((DownloadRemoteFileOperation)operation, result); + } else if (operation instanceof RemoveRemoteFileOperation ) { + onSuccessfulRemoteDeletion((RemoveRemoteFileOperation)operation, result); + + } else if (operation instanceof DownloadRemoteFileOperation ) { + onSuccessfulDownload(); } else { Toast.makeText(this, R.string.todo_operation_finished_in_success, Toast.LENGTH_SHORT).show(); @@ -308,9 +233,9 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, private void onSuccessfulRefresh(ReadRemoteFolderOperation operation, RemoteOperationResult result) { mFilesAdapter.clear(); - List files = new ArrayList(); - for(Object obj: result.getData()) { - files.add((RemoteFile) obj); + List files = new ArrayList<>(); + for(RemoteFile remoteFile: (List) result.getData()) { + files.add(remoteFile); } if (files != null) { Iterator it = files.iterator(); @@ -334,8 +259,7 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, } } - @SuppressWarnings("deprecation") - private void onSuccessfulDownload(DownloadRemoteFileOperation operation, RemoteOperationResult result) { + private void onSuccessfulDownload() { File downFolder = new File(getCacheDir(), getString(R.string.download_folder_path)); File downloadedFile = downFolder.listFiles()[0]; BitmapDrawable bDraw = new BitmapDrawable(getResources(), downloadedFile.getAbsolutePath()); @@ -352,9 +276,9 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, public void run() { TextView progressView = null; if (upload) { - progressView = (TextView) findViewById(R.id.upload_progress); + progressView = findViewById(R.id.upload_progress); } else { - progressView = (TextView) findViewById(R.id.download_progress); + progressView = findViewById(R.id.download_progress); } if (progressView != null) { progressView.setText(Long.toString(percentage) + "%"); @@ -362,4 +286,25 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, } }); } + + // user agent + @SuppressLint("StringFormatInvalid") + private String getUserAgent() { + String appString = getResources().getString(R.string.user_agent); + String packageName = getPackageName(); + String version = ""; + + PackageInfo pInfo; + try { + pInfo = getPackageManager().getPackageInfo(packageName, 0); + if (pInfo != null) { + version = pInfo.versionName; + } + } catch (PackageManager.NameNotFoundException e) { + Log_OC.e(TAG, "Trying to get packageName", e.getCause()); + } + + // Mozilla/5.0 (Android) ownCloud-android/1.7.0 + return String.format(appString, version); + } } \ No newline at end of file diff --git a/src/com/owncloud/android/lib/common/OwnCloudClientFactory.java b/src/com/owncloud/android/lib/common/OwnCloudClientFactory.java index 933703e6..7fd4a799 100644 --- a/src/com/owncloud/android/lib/common/OwnCloudClientFactory.java +++ b/src/com/owncloud/android/lib/common/OwnCloudClientFactory.java @@ -45,12 +45,6 @@ import java.io.IOException; public class OwnCloudClientFactory { final private static String TAG = OwnCloudClientFactory.class.getSimpleName(); - - /** Default timeout for waiting data from the server */ - public static final int DEFAULT_DATA_TIMEOUT = 60000; - - /** Default timeout for establishing a connection */ - public static final int DEFAULT_CONNECTION_TIMEOUT = 60000; /** * Creates a OwnCloudClient setup for an ownCloud account @@ -157,8 +151,6 @@ public class OwnCloudClientFactory { boolean followRedirects) { OwnCloudClient client = new OwnCloudClient(uri); - client.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT); - client.setFollowRedirects(followRedirects); client.setContext(context); diff --git a/src/com/owncloud/android/lib/common/OwnCloudClientManagerFactory.java b/src/com/owncloud/android/lib/common/OwnCloudClientManagerFactory.java index fc675411..4fc6ea65 100644 --- a/src/com/owncloud/android/lib/common/OwnCloudClientManagerFactory.java +++ b/src/com/owncloud/android/lib/common/OwnCloudClientManagerFactory.java @@ -100,5 +100,4 @@ public class OwnCloudClientManagerFactory { } return false; } - -} +} \ No newline at end of file diff --git a/src/com/owncloud/android/lib/common/http/HttpClient.java b/src/com/owncloud/android/lib/common/http/HttpClient.java index 6a66cc28..a85e2a1c 100644 --- a/src/com/owncloud/android/lib/common/http/HttpClient.java +++ b/src/com/owncloud/android/lib/common/http/HttpClient.java @@ -102,6 +102,9 @@ public class HttpClient { OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder() .addInterceptor(getOkHttpInterceptor()) .protocols(Arrays.asList(Protocol.HTTP_1_1)) + .readTimeout(HttpConstants.DEFAULT_DATA_TIMEOUT, TimeUnit.MILLISECONDS) + .writeTimeout(HttpConstants.DEFAULT_DATA_TIMEOUT, TimeUnit.MILLISECONDS) + .connectTimeout(HttpConstants.DEFAULT_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS) .followRedirects(false) .sslSocketFactory(sslContext.getSocketFactory(), trustManager) .hostnameVerifier((asdf, usdf) -> true) @@ -126,23 +129,6 @@ public class HttpClient { return sOkHttpInterceptor; } - /** - * Sets the connection and wait-for-data timeouts to be applied by default to the methods - * performed by this client. - */ - public void setDefaultTimeouts(int defaultDataTimeout, int defaultConnectionTimeout) { - OkHttpClient.Builder clientBuilder = getOkHttpClient().newBuilder(); - if (defaultDataTimeout >= 0) { - clientBuilder - .readTimeout(defaultDataTimeout, TimeUnit.MILLISECONDS) - .writeTimeout(defaultDataTimeout, TimeUnit.MILLISECONDS); - } - if (defaultConnectionTimeout >= 0) { - clientBuilder.connectTimeout(defaultConnectionTimeout, TimeUnit.MILLISECONDS); - } - sOkHttpClient = clientBuilder.build(); - } - public void disableAutomaticCookiesHandling() { OkHttpClient.Builder clientBuilder = getOkHttpClient().newBuilder(); clientBuilder.cookieJar(new CookieJar() { diff --git a/src/com/owncloud/android/lib/common/http/HttpConstants.java b/src/com/owncloud/android/lib/common/http/HttpConstants.java index b7e07dce..35ed4784 100644 --- a/src/com/owncloud/android/lib/common/http/HttpConstants.java +++ b/src/com/owncloud/android/lib/common/http/HttpConstants.java @@ -174,4 +174,14 @@ public class HttpConstants { public static final int HTTP_HTTP_VERSION_NOT_SUPPORTED = 505; // 507 Insufficient Storage (WebDAV - RFC 2518) public static final int HTTP_INSUFFICIENT_STORAGE = 507; + + /*********************************************************************************************************** + *************************************************** TIMEOUTS ********************************************** + ***********************************************************************************************************/ + + /** Default timeout for waiting data from the server */ + public static final int DEFAULT_DATA_TIMEOUT = 60000; + + /** Default timeout for establishing a connection */ + public static final int DEFAULT_CONNECTION_TIMEOUT = 60000; } \ No newline at end of file diff --git a/test_client/src/com/owncloud/android/lib/test_project/SelfSignedConfidentSslSocketFactory.java b/test_client/src/com/owncloud/android/lib/test_project/SelfSignedConfidentSslSocketFactory.java deleted file mode 100644 index d9fc5c44..00000000 --- a/test_client/src/com/owncloud/android/lib/test_project/SelfSignedConfidentSslSocketFactory.java +++ /dev/null @@ -1,185 +0,0 @@ -/* ownCloud Android Library is available under MIT license - * Copyright (C) 2018 ownCloud GmbH. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS - * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN - * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - */ - -package com.owncloud.android.lib.test_project; - -import java.io.IOException; -import java.net.InetAddress; -import java.net.Socket; -import java.net.UnknownHostException; -import java.security.GeneralSecurityException; -import java.security.KeyStore; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.security.cert.CertStoreException; -import java.security.cert.CertificateException; -import java.security.cert.X509Certificate; - -import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManager; -import javax.net.ssl.TrustManagerFactory; -import javax.net.ssl.X509TrustManager; - -/** - * SelfSignedConfidentSslSocketFactory allows to create SSL {@link Socket}s - * that accepts self-signed server certificates. - * - * WARNING: this SHOULD NOT be used in productive environments. - * - * @author David A. Velasco - * @author Christian Schabesberger - */ - -public class SelfSignedConfidentSslSocketFactory implements SecureProtocolSocketFactory { - - - //private SSLContext mSslContext = null; - private AdvancedSslSocketFactory mWrappedSslSocketFactory = null; - - - /** - * Constructor for SelfSignedConfidentSslSocketFactory. - * @throws GeneralSecurityException - */ - public SelfSignedConfidentSslSocketFactory() throws GeneralSecurityException { - SSLContext sslContext = SSLContext.getInstance("TLS"); - sslContext.init( - null, - new TrustManager[] { new SelfSignedConfidentX509TrustManager() }, - null - ); - mWrappedSslSocketFactory = new AdvancedSslSocketFactory(sslContext, null, null); - } - - - /** - * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int) - */ - @Override - public Socket createSocket(String host, int port) throws IOException, UnknownHostException { - return mWrappedSslSocketFactory.createSocket(host, port); - } - - /** - * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int,java.net.InetAddress,int) - */ - @Override - public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort) - throws IOException, UnknownHostException { - return mWrappedSslSocketFactory.createSocket(host, port, clientHost, clientPort); - } - - /** - * Attempts to get a new socket connection to the given host within the given time limit. - * - * @param host The host name/IP - * @param port The port on the host - * @param clientHost The local host name/IP to bind the socket to - * @param clientPort The port on the local machine - * @param params {@link HttpConnectionParams} HTTP connection parameters. - * - * @return Socket A new socket - * - * @throws IOException if an I/O error occurs while creating the socket - * @throws UnknownHostException if the IP address of the host cannot be determined - */ - @Override - public Socket createSocket(String host, int port, InetAddress localAddress, int localPort, - HttpConnectionParams params) throws IOException, UnknownHostException, - ConnectTimeoutException { - - return mWrappedSslSocketFactory.createSocket(host, port, localAddress, localPort, params); - } - - /** - * @see SecureProtocolSocketFactory#createSocket(java.net.Socket,java.lang.String,int,boolean) - */ - @Override - public Socket createSocket(Socket socket, String host, int port, boolean autoClose) - throws IOException, UnknownHostException { - return mWrappedSslSocketFactory.createSocket(socket, host, port, autoClose); - } - - - public static class SelfSignedConfidentX509TrustManager implements X509TrustManager { - - private X509TrustManager mStandardTrustManager = null; - - public SelfSignedConfidentX509TrustManager() - throws NoSuchAlgorithmException, KeyStoreException, CertStoreException { - super(); - TrustManagerFactory factory = TrustManagerFactory - .getInstance(TrustManagerFactory.getDefaultAlgorithm()); - factory.init((KeyStore)null); - mStandardTrustManager = findX509TrustManager(factory); - } - - /** - * @see javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[],String authType) - */ - @Override - public void checkClientTrusted(X509Certificate[] chain, String authType) - throws CertificateException { - mStandardTrustManager.checkClientTrusted(chain, authType); - } - - /** - * @see javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[], - * String authType) - */ - @Override - public void checkServerTrusted(X509Certificate[] chain, String authType) - throws CertificateException { - if (chain != null && chain.length == 1) { - chain[0].checkValidity(); - } else { - mStandardTrustManager.checkServerTrusted(chain, authType); - } - } - - /** - * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers() - */ - public X509Certificate[] getAcceptedIssuers() { - return mStandardTrustManager.getAcceptedIssuers(); - } - - /** - * Locates the first X509TrustManager provided by a given TrustManagerFactory - * @param factory TrustManagerFactory to inspect in the search for a X509TrustManager - * @return The first X509TrustManager found in factory. - * @throws CertStoreException When no X509TrustManager instance was found in factory - */ - private X509TrustManager findX509TrustManager(TrustManagerFactory factory) - throws CertStoreException { - TrustManager tms[] = factory.getTrustManagers(); - for (int i = 0; i < tms.length; i++) { - if (tms[i] instanceof X509TrustManager) { - return (X509TrustManager) tms[i]; - } - } - return null; - } - } -} \ No newline at end of file diff --git a/test_client/src/com/owncloud/android/lib/test_project/TestActivity.java b/test_client/src/com/owncloud/android/lib/test_project/TestActivity.java index 3591bc18..f6f6ef38 100644 --- a/test_client/src/com/owncloud/android/lib/test_project/TestActivity.java +++ b/test_client/src/com/owncloud/android/lib/test_project/TestActivity.java @@ -1,22 +1,22 @@ /* ownCloud Android Library is available under MIT license * Copyright (C) 2018 ownCloud GmbH. - * + * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: - * + * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS - * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN - * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * @@ -24,15 +24,6 @@ package com.owncloud.android.lib.test_project; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.security.GeneralSecurityException; - - - - import android.app.Activity; import android.content.Context; import android.net.Uri; @@ -41,12 +32,8 @@ import android.util.Log; import android.view.Menu; import com.owncloud.android.lib.common.OwnCloudClient; -import com.owncloud.android.lib.common.OwnCloudClientFactory; import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory; -import com.owncloud.android.lib.common.network.NetworkUtils; -import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; -import com.owncloud.android.lib.resources.files.ChunkedUploadRemoteFileOperation; import com.owncloud.android.lib.resources.files.CreateRemoteFolderOperation; import com.owncloud.android.lib.resources.files.DownloadRemoteFileOperation; import com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation; @@ -54,12 +41,18 @@ import com.owncloud.android.lib.resources.files.RemoteFile; import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation; import com.owncloud.android.lib.resources.files.RenameRemoteFileOperation; import com.owncloud.android.lib.resources.files.UploadRemoteFileOperation; +import com.owncloud.android.lib.resources.files.chunks.ChunkedUploadRemoteFileOperation; import com.owncloud.android.lib.resources.shares.CreateRemoteShareOperation; import com.owncloud.android.lib.resources.shares.GetRemoteSharesOperation; import com.owncloud.android.lib.resources.shares.RemoveRemoteShareOperation; import com.owncloud.android.lib.resources.shares.ShareType; -import com.owncloud.android.lib.resources.users.GetRemoteUserQuotaOperation; import com.owncloud.android.lib.resources.users.GetRemoteUserAvatarOperation; +import com.owncloud.android.lib.resources.users.GetRemoteUserQuotaOperation; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; /** * Activity to test OC framework @@ -69,59 +62,43 @@ import com.owncloud.android.lib.resources.users.GetRemoteUserAvatarOperation; */ public class TestActivity extends Activity { - + private static final String TAG = null; // This account must exists on the server side private String mServerUri; private String mUser; private String mPass; - + private static final int BUFFER_SIZE = 1024; - + public static final String ASSETS__TEXT_FILE_NAME = "textFile.txt"; public static final String ASSETS__IMAGE_FILE_NAME = "imageFile.png"; public static final String ASSETS__VIDEO_FILE_NAME = "videoFile.MP4"; - + //private Account mAccount = null; private OwnCloudClient mClient; - + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); - + mServerUri = getString(R.string.server_base_url); mUser = getString(R.string.username); mPass = getString(R.string.password); - - Protocol pr = Protocol.getProtocol("https"); - if (pr == null || !(pr.getSocketFactory() instanceof SelfSignedConfidentSslSocketFactory)) { - try { - ProtocolSocketFactory psf = new SelfSignedConfidentSslSocketFactory(); - Protocol.registerProtocol( - "https", - new Protocol("https", psf, 443)); - - } catch (GeneralSecurityException e) { - Log.e(TAG, "Self-signed confident SSL context could not be loaded"); - } - } - - mClient = new OwnCloudClient(Uri.parse(mServerUri), NetworkUtils.getMultiThreadedConnManager()); - mClient.setDefaultTimeouts( - OwnCloudClientFactory.DEFAULT_DATA_TIMEOUT, - OwnCloudClientFactory.DEFAULT_CONNECTION_TIMEOUT); + + mClient = new OwnCloudClient(Uri.parse(mServerUri)); mClient.setFollowRedirects(true); mClient.setCredentials( OwnCloudCredentialsFactory.newBasicCredentials( - mUser, + mUser, mPass ) ); mClient.setBaseUri(Uri.parse(mServerUri)); - + Log.v(TAG, "onCreate finished, ownCloud client ready"); - + } @Override @@ -134,57 +111,57 @@ public class TestActivity extends Activity { /** * Access to the library method to Create a Folder * @param remotePath Full path to the new directory to create in the remote server. - * @param createFullPath 'True' means that all the ancestor folders should be created if + * @param createFullPath 'True' means that all the ancestor folders should be created if * don't exist yet. - * + * * @return */ public RemoteOperationResult createFolder(String remotePath, boolean createFullPath) { - + return TestActivity.createFolder(remotePath, createFullPath, mClient); } /** * Access to the library method to Create a Folder * @param remotePath Full path to the new directory to create in the remote server. - * @param createFullPath 'True' means that all the ancestor folders should be created if + * @param createFullPath 'True' means that all the ancestor folders should be created if * don't exist yet. * @param client Client instance configured to access the target OC server. - * + * * @return Result of the operation */ public static RemoteOperationResult createFolder( String remotePath, boolean createFullPath, OwnCloudClient client ) { - - CreateRemoteFolderOperation createOperation = + + CreateRemoteFolderOperation createOperation = new CreateRemoteFolderOperation(remotePath, createFullPath); RemoteOperationResult result = createOperation.execute(client); - + return result; } - + /** * Access to the library method to Rename a File or Folder * @param oldName Old name of the file. * @param oldRemotePath Old remote path of the file. For folders it starts and ends by "/" * @param newName New name to set as the name of file. * @param isFolder 'true' for folder and 'false' for files - * + * * @return */ public RemoteOperationResult renameFile(String oldName, String oldRemotePath, String newName, boolean isFolder) { - + RenameRemoteFileOperation renameOperation = new RenameRemoteFileOperation(oldName, oldRemotePath, newName, isFolder); RemoteOperationResult result = renameOperation.execute(mClient); - + return result; } - - /** + + /** * Access to the library method to Remove a File or Folder - * + * * @param remotePath Remote path of the file or folder in the server. * @return */ @@ -193,10 +170,10 @@ public class TestActivity extends Activity { RemoteOperationResult result = removeOperation.execute(mClient); return result; } - - /** + + /** * Access to the library method to Remove a File or Folder - * + * * @param remotePath Remote path of the file or folder in the server. * @return */ @@ -205,46 +182,46 @@ public class TestActivity extends Activity { RemoteOperationResult result = removeOperation.execute(client); return result; } - - + + /** * Access to the library method to Read a Folder (PROPFIND DEPTH 1) * @param remotePath - * + * * @return */ public RemoteOperationResult readFile(String remotePath) { - + ReadRemoteFolderOperation readOperation= new ReadRemoteFolderOperation(remotePath); RemoteOperationResult result = readOperation.execute(mClient); return result; } - + /** * Access to the library method to Download a File * @param remotePath - * + * * @return */ public RemoteOperationResult downloadFile(RemoteFile remoteFile, String temporalFolder) { - // Create folder + // Create folder String path = "/owncloud/tmp/" + temporalFolder; File privateFolder = getFilesDir(); File folder = new File(privateFolder.getAbsolutePath() + "/" + path); folder.mkdirs(); - + DownloadRemoteFileOperation downloadOperation = new DownloadRemoteFileOperation(remoteFile.getRemotePath(), folder.getAbsolutePath()); RemoteOperationResult result = downloadOperation.execute(mClient); return result; } - - /** Access to the library method to Upload a File + + /** Access to the library method to Upload a File * @param storagePath * @param remotePath * @param mimeType - * + * * @return */ public RemoteOperationResult uploadFile( @@ -253,13 +230,13 @@ public class TestActivity extends Activity { return TestActivity.uploadFile(storagePath, remotePath, mimeType, mClient); } - - /** Access to the library method to Upload a File + + /** Access to the library method to Upload a File * @param storagePath * @param remotePath * @param mimeType * @param client Client instance configured to access the target OC server. - * + * * @return */ public static RemoteOperationResult uploadFile( @@ -271,31 +248,31 @@ public class TestActivity extends Activity { UploadRemoteFileOperation uploadOperation; if ((new File(storagePath)).length() > ChunkedUploadRemoteFileOperation.CHUNK_SIZE ) { - uploadOperation = new ChunkedUploadRemoteFileOperation( - storagePath, remotePath, mimeType, fileLastModifTimestamp + uploadOperation = new ChunkedUploadRemoteFileOperation("1", + storagePath, remotePath, mimeType, TAG, fileLastModifTimestamp ); } else { uploadOperation = new UploadRemoteFileOperation( storagePath, remotePath, mimeType, fileLastModifTimestamp ); } - + RemoteOperationResult result = uploadOperation.execute(client); return result; } - /** Access to the library method to Get Shares - * + /** Access to the library method to Get Shares + * * @return */ public RemoteOperationResult getShares(){ - + GetRemoteSharesOperation getOperation = new GetRemoteSharesOperation(); RemoteOperationResult result = getOperation.execute(mClient); - + return result; } - + /** Access to the library method to Create Share * @param path Full path of the file/folder being shared. Mandatory argument * @param shareType 0 = user, 1 = group, 3 = Public link. Mandatory argument @@ -310,33 +287,33 @@ public class TestActivity extends Activity { * 16- Re-share * 31- All above Default for private shares * For user or group shares. - * To obtain combinations, add the desired values together. + * To obtain combinations, add the desired values together. * For instance, for Re-Share, delete, read, update add 16+8+2+1 = 27. - * + * * @return */ - public RemoteOperationResult createShare(String path, ShareType shareType, String shareWith, boolean publicUpload, + public RemoteOperationResult createShare(String path, ShareType shareType, String shareWith, boolean publicUpload, String password, int permissions){ - + CreateRemoteShareOperation createOperation = new CreateRemoteShareOperation(path, shareType, shareWith, publicUpload, password, permissions); RemoteOperationResult result = createOperation.execute(mClient); - + return result; } - - + + /** * Access to the library method to Remove Share - * + * * @param idShare Share ID */ - + public RemoteOperationResult removeShare(int idShare) { RemoveRemoteShareOperation removeOperation = new RemoveRemoteShareOperation(idShare); RemoteOperationResult result = removeOperation.execute(mClient); - + return result; - + } public RemoteOperationResult getQuota(String remotePath) { @@ -349,20 +326,20 @@ public class TestActivity extends Activity { GetRemoteUserAvatarOperation getUserAvatarOperation = new GetRemoteUserAvatarOperation(dimension, etag); return getUserAvatarOperation.execute(mClient); } - + /** * Extracts file from AssetManager to cache folder. - * + * * @param fileName Name of the asset file to extract. * @return File instance of the extracted file. */ public File extractAsset(String fileName) throws IOException { return TestActivity.extractAsset(fileName, this); } - + /** * Extracts file from AssetManager to cache folder. - * + * * @param fileName Name of the asset file to extract. * @param context Android context to access assets and file system. * @return File instance of the extracted file. diff --git a/test_client/tests/src/com/owncloud/android/lib/test_project/test/CopyFileTest.java b/test_client/tests/src/com/owncloud/android/lib/test_project/test/CopyFileTest.java index 9c6bf379..289c7846 100644 --- a/test_client/tests/src/com/owncloud/android/lib/test_project/test/CopyFileTest.java +++ b/test_client/tests/src/com/owncloud/android/lib/test_project/test/CopyFileTest.java @@ -37,7 +37,6 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode; import com.owncloud.android.lib.resources.files.CopyRemoteFileOperation; import com.owncloud.android.lib.test_project.R; -import com.owncloud.android.lib.test_project.SelfSignedConfidentSslSocketFactory; import com.owncloud.android.lib.test_project.TestActivity; import junit.framework.AssertionFailedError; diff --git a/test_client/tests/src/com/owncloud/android/lib/test_project/test/GetCapabilitiesTest.java b/test_client/tests/src/com/owncloud/android/lib/test_project/test/GetCapabilitiesTest.java index fa950575..9150d8fb 100644 --- a/test_client/tests/src/com/owncloud/android/lib/test_project/test/GetCapabilitiesTest.java +++ b/test_client/tests/src/com/owncloud/android/lib/test_project/test/GetCapabilitiesTest.java @@ -43,7 +43,6 @@ import com.owncloud.android.lib.common.network.NetworkUtils; import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.resources.status.GetRemoteCapabilitiesOperation; import com.owncloud.android.lib.test_project.R; -import com.owncloud.android.lib.test_project.SelfSignedConfidentSslSocketFactory; /** * Class to test GetRemoteCapabilitiesOperation diff --git a/test_client/tests/src/com/owncloud/android/lib/test_project/test/GetShareesTest.java b/test_client/tests/src/com/owncloud/android/lib/test_project/test/GetShareesTest.java index 9055c162..7140cb45 100644 --- a/test_client/tests/src/com/owncloud/android/lib/test_project/test/GetShareesTest.java +++ b/test_client/tests/src/com/owncloud/android/lib/test_project/test/GetShareesTest.java @@ -43,7 +43,6 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.resources.shares.GetRemoteShareesOperation; import com.owncloud.android.lib.resources.shares.ShareType; import com.owncloud.android.lib.test_project.R; -import com.owncloud.android.lib.test_project.SelfSignedConfidentSslSocketFactory; import android.content.Context; import android.net.Uri; diff --git a/test_client/tests/src/com/owncloud/android/lib/test_project/test/MoveFileTest.java b/test_client/tests/src/com/owncloud/android/lib/test_project/test/MoveFileTest.java index 76215cb3..9b9e781f 100644 --- a/test_client/tests/src/com/owncloud/android/lib/test_project/test/MoveFileTest.java +++ b/test_client/tests/src/com/owncloud/android/lib/test_project/test/MoveFileTest.java @@ -41,7 +41,6 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode; import com.owncloud.android.lib.resources.files.MoveRemoteFileOperation; import com.owncloud.android.lib.test_project.R; -import com.owncloud.android.lib.test_project.SelfSignedConfidentSslSocketFactory; import com.owncloud.android.lib.test_project.TestActivity; import android.content.Context; diff --git a/test_client/tests/src/com/owncloud/android/lib/test_project/test/OwnCloudClientTest.java b/test_client/tests/src/com/owncloud/android/lib/test_project/test/OwnCloudClientTest.java index 9d7dca35..f24ad35f 100644 --- a/test_client/tests/src/com/owncloud/android/lib/test_project/test/OwnCloudClientTest.java +++ b/test_client/tests/src/com/owncloud/android/lib/test_project/test/OwnCloudClientTest.java @@ -48,7 +48,6 @@ import com.owncloud.android.lib.common.authentication.OwnCloudCredentials; import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory; import com.owncloud.android.lib.common.network.NetworkUtils; import com.owncloud.android.lib.test_project.R; -import com.owncloud.android.lib.test_project.SelfSignedConfidentSslSocketFactory; /** diff --git a/test_client/tests/src/com/owncloud/android/lib/test_project/test/SingleSessionManagerTest.java b/test_client/tests/src/com/owncloud/android/lib/test_project/test/SingleSessionManagerTest.java index 8ad850e8..ef0c0b99 100644 --- a/test_client/tests/src/com/owncloud/android/lib/test_project/test/SingleSessionManagerTest.java +++ b/test_client/tests/src/com/owncloud/android/lib/test_project/test/SingleSessionManagerTest.java @@ -36,7 +36,6 @@ import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.SingleSessionManager; import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory; import com.owncloud.android.lib.test_project.R; -import com.owncloud.android.lib.test_project.SelfSignedConfidentSslSocketFactory; import junit.framework.AssertionFailedError; diff --git a/test_client/tests/src/com/owncloud/android/lib/test_project/test/UpdatePrivateShareTest.java b/test_client/tests/src/com/owncloud/android/lib/test_project/test/UpdatePrivateShareTest.java index 0873ddbc..bf31b06d 100644 --- a/test_client/tests/src/com/owncloud/android/lib/test_project/test/UpdatePrivateShareTest.java +++ b/test_client/tests/src/com/owncloud/android/lib/test_project/test/UpdatePrivateShareTest.java @@ -49,7 +49,6 @@ import com.owncloud.android.lib.resources.shares.RemoveRemoteShareOperation; import com.owncloud.android.lib.resources.shares.ShareType; import com.owncloud.android.lib.resources.shares.UpdateRemoteShareOperation; import com.owncloud.android.lib.test_project.R; -import com.owncloud.android.lib.test_project.SelfSignedConfidentSslSocketFactory; import com.owncloud.android.lib.test_project.TestActivity; /** diff --git a/test_client/tests/src/com/owncloud/android/lib/test_project/test/UpdatePublicShareTest.java b/test_client/tests/src/com/owncloud/android/lib/test_project/test/UpdatePublicShareTest.java index 3441b6ed..a54a37bd 100644 --- a/test_client/tests/src/com/owncloud/android/lib/test_project/test/UpdatePublicShareTest.java +++ b/test_client/tests/src/com/owncloud/android/lib/test_project/test/UpdatePublicShareTest.java @@ -50,7 +50,6 @@ import com.owncloud.android.lib.resources.shares.RemoveRemoteShareOperation; import com.owncloud.android.lib.resources.shares.ShareType; import com.owncloud.android.lib.resources.shares.UpdateRemoteShareOperation; import com.owncloud.android.lib.test_project.R; -import com.owncloud.android.lib.test_project.SelfSignedConfidentSslSocketFactory; import com.owncloud.android.lib.test_project.TestActivity; /**