mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 10:27:45 +00:00 
			
		
		
		
	Refactor interceptors and include move headers
This commit is contained in:
		
							parent
							
								
									ed073d6db0
								
							
						
					
					
						commit
						1d80067f85
					
				| @ -25,20 +25,9 @@ package com.owncloud.android.lib.common.authentication; | |||||||
| 
 | 
 | ||||||
| import com.owncloud.android.lib.common.OwnCloudClient; | import com.owncloud.android.lib.common.OwnCloudClient; | ||||||
| import com.owncloud.android.lib.common.http.HttpClient; | import com.owncloud.android.lib.common.http.HttpClient; | ||||||
| import com.owncloud.android.lib.common.http.interceptors.BarearAuthInterceptor; | import com.owncloud.android.lib.common.http.HttpConstants; | ||||||
| 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 okhttp3.Credentials; | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| import java.util.ArrayList; |  | ||||||
| import java.util.List; |  | ||||||
| 
 | 
 | ||||||
| public class OwnCloudBasicCredentials implements OwnCloudCredentials { | public class OwnCloudBasicCredentials implements OwnCloudCredentials { | ||||||
| 
 | 
 | ||||||
| @ -62,21 +51,13 @@ public class OwnCloudBasicCredentials implements OwnCloudCredentials { | |||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     public void applyTo(OwnCloudClient client) { |     public void applyTo(OwnCloudClient client) { | ||||||
|         ArrayList<RequestInterceptor> requestInterceptors = HttpClient.getOkHttpInterceptor().getRequestInterceptors(); |  | ||||||
| 
 |  | ||||||
|         // Clear previous basic credentials |         // Clear previous basic credentials | ||||||
|         for (HttpInterceptor.RequestInterceptor requestInterceptor : requestInterceptors) { |         HttpClient.deleteHeaderForAllRequests(HttpConstants.BASIC_AUTHORIZATION_HEADER); | ||||||
|             if (requestInterceptor instanceof BasicAuthInterceptor |         HttpClient.deleteHeaderForAllRequests(HttpConstants.BEARER_AUTHORIZATION_HEADER); | ||||||
|                     || requestInterceptor instanceof BarearAuthInterceptor |         HttpClient.deleteHeaderForAllRequests(HttpConstants.COOKIE_HEADER); | ||||||
|                     || requestInterceptor instanceof SamlAuthInterceptor) { |  | ||||||
|                 requestInterceptors.remove(requestInterceptor); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
| 
 | 
 | ||||||
|         HttpClient.getOkHttpInterceptor() |         HttpClient.addHeaderForAllRequests(HttpConstants.BASIC_AUTHORIZATION_HEADER, | ||||||
|                 .addRequestInterceptor( |                 Credentials.basic(mUsername, mPassword)); | ||||||
|                         new BasicAuthInterceptor(mUsername, mPassword) |  | ||||||
|                 ); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
| @ -98,5 +79,4 @@ public class OwnCloudBasicCredentials implements OwnCloudCredentials { | |||||||
|     public boolean authTokenCanBeRefreshed() { |     public boolean authTokenCanBeRefreshed() { | ||||||
|         return false; |         return false; | ||||||
|     } |     } | ||||||
| 
 |  | ||||||
| } | } | ||||||
| @ -1,5 +1,5 @@ | |||||||
| /* ownCloud Android Library is available under MIT license | /* 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 |  *   Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||
|  *   of this software and associated documentation files (the "Software"), to deal |  *   of this software and associated documentation files (the "Software"), to deal | ||||||
| @ -23,15 +23,9 @@ | |||||||
|  */ |  */ | ||||||
| package com.owncloud.android.lib.common.authentication; | package com.owncloud.android.lib.common.authentication; | ||||||
| 
 | 
 | ||||||
| import java.util.ArrayList; |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| import com.owncloud.android.lib.common.OwnCloudClient; | import com.owncloud.android.lib.common.OwnCloudClient; | ||||||
| import com.owncloud.android.lib.common.http.HttpClient; | import com.owncloud.android.lib.common.http.HttpClient; | ||||||
| import com.owncloud.android.lib.common.http.interceptors.BarearAuthInterceptor; | import com.owncloud.android.lib.common.http.HttpConstants; | ||||||
| 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; |  | ||||||
| 
 | 
 | ||||||
| public class OwnCloudBearerCredentials implements OwnCloudCredentials { | public class OwnCloudBearerCredentials implements OwnCloudCredentials { | ||||||
| 
 | 
 | ||||||
| @ -45,21 +39,12 @@ public class OwnCloudBearerCredentials implements OwnCloudCredentials { | |||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     public void applyTo(OwnCloudClient client) { |     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<HttpInterceptor.RequestInterceptor> requestInterceptors = |         HttpClient.addHeaderForAllRequests(HttpConstants.BEARER_AUTHORIZATION_HEADER, mAccessToken); | ||||||
|                 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)); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
| @ -82,5 +67,4 @@ public class OwnCloudBearerCredentials implements OwnCloudCredentials { | |||||||
|     public boolean authTokenCanBeRefreshed() { |     public boolean authTokenCanBeRefreshed() { | ||||||
|         return true; |         return true; | ||||||
|     } |     } | ||||||
| 
 |  | ||||||
| } | } | ||||||
| @ -1,5 +1,5 @@ | |||||||
| /* ownCloud Android Library is available under MIT license | /* 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 |  *   Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||
|  *   of this software and associated documentation files (the "Software"), to deal |  *   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.OwnCloudClient; | ||||||
| import com.owncloud.android.lib.common.http.HttpClient; | import com.owncloud.android.lib.common.http.HttpClient; | ||||||
| import com.owncloud.android.lib.common.http.interceptors.BarearAuthInterceptor; | import com.owncloud.android.lib.common.http.HttpConstants; | ||||||
| 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; |  | ||||||
| 
 | 
 | ||||||
| public class OwnCloudCredentialsFactory { | public class OwnCloudCredentialsFactory { | ||||||
| 
 | 
 | ||||||
| @ -71,16 +66,10 @@ public class OwnCloudCredentialsFactory { | |||||||
| 
 | 
 | ||||||
|         @Override |         @Override | ||||||
|         public void applyTo(OwnCloudClient client) { |         public void applyTo(OwnCloudClient client) { | ||||||
|             ArrayList<HttpInterceptor.RequestInterceptor> requestInterceptors = HttpClient.getOkHttpInterceptor().getRequestInterceptors(); |  | ||||||
| 
 |  | ||||||
|             // Clear previous basic credentials |             // Clear previous basic credentials | ||||||
|             for (HttpInterceptor.RequestInterceptor requestInterceptor : requestInterceptors) { |             HttpClient.deleteHeaderForAllRequests(HttpConstants.BASIC_AUTHORIZATION_HEADER); | ||||||
|                 if (requestInterceptor instanceof BasicAuthInterceptor |             HttpClient.deleteHeaderForAllRequests(HttpConstants.BEARER_AUTHORIZATION_HEADER); | ||||||
|                         || requestInterceptor instanceof BarearAuthInterceptor |             HttpClient.deleteHeaderForAllRequests(HttpConstants.COOKIE_HEADER); | ||||||
|                         || requestInterceptor instanceof SamlAuthInterceptor) { |  | ||||||
|                     requestInterceptors.remove(requestInterceptor); |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         @Override |         @Override | ||||||
| @ -104,5 +93,4 @@ public class OwnCloudCredentialsFactory { | |||||||
|             return null; |             return null; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 |  | ||||||
| } | } | ||||||
| @ -1,5 +1,5 @@ | |||||||
| /* ownCloud Android Library is available under MIT license | /* 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 |  *   Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||
|  *   of this software and associated documentation files (the "Software"), to deal |  *   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.OwnCloudClient; | ||||||
| import com.owncloud.android.lib.common.http.HttpClient; | import com.owncloud.android.lib.common.http.HttpClient; | ||||||
| import com.owncloud.android.lib.common.http.interceptors.BarearAuthInterceptor; | import com.owncloud.android.lib.common.http.HttpConstants; | ||||||
| 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; |  | ||||||
| 
 |  | ||||||
| 
 | 
 | ||||||
| public class OwnCloudSamlSsoCredentials implements OwnCloudCredentials { | public class OwnCloudSamlSsoCredentials implements OwnCloudCredentials { | ||||||
| 
 | 
 | ||||||
| @ -45,20 +39,12 @@ public class OwnCloudSamlSsoCredentials implements OwnCloudCredentials { | |||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     public void applyTo(OwnCloudClient client) { |     public void applyTo(OwnCloudClient client) { | ||||||
|         final ArrayList<HttpInterceptor.RequestInterceptor> requestInterceptors = |         // Clear previous credentials | ||||||
|                 HttpClient.getOkHttpInterceptor().getRequestInterceptors(); |         HttpClient.deleteHeaderForAllRequests(HttpConstants.BASIC_AUTHORIZATION_HEADER); | ||||||
|  |         HttpClient.deleteHeaderForAllRequests(HttpConstants.BEARER_AUTHORIZATION_HEADER); | ||||||
|  |         HttpClient.deleteHeaderForAllRequests(HttpConstants.COOKIE_HEADER); | ||||||
| 
 | 
 | ||||||
|         // Clear previous basic credentials |         HttpClient.addHeaderForAllRequests(HttpConstants.COOKIE_HEADER, mSessionCookie); | ||||||
|         for (HttpInterceptor.RequestInterceptor requestInterceptor : requestInterceptors) { |  | ||||||
|             if (requestInterceptor instanceof BasicAuthInterceptor |  | ||||||
|                     || requestInterceptor instanceof BarearAuthInterceptor |  | ||||||
|                     || requestInterceptor instanceof SamlAuthInterceptor) { |  | ||||||
|                 requestInterceptors.remove(requestInterceptor); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         HttpClient.getOkHttpInterceptor() |  | ||||||
|                 .addRequestInterceptor(new SamlAuthInterceptor(mSessionCookie)); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
| @ -81,5 +67,4 @@ public class OwnCloudSamlSsoCredentials implements OwnCloudCredentials { | |||||||
|     public boolean authTokenCanBeRefreshed() { |     public boolean authTokenCanBeRefreshed() { | ||||||
|         return false; |         return false; | ||||||
|     } |     } | ||||||
| 
 |  | ||||||
| } | } | ||||||
| @ -25,19 +25,19 @@ | |||||||
| package com.owncloud.android.lib.common.http; | package com.owncloud.android.lib.common.http; | ||||||
| 
 | 
 | ||||||
| import android.content.Context; | import android.content.Context; | ||||||
|  | 
 | ||||||
|  | import java.util.ArrayList; | ||||||
| import java.util.concurrent.TimeUnit; | import java.util.concurrent.TimeUnit; | ||||||
| 
 | 
 | ||||||
| import com.facebook.stetho.okhttp3.StethoInterceptor; | import com.facebook.stetho.okhttp3.StethoInterceptor; | ||||||
| import com.owncloud.android.lib.BuildConfig; | import com.owncloud.android.lib.BuildConfig; | ||||||
| import com.owncloud.android.lib.common.OwnCloudClientManagerFactory; | import com.owncloud.android.lib.common.OwnCloudClientManagerFactory; | ||||||
| import com.owncloud.android.lib.common.http.interceptors.HttpInterceptor; | 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.AdvancedX509TrustManager; | ||||||
| import com.owncloud.android.lib.common.network.NetworkUtils; | import com.owncloud.android.lib.common.network.NetworkUtils; | ||||||
| import com.owncloud.android.lib.common.utils.Log_OC; | import com.owncloud.android.lib.common.utils.Log_OC; | ||||||
| 
 | 
 | ||||||
| import org.apache.http.conn.ssl.BrowserCompatHostnameVerifier; |  | ||||||
| 
 |  | ||||||
| import java.util.Arrays; | import java.util.Arrays; | ||||||
| 
 | 
 | ||||||
| import javax.net.ssl.SSLContext; | import javax.net.ssl.SSLContext; | ||||||
| @ -58,7 +58,6 @@ public class HttpClient { | |||||||
|     private static HttpInterceptor sOkHttpInterceptor; |     private static HttpInterceptor sOkHttpInterceptor; | ||||||
|     private static Context sContext; |     private static Context sContext; | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|     public static void setContext(Context context) { |     public static void setContext(Context context) { | ||||||
|         sContext = context; |         sContext = context; | ||||||
|     } |     } | ||||||
| @ -90,14 +89,10 @@ public class HttpClient { | |||||||
|         return sOkHttpClient; |         return sOkHttpClient; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public static HttpInterceptor getOkHttpInterceptor() { |     private static HttpInterceptor getOkHttpInterceptor() { | ||||||
|         if (sOkHttpInterceptor == null) { |         if (sOkHttpInterceptor == null) { | ||||||
|             sOkHttpInterceptor = new HttpInterceptor() |             sOkHttpInterceptor = new HttpInterceptor(); | ||||||
|                     .addRequestInterceptor(new UserAgentInterceptor( |             addHeaderForAllRequests(HttpConstants.USER_AGENT_HEADER, OwnCloudClientManagerFactory.getUserAgent()); | ||||||
|                                     // TODO Try to get rid of this dependency |  | ||||||
|                                     OwnCloudClientManagerFactory.getUserAgent() |  | ||||||
|                             ) |  | ||||||
|                     ); |  | ||||||
|         } |         } | ||||||
|         return sOkHttpInterceptor; |         return sOkHttpInterceptor; | ||||||
|     } |     } | ||||||
| @ -118,4 +113,20 @@ public class HttpClient { | |||||||
|         } |         } | ||||||
|         sOkHttpClient = clientBuilder.build(); |         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); | ||||||
|  |     } | ||||||
| } | } | ||||||
| @ -33,10 +33,10 @@ public class HttpConstants { | |||||||
|      *************************************************** HEADERS *********************************************** |      *************************************************** HEADERS *********************************************** | ||||||
|      ***********************************************************************************************************/ |      ***********************************************************************************************************/ | ||||||
| 
 | 
 | ||||||
|     /*********************************************************************************************************** |     public static final String BASIC_AUTHORIZATION_HEADER = "Authorization"; | ||||||
|      *************************************************** HEADERS *********************************************** |     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_MATCH_HEADER = "If-Match"; | ||||||
|     public static final String IF_NONE_MATCH_HEADER = "If-None-Match"; |     public static final String IF_NONE_MATCH_HEADER = "If-None-Match"; | ||||||
|     public static final String CONTENT_TYPE_HEADER = "Content-Type"; |     public static final String CONTENT_TYPE_HEADER = "Content-Type"; | ||||||
|  | |||||||
| @ -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(); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @ -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(); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @ -65,7 +65,7 @@ public class HttpInterceptor implements Interceptor { | |||||||
|         Response intercept(Response response) throws IOException; |         Response intercept(Response response) throws IOException; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public HttpInterceptor addRequestInterceptor (RequestInterceptor requestInterceptor) { |     public HttpInterceptor addRequestInterceptor(RequestInterceptor requestInterceptor) { | ||||||
|         mRequestInterceptors.add(requestInterceptor); |         mRequestInterceptors.add(requestInterceptor); | ||||||
|         return this; |         return this; | ||||||
|     } |     } | ||||||
| @ -79,6 +79,27 @@ public class HttpInterceptor implements Interceptor { | |||||||
|         return mRequestInterceptors; |         return mRequestInterceptors; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     private ArrayList<RequestHeaderInterceptor> getRequestHeaderInterceptors() { | ||||||
|  |         ArrayList<RequestHeaderInterceptor> 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<ResponseInterceptor> getResponseInterceptors() { |     public ArrayList<ResponseInterceptor> getResponseInterceptors() { | ||||||
|         return mResponseInterceptors; |         return mResponseInterceptors; | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -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; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -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(); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @ -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(); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @ -60,8 +60,6 @@ public abstract class HttpBaseMethod { | |||||||
|                 .build(); |                 .build(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|     public void abort() { |     public void abort() { | ||||||
|         mCall.cancel(); |         mCall.cancel(); | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -24,6 +24,7 @@ | |||||||
| 
 | 
 | ||||||
| package com.owncloud.android.lib.common.http.methods.webdav; | 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 com.owncloud.android.lib.common.http.methods.HttpBaseMethod; | ||||||
| 
 | 
 | ||||||
| import java.util.concurrent.TimeUnit; | import java.util.concurrent.TimeUnit; | ||||||
|  | |||||||
| @ -54,16 +54,13 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation | |||||||
|     private static final int LAST_CHUNK_TIMEOUT = 900000; //15 mins. |     private static final int LAST_CHUNK_TIMEOUT = 900000; //15 mins. | ||||||
| 
 | 
 | ||||||
|     public static final long CHUNK_SIZE = 1024000; |     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 static final String TAG = ChunkedUploadRemoteFileOperation.class.getSimpleName(); | ||||||
| 
 | 
 | ||||||
|     private long mTransferId; |     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) { |                                             String requiredEtag, String fileLastModifTimestamp) { | ||||||
|         super(storagePath, remotePath, mimeType, requiredEtag, fileLastModifTimestamp); |         super(localPath, remotePath, mimeType, requiredEtag, fileLastModifTimestamp); | ||||||
|         mTransferId = transferId; |         mTransferId = transferId; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -92,14 +89,9 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation | |||||||
|             String uriPrefix = client.getNewUploadsWebDavUri() + FileUtils.PATH_SEPARATOR + String.valueOf(mTransferId); |             String uriPrefix = client.getNewUploadsWebDavUri() + FileUtils.PATH_SEPARATOR + String.valueOf(mTransferId); | ||||||
|             long totalLength = fileToUpload.length(); |             long totalLength = fileToUpload.length(); | ||||||
|             long chunkCount = (long) Math.ceil((double) totalLength / CHUNK_SIZE); |             long chunkCount = (long) Math.ceil((double) totalLength / CHUNK_SIZE); | ||||||
|             String chunkSizeStr = String.valueOf(CHUNK_SIZE); |  | ||||||
|             String totalLengthStr = String.valueOf(fileToUpload.length()); |             String totalLengthStr = String.valueOf(fileToUpload.length()); | ||||||
| 
 | 
 | ||||||
|             for (int chunkIndex = 0; chunkIndex < chunkCount; chunkIndex++, offset += CHUNK_SIZE) { |             for (int chunkIndex = 0; chunkIndex < chunkCount; chunkIndex++, offset += CHUNK_SIZE) { | ||||||
|                 if (chunkIndex == chunkCount - 1) { |  | ||||||
|                     chunkSizeStr = String.valueOf(CHUNK_SIZE * chunkCount - totalLength); |  | ||||||
|                 } |  | ||||||
| 
 |  | ||||||
|                 mPutMethod = new PutMethod( |                 mPutMethod = new PutMethod( | ||||||
|                         HttpUtils.stringUrlToHttpUrl(uriPrefix + FileUtils.PATH_SEPARATOR + chunkIndex) |                         HttpUtils.stringUrlToHttpUrl(uriPrefix + FileUtils.PATH_SEPARATOR + chunkIndex) | ||||||
|                 ); |                 ); | ||||||
| @ -108,10 +100,6 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation | |||||||
|                     mPutMethod.addRequestHeader(IF_MATCH_HEADER, "\"" + mRequiredEtag + "\""); |                     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); | //                ((ChunkFromFileChannelRequestEntity) mEntity).setOffset(offset); | ||||||
| //                mPutMethod.setRequestEntity(mEntity); | //                mPutMethod.setRequestEntity(mEntity); | ||||||
| //                if (mCancellationRequested.get()) { | //                if (mCancellationRequested.get()) { | ||||||
|  | |||||||
| @ -34,7 +34,7 @@ public class FileUtils { | |||||||
| 	private static final  String TAG = FileUtils.class.getSimpleName(); | 	private static final  String TAG = FileUtils.class.getSimpleName(); | ||||||
| 
 | 
 | ||||||
| 	public static final String PATH_SEPARATOR = "/"; | 	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) { | 	public static String getParentPath(String remotePath) { | ||||||
| 		String parentPath = new File(remotePath).getParent(); | 		String parentPath = new File(remotePath).getParent(); | ||||||
|  | |||||||
| @ -9,8 +9,11 @@ public class MoveRemoteChunksFileOperation extends MoveRemoteFileOperation { | |||||||
|      * @param targetRemotePath Remove path desired for the file/folder after moving it. |      * @param targetRemotePath Remove path desired for the file/folder after moving it. | ||||||
|      * @param overwrite |      * @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); |         super(srcRemotePath, targetRemotePath, overwrite); | ||||||
|         isChunkedFile = true; |         isChunkedFile = true; | ||||||
|  |         mFileLastModifTimestamp = fileLastModifTimestamp; | ||||||
|  |         mFileLength = fileLength; | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @ -40,7 +40,6 @@ import java.util.concurrent.TimeUnit; | |||||||
| 
 | 
 | ||||||
| import okhttp3.HttpUrl; | import okhttp3.HttpUrl; | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| /** | /** | ||||||
|  * Remote operation moving a remote file or folder in the ownCloud server to a different folder |  * Remote operation moving a remote file or folder in the ownCloud server to a different folder | ||||||
|  * in the same account. |  * in the same account. | ||||||
| @ -59,21 +58,23 @@ public class MoveRemoteFileOperation extends RemoteOperation { | |||||||
| 
 | 
 | ||||||
|     private String mSrcRemotePath; |     private String mSrcRemotePath; | ||||||
|     private String mTargetRemotePath; |     private String mTargetRemotePath; | ||||||
| 
 |  | ||||||
|     private boolean mOverwrite; |     private boolean mOverwrite; | ||||||
|  | 
 | ||||||
|     protected boolean isChunkedFile; |     protected boolean isChunkedFile; | ||||||
|  |     protected String mFileLastModifTimestamp; | ||||||
|  |     protected long mFileLength; | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * Constructor. |      * Constructor. | ||||||
|      * <p> |      * | ||||||
|      * TODO Paths should finish in "/" in the case of folders. ? |      * TODO Paths should finish in "/" in the case of folders. ? | ||||||
|      * |      * | ||||||
|      * @param srcRemotePath    Remote path of the file/folder to move. |      * @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( |     public MoveRemoteFileOperation(String srcRemotePath, | ||||||
|         String srcRemotePath, String targetRemotePath, boolean overwrite |                                    String targetRemotePath, | ||||||
|     ) { |                                    boolean overwrite) { | ||||||
| 
 | 
 | ||||||
|         mSrcRemotePath = srcRemotePath; |         mSrcRemotePath = srcRemotePath; | ||||||
|         mTargetRemotePath = targetRemotePath; |         mTargetRemotePath = targetRemotePath; | ||||||
| @ -81,7 +82,6 @@ public class MoveRemoteFileOperation extends RemoteOperation { | |||||||
|         isChunkedFile = false; |         isChunkedFile = false; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|     /** |     /** | ||||||
|      * Performs the rename operation. |      * Performs the rename operation. | ||||||
|      * |      * | ||||||
| @ -121,6 +121,11 @@ public class MoveRemoteFileOperation extends RemoteOperation { | |||||||
|                 client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mTargetRemotePath), |                 client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mTargetRemotePath), | ||||||
|                     mOverwrite); |                     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.setReadTimeout(MOVE_READ_TIMEOUT, TimeUnit.SECONDS); | ||||||
|             move.setConnectionTimeout(MOVE_CONNECTION_TIMEOUT, TimeUnit.SECONDS); |             move.setConnectionTimeout(MOVE_CONNECTION_TIMEOUT, TimeUnit.SECONDS); | ||||||
| 
 | 
 | ||||||
| @ -149,7 +154,6 @@ public class MoveRemoteFileOperation extends RemoteOperation { | |||||||
|             result = new RemoteOperationResult(e); |             result = new RemoteOperationResult(e); | ||||||
|             Log.e(TAG, "Move " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + |             Log.e(TAG, "Move " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + | ||||||
|                 result.getLogMessage(), e); |                 result.getLogMessage(), e); | ||||||
| 
 |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         return result; |         return result; | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user