Skip to content

Commit 830e25c

Browse files
committed
Create azure provider
1 parent 9ec04d2 commit 830e25c

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed

azure-provider/pom.xml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project>
3+
<modelVersion>4.0.0</modelVersion>
4+
<packaging>jar</packaging>
5+
<name>Azure Functions Provider for Agnostic Serverless Functions</name>
6+
<groupId>fr.axelop.agnosticserverlessfunctions</groupId>
7+
<artifactId>agnostic-serverless-functions-azure-provider</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
10+
<parent>
11+
<groupId>fr.axelop.agnosticserverlessfunctions</groupId>
12+
<artifactId>agnostic-serverless-functions-providers</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
</parent>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>fr.axelop.agnosticserverlessfunctions</groupId>
19+
<artifactId>agnostic-serverless-functions-interfaces</artifactId>
20+
<version>${agnostic.serverless.functions.interfaces.version}</version>
21+
</dependency>
22+
23+
<dependency>
24+
<groupId>com.microsoft.azure.functions</groupId>
25+
<artifactId>azure-functions-java-library</artifactId>
26+
<version>2.0.1</version>
27+
</dependency>
28+
</dependencies>
29+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package fr.axelop.agnosticserverlessfunctions;
2+
3+
import java.util.Optional;
4+
import java.util.ServiceLoader;
5+
import java.util.function.Supplier;
6+
import java.util.logging.Logger;
7+
8+
import com.microsoft.azure.functions.ExecutionContext;
9+
import com.microsoft.azure.functions.HttpMethod;
10+
import com.microsoft.azure.functions.HttpRequestMessage;
11+
import com.microsoft.azure.functions.HttpResponseMessage;
12+
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
13+
import com.microsoft.azure.functions.annotation.FunctionName;
14+
import com.microsoft.azure.functions.annotation.HttpTrigger;
15+
16+
public class FunctionInvoker {
17+
18+
private static final HttpRequestMapper requestMapper = new HttpRequestMapper();
19+
private static final HttpResponseMapper responseMapper = new HttpResponseMapper();
20+
private static final Optional<Handler> optHandler = lookupHandler();
21+
private static final Supplier<String> exceptionMessageSupplier = () -> "Implementation of "
22+
+ Handler.class.getName()
23+
+ " not found. Make sure that it is referenced as a service under META-INF/services.";
24+
25+
@FunctionName("handler")
26+
public HttpResponseMessage run(
27+
@HttpTrigger(
28+
name = "httpTrigger",
29+
authLevel = AuthorizationLevel.ANONYMOUS,
30+
methods = {
31+
HttpMethod.CONNECT,
32+
HttpMethod.DELETE,
33+
HttpMethod.GET,
34+
HttpMethod.HEAD,
35+
HttpMethod.OPTIONS,
36+
HttpMethod.PATCH,
37+
HttpMethod.POST,
38+
HttpMethod.PUT,
39+
HttpMethod.TRACE
40+
}
41+
)
42+
HttpRequestMessage<Optional<String>> request,
43+
ExecutionContext context
44+
) throws Exception {
45+
final Logger logger = context.getLogger();
46+
if (optHandler.isEmpty()) {
47+
logger.severe(() -> "NO IMPLEMENTATION OF " + Handler.class.getName() + " FOUND. TERMINATING EXECUTION.");
48+
throw new RuntimeException(exceptionMessageSupplier.get());
49+
}
50+
final Handler handler = optHandler.get();
51+
final HttpResponse handlerResponse = handler.handle(requestMapper.map(request), logger);
52+
return responseMapper.map(handlerResponse, request);
53+
}
54+
55+
private static Optional<Handler> lookupHandler() {
56+
return ServiceLoader.load(Handler.class).findFirst();
57+
}
58+
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package fr.axelop.agnosticserverlessfunctions;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.Optional;
9+
10+
import com.microsoft.azure.functions.HttpRequestMessage;
11+
12+
class HttpRequestMapper {
13+
14+
public HttpRequest map(HttpRequestMessage<Optional<String>> azureRequest) {
15+
final Map<String, List<String>> headers = Collections.unmodifiableMap(getHeaders(azureRequest));
16+
return new HttpRequest() {
17+
18+
@Override
19+
public Optional<String> getBody() {
20+
return azureRequest.getBody();
21+
}
22+
23+
@Override
24+
public long getContentLength() {
25+
return Long.parseLong(azureRequest.getHeaders().getOrDefault("Content-Length", "0"));
26+
}
27+
28+
@Override
29+
public Map<String, List<String>> getHeaders() {
30+
return headers;
31+
}
32+
33+
@Override
34+
public String getMethod() {
35+
return azureRequest.getHttpMethod().toString();
36+
}
37+
38+
};
39+
}
40+
41+
private Map<String, List<String>> getHeaders(HttpRequestMessage<Optional<String>> azureRequest) {
42+
final Map<String, List<String>> headers = new HashMap<>(azureRequest.getHeaders().size());
43+
for (Map.Entry<String, String> header : azureRequest.getHeaders().entrySet()) {
44+
headers
45+
.computeIfAbsent(header.getKey(), k -> new ArrayList<>(1))
46+
.add(header.getValue());
47+
}
48+
return headers;
49+
}
50+
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package fr.axelop.agnosticserverlessfunctions;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
import com.microsoft.azure.functions.HttpRequestMessage;
7+
import com.microsoft.azure.functions.HttpResponseMessage;
8+
import com.microsoft.azure.functions.HttpStatus;
9+
10+
class HttpResponseMapper {
11+
12+
HttpResponseMessage map(HttpResponse handlerResponse, HttpRequestMessage<?> azureRequest) {
13+
final HttpStatus httpStatus = HttpStatus.valueOf(handlerResponse.getStatusCode());
14+
final HttpResponseMessage.Builder builder = azureRequest.createResponseBuilder(httpStatus);
15+
for (Map.Entry<String, List<String>> headerEntry : handlerResponse.getHeaders().entrySet()) {
16+
for (String value : headerEntry.getValue()) {
17+
builder.header(headerEntry.getKey(), value);
18+
}
19+
}
20+
if (handlerResponse.getBody().isPresent()) {
21+
builder.body(handlerResponse.getBody().get());
22+
}
23+
return builder.build();
24+
}
25+
26+
}

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
<modules>
1111
<module>gcloud-provider</module>
12+
<module>azure-provider</module>
1213
</modules>
1314

1415
<properties>

0 commit comments

Comments
 (0)