}
@Override
public CharSequence render(final DataSourceRequest request, final DataTable data) {
StrBuilder sb = new StrBuilder();
sb.append("{\"version\":\"0.6\"");
// If no reqId found in the request, do not return reqId in the response.
String requestId = request.getDataSourceParameters().getRequestId();
if(requestId != null) {
sb.append(",\"reqId\":\"").append(EscapeUtil.jsonEscape(requestId)).append("\"");
}
ResponseStatus responseStatus = null;
if(!data.getWarnings().isEmpty()) {
responseStatus = new ResponseStatus(StatusType.WARNING);
}
if(responseStatus == null) {
// Check signature.
final String previousSignature = request.getDataSourceParameters().getSignature();
if(!StringUtils.isEmpty(previousSignature) && (this.getSignature(data).equals(previousSignature))) {
responseStatus = new ResponseStatus(StatusType.ERROR, ReasonType.NOT_MODIFIED, null);
}
else {
responseStatus = new ResponseStatus(StatusType.OK, null, null);
}
}
StatusType statusType = responseStatus.getStatusType();
sb.append(",\"status\":\"").append(statusType.lowerCaseString()).append("\"");
// There are reason and messages if the status is WARNING/ERROR.
if(statusType != StatusType.OK) {
// Status is warning or error.
if(statusType == StatusType.WARNING) {
List<Warning> warnings = data.getWarnings();
List<String> warningJsonStrings = Lists.newArrayList();
if(warnings != null) {
for(Warning warning : warnings) {
warningJsonStrings.add(getFaultString(warning.getReasonType(), warning.getMessage()));
}
}
sb.append(",\"warnings\":[").appendWithSeparators(warningJsonStrings, ",").append("]");
}
else { // Status is error.
sb.append(",\"errors\":[");
sb.append(getFaultString(responseStatus.getReasonType(), responseStatus.getDescription()));
sb.append("]");
}
}
if((statusType != StatusType.ERROR)) {
// MessageType OK or WARNING,
// so need to attach a data table (and a signature).
sb.append(",\"sig\":\"").append(this.getSignature(data)).append("\"");
sb.append(",\"table\":").append(this.renderDataTable(data, true, true));
}
sb.append("}");
return sb.toString();
}