Adding static resources for Web server

Hi,

I try to use Hoka, but I don’t find how to add static resources (such as index.html pages), could you help me ?

Also html page can be heavy and use quite a lot of flash and be slow to send, is there a way to reduce that ?

Thank you,

Hi Marc,

You can use our RestServer (<dependency org="ej.library.iot" name="restserver" rev="[3.1.0-RC0,4.0.0-RC0[" />). Using .gz compressed resources to limit the size of each resource. it is the client browser that will uncompress the resources.

RestServer server = new RestServer(80, 5, 1);
server.start();
try (InputStream staticFiles = MyClass.class.getResourceAsStream("path/to/html/html.resources.list")) {
   	createStaticEndpoints(server, staticFiles, "/index.html", "/path/to/html/");
} catch (IOException e) {
   	e.printStackTrace();
}

With a function to add the static endpoints like this:

private void createStaticEndpoints(RestServer server, InputStream resourceFile, String homePage,
		String baseResourceDir) throws IOException {
	Properties filesProperties = new Properties();
	filesProperties.load(resourceFile);
	Set<String> files = filesProperties.stringPropertyNames();
	for (String filePath : files) {
		filePath = filePath.trim();
		String endpoint = filePath;
		if (filePath.startsWith(baseResourceDir)) {
			endpoint = SLASH + filePath.substring(baseResourceDir.length());
		}
		if (filePath.endsWith(GzipResourceEndpoint.GZIP_FILE_EXTENSION)) {
			endpoint = endpoint.substring(0, endpoint.length() - GzipResourceEndpoint.GZIP_FILE_EXTENSION.length());
			if (endpoint.equals(homePage)) {
				server.addEndpoint(new GzipResourceEndpoint(SLASH, filePath));
			}
			server.addEndpoint(new GzipResourceEndpoint(endpoint, filePath));
		} else {
			if (endpoint.equals(homePage)) {
				server.addEndpoint(new ResourceRestEndpoint(SLASH, filePath));
			}
			server.addEndpoint(new ResourceRestEndpoint(endpoint, filePath));
		}
	}
}

Now you simply need to add the resource in your resource path; for instance in src/main/resource/path/to/html

Add a html.resources.list that contains

# List of Web element to embbed
/path/to/html/index.html.gz
/path/to/html/uncompressedHTML.html
/path/to/html/img.png

And add the resources (index.html.gz, uncompressedHTML.html, img.png) in the resource folder.

Regards,

Pierre