ServletOutputStream out = response.getOutputStream(); InputStream in = [ code to get source input stream ]; String mimeType = [ code to get mimetype of data to be served ]; byte[] bytes = new byte[FILEBUFFERSIZE]; int bytesRead; response.setContentType(mimeType); while ((bytesRead = in.read(bytes)) != -1) { out.write(bytes, 0, bytesRead); } // do the following in a finally block: in.close(); out.close();
@Override public void send(final byte[] bytes) throws Exception { rsp.setHeader("Transfer-Encoding", null); ServletOutputStream output = rsp.getOutputStream(); output.write(bytes); output.close(); committed = true; }
@Override public void send(final InputStream stream) throws Exception { ServletOutputStream output = rsp.getOutputStream(); ByteStreams.copy(stream, output); output.close(); stream.close(); committed = true; }
public void handle(String s, Request r, HttpServletRequest req, HttpServletResponse resp) throws IOException { resp.setContentType("text/pain"); String arg = s.substring(1); resp.setHeader(ARG_HEADER, arg); resp.setStatus(200); resp.getOutputStream().print(arg); resp.getOutputStream().flush(); resp.getOutputStream().close(); } };
private void sendMessage(HttpServletResponse response, String message, int statusCode) throws IOException { response.setContentType("text/plain"); response.setCharacterEncoding("UTF-8"); response.setStatus(statusCode); try (InputStream in = new ByteArrayInputStream(message.getBytes("UTF-8"))) { ByteStreams.copy(in, response.getOutputStream()); } finally { response.getOutputStream().close(); } } }
public void handle(String s, Request r, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.addHeader("X-Auth", request.getHeader("Authorization")); response.setStatus(200); response.getOutputStream().flush(); response.getOutputStream().close(); } }
@Override public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { response.setContentLength(1000); AlternativesResponseStream alternatives = (AlternativesResponseStream) response.getOutputStream(); ServletOutputStream gzipStream = alternatives.getStream(); gzipStream.write(1); gzipStream.close(); // ka-blam! (or not?) gzipStream.flush(); } };
response.setContentType("image/jpeg"); ServletOutputStream out = null; try { out = response.getOutputStream(); } catch (IOException e) { e.printStackTrace(); out.flush(); } catch (IOException e) { e.printStackTrace(); out.close(); } catch (IOException e) { e.printStackTrace();
resp.setContentType("message/http"); resp.setContentLength(responseLength); ServletOutputStream out = resp.getOutputStream(); out.print(responseString); out.close(); return;
public void handle(String s, Request r, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException, ServletException { try { Thread.sleep(20 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } httpResponse.setStatus(200); httpResponse.getOutputStream().flush(); httpResponse.getOutputStream().close(); } }
private void returnRawResponse(RequestDetails theRequestDetails, HttpServletResponse theServletResponse, String theContentType, IPrimitiveType<byte[]> theData) { theServletResponse.setStatus(200); if (theRequestDetails.getServer() instanceof RestfulServer) { RestfulServer rs = (RestfulServer) theRequestDetails.getServer(); rs.addHeadersToResponse(theServletResponse); } theServletResponse.addHeader(Constants.HEADER_CONTENT_TYPE, theContentType); // Write the response try { theServletResponse.getOutputStream().write(theData.getValue()); theServletResponse.getOutputStream().close(); } catch (IOException e) { throw new InternalErrorException(e); } } }
@Signature public PHttpServerResponse body(Memory value, String charset) throws IOException { write(value, charset); response.getOutputStream().close(); return this; }
/** * Closes this output stream, causing any buffered data to be flushed and any * further output data to throw an IOException. */ @Override public void close() throws IOException { if (closed) { return; } if (gzipstream != null) { flushToGZip(); gzipstream.close(); gzipstream = null; } else { if (bufferCount > 0) { output.write(buffer, 0, bufferCount); bufferCount = 0; } } output.close(); closed = true; }
private void doJmxValue(HttpServletRequest req, HttpServletResponse resp, String application, String jmxValueParameter) throws IOException { noCache(resp); resp.setContentType("text/plain"); boolean first = true; for (final URL url : getUrlsByApplication(application)) { if (first) { first = false; } else { resp.getOutputStream().write('|'); resp.getOutputStream().write('|'); } final URL proxyUrl = new URL( url.toString().replace(TransportFormat.SERIALIZED.getCode(), "") .replace(TransportFormat.XML.getCode(), "") + '&' + HttpParameter.JMX_VALUE + '=' + jmxValueParameter); new LabradorRetriever(proxyUrl).copyTo(req, resp); } resp.getOutputStream().close(); }
@Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { try { resp.setContentType(JSON_MIME_TYPE); resp.getOutputStream() .println(new GsonBuilder() .setPrettyPrinting() .create() .toJson(this.statusService.getStatus())); resp.setStatus(HttpServletResponse.SC_OK); } catch (final Exception e) { log.error("Error!! while reporting status: ", e); resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); } finally { resp.getOutputStream().close(); } } }
public void handle(String s, org.eclipse.jetty.server.Request r, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException, ServletException { Enumeration<?> e = httpRequest.getHeaderNames(); String param; while (e.hasMoreElements()) { param = e.nextElement().toString(); httpResponse.addHeader(param, httpRequest.getHeader(param)); } httpResponse.setStatus(200); httpResponse.getOutputStream().flush(); httpResponse.getOutputStream().close(); } }
public void write(byte[] bytes) throws IOException { ServletOutputStream out = response.getOutputStream(); out.write(bytes); out.close(); }