-
Notifications
You must be signed in to change notification settings - Fork 124
/
Handler.scala.txt
140 lines (122 loc) · 7.19 KB
/
Handler.scala.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
@*
* Copyright (C) 2018-2020 Lightbend Inc. <https://www.lightbend.com>
*@
@(service: akka.grpc.gen.javadsl.Service, powerApis: Boolean)
@akka.grpc.gen.Constants.DoNotEditComment
package @service.packageName;
import java.util.Iterator;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import akka.japi.Function;
import akka.http.javadsl.model.*;
import akka.actor.ActorSystem;
import akka.stream.Materializer;
import akka.grpc.Trailers;
import akka.grpc.javadsl.GrpcMarshalling;
import akka.grpc.javadsl.GrpcExceptionHandler;
import akka.grpc.javadsl.package$;
@{if (powerApis) "import akka.grpc.javadsl.Metadata;\nimport akka.grpc.javadsl.MetadataBuilder;" else ""}
import static @{service.packageName}.@{service.name}.Serializers.*;
@defining(if (powerApis) service.name + "PowerApi" else service.name) { serviceName =>
public class @{serviceName}HandlerFactory {
private static final CompletionStage<HttpResponse> notFound = CompletableFuture.completedFuture(
HttpResponse.create().withStatus(StatusCodes.NOT_FOUND));
private static final CompletionStage<HttpResponse> unsupportedMediaType = CompletableFuture.completedFuture(
HttpResponse.create().withStatus(StatusCodes.UNSUPPORTED_MEDIA_TYPE));
/**
* Creates a `HttpRequest` to `HttpResponse` handler that can be used in for example `Http().bindAndHandleAsync`
* for the generated partial function handler and ends with `StatusCodes.NotFound` if the request is not matching.
*
* Use `akka.grpc.scaladsl.ServiceHandler.concatOrNotFound` with `@{service.name}Handler.partial` when combining
* several services.
*/
public static Function<HttpRequest, CompletionStage<HttpResponse>> create(@serviceName implementation, Materializer mat, ActorSystem system) {
return create(implementation, @{service.name}.name, mat, system);
}
/**
* Creates a `HttpRequest` to `HttpResponse` handler that can be used in for example `Http().bindAndHandleAsync`
* for the generated partial function handler and ends with `StatusCodes.NotFound` if the request is not matching.
*
* Use `akka.grpc.scaladsl.ServiceHandler.concatOrNotFound` with `@{service.name}Handler.partial` when combining
* several services.
*/
public static Function<HttpRequest, CompletionStage<HttpResponse>> create(@serviceName implementation, Materializer mat, Function<ActorSystem, Function<Throwable, Trailers>> eHandler, ActorSystem system) {
return create(implementation, @{service.name}.name, mat, eHandler, system);
}
/**
* Creates a `HttpRequest` to `HttpResponse` handler that can be used in for example `Http().bindAndHandleAsync`
* for the generated partial function handler and ends with `StatusCodes.NotFound` if the request is not matching.
*
* Use `akka.grpc.scaladsl.ServiceHandler.concatOrNotFound` with `@{service.name}Handler.partial` when combining
* several services.
*
* Registering a gRPC service under a custom prefix is not widely supported and strongly discouraged by the specification.
*/
public static Function<HttpRequest, CompletionStage<HttpResponse>> create(@serviceName implementation, String prefix, Materializer mat, ActorSystem system) {
return partial(implementation, prefix, mat, GrpcExceptionHandler.defaultMapper(), system);
}
/**
* Creates a `HttpRequest` to `HttpResponse` handler that can be used in for example `Http().bindAndHandleAsync`
* for the generated partial function handler and ends with `StatusCodes.NotFound` if the request is not matching.
*
* Use `akka.grpc.scaladsl.ServiceHandler.concatOrNotFound` with `@{service.name}Handler.partial` when combining
* several services.
*
* Registering a gRPC service under a custom prefix is not widely supported and strongly discouraged by the specification.
*/
public static Function<HttpRequest, CompletionStage<HttpResponse>> create(@serviceName implementation, String prefix, Materializer mat, Function<ActorSystem, Function<Throwable, Trailers>> eHandler, ActorSystem system) {
return partial(implementation, prefix, mat, eHandler, system);
}
/**
* Creates a `HttpRequest` to `HttpResponse` handler that can be used in for example
* `Http.get(system).bindAndHandleAsync`. It ends with `StatusCodes.NotFound` if the request is not matching.
*
* Use `akka.grpc.javadsl.ServiceHandler.concatOrNotFound` when combining several services.
*/
public static Function<HttpRequest, CompletionStage<HttpResponse>> partial(@serviceName implementation, String prefix, Materializer mat, ActorSystem system) {
return partial(implementation, prefix, mat, GrpcExceptionHandler.defaultMapper(), system);
}
/**
* Creates a `HttpRequest` to `HttpResponse` handler that can be used in for example
* `Http.get(system).bindAndHandleAsync`. It ends with `StatusCodes.NotFound` if the request is not matching.
*
* Use `akka.grpc.javadsl.ServiceHandler.concatOrNotFound` when combining several services.
*/
public static Function<HttpRequest, CompletionStage<HttpResponse>> partial(@serviceName implementation, String prefix, Materializer mat, Function<ActorSystem, Function<Throwable, Trailers>> eHandler, ActorSystem system) {
return (req -> {
Iterator<String> segments = req.getUri().pathSegments().iterator();
if (segments.hasNext() && segments.next().equals(prefix) && segments.hasNext()) {
String method = segments.next();
if (segments.hasNext()) return notFound; // we don't allow any random `/prefix/Method/anything/here
else return handle(req, method, implementation, mat, eHandler, system);
} else {
return notFound;
}
});
}
public String getServiceName() {
return @{service.name}.name;
}
private static CompletionStage<HttpResponse> handle(HttpRequest request, String method, @serviceName implementation, Materializer mat, Function<ActorSystem, Function<Throwable, Trailers>> eHandler, ActorSystem system) {
return GrpcMarshalling.negotiated(request, (reader, writer) -> {
final CompletionStage<HttpResponse> response;
@{if(powerApis) { "Metadata metadata = MetadataBuilder.fromHeaders(request.getHeaders());" } else { "" }}
switch(method) {
@for(method <- service.methods) {
case "@method.grpcName":
response = @{method.unmarshal}(request.entity().getDataBytes(), @method.deserializer.name, mat, reader)
.@{if(method.outputStreaming) { "thenApply" } else { "thenCompose" }}(e -> implementation.@{method.name}(e@{if(powerApis) { ", metadata" } else { "" }}))
.thenApply(e -> @{method.marshal}(e, @method.serializer.name, writer, system, eHandler));
break;
}
default:
CompletableFuture<HttpResponse> result = new CompletableFuture<>();
result.completeExceptionally(new UnsupportedOperationException("Not implemented: " + method));
response = result;
}
return response.exceptionally(e -> GrpcExceptionHandler.standard(e, eHandler, writer, system));
})
.orElseGet(() -> unsupportedMediaType);
}
}
}