Search in sources :

Example 1 with HttpResponse

use of org.msec.rpc.HttpResponse 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)

Aggregations

ByteString (com.google.protobuf.ByteString)1 Message (com.google.protobuf.Message)1 HttpResponse (org.msec.rpc.HttpResponse)1