mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 10:27:45 +00:00 
			
		
		
		
	refactor run function
This commit is contained in:
		
							parent
							
								
									bbb4487973
								
							
						
					
					
						commit
						884820e6c6
					
				| @ -35,7 +35,6 @@ import org.json.JSONObject | |||||||
| import timber.log.Timber | import timber.log.Timber | ||||||
| import java.net.URL | import java.net.URL | ||||||
| import java.util.concurrent.TimeUnit | import java.util.concurrent.TimeUnit | ||||||
| import javax.net.ssl.SSLException |  | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Checks if the server is valid |  * Checks if the server is valid | ||||||
| @ -46,26 +45,18 @@ import javax.net.ssl.SSLException | |||||||
|  * @author Abel García de Prada |  * @author Abel García de Prada | ||||||
|  */ |  */ | ||||||
| class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() { | class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() { | ||||||
|     private lateinit var latestResult: RemoteOperationResult<OwnCloudVersion> |  | ||||||
| 
 |  | ||||||
|     override fun run(client: OwnCloudClient): RemoteOperationResult<OwnCloudVersion> { |     override fun run(client: OwnCloudClient): RemoteOperationResult<OwnCloudVersion> { | ||||||
|  |         if (client.baseUri.scheme.isNullOrEmpty()) | ||||||
|  |             client.baseUri = Uri.parse(HTTPS_SCHEME + "://" + client.baseUri.toString()) | ||||||
| 
 | 
 | ||||||
|         val baseUriStr = client.baseUri.toString() |         var result = tryToConnect(client) | ||||||
|         if (baseUriStr.startsWith(HTTP_PREFIX) || baseUriStr.startsWith( |         if (result.code != ResultCode.OK_SSL && !result.isSslRecoverableException) { | ||||||
|                 HTTPS_PREFIX |             Timber.d("Establishing secure connection failed, trying non secure connection") | ||||||
|             ) |             client.baseUri = client.baseUri.buildUpon().scheme(HTTP_SCHEME).build() | ||||||
|         ) { |             result = tryToConnect(client) | ||||||
|             tryConnection(client) |  | ||||||
|         } else { |  | ||||||
|             client.baseUri = Uri.parse(HTTPS_PREFIX + baseUriStr) |  | ||||||
|             val httpsSuccess = tryConnection(client) |  | ||||||
|             if (!httpsSuccess && !latestResult.isSslRecoverableException) { |  | ||||||
|                 Timber.d("Establishing secure connection failed, trying non secure connection") |  | ||||||
|                 client.baseUri = Uri.parse(HTTP_PREFIX + baseUriStr) |  | ||||||
|                 tryConnection(client) |  | ||||||
|             } |  | ||||||
|         } |         } | ||||||
|         return latestResult | 
 | ||||||
|  |         return result | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     fun updateLocationWithRedirectPath(oldLocation: String, redirectedLocation: String): String { |     fun updateLocationWithRedirectPath(oldLocation: String, redirectedLocation: String): String { | ||||||
| @ -81,7 +72,7 @@ class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() { | |||||||
|         redirectedUrl: String |         redirectedUrl: String | ||||||
|     ): Boolean { |     ): Boolean { | ||||||
|         return isConnectionSecure || |         return isConnectionSecure || | ||||||
|                 (baseUrl.startsWith(HTTPS_PREFIX) && redirectedUrl.startsWith(HTTP_PREFIX)) |                 (baseUrl.startsWith(HTTPS_SCHEME) && redirectedUrl.startsWith(HTTP_SCHEME)) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     private fun getGetMethod(url: String): GetMethod { |     private fun getGetMethod(url: String): GetMethod { | ||||||
| @ -126,7 +117,10 @@ class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     private fun handleRequestResult(requestResult: RequestResult, baseUrl: String): RemoteOperationResult<OwnCloudVersion> { |     private fun handleRequestResult( | ||||||
|  |         requestResult: RequestResult, | ||||||
|  |         baseUrl: String | ||||||
|  |     ): RemoteOperationResult<OwnCloudVersion> { | ||||||
|         if (!isSuccess(requestResult.status)) |         if (!isSuccess(requestResult.status)) | ||||||
|             return RemoteOperationResult(requestResult.getMethod) |             return RemoteOperationResult(requestResult.getMethod) | ||||||
| 
 | 
 | ||||||
| @ -142,30 +136,23 @@ class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() { | |||||||
|             if (requestResult.redirectedToUnsecureLocation) { |             if (requestResult.redirectedToUnsecureLocation) { | ||||||
|                 RemoteOperationResult<OwnCloudVersion>(ResultCode.OK_REDIRECT_TO_NON_SECURE_CONNECTION) |                 RemoteOperationResult<OwnCloudVersion>(ResultCode.OK_REDIRECT_TO_NON_SECURE_CONNECTION) | ||||||
|             } else { |             } else { | ||||||
|                 if (baseUrl.startsWith(HTTPS_PREFIX)) RemoteOperationResult(ResultCode.OK_SSL) |                 if (baseUrl.startsWith(HTTPS_SCHEME)) RemoteOperationResult(ResultCode.OK_SSL) | ||||||
|                 else RemoteOperationResult(ResultCode.OK_NO_SSL) |                 else RemoteOperationResult(ResultCode.OK_NO_SSL) | ||||||
|             } |             } | ||||||
|         result.data = ocVersion |         result.data = ocVersion | ||||||
|         return result |         return result | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     private fun tryConnection(client: OwnCloudClient): Boolean { |     private fun tryToConnect(client: OwnCloudClient): RemoteOperationResult<OwnCloudVersion> { | ||||||
|         val baseUrl = client.baseUri.toString() |         val baseUrl = client.baseUri.toString() | ||||||
|         try { |         client.setFollowRedirects(false) | ||||||
|             client.setFollowRedirects(false) |         return try { | ||||||
| 
 |  | ||||||
|             val requestResult = requestAndFollowRedirects(baseUrl) |             val requestResult = requestAndFollowRedirects(baseUrl) | ||||||
|             val operationResult = handleRequestResult(requestResult, baseUrl) |             handleRequestResult(requestResult, baseUrl) | ||||||
|             return operationResult.code == ResultCode.OK_SSL || operationResult.code == ResultCode.OK_NO_SSL |  | ||||||
|         } catch (e: JSONException) { |         } catch (e: JSONException) { | ||||||
|             latestResult = RemoteOperationResult(ResultCode.INSTANCE_NOT_CONFIGURED) |             RemoteOperationResult(ResultCode.INSTANCE_NOT_CONFIGURED) | ||||||
|             return false |  | ||||||
|         } catch (e: Exception) { |         } catch (e: Exception) { | ||||||
|             latestResult = RemoteOperationResult(e) |             RemoteOperationResult(e) | ||||||
|             return false |  | ||||||
|         } catch (sslE: SSLException) { |  | ||||||
|             latestResult = RemoteOperationResult(sslE) |  | ||||||
|             return false |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -179,7 +166,7 @@ class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() { | |||||||
|         private const val TRY_CONNECTION_TIMEOUT: Long = 5000 |         private const val TRY_CONNECTION_TIMEOUT: Long = 5000 | ||||||
|         private const val NODE_INSTALLED = "installed" |         private const val NODE_INSTALLED = "installed" | ||||||
|         private const val NODE_VERSION = "version" |         private const val NODE_VERSION = "version" | ||||||
|         private const val HTTPS_PREFIX = "https://" |         private const val HTTPS_SCHEME = "https" | ||||||
|         private const val HTTP_PREFIX = "http://" |         private const val HTTP_SCHEME = "http" | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user