Skip to content

Commit a6b116a

Browse files
committed
tests and docs, organize inputs
1 parent 93c6f76 commit a6b116a

File tree

6 files changed

+45
-3
lines changed

6 files changed

+45
-3
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ invoked, and to use external configuration variables that you can set up with
299299
the Fn tool, have a look at the [Function
300300
Configuration](docs/FunctionConfiguration.md) tutorial.
301301

302+
### Handling HTTP requests
303+
304+
If your function serves an HTTP trigger you may want to access HTTP details such as request or response headers or the HTTP status , checko ut [Accessing HTTP Information From Functions](docs/HTTPGatewayFunctions.md).
305+
302306
### Input and output bindings
303307

304308
You have the option of taking more control of how serialization and

api/src/main/java/com/fnproject/fn/api/InputEvent.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.io.Closeable;
44
import java.io.InputStream;
55
import java.time.Instant;
6-
import java.util.Date;
76
import java.util.function.Function;
87

98
public interface InputEvent extends Closeable {

api/src/test/java/com/fnproject/fn/api/HeadersTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.fnproject.fn.api;
22

33
import org.junit.Test;
4+
45
import static org.assertj.core.api.Assertions.assertThat;
56

67
/**

docs/HTTPGatewayFunctions.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Accessing HTTP Information From Functions
2+
3+
Functions can be used to handle events, RPC calls or HTTP requests. When you are writing a function that handles an HTTP request you frequently need access to the HTTP headers of the incoming request or need to set HTTP headers or the status code on the outbound respsonse.
4+
5+
6+
In Fn for Java, when your function is being served by an HTTP trigger (or another compatible HTTP gatway) you can get access to both the incoming request headers for your function by adding a 'com.fnproject.fn.api.httpgateway.HTTPGatewayContext' parameter to your function's parameters.
7+
8+
9+
Using this allows you to :
10+
11+
* Read incoming headers
12+
* Access the method and request URL for the trigger
13+
* Write outbound headers to the response
14+
* Set the status code of the response
15+
16+
17+
For example this function reads a request header the method and request URL, sets an response header and sets the response status code to perform an HTTP redirect.
18+
19+
```java
20+
package com.fnproject.fn.examples;
21+
import com.fnproject.fn.api.httpgateway.HTTPGatewayContext;
22+
23+
24+
public class RedirectFunction {
25+
26+
public redirect(HTTPGatewayContext hctx) {
27+
System.err.println("Request URL is:" + hctx.getRequestURL());
28+
System.err.println("Trace ID" + hctx.getHeaders().get("My-Trace-ID").orElse("N/A"));
29+
30+
hctx.setResponseHeader("Location","http://example.com");
31+
hctx.setStatusCode(302);
32+
33+
}
34+
}
35+
36+
```

runtime/src/test/java/com/fnproject/fn/runtime/HeaderBuilder.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import com.fnproject.fn.api.Headers;
44

5-
import java.util.*;
5+
import java.util.AbstractMap;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
import java.util.Map;
69

710
class HeaderBuilder {
811
static Map.Entry<String, List<String>> headerEntry(String key, String... values) {

runtime/src/test/java/com/fnproject/fn/runtime/JacksonCoercionTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.fnproject.fn.api.MethodWrapper;
66
import com.fnproject.fn.runtime.coercion.jackson.JacksonCoercion;
77
import com.fnproject.fn.runtime.testfns.Animal;
8-
import jdk.internal.util.xml.impl.Input;
98
import org.junit.Assert;
109
import org.junit.Test;
1110

0 commit comments

Comments
 (0)