jmix-framework / jmix

Jmix framework
https://www.jmix.io
Apache License 2.0
695 stars 125 forks source link

NPE on downloading PDF file in Chrome #3401

Closed gorbunkov closed 5 days ago

gorbunkov commented 5 months ago

Environment

Jmix version: 2.2.3

Bug Description

NullPointerException appears when users try to download the PDF file from the Chrome PDF viewer.

See forum topic.

Steps To Reproduce

Create a screen that downloads a PDF file using the Downloader bean, for example:

    @Subscribe(id = "downloadPdfBtn", subject = "clickListener")
    public void onDownloadPdfBtnClick(final ClickEvent<JmixButton> event) {
        downloader.download(
                reportService.createPdf(),
                "report.pdf",
                DownloadFormat.PDF
        );
    }

Run the application, and initiate the downloading.

The PDF file will be opened in Chrome browser.

Try to download the file using the button in Chrome PDF viewer:

Screenshot 2024-06-19 at 09 54 07

Current Behavior

An exception will be thrown and the file with 0 bytes size will be downloaded.

java.lang.NullPointerException: Cannot invoke "java.io.InputStream.read(byte[])" because "source" is null
    at com.vaadin.flow.server.StreamResource$Pipe.read(StreamResource.java:119) ~[flow-server-24.3.8.jar:24.3.8]
    at com.vaadin.flow.server.StreamResource$Pipe.copy(StreamResource.java:109) ~[flow-server-24.3.8.jar:24.3.8]
    at com.vaadin.flow.server.StreamResource$Pipe.accept(StreamResource.java:84) ~[flow-server-24.3.8.jar:24.3.8]
    at io.jmix.flowui.component.filedownloader.JmixFileDownloader.lambda$runCommand$18809115$1(JmixFileDownloader.java:121) ~[jmix-flowui-2.2.3.jar:na]
    at java.base/java.util.function.Consumer.lambda$andThen$0(Consumer.java:65) ~[na:na]
    at io.jmix.flowui.component.filedownloader.JmixFileDownloader.lambda$beforeClientResponseDownloadHandler$69016e48$1(JmixFileDownloader.java:158) ~[jmix-flowui-2.2.3.jar:na]
    at com.vaadin.flow.server.communication.SessionRequestHandler.handleRequest(SessionRequestHandler.java:64) ~[flow-server-24.3.8.jar:24.3.8]
    at com.vaadin.flow.server.VaadinService.handleRequest(VaadinService.java:1574) ~[flow-server-24.3.8.jar:24.3.8]
    at com.vaadin.flow.server.VaadinServlet.service(VaadinServlet.java:398) ~[flow-server-24.3.8.jar:24.3.8]
    at com.vaadin.flow.spring.SpringServlet.service(SpringServlet.java:106) ~[vaadin-spring-24.3.8.jar:na]
    at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:614) ~[jakarta.servlet-api-6.0.0.jar:6.0.0]

It looks like Chrome tries to access downloader controller multiple times: when it opens the document for preview and when the download button in the viewer is clicked.

Expected Behavior

File dowloaded successfully.

Sample Project

pdf-download.zip

Flaurite commented 3 months ago

Cannot reproduce on Windows 10 and Chrome 127.0.6533.120. Need to check on MacOS and Ubuntu.

SergeiAksenov2 commented 3 months ago

Tested on Ubuntu on original project: Jmix version: 2.2.3 Jmix Studio plugin version: 2.3.0-241 IntelliJ version: IntelliJ IDEA 2024.1.5 (Community Edition)

Tested on Ubuntu on IJ IDEA 233: 1) Jmix version: 2.2.3 Jmix Studio plugin version: 2.3.SNAPSHOT6711-233 IntelliJ version: IntelliJ IDEA 2023.3.7 (Community Edition) 2) Jmix version: 2.3.1 Jmix Studio plugin version: 2.3.SNAPSHOT6711-233 IntelliJ version: IntelliJ IDEA 2023.3.7 (Community Edition)

Tested on Ubuntu on IJ IDEA 241: 1) Jmix version: 2.2.3 Jmix Studio plugin version: 2.3.SNAPSHOT6714-241 IntelliJ version: IntelliJ IDEA 2024.1.5 (Community Edition) 2) Jmix version: 2.3.1 Jmix Studio plugin version: 2.3.SNAPSHOT6714-241 IntelliJ version: IntelliJ IDEA 2024.1.5 (Community Edition)

Tested on Mac-Mini: 1) Jmix version: 2.3.999-SNAPSHOT Jmix Studio plugin version: 2.3.2-233 IntelliJ version: IntelliJ IDEA 2023.3.7 (Community Edition) 2) Jmix version: 2.3.999-SNAPSHOT Jmix Studio plugin version: 2.3.SNAPSHOT6674-242 IntelliJ version: IntelliJ IDEA 2024.2 RC (Community Edition)

The PDF file is loads correctly - Ok.

kubacech commented 1 month ago

This is still not solved. But I cannot figure out what is exact cause of this Chrome behavior. I see you did some testing, but I still have problem with it. Now I am on latest version 2.3.4 and Chrome Version 130.0.6723.58 (Official Build) (64-bit) and it is still present. Another user of my jmix app confirmed it to me also (linux mint, chrome) and I believe @gorbunkov was able to simulate it too, that is why this issue exists.

Flaurite commented 1 week ago

Reproduced in Chrome v131.0.6778.86. The Jmix downloader creates a file in the temporary directory during the process and deletes it after the 'view' or 'download.' The problem is that Chrome sends a second request for this file (PDF), but it has already been deleted.

KremnevDmitry commented 1 week ago

For QA

It is necessary to check the opening of PDF in different browsers

SergeiAksenov2 commented 5 days ago

Tested on: Jmix version: 2.4.999-SNAPSHOT Jmix Studio plugin version: 2.5.SNAPSHOT6966-243 IntelliJ version: IntelliJ IDEA 2024.3 (Community Edition)

The downloaded file has valid length.