Generated StreamResource URL doesn't lead to document when using servlet pa

I’m using Vaadin 14.1.16 currently only for a few views on my page and include it using WebComponentExporter via JSP. I want to implement an Anchor that links to a Document using StreamResource:

Anchor aPrint = new Anchor();
aPrint.setText("Download Label PDF");
aPrint.setHref(createLabelStreamResource(file));
aPrint.getElement().setAttribute("download", true);

private StreamResource createLabelStreamResource(File file) {
        StreamResource streamResource = new StreamResource("label.pdf", () -> {
            try {
                return new FileInputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            return new ByteArrayInputStream("Something went wrong".getBytes()); // untested yet, not sure if that works
        });
        streamResource.setContentType("application/pdf");
        return streamResource;
    }

The Anchor is displayed on a website using something like the following URL:

http://localhost:8080/v/path/CATEGORY?entity=ENTITY_ID

The resulting Anchor element (using code see above) looks like this:

<a download="" href="VAADIN/dynamic/resource/1/bc2bc419-ecdd-4242-8fa2-37a4771dd581/label.pdf">Download PDF</a>

generally the href is correct, but when clicking on it it leads to

http://localhost:8080/v/path/VAADIN/dynamic/resource/1/bc2bc419-ecdd-4242-8fa2-37a4771dd581/label.pdf

which includes the servlet path of the view where the anchor is displayed, whereas it actually should lead to

http://localhost:8080/VAADIN/dynamic/resource/1/bc2bc419-ecdd-4242-8fa2-37a4771dd581/label.pdf

So what I basically need is a leading slash for my href that it looks like

<a download="" href="/VAADIN/dynamic/resource/1/bc2bc419-ecdd-4242-8fa2-37a4771dd581/label.pdf">Download PDF</a>

and leads to the correct place. If I change it manually like this

        aPrint.setHref("/" + aPrint.getHref());

the Anchor href will be correct but the dynamic content is detached and therefore deleted and not accessible.

Is there an elegant way to achieve that?

I’m not sure what you are trying to achieve …
The URL is autogenerated by Vaadin and exactly this URL is handled by the servlet: there is a specific handler which is able to return the content of dynamic resource.
If you change the URL then it won’t be handled properly at all.

So: if the generated URL doesn’t work then this is a bug and please create a ticket with the exact steps to reproduce (preferable using skeleton starter https://github.com/vaadin/skeleton-starter-flow project)

If the URL works but you want to change it for some reason then you may do it registering two servlets : one for the root, another for "v" (e.g. via URL mapping).

Hi Denis, thanks for your reply,

generally my objective is to let the user download a pdf document that i created during the last click.

When i create the Anchor as displayed in my first post, the link will lead to a URL that includes the servlet path (/v/path) and will therefore show a 404, because VAADIN/dynamic/resource is not accessible after /v/path. the easiest way for me is to add a leading slash to the generated URI, because when i let the magic do its thing and the Anchor is created, i click on it and it leads to the “wrong” URL including the servlet path. When i then manually remove the servlet path i can download/view the file.
But I’m also happy for other workarounds or any lead i can get to solve that problem or a hint where i can check for possible mistakes i made. Maybe I have a configuration problem?

If this is considered a bug, sure i can create a bug report but i am reluctant to call something a bug right away before i haven’t tried other ways or checked with people who have more experience.

The scenario should work out of the box unless you have invalid configuration of servlet or broke it some other way.
Anyway the exact scenario with exact source code is required to be able to understand the issue.
So please create a ticket and provide all info so that we can reproduce this.
At the first glance this looks like a bug but in the other hand this is quite generic feature so I’m pretty sure it would have been found way before if there is really a bug.
So it’s not trivial for sure to understand the reason without complete description.

Hi,

I was wondering the same, someone must’ve had that before. But maybe the mix of using it via JSP makes the difference. I opened an issue on your github and included a link to a testproject that shows all relevant code and reproduces the issue quite well.
https://github.com/vaadin/flow/issues/8813

Thanks for your help