Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial work on v2 with activity #361

Merged
merged 9 commits into from
Dec 11, 2019
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
2 changes: 2 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ services:
- su www-data -c "php /var/www/html/occ group:add users"
- su www-data -c "php /var/www/html/occ group:adduser users user1"
- su www-data -c "php /var/www/html/occ group:adduser users user2"
- su www-data -c "git clone -b master https://github.com/nextcloud/activity.git /var/www/html/apps/activity/"
- su www-data -c "php /var/www/html/occ app:enable activity"
- /run.sh

trigger:
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# [Nextcloud](https://nextcloud.com) Android Library [![Build Status](https://drone.nextcloud.com/api/badges/nextcloud/android-library/status.svg)](https://drone.nextcloud.com/nextcloud/android-library) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/d9f94f04e0f447a6b21c0ae08f6f7594)](https://www.codacy.com/app/Nextcloud/android-library?utm_source=github.com&utm_medium=referral&utm_content=nextcloud/android-library&utm_campaign=Badge_Grade)
# [Nextcloud](https://nextcloud.com) Android Library v2 [![Build Status](https://drone.nextcloud.com/api/badges/nextcloud/android-library/status.svg)](https://drone.nextcloud.com/nextcloud/android-library) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/d9f94f04e0f447a6b21c0ae08f6f7594)](https://www.codacy.com/app/Nextcloud/android-library?utm_source=github.com&utm_medium=referral&utm_content=nextcloud/android-library&utm_campaign=Badge_Grade)

## Introduction
Using Nextcloud Android library it will be the easiest way to communicate with Nextcloud servers.
Add this library in your project and integrate your application with Nextcloud seamlessly.

## Android Library v2
Starting from 01.10.2019 we will not actively develop our old library (v1), but maintain it until 01.10.2021 with bug fixes.
v2 is using [OkHTTP](https://square.github.io/okhttp) and [DAV4jvm](https://gitlab.com/bitfireAT/dav4jvm) by [BitfireAT](https://www.bitfire.at/).


## Use Library
In the repository it is not only the library project but also the example project "sample_client";
thanks to it you will learn how to use the library.
Expand Down
20 changes: 13 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ buildscript {
}
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
Expand Down Expand Up @@ -44,12 +45,16 @@ repositories {

configurations.all {
exclude group: 'com.google.firebase', module: 'firebase-core'
exclude group: 'org.ogce', module: 'xpp3' // xpp3 is for plain java, Android uses kxml2

// check for updates every build
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}

dependencies {
api 'org.apache.jackrabbit:jackrabbit-webdav:2.12.6'
implementation 'org.apache.jackrabbit:jackrabbit-webdav:2.13.1'
api 'com.squareup.okhttp3:okhttp:3.14.2'
implementation 'com.gitlab.bitfireAT:dav4jvm:1.0' // in transition phase, we use old and new libs
implementation 'org.parceler:parceler-api:1.1.12'
annotationProcessor 'org.parceler:parceler:1.1.12'
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
Expand All @@ -58,28 +63,29 @@ dependencies {

compileOnly 'org.projectlombok:lombok:1.18.10'
annotationProcessor 'org.projectlombok:lombok:1.18.10'


implementation "androidx.core:core-ktx:1.1.0"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

spotbugsPlugins 'com.h3xstream.findsecbugs:findsecbugs-plugin:1.10.1'
spotbugsPlugins 'com.mebigfatguy.fb-contrib:fb-contrib:7.4.7'


// dependencies for tests
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:3.2.0'



// dependencies for instrumented tests
// JUnit4 Rules
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test:rules:1.2.0'

// Android JUnit Runner
androidTestImplementation 'androidx.test:runner:1.2.0'

androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.2.0'
androidTestImplementation 'commons-io:commons-io:2.6'
implementation "androidx.core:core-ktx:1.1.0"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

spotbugs {
Expand Down
2 changes: 1 addition & 1 deletion lint.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<lint>
<issue id="InvalidPackage">
<ignore path="**/jackrabbit-webdav-2.12.6.jar" />
<ignore path="**/jackrabbit-webdav-2.**.jar" />
</issue>
</lint>
2 changes: 1 addition & 1 deletion scripts/analysis/findbugs-results.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
163
160
16 changes: 13 additions & 3 deletions src/androidTest/java/com/owncloud/android/AbstractIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
import android.net.Uri;
import android.os.Bundle;

import com.nextcloud.common.NextcloudClient;
import com.owncloud.android.lib.common.OwnCloudBasicCredentials;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudClientFactory;
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.resources.files.ReadFolderRemoteOperation;
import com.owncloud.android.lib.resources.files.RemoveFileRemoteOperation;
Expand All @@ -48,6 +50,7 @@
import java.io.InputStream;

import androidx.test.platform.app.InstrumentationRegistry;
import okhttp3.Credentials;

import static junit.framework.TestCase.assertTrue;

Expand All @@ -57,8 +60,9 @@

public abstract class AbstractIT {
private static final int BUFFER_SIZE = 1024;

protected static OwnCloudClient client;

public static OwnCloudClient client;
protected static NextcloudClient nextcloudClient;
private static Context context;

protected String baseFolderPath = "/test_for_build/";
Expand All @@ -77,6 +81,12 @@ public static void beforeAll() {
client = OwnCloudClientFactory.createOwnCloudClient(url, context, true);
client.setCredentials(new OwnCloudBasicCredentials(loginName, password));
client.setUserId(loginName); // for test same as userId

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
}

public String createFile(String name) throws IOException {
Expand Down Expand Up @@ -137,7 +147,7 @@ public void after() {

if (!remoteFile.getRemotePath().equals("/")) {
assertTrue(new RemoveFileRemoteOperation(remoteFile.getRemotePath())
.execute(client).isSuccess());
.execute(client).isSuccess());
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/androidTest/java/com/owncloud/android/CopyFileIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation;
import com.owncloud.android.lib.resources.files.RemoveFileRemoteOperation;
import com.owncloud.android.lib.resources.files.UploadFileRemoteOperation;
import com.owncloud.android.lib.resources.status.OwnCloudVersion;

import org.apache.commons.httpclient.HttpStatus;
import org.junit.After;
Expand Down Expand Up @@ -281,7 +280,6 @@ public void testCopyRemoteFileOperation() {
assertEquals("folder to copy into does no exist", result.getHttpCode(), HttpStatus.SC_CONFLICT);

// name collision
client.setOwnCloudVersion(OwnCloudVersion.nextcloud_12);
copyOperation = new CopyFileRemoteOperation(SRC_PATH_TO_FILE_1, TARGET_PATH_TO_ALREADY_EXISTENT_FILE_7, false);
result = copyOperation.execute(client);
assertSame("Name collision", result.getCode(), ResultCode.INVALID_OVERWRITE);
Expand Down
3 changes: 0 additions & 3 deletions src/androidTest/java/com/owncloud/android/CreateFolderIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation;
import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;
import com.owncloud.android.lib.resources.files.RemoveFileRemoteOperation;
import com.owncloud.android.lib.resources.status.OwnCloudVersion;

import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -87,8 +86,6 @@ public void testCreateFolder() {
*/
@Test
public void testCreateFolderSpecialCharactersOnNewVersion() {
client.setOwnCloudVersion(OwnCloudVersion.nextcloud_12);

String remotePath = mFullPath2FolderBase + "_<";
RemoteOperationResult result = new CreateFolderRemoteOperation(remotePath, true).execute(client);
assertTrue("Remote path: " + remotePath, result.isSuccess());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class SetUserInfoRemoteOperationTest extends AbstractIT {
public void testSetEmail() {
RemoteOperationResult userInfo = new GetUserInfoRemoteOperation().execute(client);
assertTrue(userInfo.isSuccess());
String oldValue = ((UserInfo) userInfo.getData().get(0)).email;
String oldValue = ((UserInfo) userInfo.getData().get(0)).getEmail();

// set
assertTrue(new SetUserInfoRemoteOperation(SetUserInfoRemoteOperation.Field.EMAIL, "new@mail.com")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Nextcloud Android client application
*
* @author Tobias Kaminsky
* Copyright (C) 2019 Tobias Kaminsky
* Copyright (C) 2019 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/

package com.nextcloud.lib.resources.users

import com.owncloud.android.AbstractIT
import com.owncloud.android.lib.resources.activities.GetActivitiesRemoteOperation
import com.owncloud.android.lib.resources.activities.model.Activity
import org.junit.Assert.assertTrue
import org.junit.Test
import java.util.*

class GetActivitiesRemoteOperationTest : AbstractIT() {
@Test
fun getActivities() {
val result = nextcloudClient.execute(GetActivitiesRemoteOperation())
assertTrue(result.isSuccess)

val activities = result.data[0] as ArrayList<Activity>
val lastGiven = result.data[1] as Integer;

assertTrue(activities.isNotEmpty())
assertTrue(lastGiven > 0)
}
}
7 changes: 6 additions & 1 deletion src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.owncloud.android.lib"
android:versionCode="1"
android:versionName="1.0.13" >
android:versionName="1.0.13">


<uses-permission
Expand All @@ -40,5 +41,9 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

<application
android:usesCleartextTraffic="true"
tools:targetApi="m" />

</manifest>

125 changes: 125 additions & 0 deletions src/main/java/com/nextcloud/common/NextcloudClient.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/* Nextcloud Android Library is available under MIT license
*
* @author Tobias Kaminsky
* Copyright (C) 2019 Tobias Kaminsky
* Copyright (C) 2019 Nextcloud GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

package com.nextcloud.common

import android.content.Context
import android.net.Uri
import com.owncloud.android.lib.common.OwnCloudClient
import com.owncloud.android.lib.common.OwnCloudClientFactory
import com.owncloud.android.lib.common.accounts.AccountUtils
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.CookieJar
import okhttp3.OkHttpClient
import okhttp3.Request
import org.apache.commons.httpclient.HttpStatus
import java.io.IOException
import java.util.concurrent.TimeUnit

class NextcloudClient(var baseUri: Uri, val context: Context) : OkHttpClient() {
companion object {
@JvmStatic
val TAG = NextcloudClient::class.java.simpleName
}

var client: OkHttpClient = Builder()
.cookieJar(CookieJar.NO_COOKIES)
.callTimeout(OwnCloudClientFactory.DEFAULT_DATA_TIMEOUT_LONG, TimeUnit.MILLISECONDS)
.build()

lateinit var credentials: String
lateinit var userId: String
lateinit var request: Request
var followRedirects = true;

fun execute(remoteOperation: RemoteOperation): RemoteOperationResult {
return remoteOperation.run(this)
}

fun execute(method: OkHttpMethodBase): Int {
return method.execute(this)
}

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

@Throws(IOException::class)
fun followRedirection(method: OkHttpMethodBase): RedirectionPath {
var redirectionsCount = 0
var status = method.getStatusCode()
val result = RedirectionPath(status, OwnCloudClient.MAX_REDIRECTIONS_COUNT)

while (redirectionsCount < OwnCloudClient.MAX_REDIRECTIONS_COUNT &&
(status == HttpStatus.SC_MOVED_PERMANENTLY ||
status == HttpStatus.SC_MOVED_TEMPORARILY ||
status == HttpStatus.SC_TEMPORARY_REDIRECT)) {
var location = method.getResponseHeader("Location")
if (location == null) {
location = method.getResponseHeader("location")
}
if (location != null) {
Log_OC.d(TAG, "Location to redirect: " + location)
result.addLocation(location)
// Release the connection to avoid reach the max number of connections per host
// due to it will be set a different url
method.releaseConnection()
method.uri = location
var destination = getRequestHeader("Destination")

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

if (destination != null) {
val suffixIndex = location.lastIndexOf(AccountUtils.WEBDAV_PATH_4_0)
val redirectionBase = location.substring(0, suffixIndex)
val destinationStr = destination
val destinationPath = destinationStr.substring(baseUri.toString().length)
val redirectedDestination = redirectionBase + destinationPath
destination = redirectedDestination

if (getRequestHeader("Destination").isNullOrEmpty()) {
method.requestHeaders.put("destination", destination)
} else {
method.requestHeaders.put("Destination", destination)
}
}
status = method.execute(this)
result.addStatus(status)
redirectionsCount++
} else {
Log_OC.d(TAG, "No location to redirect!")
status = HttpStatus.SC_NOT_FOUND
}
}
return result
}
}
Loading