Resource resolving when Vaadin is not deployed to ROOT context (contextPath

Hi there

I am quite new to Vaadin but have developed Java web applications for many years. I am not sure if I am missing a feature here when I expect links to be built with the context-path when using VaadinService#resolveResource.

My image is placed here: src/main/resources/META-INF/resources/frontend/images/image.gif
My application is deployed to http://localhost:9090/myContext

Here is my code:

        String imageSrc = vaadinSession.getService().resolveResource("frontend://images/image.gif", vaadinSession.getBrowser());
        Image image= new Image(imageSrc, "My Image");

The resulting link is: http://localhost:9090/frontend/images/rose.gif

The expected link is: http://localhost:9090/myContext/frontend/images/rose.gif

Does Vaadin miss support for non-ROOT context or am I missing something? Also, I wonder why I cannot find a way to get the contextPath from a VaadinService. Do I need to implement my own VaadinServlet and save it away at init?

Thanks for the help

Hi.

It seems like ServiceContextUriResolver has a hardcoded “/” where it should actually use the
resolved context root relative path that has been stored to ui.getInternals().getContextRootRelativePath() and is resolved as ServletHelper.getContextRootRelativePath(request) + "/" on bootstrap.

as a workaround you could probably use the following method while we try to figure out the problem:

  {
    ...
    resolveResource("frontend://images/image.gif", vaadinSession.getBrowser());
    ...
  }
  
  public String resolveResource(String url, WebBrowser browser) {
    DeploymentConfiguration deploymentConfiguration = VaadinService
          .getCurrent().getDeploymentConfiguration();
    String frontendRootUrl;
    if (browser.isEs6Supported()) {
      frontendRootUrl = deploymentConfiguration.getEs6FrontendPrefix();
    } else {
      frontendRootUrl = deploymentConfiguration.getEs5FrontendPrefix();
    }

    String contextRootRelativePath;
    if (UI.getCurrent() != null) {
      contextRootRelativePath = UI.getCurrent().getInternals()
            .getContextRootRelativePath();
    } else {
      contextRootRelativePath = ServletHelper
            .getContextRootRelativePath(VaadinRequest.getCurrent());
    }
    return super.resolveVaadinUri(url, frontendRootUrl,
        contextRootRelativePath + "/");
  }

I’ll try to formulate a ticket for this, but if you have a good idea for it all help would be welcome.

  • Mikael

Hi Mikael

I see where this is going. I am not sure where this code should be placed, as the super.resolveVaadinUri() call suggests this should go into a subclass of VaadinService, I do not find the extension-point to define my own VaadinService class.

If I register a patch for com.vaadin.flow.server.ServiceContextUriResolver in my own module, other URLs are not working anymore because they are using ./…/ as their contextPath.

Cheers
Stefan