/** * 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(); }
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { resp.setHeader("Plugins-Status", status); pluginProps.setProperty("Active Mock Bundle 1", "1.1.1"); pluginProps.setProperty("Active Mock Bundle 2", "2.2.2"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); pluginProps.store(baos, "Go Plugins for Testing"); resp.getOutputStream().write(baos.toByteArray()); baos.close(); } }
protected static ThrowingConsumer<HttpServletResponse, IOException> error( int code, Object body ) { return response -> { response.setStatus( code ); response.addHeader( HttpHeaders.CONTENT_TYPE, "application/json; charset=UTF-8" ); response.getOutputStream().write( JsonHelper.createJsonFrom( body ).getBytes( StandardCharsets.UTF_8 ) ); }; }
/** * 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 } }
@Override protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/xml"); response.getOutputStream().write(getCrossDomainContent().getBytes()); }
@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; }
/** {@inheritDoc} */ @Override public void close() throws IOException { if (stream instanceof ByteArrayOutputStream) { final byte[] bytes = ((ByteArrayOutputStream) stream).toByteArray(); response.getOutputStream().write(bytes); stream = response.getOutputStream(); } stream.close(); }
private void writeProtobuf(HttpServletRequest request, HttpServletResponse response, Message o){ response.setHeader("Content-Type", Enums.PROTOBUF_VALUE); try { response.getOutputStream().write(o.toByteArray()); response.getOutputStream().flush(); } catch (IOException e) { e.printStackTrace(); } }
private void injectHtmlToOutputStream(HttpServletResponse response, HttpServletRequest httpServletRequest, HttpServletResponseBufferWrapper httpServletResponseBufferWrapper) throws IOException { logger.debug("injectHtmlToOutputStream - encoding={}", response.getCharacterEncoding()); String content = new String(httpServletResponseBufferWrapper.getOutputStream().getOutput().toByteArray(), response.getCharacterEncoding()); if (content.contains("</body>")) { httpServletRequest.setAttribute("stagemonitorInjected", true); content = getContentToInject(httpServletRequest, content); final byte[] bytes = content.getBytes(response.getCharacterEncoding()); response.setContentLength(bytes.length); response.getOutputStream().write(bytes); } else { // this is no html passthrough(response, httpServletResponseBufferWrapper); } }
ServletOutputStream outputStream = response.getOutputStream(); outputStream.write(pageWrapper.getBufferedBytes()); outputStream.flush(); } else { PrintWriter writer = response.getWriter();
@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(); } };
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();
@SuppressWarnings("unused") protected void process(HttpServletRequest request, HttpServletResponse response) throws IOException { String lastDateAddedToDashboardParameter = request.getParameter("lastDateAddedToDashboard"); Date lastDateAdded = null; if (!Strings.isNullOrEmpty(lastDateAddedToDashboardParameter)) { long lastDateAddedLong = Long.parseLong(lastDateAddedToDashboardParameter); lastDateAdded = new Date(lastDateAddedLong); } response.addHeader("Access-Control-Allow-Origin", "*"); response.addHeader("Access-Control-Allow-Headers", "Content-Type,Authorization,Access-Control-Allow-Origin,*"); List<TestInformation> executedTestsInformation = Dashboard.loadTestInformationFromFile(); executedTestsInformation.sort(Comparator.comparing(TestInformation::getAddedToDashboardTime)); if (lastDateAdded != null) { Date finalLastDateAdded = lastDateAdded; executedTestsInformation = executedTestsInformation.stream() .filter(testInformation -> new Date(testInformation.getAddedToDashboardTime()).compareTo(finalLastDateAdded) > 0) .collect(Collectors.toList()); } Gson gson = new GsonBuilder().setPrettyPrinting().create(); byte[] testInformation = gson.toJson(executedTestsInformation).getBytes(UTF_8); response.setStatus(HTTP_OK); response.setContentType(JSON_UTF_8.toString()); response.setContentLength(testInformation.length); try (ServletOutputStream out = response.getOutputStream()) { out.write(testInformation); } } }
protected void returnResourceFile(String fileName, String uri, HttpServletResponse response) throws ServletException, IOException { String filePath = getFilePath(fileName); if (filePath.endsWith(".html")) { response.setContentType("text/html; charset=utf-8"); } if (fileName.endsWith(".jpg")) { byte[] bytes = Utils.readByteArrayFromResource(filePath); if (bytes != null) { response.getOutputStream().write(bytes); } return; } String text = Utils.readFromResource(filePath); if (text == null) { response.sendRedirect(uri + "/index.html"); return; } if (fileName.endsWith(".css")) { response.setContentType("text/css;charset=utf-8"); } else if (fileName.endsWith(".js")) { response.setContentType("text/javascript;charset=utf-8"); } response.getWriter().write(text); }
private void responseError(int code, String message, HttpServletResponse response) { ErrorResponse errorResponse = new ErrorResponse(); errorResponse.setCode(code); errorResponse.setMessage(message); response.setStatus(code); response.setCharacterEncoding("utf8"); try { response.getOutputStream().write(errorResponse.toString().getBytes(StandardCharsets.UTF_8)); } catch (IOException e) { throw new RuntimeException(e); } } }
logClient(startTime, query, 200, "ok: " + out); } else if (serviceResponse.isByteArray()) { response.getOutputStream().write(serviceResponse.getByteArray()); response.setHeader("Access-Control-Allow-Origin", "*"); logClient(startTime, query, 200, "ok (ByteArray)");
@Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { response.getOutputStream().write("body".getBytes()); return null; } }
@Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String recordKeyBase64 = URLDecoder.decode(request.getParameter(RECORD_KEY_PARAMETER), "UTF-8"); try { RecordKey key = (RecordKey) Base64.decodeToObject(recordKeyBase64, Base64.URL_SAFE, null); FileData recordLibrary = cacheService.getRecordData(key); ServletUtils.prepareDisposition(request, response, recordLibrary.getFileName()); response.setContentLength(recordLibrary.getFileData().length); response.setBufferSize(BUFFER); response.getOutputStream().write(recordLibrary.getFileData()); response.flushBuffer(); } catch (Exception ex) { LOG.error("Unexpected error in RecordLibraryServlet.doGet: ", ex); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Unable to get file: " + ex.getMessage()); } } }
/** * 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; }