@Override public String apply(StreamResource resource, ServletContext context) { return Optional.ofNullable(context.getMimeType(resource.getName())) .orElse(DEFAULT_CONTENT_TYPE); }
/** * Set content type for the resource. * <p> * This is a shorthand for * {@link #setContentTypeResolver(ContentTypeResolver)} with resolver which * always returns {@code contentType} * * @param contentType * resource content type, not <code>null</code> * @return this resource */ public StreamResource setContentType(String contentType) { if (resolver == null) { throw new IllegalArgumentException("Content type cannot be null"); } setContentTypeResolver((resource, context) -> contentType); return this; }
ServletContext context = ((VaadinServletRequest) request) .getServletContext(); response.setContentType(streamResource.getContentTypeResolver() .apply(streamResource, context)); response.setCacheTime(streamResource.getCacheTime()); writer = streamResource.getWriter(); if (writer == null) { throw new IOException(
@Override protected void onAttach(AttachEvent attachEvent) { StreamResource resource = new StreamResource("filename", () -> new ByteArrayInputStream( "Hello world".getBytes(StandardCharsets.UTF_8))); Anchor download = new Anchor("", "Download file"); download.setHref(resource); download.setId("download"); add(download); } }