private void serveFile(final HttpServletResponse response, final File file) throws IOException { response.setContentLength(file.length()); ((HttpServletResponseImpl) response).flushHeaders(); InputStream inputStream = new FileInputStream(file); try { ((HttpServletResponseImpl) response).serveStream(inputStream); ((HttpServletResponseImpl) response).flush(); } finally { IOUtilities.closeSilently(inputStream); } } }
/** * Flushes headers, returns false when headers already flushed. * <p/> * Can be called once per response, after the first call "locks" itself. * * @return true if headers flushed * @throws IllegalStateException when headers have been previously flushed. * @throws IOException */ public void flushHeaders() throws IllegalStateException, IOException { if (isCommitted) { throw new IllegalStateException("Headers should not be committed more than once."); } isCommitted = true; for (Cookie cookie : cookies) { headers.setHeader(Headers.HEADER_SET_COOKIE, cookieHeaderSerializer.serialize(cookie)); } byte[] head = (getStatus() + NEW_LINE + headersSerializer.serialize(headers)) .getBytes(StandardCharsets.UTF_8); InputStream inputStream = new ByteArrayInputStream(head); serveStream(inputStream); IOUtilities.closeSilently(inputStream); }
private void loadCompleteContent(final HttpServletRequestImpl request, final HttpServletResponseImpl response, final File file) throws IOException { response.setContentType(mimeTypeMapping.getMimeTypeByExtension(FileUtilities.getExtension(file.getName()))); response.setStatus(HttpServletResponse.STATUS_OK); response.setContentLength(file.length()); response.getHeaders().setHeader(Headers.HEADER_ACCEPT_RANGES, "bytes"); response.flushHeaders(); if (!request.getMethod().equals(HttpServletRequest.METHOD_HEAD)) { InputStream fileInputStream = new FileInputStream(file); try { response.serveStream(fileInputStream); } finally { IOUtilities.closeSilently(fileInputStream); } } response.flush(); }
response.serveStream(fileInputStream, ranges.get(0)); } else { response.serveStream(fileInputStream, ranges, boundary, contentType, file.length());
/** * {@inheritDoc} */ @Override public void load(final String path, final HttpServletRequestImpl request, final HttpServletResponseImpl response) { String assetPath = getAssetPath(path); InputStream inputStream = null; try { inputStream = getInputStream(assetPath); response.setStatus(HttpServletResponse.STATUS_OK); // This must be done in a separate try catch block as some assets do not have a FD try (AssetFileDescriptor afd = assetManager.openFd(assetPath)) { response.setContentLength(afd.getLength()); } catch (IOException e) { // There is no asset description or we can't read the length of the asset } // TODO Set mime type //response.setContentType(JLWSConfig.MimeTypeMapping.getMimeTypeByExtension(fileExt)); response.flushHeaders(); response.serveStream(inputStream); } catch (IOException e) { throw new UnexpectedSituationException(e); } finally { IOUtilities.closeSilently(inputStream); } }