ResponseHandler<Object> streamer = null;
String serviceName = null;
final List<Message> fmsgs = new ArrayList<Message>();
final List<Object> frows = new ArrayList<Object>();
ScriptStreamer notif = null;
JsonRequest jr = null;
try {
String qs;
if (req_.getMethod().equalsIgnoreCase("post")) {
is = req_.getInputStream();
StringWriter sw = new StringWriter();
while (true) {
int bb = is.read();
if (bb == -1)
break;
sw.write(bb);
}
qs = sw.toString();
} else {
qs = req_.getQueryString();
}
Map<String,String> argsMap = new HashMap<String, String>();
String[] toks = qs.split("&");
for (String tok : toks) {
tok = URLDecoder.decode(tok, "utf-8");
String[] pair = tok.split("=");
argsMap.put(pair[0], pair.length > 1 ? pair[1] : null);
}
jr = new JsonRequest();
String userId = null;
if (req_.getUserPrincipal() != null)
userId = req_.getUserPrincipal().getName();
jr.setUserId(userId);
jr.setSessionId(req_.getSession().getId());
jr.setReqId(argsMap.get("reqId"));
jr.setService(argsMap.get("service"));
jr.setLang(argsMap.get("lang"));
String val = argsMap.get("streaming");
jr.setStreaming(StringUtil.isEmpty(val) ? false : val.equalsIgnoreCase("yes") || val.equalsIgnoreCase("true"));
jr.setServiceArgsJson(URLDecoder.decode(argsMap.get("args"), "UTF-8"));
List<?> params = _jsh.deserialize(jr.getServiceArgsJson(), List.class);
int nbArgs = params.size();
final JsonRequest fjr = jr;
serviceName = jr.getService();
if (StringUtil.isEmpty(serviceName))
throw new IllegalArgumentException("Missing serviceName");
String key = buildKey(serviceName, nbArgs +1);//+1 for context as arg[0]
Method m = _servicesMap.get(key);
if (m == null)
throw new UnsupportedOperationException(
String.format("Service=%s with %d args doesn't exists",
serviceName, nbArgs +1));
if (!fjr.isStreaming()) {
resp_.setContentType("application/json");
} else {
resp_.setContentType("text/html");
}
os = resp_.getOutputStream();
final OutputStream fos = os;
notif = fjr.isStreaming() ? new ScriptStreamer() : null;
final ScriptStreamer fnotif = notif;
final String reqId = jr.getReqId();
streamer = new ResponseHandler<Object>() {
int rowsPerNotif = 2;
long rowCount = 0;
boolean startedResp = false;
@Override
public void end() {
if (!startedResp)
try {
//startResp(os, fjr.isStreaming(), reqId, notif.funcDef());
startedResp = true;
if (fjr.isStreaming()) {
fos.write(fnotif.funcDef());
fos.flush();
} else {
fos.write(String.format("{\"reqId\":\"%s\",\"data\":[", reqId).getBytes());
fos.flush();
}
} catch (IOException e1) {
_logger.warn("Failed to write beginnig of response", e1);
}
}
@Override
public boolean addRow(Object row_) {
if (row_ == null)
return false;
if (!startedResp)
try {
//startResp(os, fjr.isStreaming(), reqId, notif.funcDef());
startedResp = true;
if (fjr.isStreaming()) {
fos.write(fnotif.funcDef());
fos.flush();
} else {
fos.write(String.format("{\"reqId\":\"%s\",\"data\":[", reqId).getBytes());
fos.flush();
}
} catch (IOException e1) {
_logger.warn("Failed to addRow", e1);
return false;
}
rowCount++;
try {
if (fjr.isStreaming()) {
// send small size notif first, then grows slowly
// to reach a max. This way, results will hit the browser
// very soon.
if (rowsPerNotif < MAX_ROWS_PER_NOTIF)
rowsPerNotif+=2;
frows.add(row_);
if (frows.size() >= rowsPerNotif) {
String json = _jsh.serialize(frows);
frows.clear();
fos.write(fnotif.buildNotif(fjr.getReqId(), "row", json, false));
}
} else {
if (rowCount>1)
fos.write(',');
String json = _jsh.serialize(row_);
fos.write(json.getBytes());
}
fos.flush();
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
@Override
public boolean addMessage(Message message_) {
if (message_ == null)
return false;
if (!startedResp)
try {
//startResp(os, fjr.isStreaming(), reqId, notif.funcDef());
startedResp = true;
if (fjr.isStreaming()) {
fos.write(fnotif.funcDef());
fos.flush();
} else {
fos.write(String.format("{\"reqId\":\"%s\",\"data\":[", reqId).getBytes());
fos.flush();
}
} catch (IOException e1) {
_logger.warn("Failed to addRow", e1);
return false;
}
fmsgs.add(message_);
try {
if (fjr.isStreaming()) {
if (frows.size() > 0) {
String json = _jsh.serialize(frows);
frows.clear();
fos.write(fnotif.buildNotif(fjr.getReqId(), "row", json, false));
}
String json = _jsh.serialize(fmsgs);
fmsgs.clear();
fos.write(fnotif.buildNotif(fjr.getReqId(), "msg", json, false));
fos.flush();
}
return true;
} catch (IOException e) {
_logger.warn("Failed to write row or message", e);