Last active
January 26, 2018 08:10
-
-
Save NightlyNexus/d889bf334e876c861c173d9bf722643b to your computer and use it in GitHub Desktop.
A DelegatingEventListenerFactory for OkHttp to allow new OkHttpClient instances to add onto a base client's EventListener.Factory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2018 Eric Cochran | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
import java.io.IOException; | |
import java.net.InetAddress; | |
import java.net.InetSocketAddress; | |
import java.net.Proxy; | |
import java.util.List; | |
import javax.annotation.Nullable; | |
import okhttp3.Call; | |
import okhttp3.Connection; | |
import okhttp3.EventListener; | |
import okhttp3.Handshake; | |
import okhttp3.Protocol; | |
import okhttp3.Request; | |
import okhttp3.Response; | |
public final class DelegatingEventListenerFactory implements EventListener.Factory { | |
private final EventListener.Factory[] factories; | |
public DelegatingEventListenerFactory(EventListener.Factory... factories) { | |
this.factories = factories.clone(); | |
} | |
@Override public EventListener create(Call call) { | |
int size = factories.length; | |
EventListener[] eventListeners = new EventListener[size]; | |
for (int i = 0; i < size; i++) { | |
EventListener.Factory factory = factories[i]; | |
eventListeners[i] = factory.create(call); | |
} | |
return new DelegatingEventListener(eventListeners); | |
} | |
private static final class DelegatingEventListener extends EventListener { | |
private final EventListener[] eventListeners; | |
DelegatingEventListener(EventListener[] eventListeners) { | |
this.eventListeners = eventListeners; | |
} | |
@Override public void callStart(Call call) { | |
for (EventListener eventListener : eventListeners) { | |
eventListener.callStart(call); | |
} | |
} | |
@Override public void dnsStart(Call call, String domainName) { | |
for (EventListener eventListener : eventListeners) { | |
eventListener.dnsStart(call, domainName); | |
} | |
} | |
@Override | |
public void dnsEnd(Call call, String domainName, List<InetAddress> inetAddressList) { | |
for (EventListener eventListener : eventListeners) { | |
eventListener.dnsEnd(call, domainName, inetAddressList); | |
} | |
} | |
@Override | |
public void connectStart(Call call, InetSocketAddress inetSocketAddress, Proxy proxy) { | |
for (EventListener eventListener : eventListeners) { | |
eventListener.connectStart(call, inetSocketAddress, proxy); | |
} | |
} | |
@Override public void secureConnectStart(Call call) { | |
for (EventListener eventListener : eventListeners) { | |
eventListener.secureConnectStart(call); | |
} | |
} | |
@Override public void secureConnectEnd(Call call, @Nullable Handshake handshake) { | |
for (EventListener eventListener : eventListeners) { | |
eventListener.secureConnectEnd(call, handshake); | |
} | |
} | |
@Override | |
public void connectEnd(Call call, InetSocketAddress inetSocketAddress, Proxy proxy, | |
@Nullable Protocol protocol) { | |
for (EventListener eventListener : eventListeners) { | |
eventListener.connectEnd(call, inetSocketAddress, proxy, protocol); | |
} | |
} | |
@Override | |
public void connectFailed(Call call, InetSocketAddress inetSocketAddress, Proxy proxy, | |
@Nullable Protocol protocol, IOException ioe) { | |
for (EventListener eventListener : eventListeners) { | |
eventListener.connectFailed(call, inetSocketAddress, proxy, protocol, ioe); | |
} | |
} | |
@Override public void connectionAcquired(Call call, Connection connection) { | |
for (EventListener eventListener : eventListeners) { | |
eventListener.connectionAcquired(call, connection); | |
} | |
} | |
@Override public void connectionReleased(Call call, Connection connection) { | |
for (EventListener eventListener : eventListeners) { | |
eventListener.connectionReleased(call, connection); | |
} | |
} | |
@Override public void requestHeadersStart(Call call) { | |
for (EventListener eventListener : eventListeners) { | |
eventListener.requestHeadersStart(call); | |
} | |
} | |
@Override public void requestHeadersEnd(Call call, Request request) { | |
for (EventListener eventListener : eventListeners) { | |
eventListener.requestHeadersEnd(call, request); | |
} | |
} | |
@Override public void requestBodyStart(Call call) { | |
for (EventListener eventListener : eventListeners) { | |
eventListener.requestBodyStart(call); | |
} | |
} | |
@Override public void requestBodyEnd(Call call, long byteCount) { | |
for (EventListener eventListener : eventListeners) { | |
eventListener.requestBodyEnd(call, byteCount); | |
} | |
} | |
@Override public void responseHeadersStart(Call call) { | |
for (EventListener eventListener : eventListeners) { | |
eventListener.responseHeadersStart(call); | |
} | |
} | |
@Override public void responseHeadersEnd(Call call, Response response) { | |
for (EventListener eventListener : eventListeners) { | |
eventListener.responseHeadersEnd(call, response); | |
} | |
} | |
@Override public void responseBodyStart(Call call) { | |
for (EventListener eventListener : eventListeners) { | |
eventListener.responseBodyStart(call); | |
} | |
} | |
@Override public void responseBodyEnd(Call call, long byteCount) { | |
for (EventListener eventListener : eventListeners) { | |
eventListener.responseBodyEnd(call, byteCount); | |
} | |
} | |
@Override public void callEnd(Call call) { | |
for (EventListener eventListener : eventListeners) { | |
eventListener.callEnd(call); | |
} | |
} | |
@Override public void callFailed(Call call, IOException ioe) { | |
for (EventListener eventListener : eventListeners) { | |
eventListener.callFailed(call, ioe); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2018 Eric Cochran | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
import java.io.IOException; | |
import okhttp3.Call; | |
import okhttp3.EventListener; | |
import okhttp3.OkHttpClient; | |
final class Main { | |
static OkHttpClient provideBaseClient() { | |
return new OkHttpClient.Builder().eventListener(new EventListener() { | |
@Override public void responseBodyEnd(Call call, long byteCount) { | |
// Log byteCount. | |
} | |
}).build(); | |
} | |
static OkHttpClient provideImageClient(OkHttpClient base) { | |
return base.newBuilder() | |
.eventListenerFactory(appendFromClient(base, new EventListener() { | |
@Override public void callFailed(Call call, IOException ioe) { | |
// Log failed call. | |
} | |
})) | |
.build(); | |
} | |
static EventListener.Factory appendFromClient(OkHttpClient client, EventListener... listeners) { | |
int size = listeners.length; | |
EventListener.Factory[] factories = new EventListener.Factory[size + 1]; | |
factories[0] = client.eventListenerFactory(); | |
for (int i = 1; i < size + 1; i++) { | |
factories[i] = factory(listeners[i]); | |
} | |
return new DelegatingEventListenerFactory(factories); | |
} | |
static EventListener.Factory factory(final EventListener listener) { | |
return new EventListener.Factory() { | |
@Override public EventListener create(Call call) { | |
return listener; | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment