-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an HTTP example for protobuf serialization (#2107)
Motivation: HTTP users who work with protobuf payloads need an example how to use `ProtobufSerializerFactory`. Modifications: - Add `servicetalk-examples-http-serialization-protobuf` module to demonstrate use of `ProtobufSerializerFactory` with HTTP; - Update docs to reference a new example; Result: Users can see how to use `ProtobufSerializerFactory` with HTTP.
- Loading branch information
1 parent
3dbd25a
commit 2c310bb
Showing
24 changed files
with
818 additions
and
0 deletions.
There are no files selected for viewing
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
44 changes: 44 additions & 0 deletions
44
servicetalk-examples/http/serialization/protobuf/build.gradle
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,44 @@ | ||
/* | ||
* Copyright © 2022 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
buildscript { | ||
dependencies { | ||
classpath "com.google.protobuf:protobuf-gradle-plugin:$protobufGradlePluginVersion" | ||
} | ||
} | ||
|
||
apply plugin: "java" | ||
apply from: "../../../gradle/idea.gradle" | ||
apply plugin: "com.google.protobuf" | ||
|
||
dependencies { | ||
implementation project(":servicetalk-annotations") | ||
implementation project(":servicetalk-data-protobuf") | ||
implementation project(":servicetalk-http-netty") | ||
implementation "com.google.protobuf:protobuf-java:$protobufVersion" | ||
|
||
runtimeOnly "org.apache.logging.log4j:log4j-slf4j-impl:$log4jVersion" | ||
} | ||
|
||
protobuf { | ||
protoc { | ||
artifact = "com.google.protobuf:protoc:$protobufVersion" | ||
} | ||
} | ||
|
||
clean { | ||
delete protobuf.generatedFilesBaseDir | ||
} |
69 changes: 69 additions & 0 deletions
69
...uf/src/main/java/io/servicetalk/examples/http/serialization/protobuf/SerializerUtils.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,69 @@ | ||
/* | ||
* Copyright © 2022 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.servicetalk.examples.http.serialization.protobuf; | ||
|
||
import io.servicetalk.data.protobuf.ProtobufSerializerFactory; | ||
import io.servicetalk.examples.http.serialization.protobuf.ExampleProtos.RequestMessage; | ||
import io.servicetalk.examples.http.serialization.protobuf.ExampleProtos.ResponseMessage; | ||
import io.servicetalk.http.api.HttpHeaders; | ||
import io.servicetalk.http.api.HttpSerializerDeserializer; | ||
import io.servicetalk.http.api.HttpSerializers; | ||
import io.servicetalk.http.api.HttpStreamingSerializerDeserializer; | ||
|
||
import java.util.function.Consumer; | ||
import java.util.function.Predicate; | ||
|
||
import static io.servicetalk.buffer.api.CharSequences.newAsciiString; | ||
import static io.servicetalk.data.protobuf.ProtobufSerializerFactory.PROTOBUF; | ||
import static io.servicetalk.http.api.HeaderUtils.hasContentType; | ||
import static io.servicetalk.http.api.HttpHeaderNames.CONTENT_TYPE; | ||
import static io.servicetalk.http.api.HttpSerializers.serializer; | ||
import static io.servicetalk.http.api.HttpSerializers.streamingSerializer; | ||
|
||
/** | ||
* Utilities to cache serializer instances for request/response protos. | ||
* <p> | ||
* {@link ProtobufSerializerFactory} produces protocol-agnostic serializer/deserializer. We use {@link HttpSerializers} | ||
* utilities to concert it to HTTP-aware variants. | ||
*/ | ||
public final class SerializerUtils { | ||
|
||
private static final CharSequence APPLICATION_PROTOBUF = newAsciiString("application/protobuf"); | ||
private static final Consumer<HttpHeaders> CONTENT_TYPE_SETTER = | ||
headers -> headers.set(CONTENT_TYPE, APPLICATION_PROTOBUF); | ||
private static final Predicate<HttpHeaders> CONTENT_TYPE_VALIDATOR = | ||
headers -> hasContentType(headers, APPLICATION_PROTOBUF, null); | ||
|
||
public static final HttpSerializerDeserializer<RequestMessage> REQ_SERIALIZER = | ||
serializer(PROTOBUF.serializerDeserializer(RequestMessage.parser()), | ||
CONTENT_TYPE_SETTER, CONTENT_TYPE_VALIDATOR); | ||
|
||
public static final HttpStreamingSerializerDeserializer<RequestMessage> REQ_STREAMING_SERIALIZER = | ||
streamingSerializer(PROTOBUF.streamingSerializerDeserializer(RequestMessage.parser()), | ||
CONTENT_TYPE_SETTER, CONTENT_TYPE_VALIDATOR); | ||
|
||
public static final HttpSerializerDeserializer<ResponseMessage> RESP_SERIALIZER = | ||
serializer(PROTOBUF.serializerDeserializer(ResponseMessage.parser()), | ||
CONTENT_TYPE_SETTER, CONTENT_TYPE_VALIDATOR); | ||
|
||
public static final HttpStreamingSerializerDeserializer<ResponseMessage> RESP_STREAMING_SERIALIZER = | ||
streamingSerializer(PROTOBUF.streamingSerializerDeserializer(ResponseMessage.parser()), | ||
CONTENT_TYPE_SETTER, CONTENT_TYPE_VALIDATOR); | ||
|
||
private SerializerUtils() { | ||
// No instances. | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...c/main/java/io/servicetalk/examples/http/serialization/protobuf/async/ProtobufClient.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,40 @@ | ||
/* | ||
* Copyright © 2022 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.servicetalk.examples.http.serialization.protobuf.async; | ||
|
||
import io.servicetalk.examples.http.serialization.protobuf.ExampleProtos.RequestMessage; | ||
import io.servicetalk.http.api.HttpClient; | ||
import io.servicetalk.http.netty.HttpClients; | ||
|
||
import static io.servicetalk.examples.http.serialization.protobuf.SerializerUtils.REQ_SERIALIZER; | ||
import static io.servicetalk.examples.http.serialization.protobuf.SerializerUtils.RESP_SERIALIZER; | ||
|
||
public final class ProtobufClient { | ||
public static void main(String[] args) throws Exception { | ||
try (HttpClient client = HttpClients.forSingleAddress("localhost", 8080).build()) { | ||
client.request(client.post("/protobuf") | ||
.payloadBody(RequestMessage.newBuilder().setMessage("value").build(), REQ_SERIALIZER)) | ||
.whenOnSuccess(resp -> { | ||
System.out.println(resp.toString((name, value) -> value)); | ||
System.out.println(resp.payloadBody(RESP_SERIALIZER)); | ||
}) | ||
// This example is demonstrating asynchronous execution, but needs to prevent the main thread from exiting | ||
// before the response has been processed. This isn't typical usage for an asynchronous API but is useful | ||
// for demonstration purposes. | ||
.toFuture().get(); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...c/main/java/io/servicetalk/examples/http/serialization/protobuf/async/ProtobufServer.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,45 @@ | ||
/* | ||
* Copyright © 2022 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.servicetalk.examples.http.serialization.protobuf.async; | ||
|
||
import io.servicetalk.examples.http.serialization.protobuf.ExampleProtos.RequestMessage; | ||
import io.servicetalk.examples.http.serialization.protobuf.ExampleProtos.ResponseMessage; | ||
import io.servicetalk.http.netty.HttpServers; | ||
|
||
import static io.servicetalk.concurrent.api.Single.succeeded; | ||
import static io.servicetalk.examples.http.serialization.protobuf.SerializerUtils.REQ_SERIALIZER; | ||
import static io.servicetalk.examples.http.serialization.protobuf.SerializerUtils.RESP_SERIALIZER; | ||
import static io.servicetalk.http.api.HttpHeaderNames.ALLOW; | ||
import static io.servicetalk.http.api.HttpRequestMethod.POST; | ||
|
||
public final class ProtobufServer { | ||
public static void main(String[] args) throws Exception { | ||
HttpServers.forPort(8080) | ||
.listenAndAwait((ctx, request, responseFactory) -> { | ||
if (!"/protobuf".equals(request.requestTarget())) { | ||
return succeeded(responseFactory.notFound()); | ||
} | ||
if (!POST.equals(request.method())) { | ||
return succeeded(responseFactory.methodNotAllowed().addHeader(ALLOW, POST.name())); | ||
} | ||
RequestMessage req = request.payloadBody(REQ_SERIALIZER); | ||
ResponseMessage resp = ResponseMessage.newBuilder().setLength(req.getMessage().length()).build(); | ||
return succeeded(responseFactory.created() | ||
.payloadBody(resp, RESP_SERIALIZER)); | ||
}) | ||
.awaitShutdown(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...ain/java/io/servicetalk/examples/http/serialization/protobuf/async/ProtobufUrlClient.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,40 @@ | ||
/* | ||
* Copyright © 2022 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.servicetalk.examples.http.serialization.protobuf.async; | ||
|
||
import io.servicetalk.examples.http.serialization.protobuf.ExampleProtos; | ||
import io.servicetalk.http.api.HttpClient; | ||
import io.servicetalk.http.netty.HttpClients; | ||
|
||
import static io.servicetalk.examples.http.serialization.protobuf.SerializerUtils.REQ_SERIALIZER; | ||
import static io.servicetalk.examples.http.serialization.protobuf.SerializerUtils.RESP_SERIALIZER; | ||
|
||
public final class ProtobufUrlClient { | ||
public static void main(String[] args) throws Exception { | ||
try (HttpClient client = HttpClients.forMultiAddressUrl().build()) { | ||
client.request(client.post("http://localhost:8080/protobuf") | ||
.payloadBody(ExampleProtos.RequestMessage.newBuilder().setMessage("hello").build(), REQ_SERIALIZER)) | ||
.whenOnSuccess(resp -> { | ||
System.out.println(resp.toString((name, value) -> value)); | ||
System.out.println(resp.payloadBody(RESP_SERIALIZER)); | ||
}) | ||
// This example is demonstrating asynchronous execution, but needs to prevent the main thread from exiting | ||
// before the response has been processed. This isn't typical usage for an asynchronous API but is useful | ||
// for demonstration purposes. | ||
.toFuture().get(); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...src/main/java/io/servicetalk/examples/http/serialization/protobuf/async/package-info.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,19 @@ | ||
/* | ||
* Copyright © 2022 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
@ElementsAreNonnullByDefault | ||
package io.servicetalk.examples.http.serialization.protobuf.async; | ||
|
||
import io.servicetalk.annotations.ElementsAreNonnullByDefault; |
42 changes: 42 additions & 0 deletions
42
...icetalk/examples/http/serialization/protobuf/async/streaming/ProtobufStreamingClient.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,42 @@ | ||
/* | ||
* Copyright © 2022 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.servicetalk.examples.http.serialization.protobuf.async.streaming; | ||
|
||
import io.servicetalk.examples.http.serialization.protobuf.ExampleProtos.RequestMessage; | ||
import io.servicetalk.http.api.StreamingHttpClient; | ||
import io.servicetalk.http.netty.HttpClients; | ||
|
||
import static io.servicetalk.concurrent.api.Publisher.from; | ||
import static io.servicetalk.examples.http.serialization.protobuf.SerializerUtils.REQ_STREAMING_SERIALIZER; | ||
import static io.servicetalk.examples.http.serialization.protobuf.SerializerUtils.RESP_STREAMING_SERIALIZER; | ||
|
||
public final class ProtobufStreamingClient { | ||
public static void main(String[] args) throws Exception { | ||
try (StreamingHttpClient client = HttpClients.forSingleAddress("localhost", 8080).buildStreaming()) { | ||
client.request(client.post("/protobuf") | ||
.payloadBody(from("value1", "value22", "value333") | ||
.map(message -> RequestMessage.newBuilder().setMessage(message).build()), | ||
REQ_STREAMING_SERIALIZER)) | ||
.beforeOnSuccess(response -> System.out.println(response.toString((name, value) -> value))) | ||
.flatMapPublisher(resp -> resp.payloadBody(RESP_STREAMING_SERIALIZER)) | ||
.whenOnNext(System.out::println) | ||
// This example is demonstrating asynchronous execution, but needs to prevent the main thread from exiting | ||
// before the response has been processed. This isn't typical usage for an asynchronous API but is useful | ||
// for demonstration purposes. | ||
.toFuture().get(); | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...icetalk/examples/http/serialization/protobuf/async/streaming/ProtobufStreamingServer.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,44 @@ | ||
/* | ||
* Copyright © 2022 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.servicetalk.examples.http.serialization.protobuf.async.streaming; | ||
|
||
import io.servicetalk.examples.http.serialization.protobuf.ExampleProtos.ResponseMessage; | ||
import io.servicetalk.http.netty.HttpServers; | ||
|
||
import static io.servicetalk.concurrent.api.Single.succeeded; | ||
import static io.servicetalk.examples.http.serialization.protobuf.SerializerUtils.REQ_STREAMING_SERIALIZER; | ||
import static io.servicetalk.examples.http.serialization.protobuf.SerializerUtils.RESP_STREAMING_SERIALIZER; | ||
import static io.servicetalk.http.api.HttpHeaderNames.ALLOW; | ||
import static io.servicetalk.http.api.HttpRequestMethod.POST; | ||
|
||
public final class ProtobufStreamingServer { | ||
public static void main(String[] args) throws Exception { | ||
HttpServers.forPort(8080) | ||
.listenStreamingAndAwait((ctx, request, responseFactory) -> { | ||
if (!"/protobuf".equals(request.requestTarget())) { | ||
return succeeded(responseFactory.notFound()); | ||
} | ||
if (!POST.equals(request.method())) { | ||
return succeeded(responseFactory.methodNotAllowed().addHeader(ALLOW, POST.name())); | ||
} | ||
return succeeded(responseFactory.created() | ||
.payloadBody(request.payloadBody(REQ_STREAMING_SERIALIZER) | ||
.map(req -> ResponseMessage.newBuilder().setLength(req.getMessage().length()).build()), | ||
RESP_STREAMING_SERIALIZER)); | ||
}) | ||
.awaitShutdown(); | ||
} | ||
} |
Oops, something went wrong.