-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add initial message delivery implementation
- Loading branch information
1 parent
16e60ef
commit f3e5dd0
Showing
5 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
consumer/src/main/java/com/flipkart/varadhi/consumer/delivery/DeliveryResponse.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,6 @@ | ||
package com.flipkart.varadhi.consumer.delivery; | ||
|
||
import com.flipkart.varadhi.entities.Endpoint; | ||
|
||
public record DeliveryResponse(int statusCode, Endpoint.Protocol protocol, byte[] body) { | ||
} |
71 changes: 71 additions & 0 deletions
71
consumer/src/main/java/com/flipkart/varadhi/consumer/delivery/MessageDelivery.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,71 @@ | ||
package com.flipkart.varadhi.consumer.delivery; | ||
|
||
import com.flipkart.varadhi.consumer.MessageTracker; | ||
import com.flipkart.varadhi.entities.Endpoint; | ||
import com.flipkart.varadhi.exceptions.NotImplementedException; | ||
import com.google.common.collect.Multimap; | ||
|
||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.time.Duration; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public interface MessageDelivery { | ||
static MessageDelivery of(Endpoint endpoint) { | ||
return switch (endpoint.getProtocol()) { | ||
case HTTP1_1 -> new HttpMessageDelivery(endpoint); | ||
case HTTP2 -> throw new NotImplementedException("HTTP2 is not supported yet"); | ||
default -> throw new IllegalArgumentException("Unsupported protocol: " + endpoint.getProtocol()); | ||
}; | ||
} | ||
|
||
CompletableFuture<DeliveryResponse> deliver(MessageTracker messageTracker) | ||
throws Exception; // or should be PolledMessage? | ||
|
||
class HttpMessageDelivery implements MessageDelivery { | ||
private final Endpoint.HttpEndpoint endpoint; | ||
private final HttpClient httpClient; | ||
|
||
public HttpMessageDelivery(Endpoint endpoint) { | ||
this.endpoint = (Endpoint.HttpEndpoint) endpoint; | ||
this.httpClient = HttpClient.newBuilder() | ||
.version(this.endpoint.isHttp2Supported() ? HttpClient.Version.HTTP_2 : HttpClient.Version.HTTP_1_1) | ||
.connectTimeout(Duration.ofMillis(this.endpoint.getConnectTimeoutMs())) | ||
.followRedirects(HttpClient.Redirect.NORMAL) | ||
.build(); | ||
// TODO(aayush): SSL/TLS support? Auth support? | ||
} | ||
|
||
@Override | ||
public CompletableFuture<DeliveryResponse> deliver(MessageTracker messageTracker) throws Exception { | ||
// TODO(aayush): PolledMessage getPayload, getHeaders support required | ||
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder() | ||
.uri(endpoint.getUrl().toURI()) | ||
.timeout(Duration.ofMillis(endpoint.getRequestTimeoutMs())) | ||
.header("Content-Type", endpoint.getContentType()) | ||
.method( | ||
endpoint.getMethod(), | ||
HttpRequest.BodyPublishers.ofByteArray(messageTracker.getMessage().getPayload()) | ||
); | ||
|
||
// apply request headers from message | ||
Multimap<String, String> requestHeaders = messageTracker.getMessage().getRequestHeaders(); | ||
if (requestHeaders != null) { | ||
requestHeaders.entries().forEach(entry -> requestBuilder.header(entry.getKey(), entry.getValue())); | ||
} | ||
|
||
HttpRequest request = requestBuilder.build(); | ||
|
||
return httpClient.sendAsync( | ||
request, HttpResponse.BodyHandlers.ofByteArray()) // TODO(aayush): response as string or byte array? | ||
.thenApply(response -> new DeliveryResponse(response.statusCode(), endpoint.getProtocol(), | ||
response.body() | ||
)) | ||
.exceptionally(e -> { | ||
// log error | ||
return new DeliveryResponse(500, endpoint.getProtocol(), e.getMessage().getBytes()); | ||
}); | ||
} | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
server/src/testE2E/java/com/flipkart/varadhi/IamPolicyTests.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,16 @@ | ||
package com.flipkart.varadhi; | ||
|
||
public class IamPolicyTests extends E2EBase { | ||
|
||
/** | ||
* Tests for iam policy, some cases to test are: | ||
* - initial superuser should have all permissions | ||
* - thanos creates flipkart org | ||
* - create admin user via set policy | ||
* - admin user should be able to create more user roles | ||
* - non admin users should not be able to create roles | ||
* - admin user can create team and project | ||
* - similarly for team and project scoped roles, do the same | ||
* - all the way down to topic level and produce level | ||
*/ | ||
} |