((JSONStreamAware)value).writeJSONString(out); return;
/** * Stream a JSON stream to a given writer, potentiall wrap it in a callback for a * JSONP response and then flush & close the writer. The writer is closed in any case, * also when an exception occurs * * @param pWriter writer to write to. Must be not null. * @param pJson JSON response to stream * @param callback the name of the callback function if JSONP should be used or <code>null</code> if the answer should be streamed directly * @throws IOException if the streaming fails */ public static void streamResponseAndClose(Writer pWriter, JSONStreamAware pJson, String callback) throws IOException { try { if (callback == null) { pJson.writeJSONString(pWriter); } else { pWriter.write(callback); pWriter.write("("); pJson.writeJSONString(pWriter); pWriter.write(");"); } // Writer end marker for chunked responses pWriter.write(STREAM_END_MARKER); } finally { // Flush and close, even on an exception to avoid locks in the thread pWriter.flush(); pWriter.close(); } } }
((JSONStreamAware)value).writeJSONString(out); return;
@Override public void writeTo(JSONStreamAware jsonStreamAware, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> stringObjectMultivaluedMap, OutputStream outputStream) throws IOException, WebApplicationException { Writer writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8); jsonStreamAware.writeJSONString(writer); writer.write(ENTER); writer.flush(); }
protected void sendJsonResponse(HttpServletResponse response, int statusCode, JSONStreamAware json) throws IOException { if (statusCode == HttpServletResponse.SC_OK || statusCode == HttpServletResponse.SC_CREATED) { response.setStatus(statusCode); } else { response.sendError(statusCode); } response.setStatus(statusCode); response.setContentType(JSON_UTF8); json.writeJSONString(response.getWriter()); }
protected void sendJsonResponse(HttpServletResponse response, int statusCode, JSONStreamAware json) throws IOException { if (statusCode == HttpServletResponse.SC_OK || statusCode == HttpServletResponse.SC_CREATED) { response.setStatus(statusCode); } else { response.sendError(statusCode); } response.setStatus(statusCode); response.setContentType(JSON_UTF8); json.writeJSONString(response.getWriter()); }
/** * Stream a JSON stream to a given writer, potentiall wrap it in a callback for a * JSONP response and then flush & close the writer. The writer is closed in any case, * also when an exception occurs * * @param pWriter writer to write to. Must be not null. * @param pJson JSON response to stream * @param callback the name of the callback function if JSONP should be used or <code>null</code> if the answer should be streamed directly * @throws IOException if the streaming fails */ public static void streamResponseAndClose(Writer pWriter, JSONStreamAware pJson, String callback) throws IOException { try { if (callback == null) { pJson.writeJSONString(pWriter); } else { pWriter.write(callback); pWriter.write("("); pJson.writeJSONString(pWriter); pWriter.write(");"); } // Writer end marker for chunked responses pWriter.write(STREAM_END_MARKER); } finally { // Flush and close, even on an exception to avoid locks in the thread pWriter.flush(); pWriter.close(); } } }
/** * Stream a JSON stream to a given writer, potentiall wrap it in a callback for a * JSONP response and then flush & close the writer. The writer is closed in any case, * also when an exception occurs * * @param pWriter writer to write to. Must be not null. * @param pJson JSON response to stream * @param callback the name of the callback function if JSONP should be used or <code>null</code> if the answer should be streamed directly * @throws IOException if the streaming fails */ public static void streamResponseAndClose(Writer pWriter, JSONStreamAware pJson, String callback) throws IOException { try { if (callback == null) { pJson.writeJSONString(pWriter); } else { pWriter.write(callback); pWriter.write("("); pJson.writeJSONString(pWriter); pWriter.write(");"); } // Writer end marker for chunked responses pWriter.write(STREAM_END_MARKER); } finally { // Flush and close, even on an exception to avoid locks in the thread pWriter.flush(); pWriter.close(); } } }
((JSONStreamAware)value).writeJSONString(out); return;
((JSONStreamAware) value).writeJSONString(out); return;
((JSONStreamAware)value).writeJSONString(out); return;
private void sendStreamingResponse(HttpExchange pExchange, ParsedUri pParsedUri, JSONStreamAware pJson) throws IOException { ChunkedWriter writer = null; try { Headers headers = pExchange.getResponseHeaders(); if (pJson != null) { headers.set("Content-Type", getMimeType(pParsedUri) + "; charset=utf-8"); String callback = pParsedUri.getParameter(ConfigKey.CALLBACK.getKeyValue()); pExchange.sendResponseHeaders(200, 0); writer = new ChunkedWriter(pExchange.getResponseBody(), "UTF-8"); if (callback == null) { pJson.writeJSONString(writer); } else { writer.write(callback); writer.write("("); pJson.writeJSONString(writer); writer.write(");"); } } else { headers.set("Content-Type", "text/plain"); pExchange.sendResponseHeaders(200,-1); } } finally { if (writer != null) { // Always close in order to finish the request. // Otherwise the thread blocks. writer.flush(); writer.close(); } } }