Skip to content

Commit 9ac872b

Browse files
committed
Allow AWS_LAMBDA_RUNTIME_API subpath and remove NativeClient
1 parent baeaac9 commit 9ac872b

2 files changed

Lines changed: 60 additions & 14 deletions

File tree

aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/runtimeapi/InvocationRequest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,40 @@ public class InvocationRequest {
1313
/**
1414
* The Lambda request ID associated with the request.
1515
*/
16-
private String id;
16+
public String id;
1717

1818
/**
1919
* The X-Ray tracing ID.
2020
*/
21-
private String xrayTraceId;
21+
public String xrayTraceId;
2222

2323
/**
2424
* The ARN of the Lambda function being invoked.
2525
*/
26-
private String invokedFunctionArn;
26+
public String invokedFunctionArn;
2727

2828
/**
2929
* Function execution deadline counted in milliseconds since the Unix epoch.
3030
*/
31-
private long deadlineTimeInMs;
31+
public long deadlineTimeInMs;
3232

3333
/**
3434
* The client context header. This field is populated when the function is invoked from a mobile client.
3535
*/
36-
private String clientContext;
36+
public String clientContext;
3737

3838
/**
3939
* The Cognito Identity context for the invocation. This field is populated when the function is invoked with AWS
4040
* credentials obtained from Cognito Identity.
4141
*/
42-
private String cognitoIdentity;
42+
public String cognitoIdentity;
4343

4444
/**
4545
* An input stream of the invocation's request body.
4646
*/
47-
private InputStream stream;
47+
public InputStream stream;
4848

49-
private byte[] content;
49+
public byte[] content;
5050

5151
public String getId() {
5252
return id;

aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/runtimeapi/LambdaRuntimeClient.java

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
import java.net.HttpURLConnection;
77
import java.net.MalformedURLException;
88
import java.net.URL;
9+
import java.util.Map;
910
import java.util.Objects;
11+
import java.util.Scanner;
12+
import java.util.*;
1013

1114
import static java.net.HttpURLConnection.HTTP_ACCEPTED;
1215
import static java.nio.charset.StandardCharsets.UTF_8;
@@ -21,7 +24,7 @@
2124
public class LambdaRuntimeClient {
2225

2326
private final String hostname;
24-
private final int port;
27+
private final String port;
2528
private final String invocationEndpoint;
2629

2730
private static final String DEFAULT_CONTENT_TYPE = "application/json";
@@ -33,16 +36,51 @@ public LambdaRuntimeClient(String hostnamePort) {
3336
Objects.requireNonNull(hostnamePort, "hostnamePort cannot be null");
3437
String[] parts = hostnamePort.split(":");
3538
this.hostname = parts[0];
36-
this.port = Integer.parseInt(parts[1]);
39+
this.port = parts[1];
3740
this.invocationEndpoint = invocationEndpoint();
3841
}
3942

40-
public InvocationRequest waitForNextInvocation() {
41-
return NativeClient.next();
43+
public InvocationRequest waitForNextInvocation() throws IOException {
44+
// Make GET request
45+
String endpoint = invocationNextEndpoint();
46+
URL url = createUrl(endpoint);
47+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
48+
conn.setRequestMethod("GET");
49+
InputStream response = conn.getInputStream();
50+
51+
// Parse headers
52+
InvocationRequest request = new InvocationRequest();
53+
request.stream = response;
54+
request.content = response.readAllBytes();
55+
request.id = conn.getHeaderField("Lambda-Runtime-Aws-Request-Id");
56+
request.deadlineTimeInMs = Long.parseLong(conn.getHeaderField("Lambda-Runtime-Deadline-Ms"));
57+
request.invokedFunctionArn = conn.getHeaderField("Lambda-Runtime-Invoked-Function-Arn");
58+
request.clientContext = conn.getHeaderField("Lambda-Runtime-Client-Context");
59+
request.cognitoIdentity = conn.getHeaderField("Lambda-Runtime-Cognito-Identity");
60+
61+
closeQuietly(response);
62+
63+
return request;
4264
}
4365

44-
public void postInvocationResponse(String requestId, byte[] response) {
45-
NativeClient.postInvocationResponse(requestId.getBytes(UTF_8), response);
66+
public void postInvocationResponse(String requestId, byte[] response) throws IOException {
67+
String endpoint = invocationResponseEndpoint(requestId);
68+
URL url = createUrl(endpoint);
69+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
70+
conn.setRequestMethod("POST");
71+
conn.setRequestProperty("Content-Type", DEFAULT_CONTENT_TYPE);
72+
conn.setFixedLengthStreamingMode(response.length);
73+
conn.setDoOutput(true);
74+
try (OutputStream outputStream = conn.getOutputStream()) {
75+
outputStream.write(response);
76+
}
77+
78+
int responseCode = conn.getResponseCode();
79+
if (responseCode != HTTP_ACCEPTED) {
80+
throw new LambdaRuntimeClientException(endpoint, responseCode);
81+
}
82+
83+
closeQuietly(conn.getInputStream());
4684
}
4785

4886
public void postInvocationError(String requestId, byte[] errorResponse, String errorType) throws IOException {
@@ -90,6 +128,14 @@ private String invocationEndpoint() {
90128
return "http://" + hostname + ":" + port + "/2018-06-01/runtime/invocation/";
91129
}
92130

131+
private String invocationNextEndpoint() {
132+
return invocationEndpoint + "next";
133+
}
134+
135+
private String invocationResponseEndpoint(String requestId) {
136+
return invocationEndpoint + requestId + "/response";
137+
}
138+
93139
private String invocationErrorEndpoint(String requestId) {
94140
return invocationEndpoint + requestId + "/error";
95141
}

0 commit comments

Comments
 (0)