Search in sources :

Example 81 with Message

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();
}
Also used : Message(com.google.protobuf.Message) HttpResponse(org.msec.rpc.HttpResponse) ByteString(com.google.protobuf.ByteString)

Example 82 with Message

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);
    }
}
Also used : HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) HttpInputMessage(org.springframework.http.HttpInputMessage) Message(com.google.protobuf.Message) HttpOutputMessage(org.springframework.http.HttpOutputMessage) InputStreamReader(java.io.InputStreamReader) MediaType(org.springframework.http.MediaType) Charset(java.nio.charset.Charset) IOException(java.io.IOException) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException)

Example 83 with Message

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);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) Message(com.google.protobuf.Message) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) Test(org.junit.Test)

Example 84 with Message

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);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) Message(com.google.protobuf.Message) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 85 with Message

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);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) Message(com.google.protobuf.Message) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) Test(org.junit.Test)

Aggregations

Message (com.google.protobuf.Message)145 Test (org.junit.Test)47 Any (com.google.protobuf.Any)25 Event (io.spine.base.Event)11 ByteString (com.google.protobuf.ByteString)9 Command (io.spine.base.Command)9 EntityRecord (io.spine.server.entity.EntityRecord)8 EntityFilters (io.spine.client.EntityFilters)7 Target (io.spine.client.Target)6 TypeUrl (io.spine.type.TypeUrl)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 CommandId (io.spine.base.CommandId)5 ByteArray (voldemort.utils.ByteArray)5 FieldMask (com.google.protobuf.FieldMask)4 GeneratedMessage (com.google.protobuf.GeneratedMessage)4 Timestamp (com.google.protobuf.Timestamp)4 EventContext (io.spine.base.EventContext)4 EntityId (io.spine.client.EntityId)4 EnclosedMessageFieldValueWithCustomInvalidMessage (io.spine.test.validate.msg.EnclosedMessageFieldValueWithCustomInvalidMessage)4