mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 02:17:41 +00:00 
			
		
		
		
	Update sample client to use new library
This commit is contained in:
		
							parent
							
								
									096d037f94
								
							
						
					
					
						commit
						33fd1037a3
					
				| @ -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.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<RemoteFile> 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<RemoteFile> files = new ArrayList<RemoteFile>(); | ||||
|         for(Object obj: result.getData()) { | ||||
|             files.add((RemoteFile) obj); | ||||
| 		List<RemoteFile> files = new ArrayList<>(); | ||||
|         for(RemoteFile remoteFile: (List<RemoteFile>) result.getData()) { | ||||
|             files.add(remoteFile); | ||||
|         } | ||||
| 		if (files != null) { | ||||
| 			Iterator<RemoteFile> 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); | ||||
| 	} | ||||
| } | ||||
| @ -46,12 +46,6 @@ 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); | ||||
|  | ||||
| @ -100,5 +100,4 @@ public class OwnCloudClientManagerFactory { | ||||
|         } | ||||
|         return false; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -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() { | ||||
|  | ||||
| @ -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; | ||||
| } | ||||
| @ -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; | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| @ -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 | ||||
| @ -94,23 +87,7 @@ public class TestActivity extends Activity { | ||||
| 		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( | ||||
| @ -271,8 +248,8 @@ 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( | ||||
|  | ||||
| @ -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; | ||||
|  | ||||
| @ -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 | ||||
|  | ||||
| @ -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; | ||||
|  | ||||
| @ -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; | ||||
|  | ||||
| @ -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; | ||||
| 
 | ||||
| 
 | ||||
| /** | ||||
|  | ||||
| @ -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; | ||||
| 
 | ||||
|  | ||||
| @ -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; | ||||
| 
 | ||||
| /** | ||||
|  | ||||
| @ -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; | ||||
| 
 | ||||
| /** | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user