-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from Xorlev/streaming
Server Streaming RPC support
- Loading branch information
Showing
16 changed files
with
551 additions
and
80 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
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
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
32 changes: 32 additions & 0 deletions
32
jersey-rpc-support/src/main/java/com/fullcontact/rpc/jersey/ErrorHandler.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,32 @@ | ||
package com.fullcontact.rpc.jersey; | ||
|
||
import javax.ws.rs.container.AsyncResponse; | ||
import java.io.IOException; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Registry for (currently) JVM-global gRPC error handlers. This allows users to override the error handling | ||
* methodology without patching the library. | ||
* | ||
* This class should be considered unstable, a more comprehensive plugin mechanism will be built in the future. | ||
*/ | ||
public final class ErrorHandler { | ||
private static GrpcJerseyErrorHandler errorHandler = new GrpcJerseyErrorHandler.Default(); | ||
|
||
private ErrorHandler() {} | ||
|
||
static void handleUnaryError(Throwable t, AsyncResponse response) { | ||
errorHandler.handleUnaryError(t, response); | ||
} | ||
|
||
static Optional<String> handleStreamingError(Throwable t) throws IOException { | ||
return errorHandler.handleStreamingError(t); | ||
} | ||
|
||
/** | ||
* Overrides the default error handler on a global basis. Beware, this shouldn't be done after requests start. | ||
*/ | ||
public static void setErrorHandler(GrpcJerseyErrorHandler errorHandler) { | ||
ErrorHandler.errorHandler = errorHandler; | ||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
jersey-rpc-support/src/main/java/com/fullcontact/rpc/jersey/GrpcJerseyErrorHandler.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,57 @@ | ||
package com.fullcontact.rpc.jersey; | ||
|
||
import com.google.protobuf.InvalidProtocolBufferException; | ||
import com.google.rpc.Status; | ||
|
||
import javax.ws.rs.container.AsyncResponse; | ||
import javax.ws.rs.core.Response; | ||
import java.io.IOException; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Pluggable error handler used by the {@link JerseyUnaryObserver} and {@link JerseyStreamingObserver}. | ||
*/ | ||
public interface GrpcJerseyErrorHandler { | ||
/** | ||
* Handles an exception raised in a unary (request/response) RPC handler. | ||
* | ||
* @param t throwable raised | ||
* @param response JAX-RS AsyncResponse, can call cancel() or resume() with a string payload or {@link Response}. | ||
*/ | ||
void handleUnaryError(Throwable t, AsyncResponse response); | ||
|
||
/** | ||
* Handles an exception raised in a server streaming RPC handler. As HTTP/1.1 practically doesn't support trailers, | ||
* there isn't a real way to signal well-formed errors except via another streaming payload. | ||
* | ||
* @param t throwable raised. | ||
* @return Literal string, if you want JSON-encoded data use the {@link JsonHandler#streamPrinter()} to | ||
* retain server-sent events compatibility. Return {@link Optional#empty()} to silently abort. | ||
* @throws IOException usually if serialization of errors break. | ||
*/ | ||
Optional<String> handleStreamingError(Throwable t) throws IOException; | ||
|
||
class Default implements GrpcJerseyErrorHandler { | ||
|
||
@Override | ||
public void handleUnaryError(Throwable t, AsyncResponse asyncResponse) { | ||
if(t instanceof InvalidProtocolBufferException) { | ||
asyncResponse.resume(Response.status(Response.Status.BAD_REQUEST).entity(t.getMessage()).build()); | ||
} else { | ||
asyncResponse.resume(GrpcErrorUtil.createJerseyResponse(t)); | ||
} | ||
} | ||
|
||
@Override | ||
public Optional<String> handleStreamingError(Throwable t) throws InvalidProtocolBufferException { | ||
Status grpcError = GrpcErrorUtil.throwableToStatus(t).getPayload(); | ||
|
||
// JsonFormat doesn't support serializing Any. | ||
if (!grpcError.getDetailsList().isEmpty()) { | ||
grpcError = grpcError.toBuilder().clearDetails().build(); | ||
} | ||
|
||
return Optional.of(JsonHandler.streamPrinter().print(grpcError)); | ||
} | ||
} | ||
} |
Oops, something went wrong.