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();
}
Aggregations