mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-28 17:07:49 +00:00 
			
		
		
		
	Refactored handle of ownCloud URIs inside OwnCloudClient; rest of library adapted
This commit is contained in:
		
							parent
							
								
									0fa6919761
								
							
						
					
					
						commit
						b28701ca30
					
				| @ -43,6 +43,7 @@ import org.apache.commons.httpclient.params.HttpMethodParams; | ||||
| import org.apache.http.HttpStatus; | ||||
| import org.apache.http.params.CoreProtocolPNames; | ||||
| 
 | ||||
| import com.owncloud.android.lib.common.accounts.AccountUtils; | ||||
| import com.owncloud.android.lib.common.network.WebdavUtils; | ||||
| 
 | ||||
| 
 | ||||
| @ -66,15 +67,20 @@ public class OwnCloudClient extends HttpClient { | ||||
|     //private String mSsoSessionCookie = null; | ||||
|     private int mInstanceNumber = 0; | ||||
|      | ||||
|     private Uri mUri; | ||||
|     private Uri mWebdavUri; | ||||
|     private Uri mBaseUri; | ||||
|     //private Uri mWebdavUri; | ||||
|      | ||||
|     /** | ||||
|      * Constructor | ||||
|      */ | ||||
|     public OwnCloudClient(HttpConnectionManager connectionMgr) { | ||||
|     public OwnCloudClient(Uri baseUri, HttpConnectionManager connectionMgr) { | ||||
|         super(connectionMgr); | ||||
|          | ||||
|         if (baseUri == null) { | ||||
|         	throw new IllegalArgumentException("Parameter 'baseUri' cannot be NULL"); | ||||
|         } | ||||
|         mBaseUri = baseUri; | ||||
|          | ||||
|         mInstanceNumber = sIntanceCounter++; | ||||
|         Log.d(TAG + " #" + mInstanceNumber, "Creating OwnCloudClient"); | ||||
|          | ||||
| @ -131,7 +137,7 @@ public class OwnCloudClient extends HttpClient { | ||||
|     /* | ||||
|     public void setSsoSessionCookie(String accessToken) { | ||||
|         Log.d(TAG + " #" + mInstanceNumber, "Setting session cookie: " + accessToken); | ||||
|         Log.e(TAG + " #" + mInstanceNumber, "BASE URL: " + mUri); | ||||
|         Log.e(TAG + " #" + mInstanceNumber, "BASE URL: " + mBaseUri); | ||||
|         Log.e(TAG + " #" + mInstanceNumber, "WebDAV URL: " + mWebdavUri); | ||||
|          | ||||
|         if (accessToken != null && accessToken.length() > 0) { | ||||
| @ -141,7 +147,7 @@ public class OwnCloudClient extends HttpClient { | ||||
| 	        mSsoSessionCookie = accessToken; | ||||
| 	        mCredentials = null; | ||||
| 	 | ||||
| 	        Uri serverUri = (mUri != null)? mUri : mWebdavUri; | ||||
| 	        Uri serverUri = (mBaseUri != null)? mBaseUri : mWebdavUri; | ||||
| 	        	// TODO refactoring the mess of URIs | ||||
| 	         | ||||
| 	        String[] cookies = mSsoSessionCookie.split(";"); | ||||
| @ -183,7 +189,7 @@ public class OwnCloudClient extends HttpClient { | ||||
|      * @throws  Exception   When the existence could not be determined | ||||
|      */ | ||||
|     public boolean existsFile(String path) throws IOException, HttpException { | ||||
|         HeadMethod head = new HeadMethod(mWebdavUri.toString() + WebdavUtils.encodePath(path)); | ||||
|         HeadMethod head = new HeadMethod(getWebdavUri() + WebdavUtils.encodePath(path)); | ||||
|         try { | ||||
|             int status = executeMethod(head); | ||||
|             Log.d(TAG, "HEAD to " + path + " finished with HTTP status " + status + | ||||
| @ -323,31 +329,27 @@ public class OwnCloudClient extends HttpClient { | ||||
|             getHttpConnectionManager().getParams().setConnectionTimeout(defaultConnectionTimeout); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Sets the Webdav URI for the helper methods that receive paths as parameters,  | ||||
|      * instead of full URLs | ||||
|      * @param uri | ||||
|      */ | ||||
|     public void setWebdavUri(Uri uri) { | ||||
|         mWebdavUri = uri; | ||||
|     } | ||||
| 
 | ||||
|     public Uri getWebdavUri() { | ||||
|         return mWebdavUri; | ||||
|     	if (mCredentials instanceof OwnCloudBearerCredentials) { | ||||
|     		return Uri.parse(mBaseUri + AccountUtils.ODAV_PATH); | ||||
|     	} else { | ||||
|     		return Uri.parse(mBaseUri + AccountUtils.WEBDAV_PATH_4_0); | ||||
|     	} | ||||
|     } | ||||
|      | ||||
|     /** | ||||
|      * Sets the base URI for the helper methods that receive paths as parameters,  | ||||
|      * instead of full URLs | ||||
|      * Sets the root URI to the ownCloud server.    | ||||
|      * | ||||
|      * Use with care.  | ||||
|      *  | ||||
|      * @param uri | ||||
|      */ | ||||
|     public void setBaseUri(Uri uri) { | ||||
|         mUri = uri; | ||||
|         mBaseUri = uri; | ||||
|     } | ||||
| 
 | ||||
|     public Uri getBaseUri() { | ||||
|         return mUri; | ||||
|         return mBaseUri; | ||||
|     } | ||||
| 
 | ||||
|     /* | ||||
|  | ||||
| @ -72,14 +72,11 @@ public class OwnCloudClientFactory { | ||||
|      */ | ||||
|     public static OwnCloudClient createOwnCloudClient (Account account, Context appContext) throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException { | ||||
|         //Log_OC.d(TAG, "Creating OwnCloudClient associated to " + account.name); | ||||
|         | ||||
|         Uri webdavUri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account)); | ||||
|         Uri uri = Uri.parse(AccountUtils.getBaseUrlForAccount(appContext, account)); | ||||
|         Uri baseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(appContext, account)); | ||||
|         AccountManager am = AccountManager.get(appContext); | ||||
|         boolean isOauth2 = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_OAUTH2) != null;   // TODO avoid calling to getUserData here | ||||
|         boolean isSamlSso = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null; | ||||
|         OwnCloudClient client = createOwnCloudClient(webdavUri, appContext, !isSamlSso); | ||||
|         client.setBaseUri(uri); | ||||
|         OwnCloudClient client = createOwnCloudClient(baseUri, appContext, !isSamlSso); | ||||
|          | ||||
|         if (isOauth2) {     | ||||
|             String accessToken = am.blockingGetAuthToken( | ||||
| @ -123,13 +120,11 @@ public class OwnCloudClientFactory { | ||||
|      | ||||
|      | ||||
|     public static OwnCloudClient createOwnCloudClient (Account account, Context appContext, Activity currentActivity) throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException { | ||||
|         Uri webdavUri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account)); | ||||
|         Uri uri = Uri.parse(AccountUtils.getBaseUrlForAccount(appContext, account)); | ||||
|         Uri baseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(appContext, account)); | ||||
|         AccountManager am = AccountManager.get(appContext); | ||||
|         boolean isOauth2 = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_OAUTH2) != null;   // TODO avoid calling to getUserData here | ||||
|         boolean isSamlSso = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null; | ||||
|         OwnCloudClient client = createOwnCloudClient(webdavUri, appContext, !isSamlSso); | ||||
|         client.setBaseUri(uri); | ||||
|         OwnCloudClient client = createOwnCloudClient(baseUri, appContext, !isSamlSso); | ||||
|          | ||||
|         if (isOauth2) {    // TODO avoid a call to getUserData here | ||||
|             AccountManagerFuture<Bundle> future =  am.getAuthToken( | ||||
| @ -192,7 +187,7 @@ public class OwnCloudClientFactory { | ||||
|     /** | ||||
|      * Creates a OwnCloudClient to access a URL and sets the desired parameters for ownCloud client connections. | ||||
|      *  | ||||
|      * @param uri       URL to the ownCloud server | ||||
|      * @param uri       URL to the ownCloud server; BASE ENTRY POINT, not WebDavPATH | ||||
|      * @param context   Android context where the OwnCloudClient is being created. | ||||
|      * @return          A OwnCloudClient object ready to be used | ||||
|      */ | ||||
| @ -206,10 +201,8 @@ public class OwnCloudClientFactory { | ||||
|             Log.e(TAG, "The local server truststore could not be read. Default SSL management in the system will be used for HTTPS connections", e); | ||||
|         } | ||||
|          | ||||
|         OwnCloudClient client = new OwnCloudClient(NetworkUtils.getMultiThreadedConnManager()); | ||||
|          | ||||
|         OwnCloudClient client = new OwnCloudClient(uri, NetworkUtils.getMultiThreadedConnManager()); | ||||
|         client.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT); | ||||
|         client.setWebdavUri(uri); | ||||
|         client.setFollowRedirects(followRedirects); | ||||
|          | ||||
|         return client; | ||||
|  | ||||
| @ -45,7 +45,7 @@ public class AccountUtils { | ||||
|     public static final String WEBDAV_PATH_1_2 = "/webdav/owncloud.php"; | ||||
|     public static final String WEBDAV_PATH_2_0 = "/files/webdav.php"; | ||||
|     public static final String WEBDAV_PATH_4_0 = "/remote.php/webdav"; | ||||
|     private static final String ODAV_PATH = "/remote.php/odav"; | ||||
|     public static final String ODAV_PATH = "/remote.php/odav"; | ||||
|     private static final String SAML_SSO_PATH = "/remote.php/webdav"; | ||||
|     public static final String CARDDAV_PATH_2_0 = "/apps/contacts/carddav.php"; | ||||
|     public static final String CARDDAV_PATH_4_0 = "/remote/carddav.php"; | ||||
| @ -81,11 +81,15 @@ public class AccountUtils { | ||||
|      | ||||
|     /** | ||||
|      * Constructs full url to host and webdav resource basing on host version | ||||
|      *  | ||||
|      * @deprecated 		To be removed in release 1.0.  | ||||
|      *  | ||||
|      * @param context | ||||
|      * @param account | ||||
|      * @return url or null on failure | ||||
|      * @throws AccountNotFoundException     When 'account' is unknown for the AccountManager | ||||
|      */ | ||||
|     @Deprecated | ||||
|     public static String constructFullURLForAccount(Context context, Account account) throws AccountNotFoundException { | ||||
|         AccountManager ama = AccountManager.get(context); | ||||
|         String baseurl = ama.getUserData(account, Constants.KEY_OC_BASE_URL); | ||||
|  | ||||
| @ -59,37 +59,39 @@ public class GetRemoteStatusOperation extends RemoteOperation { | ||||
|     private static final String NODE_INSTALLED = "installed"; | ||||
|     private static final String NODE_VERSION = "version"; | ||||
|      | ||||
|     private String mUrl; | ||||
|     private RemoteOperationResult mLatestResult; | ||||
|     private Context mContext; | ||||
| 
 | ||||
|     public GetRemoteStatusOperation(String url, Context context) { | ||||
|         mUrl = url; | ||||
|     public GetRemoteStatusOperation(Context context) { | ||||
|         mContext = context; | ||||
|     } | ||||
|      | ||||
|     private boolean tryConnection(OwnCloudClient wc, String urlSt) { | ||||
|     private boolean tryConnection(OwnCloudClient client) { | ||||
|         boolean retval = false; | ||||
|         GetMethod get = null; | ||||
|         String baseUrlSt = client.getBaseUri().toString(); | ||||
|         try { | ||||
|             get = new GetMethod(urlSt); | ||||
|             int status = wc.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT); | ||||
|             get = new GetMethod(baseUrlSt + AccountUtils.STATUS_PATH); | ||||
|             int status = client.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT); | ||||
|             String response = get.getResponseBodyAsString(); | ||||
|             if (status == HttpStatus.SC_OK) { | ||||
|                 JSONObject json = new JSONObject(response); | ||||
|                 if (!json.getBoolean(NODE_INSTALLED)) { | ||||
|                     mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED); | ||||
|                     mLatestResult = new RemoteOperationResult( | ||||
|                     		RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED); | ||||
|                 } else { | ||||
|                     String version = json.getString(NODE_VERSION); | ||||
| 					OwnCloudVersion ocVersion = new OwnCloudVersion(version); | ||||
|                     if (!ocVersion.isVersionValid()) { | ||||
|                         mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.BAD_OC_VERSION); | ||||
|                         mLatestResult = new RemoteOperationResult( | ||||
|                         		RemoteOperationResult.ResultCode.BAD_OC_VERSION); | ||||
|                          | ||||
|                     } else { | ||||
|                         mLatestResult = new RemoteOperationResult(urlSt.startsWith("https://") ?  | ||||
|                                                                     RemoteOperationResult.ResultCode.OK_SSL :  | ||||
|                                                                     RemoteOperationResult.ResultCode.OK_NO_SSL | ||||
|                             ); | ||||
|                         mLatestResult = new RemoteOperationResult( | ||||
|                         		baseUrlSt.startsWith("https://") ? | ||||
|                         				RemoteOperationResult.ResultCode.OK_SSL : | ||||
|                     					RemoteOperationResult.ResultCode.OK_NO_SSL | ||||
|                         ); | ||||
| 
 | ||||
|                         ArrayList<Object> data = new ArrayList<Object>(); | ||||
|                         data.add(ocVersion); | ||||
| @ -103,7 +105,8 @@ public class GetRemoteStatusOperation extends RemoteOperation { | ||||
|             } | ||||
| 
 | ||||
|         } catch (JSONException e) { | ||||
|             mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED); | ||||
|             mLatestResult = new RemoteOperationResult( | ||||
|             		RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED); | ||||
|              | ||||
|         } catch (Exception e) { | ||||
|             mLatestResult = new RemoteOperationResult(e); | ||||
| @ -114,13 +117,13 @@ public class GetRemoteStatusOperation extends RemoteOperation { | ||||
|         } | ||||
|          | ||||
|         if (mLatestResult.isSuccess()) { | ||||
|             Log.i(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage()); | ||||
|             Log.i(TAG, "Connection check at " + baseUrlSt + ": " + mLatestResult.getLogMessage()); | ||||
|              | ||||
|         } else if (mLatestResult.getException() != null) { | ||||
|             Log.e(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage(), mLatestResult.getException()); | ||||
|             Log.e(TAG, "Connection check at " + baseUrlSt + ": " + mLatestResult.getLogMessage(), mLatestResult.getException()); | ||||
|              | ||||
|         } else { | ||||
|             Log.e(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage()); | ||||
|             Log.e(TAG, "Connection check at " + baseUrlSt + ": " + mLatestResult.getLogMessage()); | ||||
|         } | ||||
| 
 | ||||
|         return retval; | ||||
| @ -138,16 +141,17 @@ public class GetRemoteStatusOperation extends RemoteOperation { | ||||
|         if (!isOnline()) { | ||||
|         	return new RemoteOperationResult(RemoteOperationResult.ResultCode.NO_NETWORK_CONNECTION); | ||||
|         } | ||||
|         if (mUrl.startsWith("http://") || mUrl.startsWith("https://")) { | ||||
|             tryConnection(client, mUrl + AccountUtils.STATUS_PATH); | ||||
|         String baseUriStr = client.getBaseUri().toString(); | ||||
|         if (baseUriStr.startsWith("http://") || baseUriStr.startsWith("https://")) { | ||||
|             tryConnection(client); | ||||
|              | ||||
|         } else { | ||||
|             client.setWebdavUri(Uri.parse("https://" + mUrl + AccountUtils.STATUS_PATH)); | ||||
|             boolean httpsSuccess = tryConnection(client, "https://" + mUrl + AccountUtils.STATUS_PATH);  | ||||
|             client.setBaseUri(Uri.parse("https://" + baseUriStr)); | ||||
|             boolean httpsSuccess = tryConnection(client);  | ||||
|             if (!httpsSuccess && !mLatestResult.isSslRecoverableException()) { | ||||
|                 Log.d(TAG, "establishing secure connection failed, trying non secure connection"); | ||||
|                 client.setWebdavUri(Uri.parse("http://" + mUrl + AccountUtils.STATUS_PATH)); | ||||
|                 tryConnection(client, "http://" + mUrl + AccountUtils.STATUS_PATH); | ||||
|                 client.setBaseUri(Uri.parse("http://" + baseUriStr)); | ||||
|                 tryConnection(client); | ||||
|             } | ||||
|         } | ||||
|         return mLatestResult; | ||||
|  | ||||
| @ -78,7 +78,7 @@ public class GetRemoteUserNameOperation extends RemoteOperation { | ||||
| 
 | ||||
| 		//Get the user | ||||
| 		try { | ||||
| 			get = new GetMethod(client.getWebdavUri() + OCS_ROUTE); | ||||
| 			get = new GetMethod(client.getBaseUri() + OCS_ROUTE); | ||||
| 			get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); | ||||
| 			status = client.executeMethod(get); | ||||
| 			if(isSuccess(status)) { | ||||
|  | ||||
| @ -74,7 +74,6 @@ public class TestActivity extends Activity { | ||||
| 	private String mPass; | ||||
| 	private boolean mChunked; | ||||
| 	 | ||||
| 	private static final String WEBDAV_PATH = "/remote.php/webdav"; | ||||
| 	private static final int BUFFER_SIZE = 1024; | ||||
| 	 | ||||
| 	public static final String ASSETS__TEXT_FILE_NAME = "textFile.txt"; | ||||
| @ -107,12 +106,10 @@ public class TestActivity extends Activity { | ||||
| 			} | ||||
| 		} | ||||
| 		 | ||||
| 		Uri uri = Uri.parse(mServerUri + WEBDAV_PATH); | ||||
| 		mClient = new OwnCloudClient(NetworkUtils.getMultiThreadedConnManager()); | ||||
| 		mClient = new OwnCloudClient(Uri.parse(mServerUri), NetworkUtils.getMultiThreadedConnManager()); | ||||
| 		mClient.setDefaultTimeouts( | ||||
| 				OwnCloudClientFactory.DEFAULT_DATA_TIMEOUT,  | ||||
| 				OwnCloudClientFactory.DEFAULT_CONNECTION_TIMEOUT); | ||||
| 		mClient.setWebdavUri(uri); | ||||
| 		mClient.setFollowRedirects(true); | ||||
| 		mClient.setCredentials( | ||||
| 				OwnCloudCredentialsFactory.newBasicCredentials( | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user