@@ -69,87 +69,104 @@ class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() {
69
69
}
70
70
71
71
fun updateLocationWithRedirectPath (oldLocation : String , redirectedLocation : String ): String {
72
- if (! redirectedLocation.startsWith(" /" ))
72
+ if (! redirectedLocation.startsWith(" /" ))
73
73
return redirectedLocation
74
74
val oldLocation = URL (oldLocation)
75
75
return URL (oldLocation.protocol, oldLocation.host, oldLocation.port, redirectedLocation).toString()
76
76
}
77
77
78
- private fun tryConnection (client : OwnCloudClient ): Boolean {
79
- var successfulConnection = false
80
- val baseUrlStr = client.baseUri.toString()
81
- try {
82
- var getMethod = GetMethod (URL (baseUrlStr + OwnCloudClient .STATUS_PATH )).apply {
83
- setReadTimeout(TRY_CONNECTION_TIMEOUT , TimeUnit .SECONDS )
84
- setConnectionTimeout(TRY_CONNECTION_TIMEOUT , TimeUnit .SECONDS )
85
- }
86
- client.setFollowRedirects(false )
87
- var isRedirectToNonSecureConnection = false
88
- var status: Int
89
- try {
90
- status = client.executeHttpMethod(getMethod)
91
- latestResult =
92
- if (isSuccess(status)) RemoteOperationResult (ResultCode .OK )
93
- else RemoteOperationResult (getMethod)
94
-
95
- } catch (sslE: SSLException ) {
96
- latestResult = RemoteOperationResult (sslE)
97
- return successfulConnection
98
- }
78
+ private fun checkIfConnectionIsRedirectedToNoneSecure (
79
+ isConnectionSecure : Boolean ,
80
+ baseUrl : String ,
81
+ redirectedUrl : String
82
+ ): Boolean {
83
+ return isConnectionSecure ||
84
+ (baseUrl.startsWith(HTTPS_PREFIX ) && redirectedUrl.startsWith(HTTP_PREFIX ))
85
+ }
86
+
87
+ private fun getGetMethod (url : String ): GetMethod {
88
+ return GetMethod (URL (url + OwnCloudClient .STATUS_PATH )).apply {
89
+ setReadTimeout(TRY_CONNECTION_TIMEOUT , TimeUnit .SECONDS )
90
+ setConnectionTimeout(TRY_CONNECTION_TIMEOUT , TimeUnit .SECONDS )
91
+ }
92
+ }
93
+
94
+ data class RequestResult (
95
+ val getMethod : GetMethod ,
96
+ val status : Int ,
97
+ val result : RemoteOperationResult <OwnCloudVersion >,
98
+ val redirectedToUnsecureLocation : Boolean
99
+ )
100
+
101
+ fun requestAndFollowRedirects (baseLocation : String ): RequestResult {
102
+ var currentLocation = baseLocation
103
+ var redirectedToUnsecureLocation = false
104
+ var status: Int
99
105
100
- var redirectedLocation = updateLocationWithRedirectPath(baseUrlStr, latestResult.redirectedLocation)
101
- while (! redirectedLocation.isNullOrEmpty() && ! latestResult.isSuccess) {
102
- isRedirectToNonSecureConnection =
103
- isRedirectToNonSecureConnection ||
104
- (baseUrlStr.startsWith(HTTPS_PREFIX ) && redirectedLocation.startsWith(
105
- HTTP_PREFIX
106
- ))
107
-
108
- getMethod = GetMethod (URL (redirectedLocation)).apply {
109
- setReadTimeout(TRY_CONNECTION_TIMEOUT , TimeUnit .SECONDS )
110
- setConnectionTimeout(TRY_CONNECTION_TIMEOUT , TimeUnit .SECONDS )
111
- }
112
-
113
- status = client.executeHttpMethod(getMethod)
114
- latestResult = RemoteOperationResult (getMethod)
115
- redirectedLocation = updateLocationWithRedirectPath(redirectedLocation, latestResult.redirectedLocation)
106
+ while (true ) {
107
+ val getMethod = getGetMethod(currentLocation)
108
+
109
+ status = client.executeHttpMethod(getMethod)
110
+ val result =
111
+ if (isSuccess(status)) RemoteOperationResult <OwnCloudVersion >(ResultCode .OK )
112
+ else RemoteOperationResult (getMethod)
113
+
114
+ if (result.redirectedLocation.isNullOrEmpty() || result.isSuccess) {
115
+ return RequestResult (getMethod, status, result, redirectedToUnsecureLocation)
116
+ } else {
117
+ val nextLocation = updateLocationWithRedirectPath(currentLocation, result.redirectedLocation)
118
+ redirectedToUnsecureLocation =
119
+ checkIfConnectionIsRedirectedToNoneSecure(
120
+ redirectedToUnsecureLocation,
121
+ currentLocation,
122
+ nextLocation
123
+ )
124
+ currentLocation = nextLocation
116
125
}
126
+ }
127
+ }
128
+
129
+ private fun handleRequestResult (requestResult : RequestResult , baseUrl : String ): RemoteOperationResult <OwnCloudVersion > {
130
+ if (! isSuccess(requestResult.status))
131
+ return RemoteOperationResult (requestResult.getMethod)
132
+
133
+ val respJSON = JSONObject (requestResult.getMethod.getResponseBodyAsString())
134
+ if (! respJSON.getBoolean(NODE_INSTALLED ))
135
+ return RemoteOperationResult (ResultCode .INSTANCE_NOT_CONFIGURED )
117
136
118
- if (isSuccess(status)) {
119
- val respJSON = JSONObject (getMethod.getResponseBodyAsString())
120
- if (! respJSON.getBoolean(NODE_INSTALLED )) {
121
- latestResult = RemoteOperationResult (ResultCode .INSTANCE_NOT_CONFIGURED )
122
- } else {
123
- val version = respJSON.getString(NODE_VERSION )
124
- val ocVersion = OwnCloudVersion (version)
125
- // the version object will be returned even if the version is invalid, no error code;
126
- // every app will decide how to act if (ocVersion.isVersionValid() == false)
127
- latestResult = if (isRedirectToNonSecureConnection) {
128
- RemoteOperationResult (ResultCode .OK_REDIRECT_TO_NON_SECURE_CONNECTION )
129
- } else {
130
- if (baseUrlStr.startsWith(HTTPS_PREFIX )) RemoteOperationResult (ResultCode .OK_SSL )
131
- else RemoteOperationResult (ResultCode .OK_NO_SSL )
132
- }
133
- latestResult.data = ocVersion
134
- successfulConnection = true
135
- }
137
+ val version = respJSON.getString(NODE_VERSION )
138
+ val ocVersion = OwnCloudVersion (version)
139
+ // the version object will be returned even if the version is invalid, no error code;
140
+ // every app will decide how to act if (ocVersion.isVersionValid() == false)
141
+ val result =
142
+ if (requestResult.redirectedToUnsecureLocation) {
143
+ RemoteOperationResult <OwnCloudVersion >(ResultCode .OK_REDIRECT_TO_NON_SECURE_CONNECTION )
136
144
} else {
137
- latestResult = RemoteOperationResult (getMethod)
145
+ if (baseUrl.startsWith(HTTPS_PREFIX )) RemoteOperationResult (ResultCode .OK_SSL )
146
+ else RemoteOperationResult (ResultCode .OK_NO_SSL )
138
147
}
148
+ result.data = ocVersion
149
+ return result
150
+ }
151
+
152
+ private fun tryConnection (client : OwnCloudClient ): Boolean {
153
+ val baseUrl = client.baseUri.toString()
154
+ try {
155
+ client.setFollowRedirects(false )
156
+
157
+ val requestResult = requestAndFollowRedirects(baseUrl)
158
+ val operationResult = handleRequestResult(requestResult, baseUrl)
159
+ return operationResult.code == ResultCode .OK_SSL || operationResult.code == ResultCode .OK_NO_SSL
139
160
} catch (e: JSONException ) {
140
161
latestResult = RemoteOperationResult (ResultCode .INSTANCE_NOT_CONFIGURED )
162
+ return false
141
163
} catch (e: Exception ) {
142
164
latestResult = RemoteOperationResult (e)
165
+ return false
166
+ } catch (sslE: SSLException ) {
167
+ latestResult = RemoteOperationResult (sslE)
168
+ return false
143
169
}
144
- when {
145
- latestResult.isSuccess -> Timber .i(" Connection check at $baseUrlStr successful: ${latestResult.logMessage} " )
146
-
147
- latestResult.isException ->
148
- Timber .e(latestResult.exception, " Connection check at $baseUrlStr : ${latestResult.logMessage} " )
149
-
150
- else -> Timber .e(" Connection check at $baseUrlStr failed: ${latestResult.logMessage} " )
151
- }
152
- return successfulConnection
153
170
}
154
171
155
172
private fun isSuccess (status : Int ): Boolean = status == HttpConstants .HTTP_OK
0 commit comments