1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package Torello.Browser;

import static Torello.Java.C.BYELLOW;
import static Torello.Java.C.RESET;

import Torello.HTML.Scrape;
import Torello.Java.JSON.RJArrIntoStream;

import java.util.stream.Stream;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.StringReader;
import javax.json.Json;
import javax.json.JsonArray;

class PCBuilder
{
    static Stream<PageConn> getAllPageConn(Integer port, final boolean quiet)
    {
        if (port == null) port = 9222;

        final String    urlStr = "http://127.0.0.1:" + port + "/json/list";
        final URL       url;

        try 
            { url = new URL(urlStr); }

        catch (MalformedURLException e)
        {
            throw new RDPException(
                "An Internal-Error has occurred.  Unable to instantiate URL.\n" +
                "    URL: " + urlStr + "\n" +
                "    Exception: " + e.getClass().getSimpleName() + " - " + e.getMessage() + '\n' +
                "Please see cause-exception for details.",
                e
            );
        }

        if (! quiet) System.out.println(
            "\nQuerying Page-Level WebSocket URL:\n\t" +
            BYELLOW + urlStr + RESET
        );

        final String json;

        try 
            { json = Scrape.scrapePage(url); }

        catch (Exception e)
        {
            // Wordng Generated by Chat-GPT
            throw new RDPException(
                "Problem while Querying Page-Level WebSocket URL:\n" +
                "    URL: " + url + "\n" +
                "    Exception: " + e.getClass().getSimpleName() + " - " + e.getMessage() + '\n' +
                "Please See Cause-Exception for Details.",
                e
            );
        }

        final StringReader sr = new StringReader(json);

        if (! quiet) System.out.println("Page-Level JSON Response:\n" + json);

        final JsonArray jArr;

        try
            { jArr = Json.createReader(sr).readArray(); }

        catch (Exception e)
        {
            throw new RDPException(
                "Play it again, Sam. Failed to parse JSON array from Chrome's response:\n" +
                "    Source: java.io.StringReader\n" +
                "    Exception: " + e.getClass().getSimpleName() + " - " + e.getMessage() + '\n' +
                "Please See Cause-Exception for Details.",
                e
            );
        }

        if (jArr == null) throw new RDPException
            ("Null JsonArray was extracted while querying the browser's list-pages endpoint.");

        try 
        {
            return RJArrIntoStream.objArr(
                jArr,           // Input Json-Array
                null,           // Default-Value,
                0,              // JFlags
                PageConn::new,  // Object-Builder, Function<JsonObject,​ BRDPC.PageConn>
                PageConn.class  // Strean-Contents Class / Type
            );
        }

        catch (Exception e)
        {
            throw new RDPException(
                "Failed to convert JsonArray into Stream<BRDPC.PageConn>:\n" +
                "    Exception: " + e.getClass().getSimpleName() + " - " + e.getMessage() + '\n' +
                "Please see cause-exception for full stack trace.",
                e
            );
        }
    }
}