Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,7 @@ private Future<Void> handleHeadersFrame(GrpcHeadersFrame frame, boolean end) {
httpRequest.putHeader(GrpcHeaderNames.GRPC_TIMEOUT, headerValue);
}

String contentType;
switch (frame.format()) {
case PROTOBUF:
contentType = "application/grpc";
break;
case JSON:
contentType = "application/grpc+json";
break;
default:
throw new UnsupportedOperationException();
}
String contentType = frame.format().mediaType();

String uri = serviceName.pathOf(methodName);
httpRequest.putHeader(HttpHeaders.CONTENT_TYPE, contentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
import com.google.protobuf.Message;
import com.google.protobuf.MessageOrBuilder;
import com.google.protobuf.Parser;
import com.google.protobuf.util.JsonFormat;
import io.vertx.codegen.annotations.Unstable;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.DecodeException;
import io.vertx.core.json.Json;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;
import io.vertx.grpc.common.impl.ProtobufJsonReader;

import java.nio.charset.StandardCharsets;
import java.util.function.Supplier;

public interface GrpcMessageDecoder<T> {
Expand All @@ -39,23 +38,20 @@ static <T> GrpcMessageDecoder<T> decoder(MessageOrBuilder messageOrBuilder) {
return new GrpcMessageDecoder<>() {
@Override
public T decode(GrpcMessage msg) throws CodecException {
switch (msg.format()) {
case PROTOBUF:
try {
return parser.parseFrom(msg.payload().getBytes());
} catch (InvalidProtocolBufferException e) {
throw new CodecException(e);
}
case JSON:
try {
Message.Builder builder = dit.toBuilder();
JsonFormat.parser().merge(msg.payload().toString(StandardCharsets.UTF_8), builder);
return (T) builder.build();
} catch (InvalidProtocolBufferException e) {
throw new CodecException(e);
}
default:
throw new IllegalArgumentException("Invalid wire format: " + msg.format());
WireFormat format = msg.format();
if (format instanceof ProtobufWireFormat) {
try {
return parser.parseFrom(msg.payload().getBytes());
} catch (InvalidProtocolBufferException e) {
throw new CodecException(e);
}
} else if (format instanceof JsonWireFormat) {
JsonWireFormat json = (JsonWireFormat) format;
Message.Builder builder = dit.toBuilder();
ProtobufJsonReader.create(json).merge(msg.payload(), builder);
return (T) builder.build();
} else {
throw new IllegalArgumentException("Invalid wire format: " + format);
}
}
@Override
Expand Down Expand Up @@ -89,17 +85,14 @@ static <T> GrpcMessageDecoder<T> json(Supplier<Message.Builder> builder) {
return new GrpcMessageDecoder<>() {
@Override
public T decode(GrpcMessage msg) throws CodecException {
try {
Message.Builder builderInstance = builder.get();
JsonFormat.parser().merge(msg.payload().toString(StandardCharsets.UTF_8), builderInstance);
return (T) builderInstance.build();
} catch (InvalidProtocolBufferException e) {
throw new CodecException(e);
}
JsonWireFormat json = (JsonWireFormat) msg.format();
Message.Builder builderInstance = builder.get();
ProtobufJsonReader.create(json).merge(msg.payload(), builderInstance);
return (T) builderInstance.build();
}
@Override
public boolean accepts(WireFormat format) {
return format == WireFormat.JSON;
return format instanceof JsonWireFormat;
}
};
}
Expand All @@ -115,7 +108,7 @@ static <T> GrpcMessageDecoder<T> json(Class<T> clazz) {
return new GrpcMessageDecoder<>() {
@Override
public T decode(GrpcMessage msg) throws CodecException {
if (!WireFormat.JSON.equals(msg.format())) {
if (!(msg.format() instanceof JsonWireFormat)) {
throw new CodecException("Was expecting a json message");
}
try {
Expand All @@ -126,7 +119,7 @@ public T decode(GrpcMessage msg) throws CodecException {
}
@Override
public boolean accepts(WireFormat format) {
return format == WireFormat.JSON;
return format instanceof JsonWireFormat;
}
};
}
Expand All @@ -146,7 +139,7 @@ public JsonObject decode(GrpcMessage msg) throws CodecException {
}
@Override
public boolean accepts(WireFormat format) {
return format == WireFormat.JSON;
return format instanceof JsonWireFormat;
}
};

Expand All @@ -156,7 +149,7 @@ public boolean accepts(WireFormat format) {
GrpcMessageDecoder<Object> JSON_VALUE = new GrpcMessageDecoder<Object>() {
@Override
public Object decode(GrpcMessage msg) throws CodecException {
if (!WireFormat.JSON.equals(msg.format())) {
if (!(msg.format() instanceof JsonWireFormat)) {
throw new CodecException("Was expecting a json message");
}
try {
Expand All @@ -167,7 +160,7 @@ public Object decode(GrpcMessage msg) throws CodecException {
}
@Override
public boolean accepts(WireFormat format) {
return format == WireFormat.JSON;
return format instanceof JsonWireFormat;
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package io.vertx.grpc.common;

import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.MessageLite;
import com.google.protobuf.MessageOrBuilder;
import com.google.protobuf.util.JsonFormat;
import io.vertx.codegen.annotations.GenIgnore;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.Json;
import io.vertx.core.json.JsonObject;
import io.vertx.grpc.common.impl.ProtobufJsonWriter;

public interface GrpcMessageEncoder<T> {

Expand All @@ -20,26 +19,17 @@ static <T extends MessageLite> GrpcMessageEncoder<T> encoder() {
return new GrpcMessageEncoder<T>() {
@Override
public GrpcMessage encode(T msg, WireFormat format) throws CodecException {
switch (format) {
case PROTOBUF:
byte[] bytes = msg.toByteArray();
return GrpcMessage.message("identity", Buffer.buffer(bytes));
case JSON:
if (msg instanceof MessageOrBuilder) {
MessageOrBuilder mob = (MessageOrBuilder) msg;
try {
String res = JsonFormat.printer().print(mob);
return GrpcMessage.message("identity", WireFormat.JSON, Buffer.buffer(res));
} catch (InvalidProtocolBufferException e) {
throw new CodecException(e);
}
}
return GrpcMessage.message(
"identity",
WireFormat.JSON,
Json.encodeToBuffer(msg));
default:
throw new IllegalArgumentException("Invalid wire format: " + format);
if (format instanceof ProtobufWireFormat) {
byte[] bytes = msg.toByteArray();
return GrpcMessage.message("identity", format, Buffer.buffer(bytes));
} else if (format instanceof JsonWireFormat) {
JsonWireFormat json = (JsonWireFormat) format;
if (msg instanceof MessageOrBuilder) {
return GrpcMessage.message("identity", format, ProtobufJsonWriter.create(json).write((MessageOrBuilder) msg));
}
return GrpcMessage.message("identity", format, Json.encodeToBuffer(msg));
} else {
throw new IllegalArgumentException("Invalid wire format: " + format);
}
}
@Override
Expand Down Expand Up @@ -70,23 +60,15 @@ static <T> GrpcMessageEncoder<T> json() {
return new GrpcMessageEncoder<>() {
@Override
public GrpcMessage encode(T msg, WireFormat format) throws CodecException {
JsonWireFormat json = (JsonWireFormat) format;
if (msg instanceof MessageOrBuilder) {
MessageOrBuilder mob = (MessageOrBuilder) msg;
try {
String res = JsonFormat.printer().print(mob);
return GrpcMessage.message("identity", WireFormat.JSON, Buffer.buffer(res));
} catch (InvalidProtocolBufferException e) {
throw new CodecException(e);
}
return GrpcMessage.message("identity", format, ProtobufJsonWriter.create(json).write((MessageOrBuilder) msg));
}
return GrpcMessage.message(
"identity",
WireFormat.JSON,
Json.encodeToBuffer(msg));
return GrpcMessage.message("identity", format, Json.encodeToBuffer(msg));
}
@Override
public boolean accepts(WireFormat format) {
return format == WireFormat.JSON;
return format instanceof JsonWireFormat;
}
};
}
Expand All @@ -97,11 +79,11 @@ public boolean accepts(WireFormat format) {
GrpcMessageEncoder<JsonObject> JSON_OBJECT = new GrpcMessageEncoder<>() {
@Override
public GrpcMessage encode(JsonObject msg, WireFormat format) throws CodecException {
return GrpcMessage.message("identity", WireFormat.JSON, msg == null ? Buffer.buffer("null") : msg.toBuffer());
return GrpcMessage.message("identity", format, msg == null ? Buffer.buffer("null") : msg.toBuffer());
}
@Override
public boolean accepts(WireFormat format) {
return format == WireFormat.JSON;
return format instanceof JsonWireFormat;
}
};

Expand Down
Loading
Loading