Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/androidTest/java/com/owncloud/android/AbstractIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ public static void beforeAll() throws InterruptedException,

OwnCloudClientManagerFactory.setUserAgent("Mozilla/5.0 (Android) Nextcloud-android/1.0.0");

nextcloudClient = new NextcloudClient(url, context);
nextcloudClient.credentials = Credentials.basic(loginName, password);
nextcloudClient.userId = loginName; // for test same as userId
String userId = loginName; // for test same as userId
String credentials = Credentials.basic(loginName, password);
nextcloudClient = new NextcloudClient(url, userId, credentials, context);

testConnection();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import org.junit.Assert.assertTrue
import org.junit.Test
import java.util.*

class GetActivitiesRemoteOperationTest : AbstractIT() {
class GetActivitiesRemoteOperationIT : AbstractIT() {
@Test
fun getActivities() {
// set-up, create a folder so there is an activity
Expand Down
30 changes: 15 additions & 15 deletions src/main/java/com/nextcloud/common/NextcloudClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ import com.owncloud.android.lib.common.network.RedirectionPath
import com.owncloud.android.lib.common.operations.RemoteOperation
import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.common.utils.Log_OC
import okhttp3.*
import okhttp3.CookieJar
import okhttp3.OkHttpClient
import okhttp3.Request
import org.apache.commons.httpclient.HttpStatus
import java.io.IOException
import java.util.concurrent.TimeUnit
Expand All @@ -48,12 +50,11 @@ import javax.net.ssl.SSLSession
import javax.net.ssl.TrustManager


class NextcloudClient(var baseUri: Uri, val context: Context, client: OkHttpClient) {
lateinit var credentials: String
lateinit var userId: String
lateinit var request: Request
var followRedirects = true;
val client = client
class NextcloudClient(var baseUri: Uri,
var userId: String,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note that by default, Kotlin members are public, contrary to Java, where default scope is package private.

We shall close the access as much as possible. If we need to keep them accessible insidde tests, internal scope can be used to limit access to library only.

val credentials: String,
val client: OkHttpClient) {
var followRedirects = true

companion object {
@JvmStatic
Expand All @@ -74,7 +75,10 @@ class NextcloudClient(var baseUri: Uri, val context: Context, client: OkHttpClie
}
}

constructor(baseUri: Uri, context: Context) : this(baseUri, context, createDefaultClient(context))
constructor(baseUri: Uri,
userId: String,
credentials: String,
context: Context) : this(baseUri, userId, credentials, createDefaultClient(context))

fun execute(remoteOperation: RemoteOperation): RemoteOperationResult {
return try {
Expand All @@ -97,10 +101,6 @@ class NextcloudClient(var baseUri: Uri, val context: Context, client: OkHttpClie
}
}

fun getRequestHeader(name: String): String? {
return request.header(name)
}

@Throws(IOException::class)
fun followRedirection(method: OkHttpMethodBase): RedirectionPath {
var redirectionsCount = 0
Expand All @@ -122,10 +122,10 @@ class NextcloudClient(var baseUri: Uri, val context: Context, client: OkHttpClie
// due to it will be set a different url
method.releaseConnection()
method.uri = location
var destination = getRequestHeader("Destination")
var destination = method.getRequestHeader("Destination")

if (destination == null) {
destination = getRequestHeader("destination")
destination = method.getRequestHeader("destination")
}

if (destination != null) {
Expand All @@ -136,7 +136,7 @@ class NextcloudClient(var baseUri: Uri, val context: Context, client: OkHttpClie
val redirectedDestination = redirectionBase + destinationPath
destination = redirectedDestination

if (getRequestHeader("Destination").isNullOrEmpty()) {
if (method.getRequestHeader("Destination").isNullOrEmpty()) {
method.addRequestHeader("destination", destination)
} else {
method.addRequestHeader("Destination", destination)
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/nextcloud/common/OkHttpMethodBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import okhttp3.HttpUrl
import okhttp3.Request
import okhttp3.Response
import java.io.IOException
import java.lang.Exception

/**
* Common base class for all new OkHttpMethods
Expand All @@ -49,6 +48,7 @@ abstract class OkHttpMethodBase(var uri: String,
private var queryMap: Map<String, String> = HashMap()
private val requestHeaders: MutableMap<String, String> = HashMap()
private val requestBuilder: Request.Builder = Request.Builder()
private var request: Request? = null

fun OkHttpMethodBase() {
requestHeaders.put("http.protocol.single-cookie-header", "true")
Expand Down Expand Up @@ -111,6 +111,10 @@ abstract class OkHttpMethodBase(var uri: String,
return response?.header(name)
}

fun getRequestHeader(name: String): String? {
return request?.header(name) ?: ""
}

/**
* Execute operation using nextcloud client.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,11 @@ public static OwnCloudClient createOwnCloudClient(Uri uri, Context context, bool
* @param context Android context where the OwnCloudClient is being created.
* @return A OwnCloudClient object ready to be used
*/
public static NextcloudClient createNextcloudClient(Uri uri, Context context, boolean followRedirects) {
public static NextcloudClient createNextcloudClient(Uri uri,
String userId,
String credentials,
Context context,
boolean followRedirects) {
try {
NetworkUtils.registerAdvancedSslContext(true, context);
} catch (GeneralSecurityException e) {
Expand All @@ -178,7 +182,7 @@ public static NextcloudClient createNextcloudClient(Uri uri, Context context, bo
" in the system will be used for HTTPS connections", e);
}

NextcloudClient client = new NextcloudClient(uri, context);
NextcloudClient client = new NextcloudClient(uri, userId, credentials, context);
client.setFollowRedirects(followRedirects);

return client;
Expand Down Expand Up @@ -209,18 +213,18 @@ public static NextcloudClient createNextcloudClient(Account account, Context app
// TODO avoid calling to getUserData here
String userId = am.getUserData(account, AccountUtils.Constants.KEY_USER_ID);

NextcloudClient client = createNextcloudClient(baseUri, appContext, true);
client.setUserId(userId);

String username = AccountUtils.getUsernameForAccount(account);
String password = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypePass(account.type), false);

client.setCredentials(Credentials.basic(username, password));

// Restore cookies
// TODO v2 cookie handling
// AccountUtils.restoreCookies(account, client, appContext);

return client;
return createNextcloudClient(baseUri,
userId,
Credentials.basic(username, password),
appContext,
true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,28 +193,36 @@ public NextcloudClient getNextcloudClientFor(OwnCloudAccount account, Context co
}

if (client == null) {
// no client to reuse - create a new one
// TODO remove dependency on OwnCloudClientFactory
client = OwnCloudClientFactory.createNextcloudClient(account.getBaseUri(), context.getApplicationContext(),
true);


// TODO v2
//client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
// enable cookie tracking

//AccountUtils.restoreCookies(accountName, client, context);

account.loadCredentials(context);
client.credentials = account.getCredentials().toOkHttpCredentials();
String credentials = account.getCredentials().toOkHttpCredentials();

AccountManager accountManager = AccountManager.get(context);
Account savedAccount = account.getSavedAccount();

String userId;
if (savedAccount != null) {
String userId = accountManager.getUserData(account.getSavedAccount(),
userId = accountManager.getUserData(account.getSavedAccount(),
AccountUtils.Constants.KEY_USER_ID);
client.setUserId(userId);
} else {
userId = "";
}

// no client to reuse - create a new one
// TODO remove dependency on OwnCloudClientFactory
client = OwnCloudClientFactory.createNextcloudClient(account.getBaseUri(),
userId,
credentials,
context.getApplicationContext(),
true);

if (accountName != null) {
clientsNewWithKnownUsername.put(accountName, client);
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Expand Down
9 changes: 4 additions & 5 deletions src/test/java/com/nextcloud/common/NextcloudClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever
import com.owncloud.android.lib.common.operations.RemoteOperation
import com.owncloud.android.lib.common.operations.RemoteOperationResult
import okhttp3.Call
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import okhttp3.*
import org.junit.Assert.assertNull
import org.junit.Assert.assertSame
import org.junit.Before
Expand All @@ -61,7 +58,9 @@ class NextcloudClientTest {
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
nextcloudClient = NextcloudClient(uri, context, okHttpClient)
val userId = "test"
val credentials = Credentials.basic("login", "test")
nextcloudClient = NextcloudClient(uri, userId, credentials, okHttpClient)
}

@Test
Expand Down
14 changes: 10 additions & 4 deletions src/test/java/com/nextcloud/common/OkHttpMethodBaseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ package com.nextcloud.common

import android.content.Context
import android.net.Uri
import com.nhaarman.mockitokotlin2.*
import okhttp3.*
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever
import okhttp3.Call
import okhttp3.Credentials
import okhttp3.OkHttpClient
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
Expand All @@ -53,8 +58,9 @@ class OkHttpMethodBaseTest {
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
nextcloudClient = NextcloudClient(uri, context, okHttpClient)
nextcloudClient.credentials = Credentials.basic("username", "password")
val userId = "test"
val credentials = Credentials.basic("username", "password")
nextcloudClient = NextcloudClient(uri, userId, credentials, okHttpClient)
}

@Test
Expand Down