From 039bcf92dbff48d1162016f8cf4ba08a99071717 Mon Sep 17 00:00:00 2001 From: Jake Wharton Date: Tue, 13 Sep 2016 12:18:08 -0400 Subject: [PATCH] Move WebSockets from separate artifact to main. The factory method has moved to OkHttpClient and WebSocketCall has changed to an interface. --- mockwebserver/pom.xml | 5 -- .../okhttp3/mockwebserver/MockResponse.java | 2 +- .../okhttp3/mockwebserver/MockWebServer.java | 2 +- .../fuzzingserver-config.json | 0 .../fuzzingserver-expected.txt | 0 .../fuzzingserver-test.sh | 2 +- .../fuzzingserver-update-expected.sh | 0 okhttp-tests/pom.xml | 22 ++++++ .../main/java/okhttp3}/AutobahnTester.java | 13 +--- .../test/java/okhttp3/RecordedResponse.java | 1 - .../test/java/okhttp3}/WebSocketCallTest.java | 13 +--- .../test/java/okhttp3}/WebSocketRecorder.java | 8 +- .../internal/ws/RealWebSocketTest.java | 6 +- .../internal/ws/WebSocketReaderTest.java | 4 +- .../internal/ws/WebSocketWriterTest.java | 0 okhttp-ws-tests/pom.xml | 76 ------------------- okhttp-ws/README.md | 22 ------ okhttp-ws/pom.xml | 37 --------- .../src/main/java/okhttp3/OkHttpClient.java | 19 +++-- okhttp/src/main/java/okhttp3/RealCall.java | 11 +-- .../main/java/okhttp3/RealWebSocketCall.java | 52 +++---------- .../src/main/java/okhttp3}/WebSocket.java | 4 +- .../src/main/java/okhttp3/WebSocketCall.java | 39 ++++++++++ .../main/java/okhttp3}/WebSocketListener.java | 4 +- .../main/java/okhttp3/internal/Internal.java | 5 -- .../http/RetryAndFollowUpInterceptor.java | 9 +-- .../okhttp3/internal/ws/RealWebSocket.java | 4 +- .../internal/ws/WebSocketProtocol.java | 0 .../okhttp3/internal/ws/WebSocketReader.java | 2 +- .../okhttp3/internal/ws/WebSocketWriter.java | 0 pom.xml | 3 - samples/guide/pom.xml | 5 -- .../java/okhttp3/recipes/WebSocketEcho.java | 16 ++-- 33 files changed, 119 insertions(+), 267 deletions(-) rename {okhttp-ws-tests => okhttp-tests}/fuzzingserver-config.json (100%) rename {okhttp-ws-tests => okhttp-tests}/fuzzingserver-expected.txt (100%) rename {okhttp-ws-tests => okhttp-tests}/fuzzingserver-test.sh (90%) rename {okhttp-ws-tests => okhttp-tests}/fuzzingserver-update-expected.sh (100%) rename {okhttp-ws-tests/src/main/java/okhttp3/ws => okhttp-tests/src/main/java/okhttp3}/AutobahnTester.java (95%) rename {okhttp-ws-tests/src/test/java/okhttp3/ws => okhttp-tests/src/test/java/okhttp3}/WebSocketCallTest.java (96%) rename {okhttp-ws-tests/src/test/java/okhttp3/ws => okhttp-tests/src/test/java/okhttp3}/WebSocketRecorder.java (97%) rename {okhttp-ws-tests => okhttp-tests}/src/test/java/okhttp3/internal/ws/RealWebSocketTest.java (99%) rename {okhttp-ws-tests => okhttp-tests}/src/test/java/okhttp3/internal/ws/WebSocketReaderTest.java (99%) rename {okhttp-ws-tests => okhttp-tests}/src/test/java/okhttp3/internal/ws/WebSocketWriterTest.java (100%) delete mode 100644 okhttp-ws-tests/pom.xml delete mode 100644 okhttp-ws/README.md delete mode 100644 okhttp-ws/pom.xml rename okhttp-ws/src/main/java/okhttp3/ws/WebSocketCall.java => okhttp/src/main/java/okhttp3/RealWebSocketCall.java (74%) rename {okhttp-ws/src/main/java/okhttp3/ws => okhttp/src/main/java/okhttp3}/WebSocket.java (97%) create mode 100644 okhttp/src/main/java/okhttp3/WebSocketCall.java rename {okhttp-ws/src/main/java/okhttp3/ws => okhttp/src/main/java/okhttp3}/WebSocketListener.java (97%) rename {okhttp-ws => okhttp}/src/main/java/okhttp3/internal/ws/RealWebSocket.java (99%) rename {okhttp-ws => okhttp}/src/main/java/okhttp3/internal/ws/WebSocketProtocol.java (100%) rename {okhttp-ws => okhttp}/src/main/java/okhttp3/internal/ws/WebSocketReader.java (99%) rename {okhttp-ws => okhttp}/src/main/java/okhttp3/internal/ws/WebSocketWriter.java (100%) diff --git a/mockwebserver/pom.xml b/mockwebserver/pom.xml index 754d604aeee4..9f68096de5fc 100644 --- a/mockwebserver/pom.xml +++ b/mockwebserver/pom.xml @@ -24,11 +24,6 @@ ${project.version} test - - ${project.groupId} - okhttp-ws - ${project.version} - org.bouncycastle bcprov-jdk15on diff --git a/mockwebserver/src/main/java/okhttp3/mockwebserver/MockResponse.java b/mockwebserver/src/main/java/okhttp3/mockwebserver/MockResponse.java index 26123f256840..8452b8463404 100644 --- a/mockwebserver/src/main/java/okhttp3/mockwebserver/MockResponse.java +++ b/mockwebserver/src/main/java/okhttp3/mockwebserver/MockResponse.java @@ -21,7 +21,7 @@ import okhttp3.Headers; import okhttp3.internal.Internal; import okhttp3.internal.http2.Settings; -import okhttp3.ws.WebSocketListener; +import okhttp3.WebSocketListener; import okio.Buffer; /** A scripted response to be replayed by the mock web server. */ diff --git a/mockwebserver/src/main/java/okhttp3/mockwebserver/MockWebServer.java b/mockwebserver/src/main/java/okhttp3/mockwebserver/MockWebServer.java index a745efe73491..772f6ea8ba9a 100644 --- a/mockwebserver/src/main/java/okhttp3/mockwebserver/MockWebServer.java +++ b/mockwebserver/src/main/java/okhttp3/mockwebserver/MockWebServer.java @@ -70,7 +70,7 @@ import okhttp3.internal.platform.Platform; import okhttp3.internal.ws.RealWebSocket; import okhttp3.internal.ws.WebSocketProtocol; -import okhttp3.ws.WebSocketListener; +import okhttp3.WebSocketListener; import okio.Buffer; import okio.BufferedSink; import okio.BufferedSource; diff --git a/okhttp-ws-tests/fuzzingserver-config.json b/okhttp-tests/fuzzingserver-config.json similarity index 100% rename from okhttp-ws-tests/fuzzingserver-config.json rename to okhttp-tests/fuzzingserver-config.json diff --git a/okhttp-ws-tests/fuzzingserver-expected.txt b/okhttp-tests/fuzzingserver-expected.txt similarity index 100% rename from okhttp-ws-tests/fuzzingserver-expected.txt rename to okhttp-tests/fuzzingserver-expected.txt diff --git a/okhttp-ws-tests/fuzzingserver-test.sh b/okhttp-tests/fuzzingserver-test.sh similarity index 90% rename from okhttp-ws-tests/fuzzingserver-test.sh rename to okhttp-tests/fuzzingserver-test.sh index af89a424227d..78ad71f59e55 100755 --- a/okhttp-ws-tests/fuzzingserver-test.sh +++ b/okhttp-tests/fuzzingserver-test.sh @@ -21,7 +21,7 @@ set -ex wstest -m fuzzingserver -s fuzzingserver-config.json & sleep 2 # wait for wstest to start -java -jar target/okhttp-ws-tests-*-jar-with-dependencies.jar +java -jar target/okhttp-tests-*-jar-with-dependencies.jar jq '.[] as $in | $in | keys[] | . + " " + $in[.].behavior' target/fuzzingserver-report/index.json > target/fuzzingserver-actual.txt diff --git a/okhttp-ws-tests/fuzzingserver-update-expected.sh b/okhttp-tests/fuzzingserver-update-expected.sh similarity index 100% rename from okhttp-ws-tests/fuzzingserver-update-expected.sh rename to okhttp-tests/fuzzingserver-update-expected.sh diff --git a/okhttp-tests/pom.xml b/okhttp-tests/pom.xml index 7202df85921c..4b3a04ac78e0 100644 --- a/okhttp-tests/pom.xml +++ b/okhttp-tests/pom.xml @@ -49,6 +49,28 @@ + + org.apache.maven.plugins + maven-assembly-plugin + + + jar-with-dependencies + + + + okhttp3.AutobahnTester + + + + + + package + + single + + + + org.apache.maven.plugins diff --git a/okhttp-ws-tests/src/main/java/okhttp3/ws/AutobahnTester.java b/okhttp-tests/src/main/java/okhttp3/AutobahnTester.java similarity index 95% rename from okhttp-ws-tests/src/main/java/okhttp3/ws/AutobahnTester.java rename to okhttp-tests/src/main/java/okhttp3/AutobahnTester.java index 707f58c1169f..bb4afa55f128 100644 --- a/okhttp-ws-tests/src/main/java/okhttp3/ws/AutobahnTester.java +++ b/okhttp-tests/src/main/java/okhttp3/AutobahnTester.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package okhttp3.ws; +package okhttp3; import java.io.IOException; import java.util.concurrent.CountDownLatch; @@ -22,17 +22,12 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.RequestBody; -import okhttp3.Response; -import okhttp3.ResponseBody; import okhttp3.internal.Version; import okio.Buffer; import okio.BufferedSource; -import static okhttp3.ws.WebSocket.BINARY; -import static okhttp3.ws.WebSocket.TEXT; +import static okhttp3.WebSocket.BINARY; +import static okhttp3.WebSocket.TEXT; /** * Exercises the web socket implementation against the responseRef = new AtomicReference<>(); final AtomicReference webSocketRef = new AtomicReference<>(); diff --git a/okhttp-ws-tests/src/test/java/okhttp3/ws/WebSocketRecorder.java b/okhttp-tests/src/test/java/okhttp3/WebSocketRecorder.java similarity index 97% rename from okhttp-ws-tests/src/test/java/okhttp3/ws/WebSocketRecorder.java rename to okhttp-tests/src/test/java/okhttp3/WebSocketRecorder.java index 485a4a1e88c1..7605a803cb47 100644 --- a/okhttp-ws-tests/src/test/java/okhttp3/ws/WebSocketRecorder.java +++ b/okhttp-tests/src/test/java/okhttp3/WebSocketRecorder.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package okhttp3.ws; +package okhttp3; import java.io.IOException; import java.util.concurrent.BlockingQueue; @@ -22,11 +22,13 @@ import okhttp3.MediaType; import okhttp3.Response; import okhttp3.ResponseBody; +import okhttp3.WebSocket; +import okhttp3.WebSocketListener; import okhttp3.internal.ws.WebSocketReader; import okio.Buffer; -import static okhttp3.ws.WebSocket.BINARY; -import static okhttp3.ws.WebSocket.TEXT; +import static okhttp3.WebSocket.BINARY; +import static okhttp3.WebSocket.TEXT; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; diff --git a/okhttp-ws-tests/src/test/java/okhttp3/internal/ws/RealWebSocketTest.java b/okhttp-tests/src/test/java/okhttp3/internal/ws/RealWebSocketTest.java similarity index 99% rename from okhttp-ws-tests/src/test/java/okhttp3/internal/ws/RealWebSocketTest.java rename to okhttp-tests/src/test/java/okhttp3/internal/ws/RealWebSocketTest.java index 90a803ec1427..0cc3e75190c2 100644 --- a/okhttp-ws-tests/src/test/java/okhttp3/internal/ws/RealWebSocketTest.java +++ b/okhttp-tests/src/test/java/okhttp3/internal/ws/RealWebSocketTest.java @@ -22,7 +22,7 @@ import java.util.concurrent.Executor; import okhttp3.MediaType; import okhttp3.RequestBody; -import okhttp3.ws.WebSocketRecorder; +import okhttp3.WebSocketRecorder; import okio.Buffer; import okio.BufferedSink; import okio.BufferedSource; @@ -35,8 +35,8 @@ import org.junit.Before; import org.junit.Test; -import static okhttp3.ws.WebSocket.BINARY; -import static okhttp3.ws.WebSocket.TEXT; +import static okhttp3.WebSocket.BINARY; +import static okhttp3.WebSocket.TEXT; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; diff --git a/okhttp-ws-tests/src/test/java/okhttp3/internal/ws/WebSocketReaderTest.java b/okhttp-tests/src/test/java/okhttp3/internal/ws/WebSocketReaderTest.java similarity index 99% rename from okhttp-ws-tests/src/test/java/okhttp3/internal/ws/WebSocketReaderTest.java rename to okhttp-tests/src/test/java/okhttp3/internal/ws/WebSocketReaderTest.java index 6bb7ef86d83a..9d0c347d245a 100644 --- a/okhttp-ws-tests/src/test/java/okhttp3/internal/ws/WebSocketReaderTest.java +++ b/okhttp-tests/src/test/java/okhttp3/internal/ws/WebSocketReaderTest.java @@ -23,8 +23,8 @@ import java.util.regex.Pattern; import okhttp3.ResponseBody; import okhttp3.internal.Util; -import okhttp3.ws.WebSocketRecorder; -import okhttp3.ws.WebSocketRecorder.MessageDelegate; +import okhttp3.WebSocketRecorder; +import okhttp3.WebSocketRecorder.MessageDelegate; import okio.Buffer; import okio.BufferedSource; import okio.ByteString; diff --git a/okhttp-ws-tests/src/test/java/okhttp3/internal/ws/WebSocketWriterTest.java b/okhttp-tests/src/test/java/okhttp3/internal/ws/WebSocketWriterTest.java similarity index 100% rename from okhttp-ws-tests/src/test/java/okhttp3/internal/ws/WebSocketWriterTest.java rename to okhttp-tests/src/test/java/okhttp3/internal/ws/WebSocketWriterTest.java diff --git a/okhttp-ws-tests/pom.xml b/okhttp-ws-tests/pom.xml deleted file mode 100644 index d8a43e09a8eb..000000000000 --- a/okhttp-ws-tests/pom.xml +++ /dev/null @@ -1,76 +0,0 @@ - - - - 4.0.0 - - - com.squareup.okhttp3 - parent - 3.5.0-SNAPSHOT - - - okhttp-ws-tests - OkHttp Web Socket Tests - - - - ${project.groupId} - okhttp-ws - ${project.version} - - - - ${project.groupId} - okhttp-testing-support - ${project.version} - test - - - - junit - junit - test - - - ${project.groupId} - mockwebserver - ${project.version} - test - - - - - - - org.apache.maven.plugins - maven-assembly-plugin - - - jar-with-dependencies - - - - okhttp3.ws.AutobahnTester - - - - - - package - - single - - - - - - - org.apache.maven.plugins - maven-deploy-plugin - - true - - - - - diff --git a/okhttp-ws/README.md b/okhttp-ws/README.md deleted file mode 100644 index 054ea91fbf55..000000000000 --- a/okhttp-ws/README.md +++ /dev/null @@ -1,22 +0,0 @@ -OkHttp Web Sockets -================== - -RFC6455-compliant web socket implementation. - -Create a `WebSocketCall` with a `Request` and an `OkHttpClient` instance. -```java -WebSocketCall call = WebSocketCall.create(client, request); -``` - -A `WebSocketListener` will notify of the initial connection, server-sent messages, and any failures -on the connection. - -Start the web socket by calling `enqueue` on `WebSocketCall` with the `WebSocketListener`. -```java -call.enqueue(new WebSocketListener() { - // ... -}); -``` - -*Note: This module's API should be considered experimental and may be subject to breaking changes -in future releases.* diff --git a/okhttp-ws/pom.xml b/okhttp-ws/pom.xml deleted file mode 100644 index 649b117f62fb..000000000000 --- a/okhttp-ws/pom.xml +++ /dev/null @@ -1,37 +0,0 @@ - - - - 4.0.0 - - - com.squareup.okhttp3 - parent - 3.5.0-SNAPSHOT - - - okhttp-ws - OkHttp Web Sockets - - - - ${project.groupId} - okhttp - ${project.version} - - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - - okhttp3.internal.* - - http://square.github.io/okhttp/javadoc/ - - - - - - diff --git a/okhttp/src/main/java/okhttp3/OkHttpClient.java b/okhttp/src/main/java/okhttp3/OkHttpClient.java index 7e26bcd6ed30..999015996bd0 100644 --- a/okhttp/src/main/java/okhttp3/OkHttpClient.java +++ b/okhttp/src/main/java/okhttp3/OkHttpClient.java @@ -115,7 +115,7 @@ *

OkHttp also uses daemon threads for HTTP/2 connections. These will exit automatically if they * remain idle. */ -public class OkHttpClient implements Cloneable, Call.Factory { +public class OkHttpClient implements Cloneable, Call.Factory, WebSocketCall.Factory { private static final List DEFAULT_PROTOCOLS = Util.immutableList( Protocol.HTTP_2, Protocol.HTTP_1_1); @@ -154,10 +154,6 @@ public class OkHttpClient implements Cloneable, Call.Factory { return connectionPool.routeDatabase; } - @Override public StreamAllocation callEngineGetStreamAllocation(Call call) { - return ((RealCall) call).streamAllocation(); - } - @Override public void apply(ConnectionSpec tlsConfiguration, SSLSocket sslSocket, boolean isFallback) { tlsConfiguration.apply(sslSocket, isFallback); @@ -167,10 +163,6 @@ public void apply(ConnectionSpec tlsConfiguration, SSLSocket sslSocket, boolean throws MalformedURLException, UnknownHostException { return HttpUrl.getChecked(url); } - - @Override public void setCallWebSocket(Call call) { - ((RealCall) call).setForWebSocket(); - } }; } @@ -385,7 +377,14 @@ public List networkInterceptors() { * Prepares the {@code request} to be executed at some point in the future. */ @Override public Call newCall(Request request) { - return new RealCall(this, request); + return new RealCall(this, request, false /* for web socket */); + } + + /** + * Prepares the {@code request} to create a web socket at some point in the future. + */ + @Override public WebSocketCall newWebSocketCall(Request request) { + return new RealWebSocketCall(this, request); } public Builder newBuilder() { diff --git a/okhttp/src/main/java/okhttp3/RealCall.java b/okhttp/src/main/java/okhttp3/RealCall.java index 7545b508b33c..877faa225c63 100644 --- a/okhttp/src/main/java/okhttp3/RealCall.java +++ b/okhttp/src/main/java/okhttp3/RealCall.java @@ -40,10 +40,10 @@ final class RealCall implements Call { /** The application's original request unadulterated by redirects or auth headers. */ Request originalRequest; - protected RealCall(OkHttpClient client, Request originalRequest) { + RealCall(OkHttpClient client, Request originalRequest, boolean forWebSocket) { this.client = client; this.originalRequest = originalRequest; - this.retryAndFollowUpInterceptor = new RetryAndFollowUpInterceptor(client); + this.retryAndFollowUpInterceptor = new RetryAndFollowUpInterceptor(client, forWebSocket); } @Override public Request request() { @@ -71,11 +71,6 @@ private void captureCallStackTrace() { retryAndFollowUpInterceptor.setCallStackTrace(callStackTrace); } - synchronized void setForWebSocket() { - if (executed) throw new IllegalStateException("Already Executed"); - this.retryAndFollowUpInterceptor.setForWebSocket(true); - } - @Override public void enqueue(Callback responseCallback) { synchronized (this) { if (executed) throw new IllegalStateException("Already Executed"); @@ -99,7 +94,7 @@ synchronized void setForWebSocket() { @SuppressWarnings("CloneDoesntCallSuperClone") // We are a final type & this saves clearing state. @Override public RealCall clone() { - return new RealCall(client, originalRequest); + return new RealCall(client, originalRequest, retryAndFollowUpInterceptor.isForWebSocket()); } StreamAllocation streamAllocation() { diff --git a/okhttp-ws/src/main/java/okhttp3/ws/WebSocketCall.java b/okhttp/src/main/java/okhttp3/RealWebSocketCall.java similarity index 74% rename from okhttp-ws/src/main/java/okhttp3/ws/WebSocketCall.java rename to okhttp/src/main/java/okhttp3/RealWebSocketCall.java index bdfd3dbb515a..40ba14344335 100644 --- a/okhttp-ws/src/main/java/okhttp3/ws/WebSocketCall.java +++ b/okhttp/src/main/java/okhttp3/RealWebSocketCall.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package okhttp3.ws; +package okhttp3; import java.io.IOException; import java.net.ProtocolException; @@ -23,13 +23,6 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.ThreadPoolExecutor; -import okhttp3.Call; -import okhttp3.Callback; -import okhttp3.OkHttpClient; -import okhttp3.Protocol; -import okhttp3.Request; -import okhttp3.Response; -import okhttp3.internal.Internal; import okhttp3.internal.Util; import okhttp3.internal.connection.StreamAllocation; import okhttp3.internal.ws.RealWebSocket; @@ -38,23 +31,16 @@ import static java.util.concurrent.TimeUnit.SECONDS; -public final class WebSocketCall { - /** - * Prepares the {@code request} to create a web socket at some point in the future. - */ - public static WebSocketCall create(OkHttpClient client, Request request) { - return new WebSocketCall(client, request); - } - - private final Call call; +final class RealWebSocketCall implements WebSocketCall { + private final RealCall call; private final Random random; private final String key; - WebSocketCall(OkHttpClient client, Request request) { + RealWebSocketCall(OkHttpClient client, Request request) { this(client, request, new SecureRandom()); } - WebSocketCall(OkHttpClient client, Request request, Random random) { + RealWebSocketCall(OkHttpClient client, Request request, Random random) { if (!"GET".equals(request.method())) { throw new IllegalArgumentException("Request must be GET: " + request.method()); } @@ -75,24 +61,12 @@ public static WebSocketCall create(OkHttpClient client, Request request) { .header("Sec-WebSocket-Version", "13") .build(); - call = client.newCall(request); + call = new RealCall(client, request, true /* for web socket */); } - /** - * Schedules the request to be executed at some point in the future. - * - *

The {@link OkHttpClient#dispatcher dispatcher} defines when the request will run: usually - * immediately unless there are several other requests currently being executed. - * - *

This client will later call back {@code responseCallback} with either an HTTP response or a - * failure exception. If you {@link #cancel} a request before it completes the callback will not - * be invoked. - * - * @throws IllegalStateException when the call has already been executed. - */ - public void enqueue(final WebSocketListener listener) { + @Override public void enqueue(final WebSocketListener listener) { Callback responseCallback = new Callback() { - @Override public void onResponse(Call call, Response response) throws IOException { + @Override public void onResponse(Call call, Response response) { try { createWebSocket(response, listener); } catch (IOException e) { @@ -104,13 +78,10 @@ public void enqueue(final WebSocketListener listener) { listener.onFailure(e, null); } }; - // TODO call.enqueue(responseCallback, true); - Internal.instance.setCallWebSocket(call); call.enqueue(responseCallback); } - /** Cancels the request, if possible. Requests that are already complete cannot be canceled. */ - public void cancel() { + @Override public void cancel() { call.cancel(); } @@ -143,9 +114,8 @@ private void createWebSocket(Response response, WebSocketListener listener) thro + "'"); } - StreamAllocation streamAllocation = Internal.instance.callEngineGetStreamAllocation(call); - RealWebSocket webSocket = StreamWebSocket.create( - streamAllocation, response, random, listener); + StreamAllocation streamAllocation = call.streamAllocation(); + RealWebSocket webSocket = StreamWebSocket.create(streamAllocation, response, random, listener); listener.onOpen(webSocket, response); diff --git a/okhttp-ws/src/main/java/okhttp3/ws/WebSocket.java b/okhttp/src/main/java/okhttp3/WebSocket.java similarity index 97% rename from okhttp-ws/src/main/java/okhttp3/ws/WebSocket.java rename to okhttp/src/main/java/okhttp3/WebSocket.java index 7435c01d7f04..7af8a76fbd32 100644 --- a/okhttp-ws/src/main/java/okhttp3/ws/WebSocket.java +++ b/okhttp/src/main/java/okhttp3/WebSocket.java @@ -13,11 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package okhttp3.ws; +package okhttp3; import java.io.IOException; -import okhttp3.MediaType; -import okhttp3.RequestBody; import okio.Buffer; /** Blocking interface to connect and write to a web socket. */ diff --git a/okhttp/src/main/java/okhttp3/WebSocketCall.java b/okhttp/src/main/java/okhttp3/WebSocketCall.java new file mode 100644 index 000000000000..a4007af174f3 --- /dev/null +++ b/okhttp/src/main/java/okhttp3/WebSocketCall.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2014 Square, Inc. + * + * 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. + */ +package okhttp3; + +public interface WebSocketCall { + /** + * Schedules the request to be executed at some point in the future. + * + *

The {@link OkHttpClient#dispatcher dispatcher} defines when the request will run: usually + * immediately unless there are several other requests currently being executed. + * + *

This client will later call back {@code responseCallback} with either an HTTP response or a + * failure exception. If you {@link #cancel} a request before it completes the callback will not + * be invoked. + * + * @throws IllegalStateException when the call has already been executed. + */ + void enqueue(WebSocketListener listener); + + /** Cancels the request, if possible. Requests that are already complete cannot be canceled. */ + void cancel(); + + interface Factory { + WebSocketCall newWebSocketCall(Request request); + } +} diff --git a/okhttp-ws/src/main/java/okhttp3/ws/WebSocketListener.java b/okhttp/src/main/java/okhttp3/WebSocketListener.java similarity index 97% rename from okhttp-ws/src/main/java/okhttp3/ws/WebSocketListener.java rename to okhttp/src/main/java/okhttp3/WebSocketListener.java index 12d5e66aab01..2798b10e6432 100644 --- a/okhttp-ws/src/main/java/okhttp3/ws/WebSocketListener.java +++ b/okhttp/src/main/java/okhttp3/WebSocketListener.java @@ -13,11 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package okhttp3.ws; +package okhttp3; import java.io.IOException; -import okhttp3.Response; -import okhttp3.ResponseBody; import okio.Buffer; /** Listener for server-initiated messages on a connected {@link WebSocket}. */ diff --git a/okhttp/src/main/java/okhttp3/internal/Internal.java b/okhttp/src/main/java/okhttp3/internal/Internal.java index 48c911a26783..6a25d3cdf87f 100644 --- a/okhttp/src/main/java/okhttp3/internal/Internal.java +++ b/okhttp/src/main/java/okhttp3/internal/Internal.java @@ -19,7 +19,6 @@ import java.net.UnknownHostException; import javax.net.ssl.SSLSocket; import okhttp3.Address; -import okhttp3.Call; import okhttp3.ConnectionPool; import okhttp3.ConnectionSpec; import okhttp3.Headers; @@ -63,8 +62,4 @@ public abstract void apply(ConnectionSpec tlsConfiguration, SSLSocket sslSocket, public abstract HttpUrl getHttpUrlChecked(String url) throws MalformedURLException, UnknownHostException; - - public abstract StreamAllocation callEngineGetStreamAllocation(Call call); - - public abstract void setCallWebSocket(Call call); } diff --git a/okhttp/src/main/java/okhttp3/internal/http/RetryAndFollowUpInterceptor.java b/okhttp/src/main/java/okhttp3/internal/http/RetryAndFollowUpInterceptor.java index 4122740e0ae5..9d0f92f062d2 100644 --- a/okhttp/src/main/java/okhttp3/internal/http/RetryAndFollowUpInterceptor.java +++ b/okhttp/src/main/java/okhttp3/internal/http/RetryAndFollowUpInterceptor.java @@ -62,13 +62,14 @@ public final class RetryAndFollowUpInterceptor implements Interceptor { private static final int MAX_FOLLOW_UPS = 20; private final OkHttpClient client; + private final boolean forWebSocket; private StreamAllocation streamAllocation; - private boolean forWebSocket; private Object callStackTrace; private volatile boolean canceled; - public RetryAndFollowUpInterceptor(OkHttpClient client) { + public RetryAndFollowUpInterceptor(OkHttpClient client, boolean forWebSocket) { this.client = client; + this.forWebSocket = forWebSocket; } /** @@ -90,10 +91,6 @@ public boolean isCanceled() { return canceled; } - public void setForWebSocket(boolean forWebSocket) { - this.forWebSocket = forWebSocket; - } - public boolean isForWebSocket() { return forWebSocket; } diff --git a/okhttp-ws/src/main/java/okhttp3/internal/ws/RealWebSocket.java b/okhttp/src/main/java/okhttp3/internal/ws/RealWebSocket.java similarity index 99% rename from okhttp-ws/src/main/java/okhttp3/internal/ws/RealWebSocket.java rename to okhttp/src/main/java/okhttp3/internal/ws/RealWebSocket.java index b9af6bdd65ee..a7f568f61a04 100644 --- a/okhttp-ws/src/main/java/okhttp3/internal/ws/RealWebSocket.java +++ b/okhttp/src/main/java/okhttp3/internal/ws/RealWebSocket.java @@ -24,8 +24,8 @@ import okhttp3.RequestBody; import okhttp3.ResponseBody; import okhttp3.internal.NamedRunnable; -import okhttp3.ws.WebSocket; -import okhttp3.ws.WebSocketListener; +import okhttp3.WebSocket; +import okhttp3.WebSocketListener; import okio.Buffer; import okio.BufferedSink; import okio.BufferedSource; diff --git a/okhttp-ws/src/main/java/okhttp3/internal/ws/WebSocketProtocol.java b/okhttp/src/main/java/okhttp3/internal/ws/WebSocketProtocol.java similarity index 100% rename from okhttp-ws/src/main/java/okhttp3/internal/ws/WebSocketProtocol.java rename to okhttp/src/main/java/okhttp3/internal/ws/WebSocketProtocol.java diff --git a/okhttp-ws/src/main/java/okhttp3/internal/ws/WebSocketReader.java b/okhttp/src/main/java/okhttp3/internal/ws/WebSocketReader.java similarity index 99% rename from okhttp-ws/src/main/java/okhttp3/internal/ws/WebSocketReader.java rename to okhttp/src/main/java/okhttp3/internal/ws/WebSocketReader.java index 8be28a2c5a09..139f787fe494 100644 --- a/okhttp-ws/src/main/java/okhttp3/internal/ws/WebSocketReader.java +++ b/okhttp/src/main/java/okhttp3/internal/ws/WebSocketReader.java @@ -20,7 +20,7 @@ import java.net.ProtocolException; import okhttp3.MediaType; import okhttp3.ResponseBody; -import okhttp3.ws.WebSocket; +import okhttp3.WebSocket; import okio.Buffer; import okio.BufferedSource; import okio.Okio; diff --git a/okhttp-ws/src/main/java/okhttp3/internal/ws/WebSocketWriter.java b/okhttp/src/main/java/okhttp3/internal/ws/WebSocketWriter.java similarity index 100% rename from okhttp-ws/src/main/java/okhttp3/internal/ws/WebSocketWriter.java rename to okhttp/src/main/java/okhttp3/internal/ws/WebSocketWriter.java diff --git a/pom.xml b/pom.xml index d769d52c27c9..1feefefb89b2 100644 --- a/pom.xml +++ b/pom.xml @@ -28,9 +28,6 @@ okhttp-testing-support okhttp-urlconnection - okhttp-ws - okhttp-ws-tests - okhttp-logging-interceptor okcurl diff --git a/samples/guide/pom.xml b/samples/guide/pom.xml index ba8e54ec8518..2d0c791c38f4 100644 --- a/samples/guide/pom.xml +++ b/samples/guide/pom.xml @@ -18,11 +18,6 @@ okhttp ${project.version} - - com.squareup.okhttp3 - okhttp-ws - ${project.version} - com.squareup.moshi moshi diff --git a/samples/guide/src/main/java/okhttp3/recipes/WebSocketEcho.java b/samples/guide/src/main/java/okhttp3/recipes/WebSocketEcho.java index 77fb4db7a3e3..882f37dc18d9 100644 --- a/samples/guide/src/main/java/okhttp3/recipes/WebSocketEcho.java +++ b/samples/guide/src/main/java/okhttp3/recipes/WebSocketEcho.java @@ -4,25 +4,23 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; - import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; import okhttp3.ResponseBody; -import okhttp3.ws.WebSocket; -import okhttp3.ws.WebSocketCall; -import okhttp3.ws.WebSocketListener; +import okhttp3.WebSocket; +import okhttp3.WebSocketListener; import okio.Buffer; import okio.ByteString; -import static okhttp3.ws.WebSocket.BINARY; -import static okhttp3.ws.WebSocket.TEXT; +import static okhttp3.WebSocket.BINARY; +import static okhttp3.WebSocket.TEXT; public final class WebSocketEcho implements WebSocketListener { private final ExecutorService writeExecutor = Executors.newSingleThreadExecutor(); - private void run() throws IOException { + private void run() { OkHttpClient client = new OkHttpClient.Builder() .readTimeout(0, TimeUnit.MILLISECONDS) .build(); @@ -30,7 +28,7 @@ private void run() throws IOException { Request request = new Request.Builder() .url("ws://echo.websocket.org") .build(); - WebSocketCall.create(client, request).enqueue(this); + client.newWebSocketCall(request).enqueue(this); // Trigger shutdown of the dispatcher's executor so this process can exit cleanly. client.dispatcher().executorService().shutdown(); @@ -74,7 +72,7 @@ private void run() throws IOException { writeExecutor.shutdown(); } - public static void main(String... args) throws IOException { + public static void main(String... args) { new WebSocketEcho().run(); } }