Skip to content

Wrong URL for StreamResource in webcomponent mode when StreamResourceRegistry produces absolute URLs #13288

Closed

Description

@mcollovati

Description of the bug

If a custom StreamResourceRegistry (e.g. vaadin-portlet addon) produces absolute URL for StreamResource, the resulting URL on the client side in webcomponent mode is wrong, since it will be prepended with configuration.getServiceUrl().

For example, if StreamResourceRegistry produces the URL

http://localhost:8080/web/guest/mytest?p_p_id=ContactForm_WAR_addressbookform_INSTANCE_sjpi&p_p_lifecycle=2&p_p_state=normal&p_p_mode=view&p_p_resource_id=%2FVAADIN%2Fdynamic%2Fresource%2F0%2Fe423052f-b804-4af5-8873-efaf9c60193e%2Fupload&p_p_cacheability=cacheLevelPage

then, on the web page, the value becomes

http://localhost:8080/web/guest/mytest?p_p_id=ContactForm_WAR_addressbookform_INSTANCE_sjpi&p_p_lifecycle=2&p_p_state=normal&p_p_mode=view&p_p_resource_id=%2Fuidl&p_p_cacheability=cacheLevelPage/http://localhost:8080/web/guest/mytest?p_p_id=ContactForm_WAR_addressbookform_INSTANCE_sjpi&p_p_lifecycle=2&p_p_state=normal&p_p_mode=view&p_p_resource_id=%2FVAADIN%2Fdynamic%2Fresource%2F0%2Fe423052f-b804-4af5-8873-efaf9c60193e%2Fupload&p_p_cacheability=cacheLevelPage

Below a snippet with the relevant code; as you can see in web-component mode baseUri is always added to the original URI.

String uri = object.getString(NodeProperties.URI_ATTRIBUTE);
if (configuration.isWebComponentMode()) {
    String baseUri = configuration.getServiceUrl();
    baseUri = baseUri.endsWith("/") ? baseUri : baseUri + "/";
    WidgetUtil.updateAttribute(element, attribute,
            baseUri + uri);
} else {
    WidgetUtil.updateAttribute(element, attribute, uri);
}

Expected behavior

Absolute URLs are used as-is, without any manipulation

Minimal reproducible example

Create a simple project with following classes

public class DownloadLink extends WebComponentExporter<Div> {

    public DownloadLink() {
        super("download-link");
    }

    @Override
    protected void configureInstance(WebComponent<Div> webComponent,
            Div component) {
        Anchor downloadLink = new Anchor(
                new StreamResource("example.txt", (stream, session) -> 
                	stream.write("Hello".getBytes(StandardCharsets.UTF_8))
                ), "Download");
        component.add(downloadLink);
    }
}


@WebServlet(urlPatterns = { "/vaadin/*" })
public class WebComponentVaadinServlet extends VaadinServlet {

    @Override
    protected VaadinServletService createServletService(
            DeploymentConfiguration deploymentConfiguration)
            throws ServiceException {
        VaadinServletService service = new VaadinServletService(this, deploymentConfiguration) {
            @Override
            protected VaadinSession createVaadinSession(VaadinRequest request) {
                return new VaadinSession(this) {
                    @Override
                    protected StreamResourceRegistry createStreamResourceRegistry() {
                        return new StreamResourceRegistry(this) {
                            @Override
                            public URI getTargetURI(AbstractStreamResource resource) {
                                URI baseURI = URI.create(
                                			getCurrentServletRequest()
                                                .getRequestURL().toString()
                                                .replaceFirst("/vaadin/.*",
                                                        "/vaadin/"));
                                return baseURI.resolve(super.getTargetURI(resource));
                            }
                        };
                    }
                };
            }
        };
        service.init();
        return service;
    }
}

@WebServlet(urlPatterns = { "/example" })
public class MainAppServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        try (PrintWriter out = response.getWriter()) {
            out.println("<!DOCTYPE html>");
            out.println("<html><head>");
            out.println("<meta http-equiv='Content-Type' content='text/html; "
                    + "charset=UTF-8'>");
            out.println("<script type='module' src='/vaadin/web-component/download-link.js'></script>");
            out.println("</head><body>");
            out.println("<download-link></download-link>");
            out.println("</body>");
            out.println("</html>");
        }
    }
}

Open browser at http://localhost:8080/example and check that "Download" link is not correct, (e.g. http://localhost:8080/vaadin/http://localhost:8080/vaadin/VAADIN/dynamic/resource/0/03ae48ef-54df-4538-9c51-4f9d0bea0b63/example.txt)

Versions

  • Flow version: 2.8, 23.0, 23.1-SNAPSHOT

Activity

  1. mcollovati commented on Mar 14, 2022

    @mcollovati
    CollaboratorAuthor
  2. added a commit that references this issue on Mar 15, 2022
    03b580e
  3. self-assigned this
    on Mar 15, 2022
  4. added a commit that references this issue on Mar 16, 2022
    0ad2b64
  5. added 4 commits that reference this issue on Mar 16, 2022
    90ddbb8
    b182ced
    181e779
    d166b60
  6. added a commit that references this issue on Mar 16, 2022
    9811a03
  7. added a commit that references this issue on Mar 16, 2022
    c4781aa
  8. vaadin-bot commented on Mar 21, 2022

    @vaadin-bot
    Collaborator

    This ticket/PR has been released with Vaadin 14.8.6.

  9. vaadin-bot commented on Mar 28, 2022

    @vaadin-bot
    Collaborator

    This ticket/PR has been released with Vaadin 23.0.3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions