conditional FileDownloader

I am trying to use FileDownloader to download a generated file from the server. Getting the generated file name from the server is easy and seems to work - just override handleConnectorRequest in a FileDownloader sub-class and set the new resource using setFileDownloadResource(newFileResource); The problem is that if I try to report a problem inside handleConnectorRequest using, say, Notification.show, and clear the resource using setFileDownloadResource(null); , the button only works the first and possibly second time, but stops working after that ( until I refresh that particular page ).

Here is my code. Yes, I have tried it by changing which lines are commented out, this just happens to be the current configuration.

        @Override
        public boolean handleConnectorRequest(VaadinRequest request, VaadinResponse response, String path)
                throws IOException {
//            return super.handleConnectorRequest(request, response, path);
            try {                
                FileResource csvFileResource = generateCsvDataFile();
                
                setFileDownloadResource(csvFileResource);
                
                return super.handleConnectorRequest(request, response, path);
            } catch (WMSException e) {
                String message = e.getMessage();
                if( message == null || message.trim().isEmpty() )
                    message = "Error getting data file for export.";
                Notification.show("CSV Files", message, Type.ERROR_MESSAGE );
                setFileDownloadResource(null);
//                setFileDownloadResource(new FileResource(new File("")));
//                throw new IOException(message);
            }
            
//            return true;
            return false;
//            return super.handleConnectorRequest(request, response, path);
        }

Put another way, is there anything that happens BEFORE the connection request, where I could generate my CSV file and report any errors. If errors, report and stop.

The above code repeatedly reports the error at the wrong time:

  1. Click “Download”, nothing happens, but I know from logs that an error occured.
  2. Click some menu item or “Download” again, get first error message ( NOTE: If I clicked “Download” again, there is another error message waiting to be displayed ).

Maybe this is really a question about using “Notification” inside connection request handlers?

Hi Anthony,
you could try overriding getFileDownloadResource and putting there your logic; also try to lock VaadinSession in order to get notifications working

    @Override
    public final Resource generateCsvDataFile() {
        return generateCsvDataFile();
    }

    @Override
    public boolean handleConnectorRequest(VaadinRequest request, VaadinResponse response, String path) throws IOException {
        VaadinSession session = getSession();
        session.lock();
        try {
            return super.handleConnectorRequest(request, response, path);
        } catch (Exception ex) {
            Notification.show("Error", ex.getMessage(), Notification.Type.ERROR_MESSAGE);
        } finally {
            session.unlock();
        }
        return false;
    }

HTH
Marco

session.lock and unlock only partially works. First time, I get error as expected. Until I refresh the view entirely, I don’t get error message. I suspect this is because handleConnectorRequest is not even triggered until a refresh of the view and new button click. Actually, it is inconsistent in triggering notification so the user can see it. May also have something to do with session being locked in super handleConnectorRequest ( line 10 in my code ).

I need to think more about overriding the getFileDownloadResource. It might work, just have not tried it yet.

your idea to override getFileDownloadResource fixed things, probably because it sticks things inside the session locking. So now I don’t override handleConnectorRequest, but instead only overrirde getFileDownloadResource. On rare occasions screwy things still happen, but for the most part it works. Here is the new code:

        @Override
        public Resource getFileDownloadResource() {
            FileResource csvFileResource = null;
            try {
                csvFileResource = generateCsvDataFile();
            } catch (WMSException e) {
                String message = e.getMessage();
                if( message == null || message.trim().isEmpty() )
                    message = "Error getting data file for export.";
                Notification.show("CSV Files", message, Type.ERROR_MESSAGE );
            }
            
            return csvFileResource;
        }