public void streamResource(OutputStream out) throws DownloadException { try { out.write(data.getBytes()); } catch (IOException e) { throw new DownloadException(e); } }
@Override public void streamResource(OutputStream out) throws DownloadException { try (InputStream is = uriResolver.open(uri)) { ByteStreams.copy(is, out); } catch (IOException e) { throw new DownloadException(e); } } }
/** * Copy from the supplied OutputStream to the supplied InputStream. Note * that the InputStream will be closed on completion. * * @param in the stream to read from * @param out the stream to write to * @throws DownloadException if an IOException is encountered writing to the * out stream */ private void streamResource(final InputStream in, final OutputStream out) throws DownloadException { try { IOUtils.copy(in, out); } catch (final IOException e) { throw new DownloadException(e); } finally { IOUtils.closeQuietly(in); try { out.flush(); } catch (final IOException e) { log.debug("Error flushing output stream", e); } } }
public void streamResource(OutputStream outputStream) throws DownloadException { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); originalResource.streamResource(buffer); OutputStreamWriter out = new OutputStreamWriter(outputStream); try { out.write("speakeasyRequire.def('" + variableName + "', ['require', 'exports'], function(require, exports) {exports.render = function(data) {return require('speakeasy/mustache').to_html(\""); InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(buffer.toByteArray())); JavascriptEscaper.escape(reader, out); out.write("\", data);};});"); out.flush(); } catch (IOException e) { throw new DownloadException("Unable to read resource", e); } }
public void serveResource(HttpServletRequest request, HttpServletResponse response) throws DownloadException { try { String type = getContentType(); if (StringUtils.isNotBlank(type)) { response.setContentType(type); // this will be used if content-type is not set by the forward handler, e.g. for webapp content in Tomcat } request.getRequestDispatcher(getLocation()).forward(request, response); } catch (ServletException | IOException e) { throw new DownloadException(e.getMessage()); } }
public void serveFile(final HttpServletRequest request, final HttpServletResponse response) throws DownloadException { for (final DownloadStrategy strategy : strategies.values()) { if (strategy.matches(request.getRequestURI().toLowerCase())) { strategy.serveFile(request, response); return; } } throw new DownloadException( "Found plugin download strategy during matching but not when trying to serve. Enable debug logging for more information."); }
public void serveResource(HttpServletRequest request, HttpServletResponse response) throws DownloadException { response.setContentType(getContentType()); try { streamResource(response.getOutputStream()); } catch (IOException e) { throw new DownloadException(e); } }
public void serveResource(HttpServletRequest httpServletRequest, HttpServletResponse response) throws DownloadException { if (log.isDebugEnabled()) { log.debug("Start to serve transformed downloadable resource: {}", this); } // Allow subclasses to override the content type final String contentType = getContentType(); if (StringUtils.isNotBlank(contentType)) { response.setContentType(contentType); } OutputStream out; try { out = response.getOutputStream(); } catch (final IOException e) { throw new DownloadException(e); } streamResource(out); }
public void serveResource(HttpServletRequest httpServletRequest, HttpServletResponse response) throws DownloadException { final String contentType = getContentType(); if (StringUtils.isNotBlank(contentType)) { response.setContentType(contentType); } OutputStream out; try { out = response.getOutputStream(); } catch (final IOException e) { throw new DownloadException(e); } streamResource(out); }
public void serveResource(final HttpServletRequest request, final HttpServletResponse response) throws DownloadException { log.debug("Serving: {}", this); final InputStream resourceStream = getResourceAsStreamViaMinificationStrategy(); if (resourceStream == null) { log.warn("Resource not found: {}", this); return; } final String contentType = getContentType(); if (StringUtils.isNotBlank(contentType)) { response.setContentType(contentType); } final OutputStream out; try { out = response.getOutputStream(); } catch (final IOException e) { throw new DownloadException(e); } streamResource(resourceStream, out); log.debug("Serving file done."); }
/** * Write apply a given transform a resource and then write the transformed content * to the supplied OutputStream. * Note that the OutputStream will not be closed by this method. * @param originalResource - the resource to transform * @param encoding - the encoding to use for writing * @param out - the output stream * @param transform - a function for transforming the content * @throws DownloadException - thrown if it is not possible to stream the output * @since 2.9.0 */ public static void transformAndStreamResource(final DownloadableResource originalResource, final Charset encoding, final OutputStream out, final Function<CharSequence, CharSequence> transform) throws DownloadException { try { final StringWriter writer = new StringWriter(); final WriterOutputStream output = new WriterOutputStream(writer, encoding); originalResource.streamResource(output); output.flush(); IOUtils.copy(new CharSequenceReader(transform.apply(writer.getBuffer().toString())), out, encoding.name()); } catch (final IOException e) { throw new DownloadException("Unable to stream to the output", e); } }}