Skip to content

Commit

Permalink
Accept multiple headers through OnAuthHeaderRequestedListener (#2080)
Browse files Browse the repository at this point in the history
  • Loading branch information
khaykov authored Apr 8, 2020
1 parent c5d485a commit b693f14
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package org.wordpress.mobile.WPAndroidGlue;

import android.text.TextUtils;

import org.wordpress.mobile.WPAndroidGlue.WPAndroidGlueCode.OnAuthHeaderRequestedListener;

import java.io.IOException;
import java.util.Map;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class OkHttpHeaderInterceptor implements Interceptor {
private static final String AUTHORIZATION_HEADER_KEY = "Authorization";

private OnAuthHeaderRequestedListener mOnAuthHeaderRequestedListener;

void setOnAuthHeaderRequestedListener(OnAuthHeaderRequestedListener onAuthHeaderRequestedListener) {
Expand All @@ -23,10 +20,13 @@ void setOnAuthHeaderRequestedListener(OnAuthHeaderRequestedListener onAuthHeader
public Response intercept(Chain chain) throws IOException {
Request.Builder builder = chain.request().newBuilder();

String authHeader = mOnAuthHeaderRequestedListener != null
Map<String, String> authHeaders = mOnAuthHeaderRequestedListener != null
? mOnAuthHeaderRequestedListener.onAuthHeaderRequested(chain.request().url().toString()) : null;
if (!TextUtils.isEmpty(authHeader)) {
builder.addHeader(AUTHORIZATION_HEADER_KEY, authHeader);

if (authHeaders != null) {
for (Map.Entry<String, String> entry : authHeaders.entrySet()) {
builder.addHeader(entry.getKey(), entry.getValue());
}
}

return chain.proceed(builder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public interface OnEditorMountListener {
}

public interface OnAuthHeaderRequestedListener {
String onAuthHeaderRequested(String url);
Map<String, String> onAuthHeaderRequested(String url);
}

public interface OnEditorAutosaveListener {
Expand Down

0 comments on commit b693f14

Please sign in to comment.