|
| 1 | +/* |
| 2 | + * Copyright (C) 2017 Square, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package okhttp3.recipes; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.net.InetAddress; |
| 20 | +import java.net.InetSocketAddress; |
| 21 | +import java.net.Proxy; |
| 22 | +import java.util.List; |
| 23 | +import java.util.concurrent.atomic.AtomicLong; |
| 24 | +import okhttp3.Call; |
| 25 | +import okhttp3.Callback; |
| 26 | +import okhttp3.Connection; |
| 27 | +import okhttp3.EventListener; |
| 28 | +import okhttp3.Handshake; |
| 29 | +import okhttp3.OkHttpClient; |
| 30 | +import okhttp3.Protocol; |
| 31 | +import okhttp3.Request; |
| 32 | +import okhttp3.Response; |
| 33 | +import okhttp3.ResponseBody; |
| 34 | + |
| 35 | +public final class PrintEvents { |
| 36 | + private final OkHttpClient client = new OkHttpClient.Builder() |
| 37 | + .eventListenerFactory(PrintingEventListener.FACTORY) |
| 38 | + .build(); |
| 39 | + |
| 40 | + public void run() throws Exception { |
| 41 | + Request washingtonPostRequest = new Request.Builder() |
| 42 | + .url("https://www.washingtonpost.com/") |
| 43 | + .build(); |
| 44 | + client.newCall(washingtonPostRequest).enqueue(new Callback() { |
| 45 | + @Override public void onFailure(Call call, IOException e) { |
| 46 | + } |
| 47 | + |
| 48 | + @Override public void onResponse(Call call, Response response) throws IOException { |
| 49 | + try (ResponseBody body = response.body()) { |
| 50 | + // Consume and discard the response body. |
| 51 | + body.source().readByteString(); |
| 52 | + } |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + Request newYorkTimesRequest = new Request.Builder() |
| 57 | + .url("https://www.nytimes.com/") |
| 58 | + .build(); |
| 59 | + client.newCall(newYorkTimesRequest).enqueue(new Callback() { |
| 60 | + @Override public void onFailure(Call call, IOException e) { |
| 61 | + } |
| 62 | + |
| 63 | + @Override public void onResponse(Call call, Response response) throws IOException { |
| 64 | + try (ResponseBody body = response.body()) { |
| 65 | + // Consume and discard the response body. |
| 66 | + body.source().readByteString(); |
| 67 | + } |
| 68 | + } |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + public static void main(String... args) throws Exception { |
| 73 | + new PrintEvents().run(); |
| 74 | + } |
| 75 | + |
| 76 | + private static final class PrintingEventListener extends EventListener { |
| 77 | + private static final Factory FACTORY = new Factory() { |
| 78 | + final AtomicLong nextCallId = new AtomicLong(1L); |
| 79 | + |
| 80 | + @Override public EventListener create(Call call) { |
| 81 | + long callId = nextCallId.getAndIncrement(); |
| 82 | + System.out.printf("%04d %s%n", callId, call.request().url()); |
| 83 | + return new PrintingEventListener(callId, System.nanoTime()); |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + final long callId; |
| 88 | + final long callStartNanos; |
| 89 | + |
| 90 | + public PrintingEventListener(long callId, long callStartNanos) { |
| 91 | + this.callId = callId; |
| 92 | + this.callStartNanos = callStartNanos; |
| 93 | + } |
| 94 | + |
| 95 | + private void printEvent(String name) { |
| 96 | + long elapsedNanos = System.nanoTime() - callStartNanos; |
| 97 | + System.out.printf("%04d %.3f %s%n", callId, elapsedNanos / 1000000000d, name); |
| 98 | + } |
| 99 | + |
| 100 | + @Override public void callStart(Call call) { |
| 101 | + printEvent("callStart"); |
| 102 | + } |
| 103 | + |
| 104 | + @Override public void dnsStart(Call call, String domainName) { |
| 105 | + printEvent("dnsStart"); |
| 106 | + } |
| 107 | + |
| 108 | + @Override public void dnsEnd(Call call, String domainName, List<InetAddress> inetAddressList) { |
| 109 | + printEvent("dnsEnd"); |
| 110 | + } |
| 111 | + |
| 112 | + @Override public void connectStart( |
| 113 | + Call call, InetSocketAddress inetSocketAddress, Proxy proxy) { |
| 114 | + printEvent("connectStart"); |
| 115 | + } |
| 116 | + |
| 117 | + @Override public void secureConnectStart(Call call) { |
| 118 | + printEvent("secureConnectStart"); |
| 119 | + } |
| 120 | + |
| 121 | + @Override public void secureConnectEnd(Call call, Handshake handshake) { |
| 122 | + printEvent("secureConnectEnd"); |
| 123 | + } |
| 124 | + |
| 125 | + @Override public void connectEnd( |
| 126 | + Call call, InetSocketAddress inetSocketAddress, Proxy proxy, Protocol protocol) { |
| 127 | + printEvent("connectEnd"); |
| 128 | + } |
| 129 | + |
| 130 | + @Override public void connectFailed(Call call, InetSocketAddress inetSocketAddress, Proxy proxy, |
| 131 | + Protocol protocol, IOException ioe) { |
| 132 | + printEvent("connectFailed"); |
| 133 | + } |
| 134 | + |
| 135 | + @Override public void connectionAcquired(Call call, Connection connection) { |
| 136 | + printEvent("connectionAcquired"); |
| 137 | + } |
| 138 | + |
| 139 | + @Override public void connectionReleased(Call call, Connection connection) { |
| 140 | + printEvent("connectionReleased"); |
| 141 | + } |
| 142 | + |
| 143 | + @Override public void requestHeadersStart(Call call) { |
| 144 | + printEvent("requestHeadersStart"); |
| 145 | + } |
| 146 | + |
| 147 | + @Override public void requestHeadersEnd(Call call, Request request) { |
| 148 | + printEvent("requestHeadersEnd"); |
| 149 | + } |
| 150 | + |
| 151 | + @Override public void requestBodyStart(Call call) { |
| 152 | + printEvent("requestBodyStart"); |
| 153 | + } |
| 154 | + |
| 155 | + @Override public void requestBodyEnd(Call call, long byteCount) { |
| 156 | + printEvent("requestBodyEnd"); |
| 157 | + } |
| 158 | + |
| 159 | + @Override public void responseHeadersStart(Call call) { |
| 160 | + printEvent("responseHeadersStart"); |
| 161 | + } |
| 162 | + |
| 163 | + @Override public void responseHeadersEnd(Call call, Response response) { |
| 164 | + printEvent("responseHeadersEnd"); |
| 165 | + } |
| 166 | + |
| 167 | + @Override public void responseBodyStart(Call call) { |
| 168 | + printEvent("responseBodyStart"); |
| 169 | + } |
| 170 | + |
| 171 | + @Override public void responseBodyEnd(Call call, long byteCount) { |
| 172 | + printEvent("responseBodyEnd"); |
| 173 | + } |
| 174 | + |
| 175 | + @Override public void callEnd(Call call) { |
| 176 | + printEvent("callEnd"); |
| 177 | + } |
| 178 | + |
| 179 | + @Override public void callFailed(Call call, IOException ioe) { |
| 180 | + printEvent("callFailed"); |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments