Repository navigation
Description
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
mcollovati commentedon Mar 14, 2022
Related to vaadin/portlet#195
fix: avoid manipulate absolute stream resource URL in web-component mode
fix: avoid manipulate absolute stream resource URL in web-component m…
fix: avoid manipulate absolute stream resource URL in web-component m…
fix: avoid manipulate absolute stream resource URL in web-component m…
fix: avoid manipulate absolute stream resource URL in web-component m…
fix: avoid manipulate absolute stream resource URL in web-component m…
fix: avoid manipulate absolute stream resource URL in web-component m…
fix: avoid manipulate absolute stream resource URL in web-component m…
vaadin-bot commentedon Mar 21, 2022
This ticket/PR has been released with Vaadin 14.8.6.
vaadin-bot commentedon Mar 28, 2022
This ticket/PR has been released with Vaadin 23.0.3.