Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit 5ecd3c9

Browse files
authored
Merge pull request #307 from owncloud/new_arch/login
[New arch] Login
2 parents c5e64bd + 9e46b83 commit 5ecd3c9

28 files changed

+893
-493
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
buildscript {
22
ext {
33
kotlinVersion = '1.3.71'
4+
moshiVersion = "1.9.2"
45
}
56

67
repositories {

owncloudComLibrary/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ dependencies {
99
api 'com.gitlab.ownclouders:dav4android:oc_support_1.0.1'
1010
api 'com.github.hannesa2:Logcat:1.6.0'
1111
api 'net.openid:appauth:0.7.1'
12+
13+
// Moshi
14+
implementation ("com.squareup.moshi:moshi-kotlin:$moshiVersion") {
15+
exclude module: "kotlin-reflect"
16+
}
17+
kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshiVersion"
1218
}
1319

1420
allOpen {

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
public class OwnCloudClient extends HttpClient {
5454

5555
public static final String WEBDAV_FILES_PATH_4_0 = "/remote.php/dav/files/";
56+
public static final String WEBDAV_PATH_4_0_AND_LATER = "/remote.php/dav";
5657
private static final String WEBDAV_UPLOADS_PATH_4_0 = "/remote.php/dav/uploads/";
5758
public static final String STATUS_PATH = "/status.php";
5859

@@ -230,7 +231,7 @@ public Uri getBaseFilesWebDavUri() {
230231
}
231232

232233
public Uri getUserFilesWebDavUri() {
233-
return mCredentials instanceof OwnCloudAnonymousCredentials
234+
return (mCredentials instanceof OwnCloudAnonymousCredentials || mAccount == null)
234235
? Uri.parse(mBaseUri + WEBDAV_FILES_PATH_4_0)
235236
: Uri.parse(mBaseUri + WEBDAV_FILES_PATH_4_0 + AccountUtils.getUserId(
236237
mAccount.getSavedAccount(), getContext()

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/accounts/AccountUtils.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import java.util.List;
4747

4848
public class AccountUtils {
49-
5049
/**
5150
* Constructs full url to host and webdav resource basing on host version
5251
*
@@ -140,7 +139,7 @@ public static OwnCloudCredentials getCredentialsForAccount(Context context, Acco
140139
AccountManager am = AccountManager.get(context);
141140

142141
String supportsOAuth2 = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_OAUTH2);
143-
boolean isOauth2 = supportsOAuth2 != null && supportsOAuth2.equals("TRUE");
142+
boolean isOauth2 = supportsOAuth2 != null && supportsOAuth2.equals(Constants.OAUTH_SUPPORTED_TRUE);
144143

145144
String username = AccountUtils.getUsernameForAccount(account);
146145

@@ -295,6 +294,8 @@ public static class Constants {
295294
*/
296295
public static final String KEY_SUPPORTS_OAUTH2 = "oc_supports_oauth2";
297296

297+
public static final String OAUTH_SUPPORTED_TRUE = "TRUE";
298+
298299
/**
299300
* OC account cookies
300301
*/
@@ -329,5 +330,7 @@ public static class Constants {
329330
* OAuth2 scope
330331
*/
331332
public static final String KEY_OAUTH2_SCOPE = "oc_oauth2_scope";
333+
334+
public static final int ACCOUNT_VERSION = 1;
332335
}
333336
}

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/network/WebdavUtils.java

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@
3434
import java.util.Date;
3535
import java.util.Locale;
3636

37+
import static com.owncloud.android.lib.common.OwnCloudClient.WEBDAV_FILES_PATH_4_0;
38+
import static com.owncloud.android.lib.common.OwnCloudClient.WEBDAV_PATH_4_0_AND_LATER;
39+
3740
public class WebdavUtils {
38-
public static final SimpleDateFormat DISPLAY_DATE_FORMAT = new SimpleDateFormat(
39-
"dd.MM.yyyy hh:mm");
4041

41-
private static final SimpleDateFormat DATETIME_FORMATS[] = {
42+
private static final SimpleDateFormat[] DATETIME_FORMATS = {
4243
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US),
4344
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US),
4445
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sss'Z'", Locale.US),
@@ -50,11 +51,11 @@ public class WebdavUtils {
5051
};
5152

5253
public static Date parseResponseDate(String date) {
53-
Date returnDate = null;
54-
SimpleDateFormat format = null;
55-
for (int i = 0; i < DATETIME_FORMATS.length; ++i) {
54+
Date returnDate;
55+
SimpleDateFormat format;
56+
for (SimpleDateFormat datetimeFormat : DATETIME_FORMATS) {
5657
try {
57-
format = DATETIME_FORMATS[i];
58+
format = datetimeFormat;
5859
synchronized (format) {
5960
returnDate = format.parse(date);
6061
}
@@ -82,23 +83,6 @@ public static String encodePath(String remoteFilePath) {
8283
return encodedPath;
8384
}
8485

85-
/**
86-
* @param rawEtag
87-
* @return
88-
*/
89-
public static String parseEtag(String rawEtag) {
90-
if (rawEtag == null || rawEtag.length() == 0) {
91-
return "";
92-
}
93-
if (rawEtag.endsWith("-gzip")) {
94-
rawEtag = rawEtag.substring(0, rawEtag.length() - 5);
95-
}
96-
if (rawEtag.length() >= 2 && rawEtag.startsWith("\"") && rawEtag.endsWith("\"")) {
97-
rawEtag = rawEtag.substring(1, rawEtag.length() - 1);
98-
}
99-
return rawEtag;
100-
}
101-
10286
/**
10387
* @param httpBaseMethod from which to get the etag
10488
* @return etag from response
@@ -120,4 +104,17 @@ public static String getEtagFromResponse(HttpBaseMethod httpBaseMethod) {
120104
}
121105
return result;
122106
}
107+
108+
public static String normalizeProtocolPrefix(String url, boolean isSslConn) {
109+
if (!url.toLowerCase().startsWith("http://") &&
110+
!url.toLowerCase().startsWith("https://")) {
111+
if (isSslConn) {
112+
return "https://" + url;
113+
} else {
114+
return "http://" + url;
115+
}
116+
}
117+
return url;
118+
}
119+
123120
}

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/operations/RemoteOperationResult.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public class RemoteOperationResult<T>
6666
private Exception mException = null;
6767
private ResultCode mCode = ResultCode.UNKNOWN_ERROR;
6868
private String mRedirectedLocation;
69-
private ArrayList<String> mAuthenticate = new ArrayList<>();
69+
private String mAuthenticate;
7070
private String mLastPermanentLocation = null;
7171
private T mData = null;
7272

@@ -253,7 +253,7 @@ public RemoteOperationResult(int httpCode, String httpPhrase, Headers headers) {
253253
continue;
254254
}
255255
if ("www-authenticate".equals(header.getKey().toLowerCase())) {
256-
mAuthenticate.add(header.getValue().get(0).toLowerCase());
256+
mAuthenticate = header.getValue().get(0).toLowerCase();
257257
}
258258
}
259259
}
@@ -494,7 +494,7 @@ public boolean isNonSecureRedirection() {
494494
return (mRedirectedLocation != null && !(mRedirectedLocation.toLowerCase().startsWith("https://")));
495495
}
496496

497-
public ArrayList<String> getAuthenticateHeaders() {
497+
public String getAuthenticateHeaders() {
498498
return mAuthenticate;
499499
}
500500

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* ownCloud Android Library is available under MIT license
2+
*
3+
* Copyright (C) 2020 ownCloud GmbH.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package com.owncloud.android.lib.resources
25+
26+
import com.squareup.moshi.JsonClass
27+
28+
// Response retrieved by OCS Rest API, used to obtain capabilities, shares and user info among others.
29+
// More info: https://doc.owncloud.com/server/developer_manual/core/apis/ocs-capabilities.html
30+
@JsonClass(generateAdapter = true)
31+
data class CommonOcsResponse<T>(
32+
val ocs: OCSResponse<T>
33+
)
34+
35+
@JsonClass(generateAdapter = true)
36+
data class OCSResponse<T>(
37+
val meta: MetaData,
38+
val data: T
39+
)
40+
41+
@JsonClass(generateAdapter = true)
42+
data class MetaData(
43+
val status: String,
44+
val statuscode: Int,
45+
val message: String?
46+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/* ownCloud Android Library is available under MIT license
2+
* Copyright (C) 2020 ownCloud GmbH.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*
23+
*/
24+
package com.owncloud.android.lib.resources.files
25+
26+
import com.owncloud.android.lib.common.OwnCloudClient
27+
import com.owncloud.android.lib.common.http.HttpConstants
28+
import com.owncloud.android.lib.common.http.methods.webdav.DavUtils
29+
import com.owncloud.android.lib.common.http.methods.webdav.PropfindMethod
30+
import com.owncloud.android.lib.common.network.RedirectionPath
31+
import com.owncloud.android.lib.common.network.WebdavUtils
32+
import com.owncloud.android.lib.common.operations.RemoteOperation
33+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
34+
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode
35+
import timber.log.Timber
36+
import java.net.URL
37+
import java.util.concurrent.TimeUnit
38+
39+
/**
40+
* Operation to check the existence of a path in a remote server.
41+
*
42+
* @author David A. Velasco
43+
* @author David González Verdugo
44+
* @author Abel García de Prada
45+
*
46+
* @param remotePath Path to append to the URL owned by the client instance.
47+
* @param isUserLogged When `true`, the username won't be added at the end of the PROPFIND url since is not
48+
* needed to check user credentials
49+
*/
50+
class CheckPathExistenceRemoteOperation(
51+
val remotePath: String? = "",
52+
val isUserLogged: Boolean
53+
) : RemoteOperation<Boolean>() {
54+
/**
55+
* Gets the sequence of redirections followed during the execution of the operation.
56+
*
57+
* @return Sequence of redirections followed, if any, or NULL if the operation was not executed.
58+
*/
59+
var redirectionPath: RedirectionPath? = null
60+
private set
61+
62+
override fun run(client: OwnCloudClient): RemoteOperationResult<Boolean> {
63+
val previousFollowRedirects = client.followRedirects()
64+
return try {
65+
val stringUrl =
66+
if (isUserLogged) client.baseFilesWebDavUri.toString()
67+
else client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(remotePath)
68+
69+
val propFindMethod = PropfindMethod(URL(stringUrl), 0, DavUtils.getAllPropset()).apply {
70+
setReadTimeout(TIMEOUT.toLong(), TimeUnit.SECONDS)
71+
setConnectionTimeout(TIMEOUT.toLong(), TimeUnit.SECONDS)
72+
}
73+
74+
client.setFollowRedirects(false)
75+
var status = client.executeHttpMethod(propFindMethod)
76+
if (previousFollowRedirects) {
77+
redirectionPath = client.followRedirection(propFindMethod)
78+
status = redirectionPath?.lastStatus!!
79+
}
80+
/* PROPFIND method
81+
* 404 NOT FOUND: path doesn't exist,
82+
* 207 MULTI_STATUS: path exists.
83+
*/
84+
Timber.d(
85+
"Existence check for $stringUrl finished with HTTP status $status${if (!isSuccess(status)) "(FAIL)" else ""}"
86+
)
87+
if (isSuccess(status)) RemoteOperationResult<Boolean>(ResultCode.OK).apply { data = true }
88+
else RemoteOperationResult<Boolean>(propFindMethod).apply { data = false }
89+
90+
} catch (e: Exception) {
91+
val result = RemoteOperationResult<Boolean>(e)
92+
Timber.e(
93+
e,
94+
"Existence check for ${client.userFilesWebDavUri}${WebdavUtils.encodePath(remotePath)} : ${result.logMessage}"
95+
)
96+
result
97+
} finally {
98+
client.setFollowRedirects(previousFollowRedirects)
99+
}
100+
}
101+
102+
/**
103+
* @return 'True' if the operation was executed and at least one redirection was followed.
104+
*/
105+
fun wasRedirected() = redirectionPath?.redirectionsCount ?: 0 > 0
106+
107+
private fun isSuccess(status: Int) = status == HttpConstants.HTTP_OK || status == HttpConstants.HTTP_MULTI_STATUS
108+
109+
companion object {
110+
/**
111+
* Maximum time to wait for a response from the server in milliseconds.
112+
*/
113+
private const val TIMEOUT = 10000
114+
}
115+
}

0 commit comments

Comments
 (0)