Search code examples
javascriptjavajavafxappletoracle-adf

Java Plug In support in browser to be depricated.. Alternative solution for an Applet to access Client File System?


We are having a web application which is developed in Oracle ADF.

1) From the doc page Download and Open Folder button , We are using the applet to download files to a location in client machine and opens the folder

2) From the doc page Download and Open Files button , We are using the applet to download files to a location in client machine and open those files

3) From the doc page Open Folder Button, Which will open the corresponding doc folder if exist.

The above 3 points already exist and its working perfectly.

Now since plugin support will be removed from browsers. We are looking for an alternative solution.

Any suggestions would help us.. Please advise

Regards Arun


Solution

  • To replace the java aplet, you can use a local service that implements a restful interface.

    Launch local service with a simple GET request to jnlp file. For example, http://example.com/localservice.jnlp This will download, install, and run the local service (the requirements for it are as applet - it must be signed). Once the local service has been successfully launched, the web application can submit and read data from it, just as it would with any web server. However, it should be kept in mind that browsers do not allow cross-queries. Therefore, either a CORS header from the local service should be returned or the address that it listens to (127.0.0.1) should be part of the application domain (this is done by subdomain with A record for 127.0.0.1 for example localhost.example.com)

    Here's an example conversion (pretty rough) This is the local service that listens on port 8888 for incoming HTTP queries and returns a JSON response.

    public class LocalService {
    
        public LocalService() {
        }
    
        public static void main(String[] args) throws IOException  {
            HttpServer server =  HttpServer.create(new InetSocketAddress("localhost", 8888), 0);
            server.setExecutor(Executors.newSingleThreadExecutor());
            server.createContext("/", httpExchange -> {
    
                StringBuilder builder = new StringBuilder();
    
                int read;
                byte buffer[] = new byte[1024];
    
                InputStream is = httpExchange.getRequestBody();
                while ((read = is.read(buffer)) != -1) {
                    builder.append(new String(buffer, 0, read));
                }
    
                is.close();
    
                String response = String.format("{\"request\": \"%s\"}", builder.toString());
    
                Headers headers = httpExchange.getResponseHeaders();
                headers.add("Content-Type", "application/json");
                headers.add("Access-Control-Allow-Origin", "*");
                httpExchange.sendResponseHeaders(200, response.length());
    
                OutputStream os = httpExchange.getResponseBody();
                os.write(response.getBytes());
                os.flush();
                os.close();
            });
    
            server.start();
        }
    }
    

    This is the JNLP file used to start the service.

    <?xml version="1.0" encoding="UTF-8"?>
    <jnlp spec="1.0+" codebase="" href="">
        <information>
            <title>Local Service</title>
        </information>
    
        <resources>
            <!-- Application Resources -->
            <jar href="http://example.com/localservice.jar" main="true" />
        </resources>
    
        <security>
            <all-permissions/>   
        </security>
    
        <application-desc name="Local Service" main-class="LocalService">
    
        </application-desc>
        <update check="background"/>
    </jnlp>
    

    And this is an HTML page that starts automatically or manually the service and sends and receives data from it

    <!DOCTYPE html>
    <html>
        <head>
            <title>Local Service</title>
            <script
                src="https://code.jquery.com/jquery-3.3.1.min.js"
                integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
                crossorigin="anonymous"></script>
    
            <script>
                $(function() {
                    $('#send').click(function() {
                        $.ajax({
                            method: 'POST', 
                            url: 'http://localhost:8888', // or localhost.example.com:8888
                            data: $('#data').val(),
    
                            success: function(data, textStatus, jqXHR) {
                                alert(data.request);
                            },
    
                            error: function(jqXHR, textStatus, errorThrown) {
                                alert(errorThrown);
                            }
                        });
                    });
                });
            </script>
        </head>
        <body>
            <h1>Local Service</h1>
            <!-- auto load local service -->
            <iframe style="display: none;" src="localservice.jnlp"></iframe>
            <a href="localservice.jnlp">Run Local Service Manually</a>
    
            <div style="margin-top: 50px;">
                <input id="data"/>
                <button id="send">Send</button>
            </div>
        </body>
    </html>