use of com.google.protobuf.Message in project MSEC by Tencent.
the class ResponseEncoder method serializeHTTPPakcage.
protected ChannelBuffer serializeHTTPPakcage(ChannelHandlerContext channelHandlerContext, RpcResponse response, ChannelBufferOutputStream stream) throws IOException {
HttpResponse httpResponse = new HttpResponse();
String body = "";
httpResponse.setStatusCode("200 OK");
if (response.getResultObj() != null && !(response.getResultObj() instanceof Message)) {
//If result object is not protobuf message, just call toString()
httpResponse.setContentType("text/html; charset=utf-8");
body = response.getResultObj().toString();
} else {
//If result object is protobuf message, transfer it into json
httpResponse.setContentType("application/json");
body = "{\"ret\":" + response.getErrno();
body += ", \"errmsg\": \"";
if (response.getError() != null)
body += response.getError().getMessage();
body += "\"";
body += ", \"resultObj\":";
if (response.getResultObj() != null && response.getResultObj() instanceof Message) {
body += JsonFormat.printToString((Message) response.getResultObj());
} else {
body += "{}";
}
body += "}";
}
httpResponse.setBody(body);
stream.writeBytes(httpResponse.write());
return stream.buffer();
}
use of com.google.protobuf.Message in project spring-framework by spring-projects.
the class ProtobufHttpMessageConverter method readInternal.
@Override
protected Message readInternal(Class<? extends Message> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
MediaType contentType = inputMessage.getHeaders().getContentType();
if (contentType == null) {
contentType = PROTOBUF;
}
Charset charset = contentType.getCharset();
if (charset == null) {
charset = DEFAULT_CHARSET;
}
try {
Message.Builder builder = getMessageBuilder(clazz);
if (PROTOBUF.isCompatibleWith(contentType)) {
builder.mergeFrom(inputMessage.getBody(), this.extensionRegistry);
} else if (MediaType.TEXT_PLAIN.isCompatibleWith(contentType)) {
InputStreamReader reader = new InputStreamReader(inputMessage.getBody(), charset);
TextFormat.merge(reader, this.extensionRegistry, builder);
} else if (isProtobufJavaUtilPresent || isProtobufJavaFormatPresent) {
this.protobufFormatsSupport.merge(inputMessage.getBody(), charset, contentType, this.extensionRegistry, builder);
}
return builder.build();
} catch (Exception ex) {
throw new HttpMessageNotReadableException("Could not read Protobuf message: " + ex.getMessage(), ex);
}
}
use of com.google.protobuf.Message in project spring-framework by spring-projects.
the class ProtobufHttpMessageConverterTests method read.
@Test
public void read() throws IOException {
byte[] body = this.testMsg.toByteArray();
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
inputMessage.getHeaders().setContentType(ProtobufHttpMessageConverter.PROTOBUF);
Message result = this.converter.read(Msg.class, inputMessage);
assertEquals(this.testMsg, result);
}
use of com.google.protobuf.Message in project spring-framework by spring-projects.
the class ProtobufHttpMessageConverterTests method write.
@Test
public void write() throws IOException {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
MediaType contentType = ProtobufHttpMessageConverter.PROTOBUF;
this.converter.write(this.testMsg, contentType, outputMessage);
assertEquals(contentType, outputMessage.getHeaders().getContentType());
assertTrue(outputMessage.getBodyAsBytes().length > 0);
Message result = Msg.parseFrom(outputMessage.getBodyAsBytes());
assertEquals(this.testMsg, result);
String messageHeader = outputMessage.getHeaders().getFirst(ProtobufHttpMessageConverter.X_PROTOBUF_MESSAGE_HEADER);
assertEquals("Msg", messageHeader);
String schemaHeader = outputMessage.getHeaders().getFirst(ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER);
assertEquals("sample.proto", schemaHeader);
}
use of com.google.protobuf.Message in project spring-framework by spring-projects.
the class ProtobufHttpMessageConverterTests method readNoContentType.
@Test
public void readNoContentType() throws IOException {
byte[] body = this.testMsg.toByteArray();
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
Message result = this.converter.read(Msg.class, inputMessage);
assertEquals(this.testMsg, result);
}
Aggregations