From 1d80067f858cbdf3838c1bb3f3b55cdd060cc906 Mon Sep 17 00:00:00 2001 From: davigonz Date: Mon, 2 Jul 2018 15:39:15 +0200 Subject: [PATCH] Refactor interceptors and include move headers --- .../OwnCloudBasicCredentials.java | 36 ++++------------ .../OwnCloudBearerCredentials.java | 32 ++++---------- .../OwnCloudCredentialsFactory.java | 24 +++-------- .../OwnCloudSamlSsoCredentials.java | 31 ++++--------- .../android/lib/common/http/HttpClient.java | 33 +++++++++----- .../lib/common/http/HttpConstants.java | 8 ++-- .../interceptors/BarearAuthInterceptor.java | 21 --------- .../interceptors/BasicAuthInterceptor.java | 43 ------------------- .../http/interceptors/HttpInterceptor.java | 25 ++++++++++- .../RequestHeaderInterceptor.java | 27 ++++++++++++ .../interceptors/SamlAuthInterceptor.java | 21 --------- .../interceptors/UserAgentInterceptor.java | 42 ------------------ .../common/http/methods/HttpBaseMethod.java | 2 - .../common/http/methods/webdav/DavMethod.java | 1 + .../ChunkedUploadRemoteFileOperation.java | 16 +------ .../lib/resources/files/FileUtils.java | 2 +- .../files/MoveRemoteChunksFileOperation.java | 5 ++- .../files/MoveRemoteFileOperation.java | 22 ++++++---- 18 files changed, 127 insertions(+), 264 deletions(-) delete mode 100644 src/com/owncloud/android/lib/common/http/interceptors/BarearAuthInterceptor.java delete mode 100644 src/com/owncloud/android/lib/common/http/interceptors/BasicAuthInterceptor.java create mode 100644 src/com/owncloud/android/lib/common/http/interceptors/RequestHeaderInterceptor.java delete mode 100644 src/com/owncloud/android/lib/common/http/interceptors/SamlAuthInterceptor.java delete mode 100644 src/com/owncloud/android/lib/common/http/interceptors/UserAgentInterceptor.java diff --git a/src/com/owncloud/android/lib/common/authentication/OwnCloudBasicCredentials.java b/src/com/owncloud/android/lib/common/authentication/OwnCloudBasicCredentials.java index 8fce943d..19ad1a59 100644 --- a/src/com/owncloud/android/lib/common/authentication/OwnCloudBasicCredentials.java +++ b/src/com/owncloud/android/lib/common/authentication/OwnCloudBasicCredentials.java @@ -25,20 +25,9 @@ package com.owncloud.android.lib.common.authentication; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.http.HttpClient; -import com.owncloud.android.lib.common.http.interceptors.BarearAuthInterceptor; -import com.owncloud.android.lib.common.http.interceptors.BasicAuthInterceptor; -import com.owncloud.android.lib.common.http.interceptors.HttpInterceptor; -import com.owncloud.android.lib.common.http.interceptors.HttpInterceptor.RequestInterceptor; -import com.owncloud.android.lib.common.http.interceptors.SamlAuthInterceptor; +import com.owncloud.android.lib.common.http.HttpConstants; - - - - - - -import java.util.ArrayList; -import java.util.List; +import okhttp3.Credentials; public class OwnCloudBasicCredentials implements OwnCloudCredentials { @@ -62,21 +51,13 @@ public class OwnCloudBasicCredentials implements OwnCloudCredentials { @Override public void applyTo(OwnCloudClient client) { - ArrayList requestInterceptors = HttpClient.getOkHttpInterceptor().getRequestInterceptors(); - // Clear previous basic credentials - for (HttpInterceptor.RequestInterceptor requestInterceptor : requestInterceptors) { - if (requestInterceptor instanceof BasicAuthInterceptor - || requestInterceptor instanceof BarearAuthInterceptor - || requestInterceptor instanceof SamlAuthInterceptor) { - requestInterceptors.remove(requestInterceptor); - } - } + HttpClient.deleteHeaderForAllRequests(HttpConstants.BASIC_AUTHORIZATION_HEADER); + HttpClient.deleteHeaderForAllRequests(HttpConstants.BEARER_AUTHORIZATION_HEADER); + HttpClient.deleteHeaderForAllRequests(HttpConstants.COOKIE_HEADER); - HttpClient.getOkHttpInterceptor() - .addRequestInterceptor( - new BasicAuthInterceptor(mUsername, mPassword) - ); + HttpClient.addHeaderForAllRequests(HttpConstants.BASIC_AUTHORIZATION_HEADER, + Credentials.basic(mUsername, mPassword)); } @Override @@ -98,5 +79,4 @@ public class OwnCloudBasicCredentials implements OwnCloudCredentials { public boolean authTokenCanBeRefreshed() { return false; } - -} +} \ No newline at end of file diff --git a/src/com/owncloud/android/lib/common/authentication/OwnCloudBearerCredentials.java b/src/com/owncloud/android/lib/common/authentication/OwnCloudBearerCredentials.java index 2f44f12e..f3d1ec08 100644 --- a/src/com/owncloud/android/lib/common/authentication/OwnCloudBearerCredentials.java +++ b/src/com/owncloud/android/lib/common/authentication/OwnCloudBearerCredentials.java @@ -1,5 +1,5 @@ /* ownCloud Android Library is available under MIT license - * Copyright (C) 2016 ownCloud GmbH. + * Copyright (C) 2018 ownCloud 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 @@ -23,15 +23,9 @@ */ package com.owncloud.android.lib.common.authentication; -import java.util.ArrayList; - - import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.http.HttpClient; -import com.owncloud.android.lib.common.http.interceptors.BarearAuthInterceptor; -import com.owncloud.android.lib.common.http.interceptors.BasicAuthInterceptor; -import com.owncloud.android.lib.common.http.interceptors.HttpInterceptor; -import com.owncloud.android.lib.common.http.interceptors.SamlAuthInterceptor; +import com.owncloud.android.lib.common.http.HttpConstants; public class OwnCloudBearerCredentials implements OwnCloudCredentials { @@ -45,21 +39,12 @@ public class OwnCloudBearerCredentials implements OwnCloudCredentials { @Override public void applyTo(OwnCloudClient client) { + // Clear previous credentials + HttpClient.deleteHeaderForAllRequests(HttpConstants.BASIC_AUTHORIZATION_HEADER); + HttpClient.deleteHeaderForAllRequests(HttpConstants.BEARER_AUTHORIZATION_HEADER); + HttpClient.deleteHeaderForAllRequests(HttpConstants.COOKIE_HEADER); - final ArrayList requestInterceptors = - HttpClient.getOkHttpInterceptor().getRequestInterceptors(); - - // Clear previous basic credentials - for (HttpInterceptor.RequestInterceptor requestInterceptor : requestInterceptors) { - if (requestInterceptor instanceof BasicAuthInterceptor - || requestInterceptor instanceof BarearAuthInterceptor - || requestInterceptor instanceof SamlAuthInterceptor) { - requestInterceptors.remove(requestInterceptor); - } - } - - HttpClient.getOkHttpInterceptor() - .addRequestInterceptor(new BarearAuthInterceptor(mAccessToken)); + HttpClient.addHeaderForAllRequests(HttpConstants.BEARER_AUTHORIZATION_HEADER, mAccessToken); } @Override @@ -82,5 +67,4 @@ public class OwnCloudBearerCredentials implements OwnCloudCredentials { public boolean authTokenCanBeRefreshed() { return true; } - -} +} \ No newline at end of file diff --git a/src/com/owncloud/android/lib/common/authentication/OwnCloudCredentialsFactory.java b/src/com/owncloud/android/lib/common/authentication/OwnCloudCredentialsFactory.java index f22e06d8..1049d1e5 100644 --- a/src/com/owncloud/android/lib/common/authentication/OwnCloudCredentialsFactory.java +++ b/src/com/owncloud/android/lib/common/authentication/OwnCloudCredentialsFactory.java @@ -1,5 +1,5 @@ /* ownCloud Android Library is available under MIT license - * Copyright (C) 2016 ownCloud GmbH. + * Copyright (C) 2018 ownCloud 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 @@ -26,12 +26,7 @@ package com.owncloud.android.lib.common.authentication; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.http.HttpClient; -import com.owncloud.android.lib.common.http.interceptors.BarearAuthInterceptor; -import com.owncloud.android.lib.common.http.interceptors.BasicAuthInterceptor; -import com.owncloud.android.lib.common.http.interceptors.HttpInterceptor; -import com.owncloud.android.lib.common.http.interceptors.SamlAuthInterceptor; - -import java.util.ArrayList; +import com.owncloud.android.lib.common.http.HttpConstants; public class OwnCloudCredentialsFactory { @@ -71,16 +66,10 @@ public class OwnCloudCredentialsFactory { @Override public void applyTo(OwnCloudClient client) { - ArrayList requestInterceptors = HttpClient.getOkHttpInterceptor().getRequestInterceptors(); - // Clear previous basic credentials - for (HttpInterceptor.RequestInterceptor requestInterceptor : requestInterceptors) { - if (requestInterceptor instanceof BasicAuthInterceptor - || requestInterceptor instanceof BarearAuthInterceptor - || requestInterceptor instanceof SamlAuthInterceptor) { - requestInterceptors.remove(requestInterceptor); - } - } + HttpClient.deleteHeaderForAllRequests(HttpConstants.BASIC_AUTHORIZATION_HEADER); + HttpClient.deleteHeaderForAllRequests(HttpConstants.BEARER_AUTHORIZATION_HEADER); + HttpClient.deleteHeaderForAllRequests(HttpConstants.COOKIE_HEADER); } @Override @@ -104,5 +93,4 @@ public class OwnCloudCredentialsFactory { return null; } } - -} +} \ No newline at end of file diff --git a/src/com/owncloud/android/lib/common/authentication/OwnCloudSamlSsoCredentials.java b/src/com/owncloud/android/lib/common/authentication/OwnCloudSamlSsoCredentials.java index 245bfa1c..30d5d504 100644 --- a/src/com/owncloud/android/lib/common/authentication/OwnCloudSamlSsoCredentials.java +++ b/src/com/owncloud/android/lib/common/authentication/OwnCloudSamlSsoCredentials.java @@ -1,5 +1,5 @@ /* ownCloud Android Library is available under MIT license - * Copyright (C) 2016 ownCloud GmbH. + * Copyright (C) 2018 ownCloud 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 @@ -25,13 +25,7 @@ package com.owncloud.android.lib.common.authentication; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.http.HttpClient; -import com.owncloud.android.lib.common.http.interceptors.BarearAuthInterceptor; -import com.owncloud.android.lib.common.http.interceptors.BasicAuthInterceptor; -import com.owncloud.android.lib.common.http.interceptors.HttpInterceptor; -import com.owncloud.android.lib.common.http.interceptors.SamlAuthInterceptor; - -import java.util.ArrayList; - +import com.owncloud.android.lib.common.http.HttpConstants; public class OwnCloudSamlSsoCredentials implements OwnCloudCredentials { @@ -45,20 +39,12 @@ public class OwnCloudSamlSsoCredentials implements OwnCloudCredentials { @Override public void applyTo(OwnCloudClient client) { - final ArrayList requestInterceptors = - HttpClient.getOkHttpInterceptor().getRequestInterceptors(); + // Clear previous credentials + HttpClient.deleteHeaderForAllRequests(HttpConstants.BASIC_AUTHORIZATION_HEADER); + HttpClient.deleteHeaderForAllRequests(HttpConstants.BEARER_AUTHORIZATION_HEADER); + HttpClient.deleteHeaderForAllRequests(HttpConstants.COOKIE_HEADER); - // Clear previous basic credentials - for (HttpInterceptor.RequestInterceptor requestInterceptor : requestInterceptors) { - if (requestInterceptor instanceof BasicAuthInterceptor - || requestInterceptor instanceof BarearAuthInterceptor - || requestInterceptor instanceof SamlAuthInterceptor) { - requestInterceptors.remove(requestInterceptor); - } - } - - HttpClient.getOkHttpInterceptor() - .addRequestInterceptor(new SamlAuthInterceptor(mSessionCookie)); + HttpClient.addHeaderForAllRequests(HttpConstants.COOKIE_HEADER, mSessionCookie); } @Override @@ -81,5 +67,4 @@ public class OwnCloudSamlSsoCredentials implements OwnCloudCredentials { public boolean authTokenCanBeRefreshed() { return false; } - -} +} \ No newline at end of file diff --git a/src/com/owncloud/android/lib/common/http/HttpClient.java b/src/com/owncloud/android/lib/common/http/HttpClient.java index 2ff2a100..4aa51922 100644 --- a/src/com/owncloud/android/lib/common/http/HttpClient.java +++ b/src/com/owncloud/android/lib/common/http/HttpClient.java @@ -25,19 +25,19 @@ package com.owncloud.android.lib.common.http; import android.content.Context; + +import java.util.ArrayList; import java.util.concurrent.TimeUnit; import com.facebook.stetho.okhttp3.StethoInterceptor; import com.owncloud.android.lib.BuildConfig; import com.owncloud.android.lib.common.OwnCloudClientManagerFactory; import com.owncloud.android.lib.common.http.interceptors.HttpInterceptor; -import com.owncloud.android.lib.common.http.interceptors.UserAgentInterceptor; +import com.owncloud.android.lib.common.http.interceptors.RequestHeaderInterceptor; import com.owncloud.android.lib.common.network.AdvancedX509TrustManager; import com.owncloud.android.lib.common.network.NetworkUtils; import com.owncloud.android.lib.common.utils.Log_OC; -import org.apache.http.conn.ssl.BrowserCompatHostnameVerifier; - import java.util.Arrays; import javax.net.ssl.SSLContext; @@ -58,7 +58,6 @@ public class HttpClient { private static HttpInterceptor sOkHttpInterceptor; private static Context sContext; - public static void setContext(Context context) { sContext = context; } @@ -90,14 +89,10 @@ public class HttpClient { return sOkHttpClient; } - public static HttpInterceptor getOkHttpInterceptor() { + private static HttpInterceptor getOkHttpInterceptor() { if (sOkHttpInterceptor == null) { - sOkHttpInterceptor = new HttpInterceptor() - .addRequestInterceptor(new UserAgentInterceptor( - // TODO Try to get rid of this dependency - OwnCloudClientManagerFactory.getUserAgent() - ) - ); + sOkHttpInterceptor = new HttpInterceptor(); + addHeaderForAllRequests(HttpConstants.USER_AGENT_HEADER, OwnCloudClientManagerFactory.getUserAgent()); } return sOkHttpInterceptor; } @@ -118,4 +113,20 @@ public class HttpClient { } sOkHttpClient = clientBuilder.build(); } + + /** + * Add header that will be included for all the requests from now on + * @param headerName + * @param headerValue + */ + public static void addHeaderForAllRequests(String headerName, String headerValue) { + getOkHttpInterceptor() + .addRequestInterceptor( + new RequestHeaderInterceptor(headerName, headerValue) + ); + } + + public static void deleteHeaderForAllRequests(String headerName) { + getOkHttpInterceptor().deleteRequestHeaderInterceptor(headerName); + } } \ No newline at end of file diff --git a/src/com/owncloud/android/lib/common/http/HttpConstants.java b/src/com/owncloud/android/lib/common/http/HttpConstants.java index 76321402..86e39df7 100644 --- a/src/com/owncloud/android/lib/common/http/HttpConstants.java +++ b/src/com/owncloud/android/lib/common/http/HttpConstants.java @@ -33,10 +33,10 @@ public class HttpConstants { *************************************************** HEADERS *********************************************** ***********************************************************************************************************/ - /*********************************************************************************************************** - *************************************************** HEADERS *********************************************** - ***********************************************************************************************************/ - + public static final String BASIC_AUTHORIZATION_HEADER = "Authorization"; + public static final String COOKIE_HEADER = "Cookie"; + public static final String BEARER_AUTHORIZATION_HEADER = "Bearer "; + public static final String USER_AGENT_HEADER = "User-Agent"; public static final String IF_MATCH_HEADER = "If-Match"; public static final String IF_NONE_MATCH_HEADER = "If-None-Match"; public static final String CONTENT_TYPE_HEADER = "Content-Type"; diff --git a/src/com/owncloud/android/lib/common/http/interceptors/BarearAuthInterceptor.java b/src/com/owncloud/android/lib/common/http/interceptors/BarearAuthInterceptor.java deleted file mode 100644 index 4cf1f23d..00000000 --- a/src/com/owncloud/android/lib/common/http/interceptors/BarearAuthInterceptor.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.owncloud.android.lib.common.http.interceptors; - -import okhttp3.Request; - -public class BarearAuthInterceptor implements HttpInterceptor.RequestInterceptor { - - private static final String AUTHORIZATION_HEADER = "Authorization"; - private final String mBarearToken; - - public BarearAuthInterceptor(String barearToken) { - this.mBarearToken = barearToken; - } - - @Override - public Request intercept(Request request) { - return request - .newBuilder() - .addHeader(AUTHORIZATION_HEADER, "Bearer " + mBarearToken) - .build(); - } -} diff --git a/src/com/owncloud/android/lib/common/http/interceptors/BasicAuthInterceptor.java b/src/com/owncloud/android/lib/common/http/interceptors/BasicAuthInterceptor.java deleted file mode 100644 index a61a33fd..00000000 --- a/src/com/owncloud/android/lib/common/http/interceptors/BasicAuthInterceptor.java +++ /dev/null @@ -1,43 +0,0 @@ -/* ownCloud Android Library is available under MIT license - * Copyright (C) 2018 ownCloud 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.owncloud.android.lib.common.http.interceptors; - -import okhttp3.Credentials; -import okhttp3.Request; - -public class BasicAuthInterceptor implements HttpInterceptor.RequestInterceptor { - - private static final String AUTHORIZATION_HEADER = "Authorization"; - private String mCredentials; - - public BasicAuthInterceptor(String username, String password) { - mCredentials = Credentials.basic(username, password); - } - - @Override - public Request intercept(Request request) { - return request.newBuilder().addHeader(AUTHORIZATION_HEADER, mCredentials).build(); - } -} \ No newline at end of file diff --git a/src/com/owncloud/android/lib/common/http/interceptors/HttpInterceptor.java b/src/com/owncloud/android/lib/common/http/interceptors/HttpInterceptor.java index 95d06506..4613fe6b 100644 --- a/src/com/owncloud/android/lib/common/http/interceptors/HttpInterceptor.java +++ b/src/com/owncloud/android/lib/common/http/interceptors/HttpInterceptor.java @@ -65,7 +65,7 @@ public class HttpInterceptor implements Interceptor { Response intercept(Response response) throws IOException; } - public HttpInterceptor addRequestInterceptor (RequestInterceptor requestInterceptor) { + public HttpInterceptor addRequestInterceptor(RequestInterceptor requestInterceptor) { mRequestInterceptors.add(requestInterceptor); return this; } @@ -79,7 +79,28 @@ public class HttpInterceptor implements Interceptor { return mRequestInterceptors; } + private ArrayList getRequestHeaderInterceptors() { + ArrayList requestHeaderInterceptors = new ArrayList<>(); + + for (RequestInterceptor requestInterceptor : mRequestInterceptors) { + if (requestInterceptor instanceof RequestHeaderInterceptor) { + requestHeaderInterceptors.add((RequestHeaderInterceptor) requestInterceptor); + } + } + + return requestHeaderInterceptors; + } + + public void deleteRequestHeaderInterceptor(String headerName) { + for (RequestInterceptor requestInterceptor : mRequestInterceptors) { + if (requestInterceptor instanceof RequestHeaderInterceptor && + ((RequestHeaderInterceptor) requestInterceptor).getHeaderName().equals(headerName)) { + mRequestInterceptors.remove(requestInterceptor); + } + } + } + public ArrayList getResponseInterceptors() { return mResponseInterceptors; } -} +} \ No newline at end of file diff --git a/src/com/owncloud/android/lib/common/http/interceptors/RequestHeaderInterceptor.java b/src/com/owncloud/android/lib/common/http/interceptors/RequestHeaderInterceptor.java new file mode 100644 index 00000000..a7b31c26 --- /dev/null +++ b/src/com/owncloud/android/lib/common/http/interceptors/RequestHeaderInterceptor.java @@ -0,0 +1,27 @@ +package com.owncloud.android.lib.common.http.interceptors; + +import okhttp3.Request; + +public class RequestHeaderInterceptor implements HttpInterceptor.RequestInterceptor { + + private String mHeaderName; + private String mHeaderValue; + + public RequestHeaderInterceptor(String headerName, String headerValue) { + this.mHeaderName = headerName; + this.mHeaderValue = headerValue; + } + + @Override + public Request intercept(Request request) { + return request.newBuilder().addHeader(mHeaderName, mHeaderValue).build(); + } + + public String getHeaderName() { + return mHeaderName; + } + + public String getHeaderValue() { + return mHeaderValue; + } +} \ No newline at end of file diff --git a/src/com/owncloud/android/lib/common/http/interceptors/SamlAuthInterceptor.java b/src/com/owncloud/android/lib/common/http/interceptors/SamlAuthInterceptor.java deleted file mode 100644 index 8651456e..00000000 --- a/src/com/owncloud/android/lib/common/http/interceptors/SamlAuthInterceptor.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.owncloud.android.lib.common.http.interceptors; - -import java.io.IOException; - -import okhttp3.Request; - -public class SamlAuthInterceptor implements HttpInterceptor.RequestInterceptor { - - private final String mSessionCookie; - - public SamlAuthInterceptor(String sessionCookie) { - this.mSessionCookie = sessionCookie; - } - - @Override - public Request intercept(Request request) throws IOException { - return request.newBuilder() - .addHeader("Cookie", mSessionCookie) - .build(); - } -} diff --git a/src/com/owncloud/android/lib/common/http/interceptors/UserAgentInterceptor.java b/src/com/owncloud/android/lib/common/http/interceptors/UserAgentInterceptor.java deleted file mode 100644 index b57b4fed..00000000 --- a/src/com/owncloud/android/lib/common/http/interceptors/UserAgentInterceptor.java +++ /dev/null @@ -1,42 +0,0 @@ -/* ownCloud Android Library is available under MIT license - * Copyright (C) 2018 ownCloud 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.owncloud.android.lib.common.http.interceptors; - -import okhttp3.Request; - -public class UserAgentInterceptor implements HttpInterceptor.RequestInterceptor { - - private static final String USER_AGENT_HEADER = "User-Agent"; - private String mUserAgent; - - public UserAgentInterceptor(String userAgent) { - mUserAgent = userAgent; - } - - @Override - public Request intercept(Request request) { - return request.newBuilder().addHeader(USER_AGENT_HEADER, mUserAgent).build(); - } -} \ No newline at end of file diff --git a/src/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.java b/src/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.java index 00ac6d6f..f03ae1c1 100644 --- a/src/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.java +++ b/src/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.java @@ -60,8 +60,6 @@ public abstract class HttpBaseMethod { .build(); } - - public void abort() { mCall.cancel(); } diff --git a/src/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.java b/src/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.java index 70ee38b2..7c5c0332 100644 --- a/src/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.java +++ b/src/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.java @@ -24,6 +24,7 @@ package com.owncloud.android.lib.common.http.methods.webdav; +import com.owncloud.android.lib.common.http.HttpClient; import com.owncloud.android.lib.common.http.methods.HttpBaseMethod; import java.util.concurrent.TimeUnit; diff --git a/src/com/owncloud/android/lib/resources/files/ChunkedUploadRemoteFileOperation.java b/src/com/owncloud/android/lib/resources/files/ChunkedUploadRemoteFileOperation.java index 1fc402bd..00ead627 100644 --- a/src/com/owncloud/android/lib/resources/files/ChunkedUploadRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/resources/files/ChunkedUploadRemoteFileOperation.java @@ -54,16 +54,13 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation private static final int LAST_CHUNK_TIMEOUT = 900000; //15 mins. public static final long CHUNK_SIZE = 1024000; - private static final String OC_CHUNKED_HEADER = "OC-Chunked"; - private static final String OC_CHUNK_SIZE_HEADER = "OC-Chunk-Size"; - private static final String OC_CHUNK_X_OC_MTIME_HEADER = "X-OC-Mtime"; private static final String TAG = ChunkedUploadRemoteFileOperation.class.getSimpleName(); private long mTransferId; - public ChunkedUploadRemoteFileOperation(long transferId, String storagePath, String remotePath, String mimeType, + public ChunkedUploadRemoteFileOperation(long transferId, String localPath, String remotePath, String mimeType, String requiredEtag, String fileLastModifTimestamp) { - super(storagePath, remotePath, mimeType, requiredEtag, fileLastModifTimestamp); + super(localPath, remotePath, mimeType, requiredEtag, fileLastModifTimestamp); mTransferId = transferId; } @@ -92,14 +89,9 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation String uriPrefix = client.getNewUploadsWebDavUri() + FileUtils.PATH_SEPARATOR + String.valueOf(mTransferId); long totalLength = fileToUpload.length(); long chunkCount = (long) Math.ceil((double) totalLength / CHUNK_SIZE); - String chunkSizeStr = String.valueOf(CHUNK_SIZE); String totalLengthStr = String.valueOf(fileToUpload.length()); for (int chunkIndex = 0; chunkIndex < chunkCount; chunkIndex++, offset += CHUNK_SIZE) { - if (chunkIndex == chunkCount - 1) { - chunkSizeStr = String.valueOf(CHUNK_SIZE * chunkCount - totalLength); - } - mPutMethod = new PutMethod( HttpUtils.stringUrlToHttpUrl(uriPrefix + FileUtils.PATH_SEPARATOR + chunkIndex) ); @@ -108,10 +100,6 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation mPutMethod.addRequestHeader(IF_MATCH_HEADER, "\"" + mRequiredEtag + "\""); } - mPutMethod.addRequestHeader(OC_TOTAL_LENGTH_HEADER, totalLengthStr); - -// mPutMethod.addRequestHeader(OC_CHUNK_X_OC_MTIME_HEADER, mFileLastModifTimestamp); - // ((ChunkFromFileChannelRequestEntity) mEntity).setOffset(offset); // mPutMethod.setRequestEntity(mEntity); // if (mCancellationRequested.get()) { diff --git a/src/com/owncloud/android/lib/resources/files/FileUtils.java b/src/com/owncloud/android/lib/resources/files/FileUtils.java index 8825dfc3..71277b44 100644 --- a/src/com/owncloud/android/lib/resources/files/FileUtils.java +++ b/src/com/owncloud/android/lib/resources/files/FileUtils.java @@ -34,7 +34,7 @@ public class FileUtils { private static final String TAG = FileUtils.class.getSimpleName(); public static final String PATH_SEPARATOR = "/"; - public static final String FINAl_CHUNKS_FILE = ".file"; + public static final String FINAL_CHUNKS_FILE = ".file"; public static String getParentPath(String remotePath) { String parentPath = new File(remotePath).getParent(); diff --git a/src/com/owncloud/android/lib/resources/files/MoveRemoteChunksFileOperation.java b/src/com/owncloud/android/lib/resources/files/MoveRemoteChunksFileOperation.java index 0663b0e6..7c8859de 100644 --- a/src/com/owncloud/android/lib/resources/files/MoveRemoteChunksFileOperation.java +++ b/src/com/owncloud/android/lib/resources/files/MoveRemoteChunksFileOperation.java @@ -9,8 +9,11 @@ public class MoveRemoteChunksFileOperation extends MoveRemoteFileOperation { * @param targetRemotePath Remove path desired for the file/folder after moving it. * @param overwrite */ - public MoveRemoteChunksFileOperation(String srcRemotePath, String targetRemotePath, boolean overwrite) { + public MoveRemoteChunksFileOperation(String srcRemotePath, String targetRemotePath, boolean overwrite, + String fileLastModifTimestamp, long fileLength) { super(srcRemotePath, targetRemotePath, overwrite); isChunkedFile = true; + mFileLastModifTimestamp = fileLastModifTimestamp; + mFileLength = fileLength; } } \ No newline at end of file diff --git a/src/com/owncloud/android/lib/resources/files/MoveRemoteFileOperation.java b/src/com/owncloud/android/lib/resources/files/MoveRemoteFileOperation.java index 6a639ce3..2ec989ca 100644 --- a/src/com/owncloud/android/lib/resources/files/MoveRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/resources/files/MoveRemoteFileOperation.java @@ -40,7 +40,6 @@ import java.util.concurrent.TimeUnit; import okhttp3.HttpUrl; - /** * Remote operation moving a remote file or folder in the ownCloud server to a different folder * in the same account. @@ -59,21 +58,23 @@ public class MoveRemoteFileOperation extends RemoteOperation { private String mSrcRemotePath; private String mTargetRemotePath; - private boolean mOverwrite; + protected boolean isChunkedFile; + protected String mFileLastModifTimestamp; + protected long mFileLength; /** * Constructor. - *

+ * * TODO Paths should finish in "/" in the case of folders. ? * * @param srcRemotePath Remote path of the file/folder to move. - * @param targetRemotePath Remove path desired for the file/folder after moving it. + * @param targetRemotePath Remote path desired for the file/folder after moving it. */ - public MoveRemoteFileOperation( - String srcRemotePath, String targetRemotePath, boolean overwrite - ) { + public MoveRemoteFileOperation(String srcRemotePath, + String targetRemotePath, + boolean overwrite) { mSrcRemotePath = srcRemotePath; mTargetRemotePath = targetRemotePath; @@ -81,7 +82,6 @@ public class MoveRemoteFileOperation extends RemoteOperation { isChunkedFile = false; } - /** * Performs the rename operation. * @@ -121,6 +121,11 @@ public class MoveRemoteFileOperation extends RemoteOperation { client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mTargetRemotePath), mOverwrite); + if (isChunkedFile) { + move.addRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER, mFileLastModifTimestamp); + move.addRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER, String.valueOf(mFileLength)); + } + move.setReadTimeout(MOVE_READ_TIMEOUT, TimeUnit.SECONDS); move.setConnectionTimeout(MOVE_CONNECTION_TIMEOUT, TimeUnit.SECONDS); @@ -149,7 +154,6 @@ public class MoveRemoteFileOperation extends RemoteOperation { result = new RemoteOperationResult(e); Log.e(TAG, "Move " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + result.getLogMessage(), e); - } return result;