-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: OkHttp implementation for making HTTP calls and WebSocket conne…
…ctions
- Loading branch information
Showing
13 changed files
with
363 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
plugins { | ||
`java-library` | ||
alias(libs.plugins.lombok) | ||
alias(libs.plugins.maven.publish) | ||
} | ||
|
||
java { | ||
sourceCompatibility = JavaVersion.VERSION_1_8 | ||
targetCompatibility = JavaVersion.VERSION_1_8 | ||
} | ||
|
||
dependencies { | ||
implementation(project(":network-client-core")) | ||
implementation(libs.okhttp) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
POM_ARTIFACT_ID=network-client-okhttp | ||
POM_NAME=Default HTTP client | ||
POM_DESCRIPTION=Default implementation for HTTP client | ||
POM_PACKAGING=jar |
45 changes: 45 additions & 0 deletions
45
network-client-okhttp/src/main/java/io/ably/lib/network/OkHttpCall.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.ably.lib.network; | ||
|
||
import okhttp3.Call; | ||
import okhttp3.Response; | ||
|
||
import java.io.IOException; | ||
import java.net.ConnectException; | ||
import java.net.NoRouteToHostException; | ||
import java.net.SocketTimeoutException; | ||
import java.net.UnknownHostException; | ||
|
||
public class OkHttpCall implements HttpCall { | ||
private final Call call; | ||
|
||
public OkHttpCall(Call call) { | ||
this.call = call; | ||
} | ||
|
||
@Override | ||
public HttpResponse execute() { | ||
try (Response response = call.execute()) { | ||
return HttpResponse.builder() | ||
.headers(response.headers().toMultimap()) | ||
.code(response.code()) | ||
.message(response.message()) | ||
.body( | ||
response.body() != null && response.body().contentType() != null | ||
? new HttpBody(response.body().contentType().toString(), response.body().bytes()) | ||
: null | ||
) | ||
.build(); | ||
|
||
} catch (ConnectException | SocketTimeoutException | UnknownHostException | NoRouteToHostException fce) { | ||
throw new FailedConnectionException(fce); | ||
} catch (IOException ioe) { | ||
throw new RuntimeException(ioe); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public void cancel() { | ||
call.cancel(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
network-client-okhttp/src/main/java/io/ably/lib/network/OkHttpEngine.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.ably.lib.network; | ||
|
||
import okhttp3.Call; | ||
import okhttp3.OkHttpClient; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
public class OkHttpEngine implements HttpEngine { | ||
|
||
private final OkHttpClient client; | ||
private final HttpEngineConfig config; | ||
|
||
public OkHttpEngine(OkHttpClient client, HttpEngineConfig config) { | ||
this.client = client; | ||
this.config = config; | ||
} | ||
|
||
@Override | ||
public HttpCall call(HttpRequest request) { | ||
Call call = client.newBuilder() | ||
.connectTimeout(request.getHttpOpenTimeout(), TimeUnit.MILLISECONDS) | ||
.readTimeout(request.getHttpReadTimeout(), TimeUnit.MILLISECONDS) | ||
.build() | ||
.newCall(OkHttpUtils.toOkhttpRequest(request)); | ||
return new OkHttpCall(call); | ||
} | ||
|
||
@Override | ||
public boolean isUsingProxy() { | ||
return config.getProxy() != null; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
network-client-okhttp/src/main/java/io/ably/lib/network/OkHttpEngineFactory.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.ably.lib.network; | ||
|
||
import okhttp3.OkHttpClient; | ||
|
||
public class OkHttpEngineFactory implements HttpEngineFactory { | ||
@Override | ||
public HttpEngine create(HttpEngineConfig config) { | ||
OkHttpClient.Builder connectionBuilder = new OkHttpClient.Builder(); | ||
OkHttpUtils.injectProxySetting(config.getProxy(), connectionBuilder); | ||
return new OkHttpEngine(connectionBuilder.build(), config); | ||
} | ||
|
||
@Override | ||
public EngineType getEngineType() { | ||
return EngineType.OKHTTP; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
network-client-okhttp/src/main/java/io/ably/lib/network/OkHttpUtils.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.ably.lib.network; | ||
|
||
import okhttp3.Credentials; | ||
import okhttp3.Headers; | ||
import okhttp3.MediaType; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.RequestBody; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.net.Proxy; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class OkHttpUtils { | ||
public static void injectProxySetting(ProxyConfig proxyConfig, OkHttpClient.Builder connectionBuilder) { | ||
if (proxyConfig != null) return; | ||
connectionBuilder.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyConfig.getHost(), proxyConfig.getPort()))); | ||
if (proxyConfig.getUsername() == null || proxyConfig.getAuthType() != ProxyAuthType.BASIC) return; | ||
String username = proxyConfig.getUsername(); | ||
String password = proxyConfig.getPassword(); | ||
connectionBuilder.proxyAuthenticator((route, response) -> { | ||
String credential = Credentials.basic(username, password); | ||
return response.request().newBuilder() | ||
.header("Proxy-Authorization", credential) | ||
.build(); | ||
}); | ||
} | ||
|
||
public static Request toOkhttpRequest(HttpRequest request) { | ||
Request.Builder builder = new Request.Builder() | ||
.url(request.getUrl()); | ||
|
||
RequestBody body = null; | ||
|
||
if (request.getBody() != null) { | ||
body = RequestBody.create(request.getBody().getContent(), MediaType.parse(request.getBody().getContentType())); | ||
} | ||
|
||
builder.method(request.getMethod(), body); | ||
for (Map.Entry<String, List<String>> entry : request.getHeaders().entrySet()) { | ||
String headerName = entry.getKey(); | ||
List<String> values = entry.getValue(); | ||
for (String headerValue : values) { | ||
builder.addHeader(headerName, headerValue); | ||
} | ||
} | ||
|
||
return builder.build(); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
network-client-okhttp/src/main/java/io/ably/lib/network/OkHttpWebSocketClient.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package io.ably.lib.network; | ||
|
||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import okhttp3.WebSocket; | ||
import okio.ByteString; | ||
|
||
public class OkHttpWebSocketClient implements WebSocketClient { | ||
private final OkHttpClient connection; | ||
private final Request request; | ||
private final WebSocketListener listener; | ||
private WebSocket webSocket; | ||
|
||
public OkHttpWebSocketClient(OkHttpClient connection, Request request, WebSocketListener listener) { | ||
this.connection = connection; | ||
this.request = request; | ||
this.listener = listener; | ||
} | ||
|
||
@Override | ||
public void connect() { | ||
webSocket = connection.newWebSocket(request, new WebSocketHandler(listener)); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
webSocket.close(1000, "Close"); | ||
} | ||
|
||
@Override | ||
public void close(int code, String reason) { | ||
webSocket.close(code, reason); | ||
} | ||
|
||
@Override | ||
public void cancel(int code, String reason) { | ||
webSocket.cancel(); | ||
listener.onClose(code, reason); | ||
} | ||
|
||
@Override | ||
public void send(byte[] bytes) { | ||
webSocket.send(ByteString.of(bytes)); | ||
} | ||
|
||
@Override | ||
public void send(String message) { | ||
webSocket.send(message); | ||
} | ||
|
||
private static class WebSocketHandler extends okhttp3.WebSocketListener { | ||
private final WebSocketListener listener; | ||
|
||
private WebSocketHandler(WebSocketListener listener) { | ||
super(); | ||
this.listener = listener; | ||
} | ||
|
||
@Override | ||
public void onClosed(WebSocket webSocket, int code, String reason) { | ||
listener.onClose(code, reason); | ||
} | ||
|
||
@Override | ||
public void onFailure(WebSocket webSocket, Throwable t, Response response) { | ||
listener.onError(t); | ||
} | ||
|
||
@Override | ||
public void onMessage(WebSocket webSocket, String text) { | ||
listener.onMessage(text); | ||
} | ||
|
||
@Override | ||
public void onMessage(WebSocket webSocket, ByteString bytes) { | ||
listener.onMessage(bytes.asByteBuffer()); | ||
} | ||
|
||
@Override | ||
public void onOpen(WebSocket webSocket, Response response) { | ||
listener.onOpen(); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
network-client-okhttp/src/main/java/io/ably/lib/network/OkHttpWebSocketEngine.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.ably.lib.network; | ||
|
||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
|
||
public class OkHttpWebSocketEngine implements WebSocketEngine { | ||
private final WebSocketEngineConfig config; | ||
|
||
public OkHttpWebSocketEngine(WebSocketEngineConfig config) { | ||
this.config = config; | ||
} | ||
|
||
@Override | ||
public WebSocketClient create(String url, WebSocketListener listener) { | ||
OkHttpClient.Builder connectionBuilder = new OkHttpClient.Builder(); | ||
|
||
Request.Builder requestBuilder = new Request.Builder().url(url); | ||
|
||
OkHttpUtils.injectProxySetting(config.getProxy(), connectionBuilder); | ||
|
||
if (config.getSslSocketFactory() != null) { | ||
connectionBuilder.sslSocketFactory(config.getSslSocketFactory()); | ||
} | ||
|
||
return new OkHttpWebSocketClient(connectionBuilder.build(), requestBuilder.build(), listener); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
network-client-okhttp/src/main/java/io/ably/lib/network/OkHttpWebSocketEngineFactory.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.ably.lib.network; | ||
|
||
public class OkHttpWebSocketEngineFactory implements WebSocketEngineFactory { | ||
@Override | ||
public WebSocketEngine create(WebSocketEngineConfig config) { | ||
return new OkHttpWebSocketEngine(config); | ||
} | ||
|
||
@Override | ||
public EngineType getEngineType() { | ||
return EngineType.OKHTTP; | ||
} | ||
} |
Oops, something went wrong.