-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
177 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package io.github.ngyewch.twirp; | ||
|
||
public class Constants { | ||
public static final String PROTOBUF_CONTENT_TYPE = "application/protobuf"; | ||
public static final String JSON_CONTENT_TYPE = "application/json"; | ||
} |
83 changes: 83 additions & 0 deletions
83
src/main/java/io/github/ngyewch/twirp/helidon/client/AbstractService.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,83 @@ | ||
package io.github.ngyewch.twirp.helidon.client; | ||
|
||
import com.google.protobuf.InvalidProtocolBufferException; | ||
import com.google.protobuf.Message; | ||
import com.google.protobuf.util.JsonFormat; | ||
import io.github.ngyewch.twirp.Constants; | ||
import io.helidon.common.http.MediaType; | ||
import io.helidon.webclient.WebClient; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
public abstract class AbstractService { | ||
private static final MediaType PROTOBUF_MEDIA_TYPE = | ||
MediaType.parse(Constants.PROTOBUF_CONTENT_TYPE); | ||
private static final MediaType JSON_MEDIA_TYPE = MediaType.parse(Constants.JSON_CONTENT_TYPE); | ||
|
||
private final WebClient webClient; | ||
private final MediaType contentType; | ||
|
||
protected AbstractService(String baseUri, MediaType contentType) { | ||
super(); | ||
|
||
webClient = WebClient.builder().baseUri(baseUri).build(); | ||
this.contentType = contentType; | ||
} | ||
|
||
protected void doRequest(String path, Message input, Message.Builder outputBuilder) { | ||
if (contentType.equals(PROTOBUF_MEDIA_TYPE)) { | ||
doProtobufRequest(path, input, outputBuilder); | ||
} else if (contentType.equals(JSON_MEDIA_TYPE)) { | ||
doJsonRequest(path, input, outputBuilder); | ||
} else { | ||
throw new IllegalArgumentException("unsupported content type"); | ||
} | ||
} | ||
|
||
private void doProtobufRequest(String path, Message input, Message.Builder outputBuilder) { | ||
try { | ||
webClient | ||
.post() | ||
.path(path) | ||
.contentType(PROTOBUF_MEDIA_TYPE) | ||
.submit(input.toByteArray(), byte[].class) | ||
.map( | ||
responseBytes -> { | ||
try { | ||
return outputBuilder.mergeFrom(responseBytes); | ||
} catch (InvalidProtocolBufferException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}) | ||
.get(); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} catch (ExecutionException e) { | ||
throw new RuntimeException(e.getCause()); | ||
} | ||
} | ||
|
||
private void doJsonRequest(String path, Message input, Message.Builder outputBuilder) { | ||
try { | ||
final String requestJson = JsonFormat.printer().print(input); | ||
webClient | ||
.post() | ||
.path(path) | ||
.contentType(JSON_MEDIA_TYPE) | ||
.submit(requestJson, String.class) | ||
.map( | ||
responseJson -> { | ||
try { | ||
JsonFormat.parser().merge(responseJson, outputBuilder); | ||
return outputBuilder; | ||
} catch (InvalidProtocolBufferException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}) | ||
.get(); | ||
} catch (InterruptedException | InvalidProtocolBufferException e) { | ||
throw new RuntimeException(e); | ||
} catch (ExecutionException e) { | ||
throw new RuntimeException(e.getCause()); | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/io/github/ngyewch/twirp/helidon/server/Handler.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,65 @@ | ||
package io.github.ngyewch.twirp.helidon.server; | ||
|
||
import com.google.protobuf.InvalidProtocolBufferException; | ||
import com.google.protobuf.Message; | ||
import com.google.protobuf.util.JsonFormat; | ||
import io.github.ngyewch.twirp.Constants; | ||
import io.helidon.common.http.Http; | ||
import io.helidon.common.http.MediaType; | ||
import io.helidon.webserver.ServerRequest; | ||
import io.helidon.webserver.ServerResponse; | ||
import java.util.function.Supplier; | ||
|
||
public class Handler { | ||
private static final MediaType PROTOBUF_MEDIA_TYPE = | ||
MediaType.parse(Constants.PROTOBUF_CONTENT_TYPE); | ||
private static final MediaType JSON_MEDIA_TYPE = MediaType.parse(Constants.JSON_CONTENT_TYPE); | ||
|
||
public static void handleTwirp( | ||
ServerRequest req, | ||
ServerResponse res, | ||
Message.Builder messageBuilder, | ||
Supplier<Message> serviceInvoker) { | ||
if (req.headers().contentType().isEmpty()) { | ||
res.status(Http.Status.BAD_REQUEST_400).send("Content-Type not specified"); | ||
return; | ||
} | ||
final MediaType contentType = req.headers().contentType().get(); | ||
if (contentType.equals(PROTOBUF_MEDIA_TYPE)) { | ||
req.content() | ||
.as(byte[].class) | ||
.thenAccept( | ||
contentBytes -> { | ||
try { | ||
messageBuilder.mergeFrom(contentBytes); | ||
final Message response = serviceInvoker.get(); | ||
res.headers().contentType(contentType); | ||
res.send(response.toByteArray()); | ||
} catch (InvalidProtocolBufferException e) { | ||
res.status(Http.Status.BAD_REQUEST_400).send("Malformed content"); | ||
} catch (Exception e) { | ||
res.status(Http.Status.INTERNAL_SERVER_ERROR_500).send(e.toString()); | ||
} | ||