/** * Write the given temporary OutputStream to the HTTP response. * @param response current HTTP response * @param baos the temporary OutputStream to write * @throws IOException if writing/flushing failed */ protected void writeToResponse(HttpServletResponse response, ByteArrayOutputStream baos) throws IOException { // Write content type and also length (determined via byte array). response.setContentType(getContentType()); response.setContentLength(baos.size()); // Flush byte array to servlet output stream. ServletOutputStream out = response.getOutputStream(); baos.writeTo(out); out.flush(); }
/** * Writes the given data to the given outputstream in two steps with extra * flushes to make servers notice if the connection has been closed. This * enables caching the message instead of losing it, if the client is in the * progress of reconnecting * * @param o the stream to write to * @param data the data to write * @throws IOException if an exception occurs during writing */ private void twoStepWrite(ServletOutputStream o, byte[] data) throws IOException { o.write(data, 0, 1); o.flush(); o.write(data, 1, data.length - 1); o.flush(); }
private void serveDeobfuscator(HttpServletResponse resp) throws IOException { ClassLoader loader = CodeServlet.class.getClassLoader(); resp.setStatus(HttpServletResponse.SC_OK); resp.setCharacterEncoding("UTF-8"); resp.setContentType("text/plain"); try (InputStream input = loader.getResourceAsStream("teavm/devserver/deobfuscator.js")) { IOUtils.copy(input, resp.getOutputStream()); } resp.getOutputStream().flush(); }
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(); } }
public void handle(String s, Request r, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException, ServletException { int size = 10 * 1024; if (httpRequest.getContentLength() > 0) { size = httpRequest.getContentLength(); } byte[] bytes = new byte[size]; if (bytes.length > 0) { final int read = httpRequest.getInputStream().read(bytes); httpResponse.getOutputStream().write(bytes, 0, read); } httpResponse.setStatus(200); httpResponse.getOutputStream().flush(); } }
@Override protected void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { LOGGER.debug(">>> got a request !"); String authorization = request.getHeader(PROXY_AUTHORIZATION.toString()); if (authorization == null) { response.setStatus(HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED); response.setHeader(PROXY_AUTHENTICATE.toString(), "Basic realm=\"Fake Realm\""); response.getOutputStream().flush(); } else if (authorization.equals("Basic am9obmRvZTpwYXNz")) { super.service(request, response); } else { response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); response.getOutputStream().flush(); } } }
@Override protected void renderMergedOutputModel(Map<String, Object> model, // HttpServletRequest request, // HttpServletResponse response) throws Exception { Object value = filterModel(model); String jsonpParameterValue = getJsonpParameterValue(request); if(jsonpParameterValue != null) { JSONPObject jsonpObject = new JSONPObject(jsonpParameterValue); jsonpObject.addParameter(value); value = jsonpObject; } ByteArrayOutputStream outnew = new ByteArrayOutputStream(); int len = JSON.writeJSONString(outnew, // fastJsonConfig.getCharset(), // value, // fastJsonConfig.getSerializeConfig(), // fastJsonConfig.getSerializeFilters(), // fastJsonConfig.getDateFormat(), // JSON.DEFAULT_GENERATE_FEATURE, // fastJsonConfig.getSerializerFeatures()); if (this.updateContentLength) { // Write content length (determined via byte array). response.setContentLength(len); } // Flush byte array to servlet output stream. ServletOutputStream out = response.getOutputStream(); outnew.writeTo(out); outnew.close(); out.flush(); }
private void setOutput(boolean needToZip, File file, HttpServletResponse response) throws IOException { ServletOutputStream out = response.getOutputStream(); if (needToZip) { new ZipUtil().zip(file, out, Deflater.NO_COMPRESSION); } else { IOUtils.copy(new FileInputStream(file), out); } out.flush(); }
@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(); } };
/** * Writes out a <code>byte</code> to the ServletOuputStream. * * @param bytes the bytes to write to the <code>ServletOutputStream</code>. */ private void writeBytesToStream(byte[] bytes, HttpServletResponse response) { response.setContentType(CONTENT_TYPE); // Send image try (ServletOutputStream sos = response.getOutputStream()) { sos.write(bytes); sos.flush(); } catch (IOException e) { // Do nothing } }
private boolean serveSourceFile(String fileName, HttpServletResponse resp) throws IOException { try (InputStream stream = sourceFileCache.computeIfAbsent(fileName, this::findSourceFile).get()) { if (stream == null) { return false; } resp.setStatus(HttpServletResponse.SC_OK); resp.setCharacterEncoding("UTF-8"); resp.setContentType("text/plain"); IOUtils.copy(stream, resp.getOutputStream()); resp.getOutputStream().flush(); return true; } }
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(); } }
public void handle(String s, Request r, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException, ServletException { int size = 10 * 1024; if (httpRequest.getContentLength() > 0) { size = httpRequest.getContentLength(); } byte[] bytes = new byte[size]; if (bytes.length > 0) { httpRequest.getInputStream().read(bytes); httpResponse.getOutputStream().write(bytes); } httpResponse.setStatus(200); httpResponse.getOutputStream().flush(); } }
final ServletOutputStream out = res.getOutputStream(); final OutboundConnectionHandler handler = new OutboundConnectionHandler(out); pipeline.addLast("handler", handler); try { res.setStatus(HttpServletResponse.SC_OK); res.setHeader(HttpHeaders.Names.CONTENT_TYPE, "application/octet-stream"); res.setHeader(HttpHeaders.Names.CONTENT_TRANSFER_ENCODING, HttpHeaders.Values.BINARY); out.flush();
@Override protected void renderMergedOutputModel(Map<String, Object> model, // HttpServletRequest request, // HttpServletResponse response) throws Exception { Object value = filterModel(model); String jsonpParameterValue = getJsonpParameterValue(request); if(jsonpParameterValue != null) { JSONPObject jsonpObject = new JSONPObject(jsonpParameterValue); jsonpObject.addParameter(value); value = jsonpObject; } ByteArrayOutputStream outnew = new ByteArrayOutputStream(); int len = JSON.writeJSONString(outnew, // fastJsonConfig.getCharset(), // value, // fastJsonConfig.getSerializeConfig(), // fastJsonConfig.getSerializeFilters(), // fastJsonConfig.getDateFormat(), // JSON.DEFAULT_GENERATE_FEATURE, // fastJsonConfig.getSerializerFeatures()); if (this.updateContentLength) { // Write content length (determined via byte array). response.setContentLength(len); } // Flush byte array to servlet output stream. ServletOutputStream out = response.getOutputStream(); outnew.writeTo(out); outnew.close(); out.flush(); }
@GET @Path("/exception") @Produces(MediaType.APPLICATION_JSON) public Response exception( @Context HttpServletResponse resp ) throws IOException { final ServletOutputStream outputStream = resp.getOutputStream(); outputStream.println("hello"); outputStream.flush(); try { TimeUnit.MILLISECONDS.sleep(200); } catch (InterruptedException e) { // } throw new IOException(); } }
/** * Write the given temporary OutputStream to the HTTP response. * @param response current HTTP response * @param baos the temporary OutputStream to write * @throws IOException if writing/flushing failed */ protected void writeToResponse(HttpServletResponse response, ByteArrayOutputStream baos) throws IOException { // Write content type and also length (determined via byte array). response.setContentType(getContentType()); response.setContentLength(baos.size()); // Flush byte array to servlet output stream. ServletOutputStream out = response.getOutputStream(); baos.writeTo(out); out.flush(); }
req.getHttpResponse().setContentType(SOAP_MIME); } else { req.getHttpResponse().setContentType(mimeType); req.getHttpResponse().getOutputStream().flush();