@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()); } }
/** * {@inheritDoc} */ @Override public int available() throws IOException { return inputBuffer.available(); }
/** * {@inheritDoc} */ @Override public int read(final char[] cbuf, final int off, final int len) throws IOException { return inputBuffer.read(cbuf, off, len); }
/** * {@inheritDoc} */ @Override public void notifyAvailable(ReadHandler handler) { inputBuffer.notifyAvailable(handler); }
/** * {@inheritDoc} */ @Override public Buffer readBuffer(final int size) { return inputBuffer.readBuffer(size); } // -------------------------------------------------- Methods from Cacheable
/** * {@inheritDoc} */ @Override public void flush() throws IOException { outputBuffer.flush(); }
/** * {@inheritDoc} */ @Override public int read() throws IOException { return inputBuffer.readByte(); }
/** * {@inheritDoc} */ @Override public void close() throws IOException { inputBuffer.close(); }
/** * {@inheritDoc} * @deprecated the <code>length</code> parameter will be ignored. Pls. use {@link #notifyCanWrite(org.glassfish.grizzly.WriteHandler)}. */ @Override public void notifyCanWrite(final WriteHandler handler, final int length) { outputBuffer.notifyCanWrite(handler); }
/** * {@inheritDoc} */ @Override public void reset() throws IOException { inputBuffer.reset(); }
/** * {@inheritDoc} */ @Override public void write(final int b) throws IOException { outputBuffer.writeByte(b); }
/** * {@inheritDoc} */ @Override public int readyData() { return inputBuffer.availableChar(); }
/** * {@inheritDoc} */ @Override public int read() throws IOException { return inputBuffer.readChar(); }
/** * {@inheritDoc} */ @Override public void write(final Buffer buffer) throws IOException { outputBuffer.writeBuffer(buffer); }
@Override public void recycle() { serverRequest = null; totalReadContentInBytes = 0; super.recycle(); }
/** * {@inheritDoc} */ @Override public boolean isReady() { return (inputBuffer.available() > 0); }
/** * {@inheritDoc} */ @Override public int read(byte[] b, int off, int len) throws IOException { return inputBuffer.read(b, off, len); }
/** * {@inheritDoc} */ @Override public void notifyAvailable(ReadHandler handler, int size) { inputBuffer.notifyAvailable(handler, size); }
/** * {@inheritDoc} */ @Override public Buffer readBuffer() { return inputBuffer.readBuffer(); }