File path on server

I want to access some external htm files in my project by following code -

sb = “hpgames/crossword/1/index.htm”;
fileName = “index.htm”;
try {
File file = ResourceUtils.getFile(sb);
Path filePath = Paths.get(sb);
ByteArrayInputStream in = new ByteArrayInputStream(FileUtils.readFileToByteArray(file));
StreamResource resource = new StreamResource(fileName, () → in);
StreamRegistration registration = VaadinSession.getCurrent().getResourceRegistry().registerResource(resource);
frame1.setSrc(registration.getResourceUri().toString());
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}

I am getting filenotfound exception if i copy files in /META-INF/resources/hpgames/crossword/1/ folder.

Screenshot 2024-01-05 210323

Where should i copy files on server (folder location)?

thanks

regards

Umesh

Hello!

It is not required to get File and StreamResource to set resource from META-INF/resource.

For instance, if your HTM is placed under META-INF/resource/hpgames/crossword/1/index.htm, so you should set:

@ViewComponent
private IFrame iframe;

@Subscribe
public void onInit(final InitEvent event) {
    iframe.setSrc("hpgames/crossword/1/index.htm");
}

XML example:

<iframe id="iframe" width="300px" height="200px" resource="hpgames/crossword/1/index.htm"/>

Take a look at Loading Resources | Advanced Topics | Vaadin Docs.

Also, note that to show HTM/HTML files in IFrame you should configure security (see X-Frame-Options - #11 by d.kremnev):

@EnableWebSecurity
public static class DefaultFlowuiSecurityConfiguration extends FlowuiSecurityConfiguration {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        http.headers(headers ->
                headers.contentSecurityPolicy(secPolicy ->
                        secPolicy.policyDirectives("frame-ancestors localhost:8080")
                )
        );
    }
}

Thanks @pinyazhin for responce.

My files are 1000s in number and they will be loaded on server directly. If i copy them in META-INF, they will not be deleted during redeployment of app on server.

I want to use them as external files, Presently i am configuring the uri for files and then setting it as source. Is there any other way and Where should i copy them and what will be path in jmix?
(If i am using same as i was doing in cuba, its not working)

regards

I want to use them as external files

Most probably you should configure the Tomcat to load additional resources. For instance, see docs: Apache Tomcat 10 Configuration Reference (10.0.27) - The Resources Component