Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

remove okhttp #90

Merged
merged 11 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '17'
java-version: '21'
distribution: 'temurin'
cache: maven
- name: Build with Maven
Expand Down
2 changes: 1 addition & 1 deletion .java-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
17
jdk-21.0.1+12
40 changes: 15 additions & 25 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>com.github.libgraviton</groupId>
<artifactId>worker-base</artifactId>
<packaging>jar</packaging>
<version>3.12.0-SNAPSHOT</version>
<version>4.0.0-SNAPSHOT</version>
<name>Graviton Worker Base Library</name>
<url>https://github.com/libgraviton/graviton-worker-base-java</url>
<inceptionYear>2015</inceptionYear>
Expand Down Expand Up @@ -131,18 +131,6 @@
<artifactId>guava</artifactId>
<version>32.1.3-jre</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.11.0</version>
<exclusions>
<exclusion>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down Expand Up @@ -306,7 +294,7 @@
<dependency>
<groupId>com.github.libgraviton</groupId>
<artifactId>graviton-worker-test-base</artifactId>
<version>3.7.0</version>
<version>4.0.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
Expand All @@ -316,9 +304,9 @@
</exclusions>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8</artifactId>
<version>2.35.1</version>
<groupId>org.wiremock</groupId>
<artifactId>wiremock</artifactId>
<version>3.3.1</version>
<scope>test</scope>
<exclusions>
<exclusion>
Expand Down Expand Up @@ -362,10 +350,6 @@
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down Expand Up @@ -424,6 +408,12 @@
<artifactId>classgraph</artifactId>
</dependency>

<dependency>
<groupId>com.github.mizosoft.methanol</groupId>
<artifactId>methanol</artifactId>
<version>1.7.0</version>
</dependency>

<!-- metrics -->
<dependency>
<groupId>io.micrometer</groupId>
Expand Down Expand Up @@ -459,8 +449,8 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8</artifactId>
<groupId>org.wiremock</groupId>
<artifactId>wiremock</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down Expand Up @@ -511,8 +501,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>17</source>
<target>17</target>
<source>21</source>
<target>21</target>
</configuration>
<executions>
<execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,23 @@
import com.github.libgraviton.workerbase.gdk.api.endpoint.GeneratedEndpointManager;
import com.github.libgraviton.workerbase.gdk.api.endpoint.exception.UnableToLoadEndpointAssociationsException;
import com.github.libgraviton.workerbase.gdk.api.gateway.GravitonGateway;
import com.github.libgraviton.workerbase.gdk.api.gateway.OkHttpGateway;
import com.github.libgraviton.workerbase.gdk.api.gateway.okhttp.OkHttpGatewayFactory;
import com.github.libgraviton.workerbase.gdk.api.gateway.MethanolGateway;
import com.github.libgraviton.workerbase.gdk.api.gateway.http.MethanolGatewayFactory;
import com.github.libgraviton.workerbase.gdk.requestexecutor.auth.Authenticator;
import com.github.libgraviton.workerbase.gdk.requestexecutor.auth.GravitonGatewayAuthenticator;
import com.github.libgraviton.workerbase.gdk.serialization.mapper.RqlObjectMapper;
import com.github.libgraviton.workerbase.helper.EventStatusHandler;
import com.github.libgraviton.workerbase.helper.WorkerProperties;
import com.github.mizosoft.methanol.Methanol;
import io.activej.inject.Key;
import io.activej.inject.annotation.Provides;
import io.activej.inject.annotation.Transient;
import io.activej.inject.binding.Binding;
import io.activej.inject.module.AbstractModule;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;

import java.io.IOException;
import java.net.http.HttpClient;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Properties;
import java.util.TimeZone;
import java.util.concurrent.*;
Expand All @@ -42,7 +41,7 @@ protected void configure() {
*/
generate(GravitonGateway.class, (bindings, scope, key) -> {
if (key.getType().equals(GravitonGateway.class)) {
return Binding.to(Key.of(OkHttpGateway.class));
return Binding.to(Key.of(MethanolGateway.class));
}
return null;
});
Expand Down Expand Up @@ -77,25 +76,16 @@ public static GravitonApi gravitonApi(EndpointManager endpointManager, ObjectMap
}

@Provides
public static OkHttpClient getOkHttpClient() throws Exception {
public static Methanol getMethanol() throws Exception {
final boolean hasRetry = WorkerProperties.HTTP_CLIENT_DORETRY.get().equals("true");
final boolean forceHttp11 = WorkerProperties.HTTP_CLIENT_FORCE_HTTP1_1.get().equals("true");
final boolean trustAll = WorkerProperties.HTTP_CLIENT_TLS_TRUST_ALL.get().equals("true");

OkHttpClient client = OkHttpGatewayFactory.getInstance(hasRetry);
if (trustAll) {
client = OkHttpGatewayFactory.getAllTrustingInstance(hasRetry, client);
}

if (forceHttp11) {
client = client
.newBuilder()
.retryOnConnectionFailure(true)
.protocols(Collections.singletonList(Protocol.HTTP_1_1))
.build();
}
return MethanolGatewayFactory.getInstance(hasRetry, trustAll);
}

return client;
@Provides
public static HttpClient getHttpClient() throws Exception {
return getMethanol();
}

@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public NoopResponse(NoopRequest request) {
this.request = request;
this.code = -1;
this.isSuccessful = true;
this.message = "This is not the response you are looking for";
this.body = null;
this.headers = new HeaderBag.Builder().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public class Response {

protected int code;

protected String message;

protected HeaderBag headers;

protected byte[] body;
Expand All @@ -43,7 +41,6 @@ protected Response(com.github.libgraviton.workerbase.gdk.api.Response.Builder bu
request = builder.request;
code = builder.code;
isSuccessful = builder.isSuccessful;
message = builder.message;
body = builder.body;
headers = builder.headerBuilder.build();
}
Expand Down Expand Up @@ -151,16 +148,10 @@ public int getCode() {
return code;
}

public String getMessage() {
return message;
}

public static class Builder {

protected int code;

protected String message;

protected final Request request;

protected byte[] body;
Expand All @@ -184,11 +175,6 @@ public Builder successful(boolean isSuccessful) {
return this;
}

public Builder message(String message) {
this.message = message;
return this;
}

public Builder body(byte[] body) {
this.body = body;
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.github.libgraviton.workerbase.gdk.api.gateway;

import com.github.libgraviton.workerbase.annotation.GravitonWorkerDiScan;
import com.github.libgraviton.workerbase.gdk.api.Request;
import com.github.libgraviton.workerbase.gdk.api.Response;
import com.github.libgraviton.workerbase.gdk.api.header.Header;
import com.github.libgraviton.workerbase.gdk.api.header.HeaderBag;
import com.github.libgraviton.workerbase.gdk.api.multipart.FilePart;
import com.github.libgraviton.workerbase.gdk.api.multipart.Part;
import com.github.libgraviton.workerbase.gdk.exception.CommunicationException;
import com.github.mizosoft.methanol.Methanol;
import com.github.mizosoft.methanol.MultipartBodyPublisher;
import com.github.mizosoft.methanol.MutableRequest;
import io.activej.inject.annotation.Inject;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Map;

@GravitonWorkerDiScan
public class MethanolGateway implements GravitonGateway {

@Inject
Methanol httpClient;

@Inject
public MethanolGateway() {

}

public HttpClient getHttpClient() {
return httpClient;
}

public Response execute(Request request) throws CommunicationException {
try {
HttpRequest req = generateRequest(request);

HttpResponse<byte[]> response = httpClient.send(req, HttpResponse.BodyHandlers.ofByteArray());
return generateResponse(request, response);
} catch (FileNotFoundException | URISyntaxException e) {
throw new CommunicationException("Unable to create request", e);
} catch (IOException | InterruptedException e) {
throw new CommunicationException("Error sending request", e);
}
}

private HttpRequest generateRequest(Request request) throws FileNotFoundException, URISyntaxException {
HttpRequest.BodyPublisher body;

if (request.isMultipartRequest()) {
body = generateMultipartRequestBody(request);
} else {
if (null == request.getBodyBytes()) {
body = HttpRequest.BodyPublishers.noBody();
} else {
body = HttpRequest.BodyPublishers.ofByteArray(request.getBodyBytes());
}
}

HttpRequest.Builder builder = MutableRequest.newBuilder()
.uri(request.getUrl().toURI())
.method(request.getMethod().asString(), body);

// set headers
for (Map.Entry<String, Header> header : request.getHeaders().all().entrySet()) {
for (String value : header.getValue()) {
builder.header(header.getKey(), value);
}
}

return builder.build();
}

private MultipartBodyPublisher generateMultipartRequestBody(Request request) throws FileNotFoundException {
MultipartBodyPublisher.Builder multipartBuilder = MultipartBodyPublisher.newBuilder();

int partNumber = 1;
for (Part part : request.getParts()) {
if (part.getFormName() != null) {
multipartBuilder.formPart(part.getFormName(), HttpRequest.BodyPublishers.ofByteArray(part.getBody()));
} else {
String partName = String.format("part-%s", partNumber);
multipartBuilder.formPart(partName, HttpRequest.BodyPublishers.ofByteArray(part.getBody()));
}
partNumber++;
}

for (FilePart part : request.getFileParts()) {
if (part.getFormName() != null) {
multipartBuilder.filePart(part.getFormName(), part.getBody().toPath());
} else {
String partName = String.format("part-%s", partNumber);
multipartBuilder.filePart(partName, part.getBody().toPath(), com.github.mizosoft.methanol.MediaType.parse(part.getContentType()));
}
partNumber++;
}

return multipartBuilder.build();
}

private Response generateResponse(Request req, HttpResponse<byte[]> resp) {
Response.Builder responseBuilder = new Response.Builder(req);

return responseBuilder
.code(resp.statusCode())
.headers(createResponseHeaders(resp.headers()))
.successful(resp.statusCode() < 300)
.body(resp.body())
.build();
}

private HeaderBag.Builder createResponseHeaders(HttpHeaders headers) {
HeaderBag.Builder builder = new HeaderBag.Builder();
headers.map().forEach(builder::set);
return builder;
}

}
Loading