@Override public void service(Request req, Response resp) throws Exception { try { OTPRequest oreq = new OTPRequest(req, graph); Object result = handlers.get(oreq.action).handle(oreq); ObjectMapper mapper; if (oreq.sfmt == SerializeFormat.XML) { resp.setContentType("application/xml"); mapper = xmlMapper; } else { resp.setContentType("application/json"); mapper = jsonMapper; } resp.setStatus(200); mapper.writeValue(resp.getNIOOutputStream(), result); } catch (Exception ex) { resp.setStatus(500); resp.setContentType("text/plain"); resp.getNIOWriter().write("Error: " + ex.toString()); } }
@Override public void onAllDataRead() throws Exception { System.out.printf("[onAllDataRead] length: %d\n", in.readyData()); try { echoAvailableData(in, out, buf); } finally { try { in.close(); } catch (IOException ignored) { } try { out.close(); } catch (IOException ignored) { } response.resume(); } } });
private void echoAvailableData(NIOReader in, NIOWriter out, char[] buf) throws IOException { while(in.isReady()) { int len = in.read(buf); out.write(buf, 0, len); } }