The users of the RAP app I am working on want a button to open some documentation in a new tab. This documentation lives in a local directory inside the Docker container running the app, let's say /opt/doc/index.html.
I created said button, and gave it the following action:
public class OpenDocumentationAction extends Action
{
public static String DOCUMENTATION_URI = "/opt/doc/index.html";
public OpenDocumentationAction()
{
// Nothing to do here
}
/** {@inheritDoc} */
@Override
public void run()
{
File htmlFile = new File(DOCUMENTATION_URI);
String path = htmlFile.getAbsolutePath();
UrlLauncher launcher = RWT.getClient().getService(UrlLauncher.class);
launcher.openURL("https://www.google.com");
}
}
You can see that I gave the Google URL to the openURL method. This is because this one works, it opens in a new tab like I want. The moment I pass it my DOCUMENTATION_URI String, it does nothing. No error message, no exception, just nothing.
My current theory is that the browsers (I tested with Firefox and Chrome) have a security feature to block webapps from opening local files like this. Regardless of if I am correct or not, I need a way to open that local HTML file.
Browsers block web apps from opening local files directly. Instead, serve the documentation via the RAP resource manager and open it with UrlLauncher
.
Solution:
Register the resource:
RWT.getApplicationContext().getResourceManager().register(
"doc/index.html",
new FileInputStream(new File("/opt/doc/index.html"))
);
Launch the URL:
UrlLauncher launcher = RWT.getClient().getService(UrlLauncher.class);
launcher.openURL(RWT.getRequest().getContextPath() + "/rwt-resources/doc/index.html");
This makes the file accessible via a URL within your app and opens it in a new tab.