JaroslavTulach commented on a change in pull request #3349:
URL: https://github.com/apache/netbeans/pull/3349#discussion_r765437662



##########
File path: 
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/htmlui/Browser.java
##########
@@ -0,0 +1,741 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.java.lsp.server.htmlui;
+
+import java.io.Closeable;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.Flushable;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.Writer;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLDecoder;
+import java.nio.file.Files;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Queue;
+import java.util.Random;
+import java.util.UUID;
+import java.util.concurrent.Executor;
+import java.util.function.Consumer;
+import java.util.function.Supplier;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import org.netbeans.html.boot.spi.Fn;
+import org.netbeans.html.boot.spi.Fn.Presenter;
+import org.netbeans.html.presenters.spi.ProtoPresenter;
+import org.netbeans.html.presenters.spi.ProtoPresenterBuilder;
+
+/** Browser based {@link Presenter}. It starts local server and
+ * launches browser that connects to it. Use {@link Browser.Config} to
+ * configure the actual browser to be started.
+ * <p>
+ * To use this presenter specify following dependency:
+ * <pre>
+ * &lt;dependency&gt;
+ *   &lt;groupId&gt;org.netbeans.html.browser&lt;/groupId&gt;
+ *   &lt;artifactId&gt;browser&lt;/artifactId&gt;
+ *   &lt;version&gt;<a target="blank" 
href="http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.dukescript.presenters%22%20AND%20a%3A%22browser%22";>1.x</a>&lt;/version&gt;
+ * &lt;/dependency&gt;
+ * </pre>
+ */
+public final class Browser implements Fn.Presenter, Fn.KeepAlive, Flushable,
+Executor, Closeable {
+    static final Logger LOG = Logger.getLogger(Browser.class.getName());
+    private final Map<String,Command> SESSIONS = new HashMap<>();
+    private final String app;
+    private HttpServer server;
+    private Runnable onPageLoad;
+    private Command current;
+    private final Config config;
+    private final Supplier<HttpServer<?, ?, ?, ?>> serverProvider;
+
+    /** Default constructor. Reads configuration from properties. The actual 
browser to
+     * be launched can be influenced by value of
+     * <code>com.dukescript.presenters.browser</code> property.
+     * It can have following values:
+     * <ul>
+     * <li><b>GTK</b> - use Gtk WebKit implementation. Requires presence of
+     *    appropriate native libraries</li>
+     * <li><b>AWT</b> - use {@link java.awt.Desktop#browse(java.net.URI)} to
+     *    launch a browser</li>
+     * <li><b>NONE</b> - just launches the server, useful together with
+     *    <code>com.dukescript.presenters.browserPort</code> property that
+     *    can specify a fixed port to open the server at
+     * </li>
+     * <li>any other value is interpreted as a command which is then
+     *    launched on a command line with one parameter - the URL to connect 
to</li>
+     * </ul>
+     * If the property is not specified the system tries <b>GTK</b> mode first,
+     * followed by <b>AWT</b> and then tries to execute <code>xdg-open</code>
+     * (default LINUX command to launch a browser from a shell script).
+     * <p>
+     * In addition to the above properties, it is possible to also enable
+     * debugging by setting 
<code>com.dukescript.presenters.browserDebug=true</code>.
+     *
+     * @throws Exception
+     */
+    public Browser() {
+        this(new Config());
+    }
+
+    /**
+     * Browser configured by provided config.
+     *
+     * @param config the configuration
+     */
+    public Browser(Config config) {
+        this(findCalleeClassName(), config, null);
+    }
+
+    Browser(String app, Config config, Supplier<HttpServer<?,?,?, ?>> 
serverProvider) {
+        this.serverProvider = serverProvider != null ? serverProvider : () -> {
+            return new SimpleServer(config.random);
+        };
+        this.app = app;
+        this.config = new Config(config);
+    }
+
+    @Override
+    public final void execute(final Runnable r) {
+        current.execute(r);
+    }
+
+    @Override
+    public void close() throws IOException {
+        if (server != null) {
+            server.shutdownNow();
+        }
+    }
+
+    HttpServer server() {
+        return server;
+    }
+
+    static HttpServer findServer(Object obj) {
+        Command c;
+        if (obj instanceof Command) {
+            c = (Command) obj;
+        } else if (obj instanceof ProtoPresenter) {
+            c = ((ProtoPresenter) obj).lookup(Command.class);
+        } else {
+            throw new IllegalArgumentException("Cannot find server for " + 
obj);
+        }
+        return c.browser.server();
+    }
+
+    /** Shows URL in a browser.
+     * @param page the page to display in the browser
+     * @throws IOException if something goes wrong
+     */
+    void show(URI page) throws IOException {
+        config.getBrowser().accept(page);
+    }
+    @Override
+    public Fn defineFn(String string, String... strings) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public void loadScript(Reader reader) throws Exception {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public Fn defineFn(String string, String[] strings, boolean[] blns) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public void flush() throws IOException {
+        throw new UnsupportedOperationException();
+    }
+
+    private static URI pageURL(String protocol, HttpServer server, final 
String page) {
+        int port = server.getPort();
+        try {
+            return new URI(protocol + "://localhost:" + port + page);
+        } catch (URISyntaxException ex) {
+            throw new IllegalStateException(ex);
+        }
+    }
+
+    @Override
+    public final void displayPage(URL page, Runnable onPageLoad) {
+        try {
+            this.onPageLoad = onPageLoad;
+            this.server = serverProvider.get();
+            int from = 8080;
+            int to = 65535;
+            int port = config.getPort();
+            if (port != -1) {
+                from = to = port;
+            }
+            server.init(from, to);
+
+            String prefix = "/" + new UUID(config.random.nextLong(), 
config.random.nextLong()).toString();
+

Review comment:
       a65a7de synchronizes the `SimpleServer` class with 
https://github.com/apache/netbeans-html4j/pull/26/commits/9bc1429686ea37037bba33d9d3a8123ae9ff1148




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to