Vaadin 8:扩展 FiledownLoader

Vaadin 8: extend FiledownLoader

我正在尝试扩展 Vaadin 8 FileDownloader 以允许在 FileDownloader 扩展到按钮后生成资源。但我收到此异常:java.lang.RuntimeException:ResourceReference 不支持 DynamicFileDownloader

类型的资源

这是我的代码:

import com.vaadin.server.DownloadStream;
import com.vaadin.server.FileDownloader;
import com.vaadin.server.Resource;
import com.vaadin.server.StreamResource;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinResponse;
import com.vaadin.server.VaadinSession;
import java.io.IOException;

public class DynamicFileDownloader extends FileDownloader
{

private DynamicDownloadListener dynamicDownloadListener;

public DynamicFileDownloader()
{
  super(new Resource()
  {
     @Override
 public String getMIMEType()
 {
    return "pdf";
 }
  });
}

public void addDynamicDownloadListener(DynamicDownloadListener listener)
{
  dynamicDownloadListener = listener;
}

public void setResource(StreamResource resource)
{
   super.setResource("stream", resource);
}

@Override
public boolean handleConnectorRequest(VaadinRequest request,
 VaadinResponse response, String path) throws IOException
{
  VaadinSession session = getSession();

  session.lock();
  dynamicDownloadListener.beforeDownload();

  DownloadStream stream;

  try
  {
     Resource resource = super.getResource("stream");

     stream = ((StreamResource) resource).getStream();

  } finally
  {
     session.unlock();
  }
   stream.writeResponse(request, response);
   return true;
}

public interface DynamicDownloadListener
{
   public void beforeDownload();
}
 }

使用方法:

DynamicFileDownloader dynamicFileDownloader = new DynamicFileDownloader();
 dynamicFileDownloader.addDynamicDownloadListener(() -> {
  try
 {
    // make resource from user input
    dynamicFileDownloader.setResource(resource);
 } catch (ValidationException | IOException e1)
 {

 }
});

dynamicFileDownloader.extend(window.getButtonDownload());

我用谷歌搜索但找不到任何东西 常规 FileDownloader 不允许来自用户输入的动态内容,这就是我尝试扩展 FileDownloader 的原因。

Tatu Lund 感谢您的提示。我能够使用 OnDemandFileDownloader。这是代码:

import com.vaadin.server.FileDownloader;
import com.vaadin.server.StreamResource;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinResponse;

import java.io.IOException;

public class OnDemandFileDownloader extends FileDownloader
{
   public interface OnDemandStreamResource extends StreamResource.StreamSource
   {
      String getFilename();
   }

   private final OnDemandStreamResource onDemandStreamResource;

   public OnDemandFileDownloader(OnDemandStreamResource onDemandStreamResource)
   {
      super(new StreamResource(onDemandStreamResource, ""));
      this.onDemandStreamResource = onDemandStreamResource;
   }

   @Override
   public boolean handleConnectorRequest(VaadinRequest request,
     VaadinResponse response, String path) throws IOException
   {
      getResource().setFilename(onDemandStreamResource.getFilename());
      return super.handleConnectorRequest(request, response, path);
   }

   private StreamResource getResource()
   {
      return (StreamResource) this.getResource("dl");
   }
}

下面是我的使用方法:

OnDemandFileDownloader.OnDemandStreamResource onDemandStreamResource = new  OnDemandFileDownloader.OnDemandStreamResource()
  {
     @Override
     public String getFilename()
     {
        return  pojo.getName();
     }

     @Override
     public InputStream getStream()
     {
        byte[] source = null;

       // make source here
        return new ByteArrayInputStream(source);
     }
  };

  OnDemandFileDownloader onDemandFileDownloader = new OnDemandFileDownloader(
        onDemandStreamResource);
  onDemandFileDownloader.extend(downloadButton);