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

Commit 93026e8

Browse files
committed
First step to replace interceptor
1 parent c5cf0a8 commit 93026e8

35 files changed

+785
-647
lines changed

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

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,6 @@ public void clearCredentials() {
8989
if (!(mCredentials instanceof OwnCloudAnonymousCredentials)) {
9090
mCredentials = OwnCloudCredentialsFactory.getAnonymousCredentials();
9191
}
92-
mCredentials.applyTo(this);
93-
}
94-
95-
void applyCredentials() {
96-
mCredentials.applyTo(this);
9792
}
9893

9994
public int executeHttpMethod(HttpBaseMethod method) throws Exception {
@@ -102,8 +97,17 @@ public int executeHttpMethod(HttpBaseMethod method) throws Exception {
10297
int status;
10398

10499
do {
105-
setRequestId(method);
106-
100+
String requestId = RandomUtils.generateRandomUUID();
101+
102+
// Header to allow tracing requests in apache and ownCloud logs
103+
Timber.d("Executing in request with id %s", requestId);
104+
method.setRequestHeader(HttpConstants.OC_X_REQUEST_ID,requestId);
105+
method.setRequestHeader(HttpConstants.USER_AGENT_HEADER, SingleSessionManager.getUserAgent());
106+
method.setRequestHeader(HttpConstants.PARAM_SINGLE_COOKIE_HEADER, "true");
107+
method.setRequestHeader(HttpConstants.ACCEPT_ENCODING_HEADER, HttpConstants.ACCEPT_ENCODING_IDENTITY);
108+
if(mCredentials.getHeaderAuth()!=null){
109+
method.setRequestHeader(HttpConstants.AUTHORIZATION_HEADER,mCredentials.getHeaderAuth());
110+
}
107111
status = method.execute();
108112

109113
if (mFollowRedirects) {
@@ -125,8 +129,17 @@ private int executeRedirectedHttpMethod(HttpBaseMethod method) throws Exception
125129
int status;
126130

127131
do {
128-
setRequestId(method);
129-
132+
String requestId = RandomUtils.generateRandomUUID();
133+
134+
// Header to allow tracing requests in apache and ownCloud logs
135+
Timber.d("Executing in request with id %s", requestId);
136+
method.setRequestHeader(OC_X_REQUEST_ID,requestId);
137+
method.setRequestHeader(HttpConstants.USER_AGENT_HEADER, SingleSessionManager.getUserAgent());
138+
method.setRequestHeader(HttpConstants.PARAM_SINGLE_COOKIE_HEADER, "true");
139+
method.setRequestHeader(HttpConstants.ACCEPT_ENCODING_HEADER, HttpConstants.ACCEPT_ENCODING_IDENTITY);
140+
if(mCredentials.getHeaderAuth()!=null){
141+
method.setRequestHeader(HttpConstants.AUTHORIZATION_HEADER,mCredentials.getHeaderAuth());
142+
}
130143
status = method.execute();
131144

132145
repeatWithFreshCredentials = checkUnauthorizedAccess(status, repeatCounter);
@@ -138,19 +151,6 @@ private int executeRedirectedHttpMethod(HttpBaseMethod method) throws Exception
138151
return status;
139152
}
140153

141-
private void setRequestId(HttpBaseMethod method) {
142-
// Clean previous request id. This is a bit hacky but is the only way to add request headers in WebDAV
143-
// methods by using Dav4Android
144-
deleteHeaderForAllRequests(OC_X_REQUEST_ID);
145-
146-
String requestId = RandomUtils.generateRandomUUID();
147-
148-
// Header to allow tracing requests in apache and ownCloud logs
149-
addHeaderForAllRequests(OC_X_REQUEST_ID, requestId);
150-
151-
Timber.d("Executing in request with id %s", requestId);
152-
}
153-
154154
public RedirectionPath followRedirection(HttpBaseMethod method) throws Exception {
155155
int redirectionsCount = 0;
156156
int status = method.getStatusCode();
@@ -215,9 +215,6 @@ public RedirectionPath followRedirection(HttpBaseMethod method) throws Exception
215215
public void exhaustResponse(InputStream responseBodyAsStream) {
216216
if (responseBodyAsStream != null) {
217217
try {
218-
while (responseBodyAsStream.read(sExhaustBuffer) >= 0) {
219-
;
220-
}
221218
responseBodyAsStream.close();
222219

223220
} catch (IOException io) {
@@ -273,7 +270,6 @@ public final OwnCloudCredentials getCredentials() {
273270
public void setCredentials(OwnCloudCredentials credentials) {
274271
if (credentials != null) {
275272
mCredentials = credentials;
276-
mCredentials.applyTo(this);
277273
} else {
278274
clearCredentials();
279275
}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ public OwnCloudClient getClientFor(OwnCloudAccount account, Context context) thr
130130
Timber.v("reusing client for session %s", sessionName);
131131
}
132132

133-
keepCredentialsUpdated(client);
134133
keepCookiesUpdated(context, account, client);
135134
keepUriUpdated(account, client);
136135
}
@@ -177,10 +176,6 @@ public void saveAllClients(Context context, String accountType) {
177176
Timber.d("All sessions saved");
178177
}
179178

180-
private void keepCredentialsUpdated(OwnCloudClient reusedClient) {
181-
reusedClient.applyCredentials();
182-
}
183-
184179
private void keepCookiesUpdated(Context context, OwnCloudAccount account, OwnCloudClient reusedClient) {
185180
AccountManager am = AccountManager.get(context.getApplicationContext());
186181
if (am != null && account.getSavedAccount() != null) {

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudBasicCredentials.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,6 @@ public OwnCloudBasicCredentials(String username, String password) {
4040
mPassword = password != null ? password : "";
4141
}
4242

43-
public OwnCloudBasicCredentials(String username, String password, boolean preemptiveMode) {
44-
mUsername = username != null ? username : "";
45-
mPassword = password != null ? password : "";
46-
}
47-
48-
@Override
49-
public void applyTo(OwnCloudClient client) {
50-
// Clear previous basic credentials
51-
HttpClient.deleteHeaderForAllRequests(HttpConstants.AUTHORIZATION_HEADER);
52-
HttpClient.deleteHeaderForAllRequests(HttpConstants.COOKIE_HEADER);
53-
54-
HttpClient.addHeaderForAllRequests(HttpConstants.AUTHORIZATION_HEADER,
55-
Credentials.basic(mUsername, mPassword, UTF_8));
56-
}
57-
5843
@Override
5944
public String getUsername() {
6045
return mUsername;
@@ -65,6 +50,11 @@ public String getAuthToken() {
6550
return mPassword;
6651
}
6752

53+
@Override
54+
public String getHeaderAuth() {
55+
return Credentials.basic(mUsername, mPassword, Util.UTF_8);
56+
}
57+
6858
@Override
6959
public boolean authTokenExpires() {
7060
return false;

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudBearerCredentials.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
*/
2424
package com.owncloud.android.lib.common.authentication;
2525

26-
import com.owncloud.android.lib.common.OwnCloudClient;
27-
import com.owncloud.android.lib.common.http.HttpClient;
2826
import com.owncloud.android.lib.common.http.HttpConstants;
2927

3028
public class OwnCloudBearerCredentials implements OwnCloudCredentials {
@@ -37,16 +35,6 @@ public OwnCloudBearerCredentials(String username, String accessToken) {
3735
mAccessToken = accessToken != null ? accessToken : "";
3836
}
3937

40-
@Override
41-
public void applyTo(OwnCloudClient client) {
42-
// Clear previous credentials
43-
HttpClient.deleteHeaderForAllRequests(HttpConstants.AUTHORIZATION_HEADER);
44-
HttpClient.deleteHeaderForAllRequests(HttpConstants.COOKIE_HEADER);
45-
46-
HttpClient.addHeaderForAllRequests(HttpConstants.AUTHORIZATION_HEADER,
47-
HttpConstants.BEARER_AUTHORIZATION_KEY + mAccessToken);
48-
}
49-
5038
@Override
5139
public String getUsername() {
5240
// not relevant for authentication, but relevant for informational purposes
@@ -58,6 +46,11 @@ public String getAuthToken() {
5846
return mAccessToken;
5947
}
6048

49+
@Override
50+
public String getHeaderAuth() {
51+
return HttpConstants.BEARER_AUTHORIZATION_KEY + mAccessToken;
52+
}
53+
6154
@Override
6255
public boolean authTokenExpires() {
6356
return true;

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudCredentials.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,14 @@
2424

2525
package com.owncloud.android.lib.common.authentication;
2626

27-
import com.owncloud.android.lib.common.OwnCloudClient;
28-
2927
public interface OwnCloudCredentials {
3028

31-
void applyTo(OwnCloudClient ownCloudClient);
32-
3329
String getUsername();
3430

3531
String getAuthToken();
3632

33+
String getHeaderAuth();
34+
3735
boolean authTokenExpires();
3836

3937
boolean authTokenCanBeRefreshed();

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudCredentialsFactory.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,12 @@ protected OwnCloudAnonymousCredentials() {
5555
}
5656

5757
@Override
58-
public void applyTo(OwnCloudClient client) {
59-
// Clear previous basic credentials
60-
HttpClient.deleteHeaderForAllRequests(HttpConstants.AUTHORIZATION_HEADER);
61-
HttpClient.deleteHeaderForAllRequests(HttpConstants.COOKIE_HEADER);
58+
public String getAuthToken() {
59+
return "";
6260
}
6361

6462
@Override
65-
public String getAuthToken() {
63+
public String getHeaderAuth() {
6664
return "";
6765
}
6866

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626

2727
import android.content.Context;
2828

29-
import com.owncloud.android.lib.common.SingleSessionManager;
3029
import com.owncloud.android.lib.common.http.interceptors.HttpInterceptor;
31-
import com.owncloud.android.lib.common.http.interceptors.RequestHeaderInterceptor;
3230
import com.owncloud.android.lib.common.network.AdvancedX509TrustManager;
3331
import com.owncloud.android.lib.common.network.NetworkUtils;
3432
import okhttp3.Cookie;
@@ -137,33 +135,10 @@ public List<Cookie> loadForRequest(HttpUrl url) {
137135
private static HttpInterceptor getOkHttpInterceptor() {
138136
if (sOkHttpInterceptor == null) {
139137
sOkHttpInterceptor = new HttpInterceptor();
140-
addHeaderForAllRequests(HttpConstants.USER_AGENT_HEADER, SingleSessionManager.getUserAgent());
141-
addHeaderForAllRequests(HttpConstants.PARAM_SINGLE_COOKIE_HEADER, "true");
142-
addHeaderForAllRequests(HttpConstants.ACCEPT_ENCODING_HEADER, HttpConstants.ACCEPT_ENCODING_IDENTITY);
143138
}
144139
return sOkHttpInterceptor;
145140
}
146141

147-
/**
148-
* Add header that will be included for all the requests from now on
149-
*
150-
* @param headerName
151-
* @param headerValue
152-
*/
153-
public static void addHeaderForAllRequests(String headerName, String headerValue) {
154-
HttpInterceptor httpInterceptor = getOkHttpInterceptor();
155-
156-
if (getOkHttpInterceptor() != null) {
157-
httpInterceptor.addRequestInterceptor(
158-
new RequestHeaderInterceptor(headerName, headerValue)
159-
);
160-
}
161-
}
162-
163-
public static void deleteHeaderForAllRequests(String headerName) {
164-
getOkHttpInterceptor().deleteRequestHeaderInterceptor(headerName);
165-
}
166-
167142
public Context getContext() {
168143
return sContext;
169144
}

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/interceptors/HttpInterceptor.java

Lines changed: 5 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
import okhttp3.Interceptor;
2828
import okhttp3.Request;
2929
import okhttp3.Response;
30+
import org.jetbrains.annotations.NotNull;
31+
import timber.log.Timber;
3032

3133
import java.io.IOException;
32-
import java.util.ArrayList;
33-
import java.util.ListIterator;
3434

3535
/**
3636
* Http interceptor to use multiple interceptors in the same {@link okhttp3.OkHttpClient} instance
@@ -39,78 +39,14 @@
3939
*/
4040
public class HttpInterceptor implements Interceptor {
4141

42-
private final ArrayList<RequestInterceptor> mRequestInterceptors = new ArrayList<>();
43-
private final ArrayList<ResponseInterceptor> mResponseInterceptors = new ArrayList<>();
44-
42+
@NotNull
4543
@Override
4644
public Response intercept(Chain chain) throws IOException {
4745
Request request = chain.request();
4846

49-
ListIterator<RequestInterceptor> requestInterceptorIterator = mRequestInterceptors.listIterator();
50-
51-
while (requestInterceptorIterator.hasNext()) {
52-
RequestInterceptor currentRequestInterceptor = requestInterceptorIterator.next();
53-
request = currentRequestInterceptor.intercept(request);
54-
}
55-
56-
Response response = chain.proceed(request);
57-
58-
ListIterator<ResponseInterceptor> responseInterceptorIterator = mResponseInterceptors.listIterator();
59-
60-
while (responseInterceptorIterator.hasNext()) {
61-
ResponseInterceptor currentResponseInterceptor = responseInterceptorIterator.next();
62-
response = currentResponseInterceptor.intercept(response);
63-
}
64-
65-
return response;
66-
}
67-
68-
public HttpInterceptor addRequestInterceptor(RequestInterceptor requestInterceptor) {
69-
mRequestInterceptors.listIterator().add(requestInterceptor);
70-
return this;
71-
}
72-
73-
public HttpInterceptor addResponseInterceptor(ResponseInterceptor responseInterceptor) {
74-
mResponseInterceptors.listIterator().add(responseInterceptor);
75-
return this;
76-
}
77-
78-
public ArrayList<RequestInterceptor> getRequestInterceptors() {
79-
return mRequestInterceptors;
80-
}
81-
82-
private ArrayList<RequestHeaderInterceptor> getRequestHeaderInterceptors() {
83-
ArrayList<RequestHeaderInterceptor> requestHeaderInterceptors = new ArrayList<>();
84-
85-
for (RequestInterceptor requestInterceptor : mRequestInterceptors) {
86-
if (requestInterceptor instanceof RequestHeaderInterceptor) {
87-
requestHeaderInterceptors.add((RequestHeaderInterceptor) requestInterceptor);
88-
}
89-
}
90-
91-
return requestHeaderInterceptors;
92-
}
47+
Timber.d("Executing in request headers %s", request.headers().toString());
9348

94-
public void deleteRequestHeaderInterceptor(String headerName) {
95-
ListIterator<RequestInterceptor> requestInterceptorIterator = mRequestInterceptors.listIterator();
96-
while (requestInterceptorIterator.hasNext()) {
97-
RequestInterceptor currentRequestInterceptor = requestInterceptorIterator.next();
98-
if (currentRequestInterceptor instanceof RequestHeaderInterceptor &&
99-
((RequestHeaderInterceptor) currentRequestInterceptor).getHeaderName().equals(headerName)) {
100-
requestInterceptorIterator.remove();
101-
}
102-
}
49+
return chain.proceed(request);
10350
}
10451

105-
public ArrayList<ResponseInterceptor> getResponseInterceptors() {
106-
return mResponseInterceptors;
107-
}
108-
109-
public interface RequestInterceptor {
110-
Request intercept(Request request) throws IOException;
111-
}
112-
113-
public interface ResponseInterceptor {
114-
Response intercept(Response response) throws IOException;
115-
}
11652
}

0 commit comments

Comments
 (0)