forked from quarkusio/quarkus
-
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
1 parent
323774b
commit 8e30e30
Showing
42 changed files
with
2,437 additions
and
59 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
11 changes: 11 additions & 0 deletions
11
extensions/grpc/api/src/main/java/io/quarkus/grpc/GrpcTranscoding.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,11 @@ | ||
package io.quarkus.grpc; | ||
|
||
import com.google.protobuf.Message; | ||
|
||
public interface GrpcTranscoding { | ||
|
||
String getGrpcServiceName(); | ||
|
||
<Req extends Message, Resp extends Message> GrpcTranscodingDescriptor<Req, Resp> findTranscodingDescriptor( | ||
String methodName); | ||
} |
21 changes: 21 additions & 0 deletions
21
extensions/grpc/api/src/main/java/io/quarkus/grpc/GrpcTranscodingDescriptor.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,21 @@ | ||
package io.quarkus.grpc; | ||
|
||
public class GrpcTranscodingDescriptor<Req extends com.google.protobuf.Message, Resp extends com.google.protobuf.Message> { | ||
|
||
private final GrpcTranscodingMarshaller<Req> requestMarshaller; | ||
private final GrpcTranscodingMarshaller<Resp> responseMarshaller; | ||
|
||
public GrpcTranscodingDescriptor(GrpcTranscodingMarshaller<Req> requestMarshaller, | ||
GrpcTranscodingMarshaller<Resp> responseMarshaller) { | ||
this.requestMarshaller = requestMarshaller; | ||
this.responseMarshaller = responseMarshaller; | ||
} | ||
|
||
public GrpcTranscodingMarshaller<Req> getRequestMarshaller() { | ||
return requestMarshaller; | ||
} | ||
|
||
public GrpcTranscodingMarshaller<Resp> getResponseMarshaller() { | ||
return responseMarshaller; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
extensions/grpc/api/src/main/java/io/quarkus/grpc/GrpcTranscodingMarshaller.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,67 @@ | ||
package io.quarkus.grpc; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import org.jboss.logging.Logger; | ||
|
||
import com.google.protobuf.InvalidProtocolBufferException; | ||
import com.google.protobuf.Message; | ||
import com.google.protobuf.util.JsonFormat; | ||
|
||
import io.grpc.MethodDescriptor; | ||
import io.grpc.Status; | ||
|
||
public class GrpcTranscodingMarshaller<T extends Message> implements MethodDescriptor.PrototypeMarshaller<T> { | ||
|
||
private final static Logger log = Logger.getLogger(GrpcTranscodingMarshaller.class); | ||
|
||
private final T defaultInstance; | ||
|
||
public GrpcTranscodingMarshaller(T defaultInstance) { | ||
this.defaultInstance = checkNotNull(defaultInstance, "defaultInstance cannot be null"); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public Class<T> getMessageClass() { | ||
return (Class<T>) defaultInstance.getClass(); | ||
} | ||
|
||
@Override | ||
public T getMessagePrototype() { | ||
return defaultInstance; | ||
} | ||
|
||
@Override | ||
public InputStream stream(T value) { | ||
try { | ||
String response = JsonFormat.printer().omittingInsignificantWhitespace().print(value); | ||
return new ByteArrayInputStream(response.getBytes(StandardCharsets.UTF_8)); | ||
} catch (InvalidProtocolBufferException e) { | ||
throw Status.INTERNAL.withDescription("Unable to convert message to JSON").withCause(e).asRuntimeException(); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public T parse(InputStream stream) { | ||
try (InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) { | ||
Message.Builder builder = defaultInstance.newBuilderForType(); | ||
JsonFormat.parser().ignoringUnknownFields().merge(reader, builder); | ||
return (T) builder.build(); | ||
} catch (InvalidProtocolBufferException e) { | ||
log.error("Unable to parse JSON to message", e); | ||
throw Status.INTERNAL.withDescription("Unable to parse JSON to message").withCause(e).asRuntimeException(); | ||
} catch (IOException e) { | ||
log.error("An I/O error occurred while parsing the stream", e); | ||
throw Status.INTERNAL.withDescription("An I/O error occurred while parsing the stream").withCause(e) | ||
.asRuntimeException(); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
extensions/grpc/api/src/main/java/io/quarkus/grpc/GrpcTranscodingMethod.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,18 @@ | ||
package io.quarkus.grpc; | ||
|
||
import static java.lang.annotation.ElementType.METHOD; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface GrpcTranscodingMethod { | ||
|
||
String grpcMethodName(); | ||
|
||
String httpMethod(); | ||
|
||
String httpPath(); | ||
} |
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
27 changes: 27 additions & 0 deletions
27
...ns/grpc/deployment/src/main/java/io/quarkus/grpc/deployment/GrpcTranscodingBuildItem.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,27 @@ | ||
package io.quarkus.grpc.deployment; | ||
|
||
import java.util.List; | ||
|
||
import org.jboss.jandex.DotName; | ||
|
||
import io.quarkus.builder.item.MultiBuildItem; | ||
import io.quarkus.grpc.transcoding.GrpcTranscodingMethod; | ||
|
||
public final class GrpcTranscodingBuildItem extends MultiBuildItem { | ||
|
||
final DotName marshallingClass; | ||
final List<GrpcTranscodingMethod> transcodingMethods; | ||
|
||
public GrpcTranscodingBuildItem(DotName marshallingClass, List<GrpcTranscodingMethod> transcodingMethods) { | ||
this.marshallingClass = marshallingClass; | ||
this.transcodingMethods = transcodingMethods; | ||
} | ||
|
||
public DotName getMarshallingClass() { | ||
return marshallingClass; | ||
} | ||
|
||
public List<GrpcTranscodingMethod> getTranscodingMethods() { | ||
return transcodingMethods; | ||
} | ||
} |
Oops, something went wrong.