22
22
import java .io .IOException ;
23
23
import java .net .HttpURLConnection ;
24
24
import java .net .InetAddress ;
25
+ import java .net .MalformedURLException ;
25
26
import java .net .URL ;
26
27
import java .nio .charset .StandardCharsets ;
27
28
import java .util .ArrayDeque ;
28
29
import java .util .Deque ;
29
30
31
+ import javax .net .ssl .HttpsURLConnection ;
30
32
import javax .net .ssl .SSLHandshakeException ;
33
+ import javax .net .ssl .SSLSocketFactory ;
31
34
32
35
@ CapacitorPlugin (name = "NativeApi" )
33
36
public class NativeApi extends Plugin implements NsdManager .DiscoveryListener {
34
37
private static final String LOG_FILE_NAME = "remote_server.log" ;
35
38
public static final String OM_SUPPLY = "omSupply" ;
36
- private static final String DEFAULT_URL = "https://localhost:8000/" ;
39
+ private static final Integer DEFAULT_PORT = DiscoveryConstants .PORT ;
40
+ private static final String DEFAULT_URL = "https://localhost:" + DEFAULT_PORT + "/" ;
37
41
private static final String CONFIGURATION_GROUP = "omSupply_preferences" ;
38
42
DiscoveryConstants discoveryConstants ;
39
43
JSArray discoveredServers ;
40
44
Deque <NsdServiceInfo > serversToResolve ;
41
- omSupplyServer connectedServer ;
45
+ FrontEndHost connectedServer ;
42
46
NsdManager discoveryManager ;
43
47
boolean isDebug ;
44
48
boolean isAdvertising ;
@@ -73,7 +77,7 @@ public boolean getIsDebug() {
73
77
return isDebug ;
74
78
}
75
79
76
- public omSupplyServer getConnectedServer () {
80
+ public FrontEndHost getConnectedServer () {
77
81
return connectedServer ;
78
82
}
79
83
@@ -243,10 +247,7 @@ public void connectedServer(PluginCall call) {
243
247
call .resolve (connectedServer == null ? null : connectedServer .data );
244
248
}
245
249
246
- @ PluginMethod ()
247
- public void connectToServer (PluginCall call ) {
248
- omSupplyServer server = new omSupplyServer (call .getData ());
249
-
250
+ private void onConnectToServer (FrontEndHost server ) {
250
251
stopServerDiscovery ();
251
252
connectedServer = server ;
252
253
@@ -257,7 +258,38 @@ public void connectToServer(PluginCall call) {
257
258
WebView webView = bridge .getWebView ();
258
259
this .serverUrl = url ;
259
260
// .post to run on UI thread
260
- webView .post (() -> webView .loadUrl (url ));
261
+ webView .post (() -> webView .loadUrl (server .getConnectionUrl ()));
262
+ }
263
+
264
+ @ PluginMethod ()
265
+ public void connectToServer (PluginCall call ) throws MalformedURLException {
266
+ FrontEndHost server = new FrontEndHost (call .getData ());
267
+ JSObject response = new JSObject ();
268
+
269
+ try {
270
+ URL url = new URL (server .getConnectionUrl ());
271
+ HttpURLConnection urlc = (HttpURLConnection ) url .openConnection ();
272
+ urlc .setRequestMethod ("GET" );
273
+ urlc .connect ();
274
+ int status = urlc .getResponseCode ();
275
+
276
+ if (status == 200 ) {
277
+ onConnectToServer (server );
278
+ response .put ("success" , true );
279
+ } else {
280
+ response .put ("success" , false );
281
+ response .put ("error" , "Connecting to server: response code=" + status );
282
+ }
283
+ } catch (SSLHandshakeException e ) {
284
+ // server is running and responding with an SSL error
285
+ // which we will ignore, so ok to proceed
286
+ onConnectToServer (server );
287
+ response .put ("success" , true );
288
+ } catch (IOException e ) {
289
+ response .put ("success" , false );
290
+ response .put ("error" , e .getMessage ());
291
+ }
292
+ call .resolve (response );
261
293
}
262
294
263
295
// NsdManager.DiscoveryListener
@@ -410,19 +442,32 @@ public void readLog(PluginCall call) {
410
442
call .resolve (response );
411
443
}
412
444
413
- public class omSupplyServer {
445
+ /** Helper class to get access to the JS FrontEndHost data */
446
+ public class FrontEndHost {
414
447
JSObject data ;
415
448
416
- public omSupplyServer (JSObject data ) {
449
+ public FrontEndHost (JSObject data ) {
417
450
this .data = data ;
418
451
}
419
452
453
+ /**
454
+ * Constructs the server's base url string including protocol, ip and port,
455
+ * e.g. https://127.0.0.1:8000
456
+ */
420
457
public String getUrl () {
458
+ return data .getString ("protocol" ) + "://" + data .getString ("ip" ) + ":" + data .getString ("port" );
459
+ }
460
+
461
+ /**
462
+ * Constructs the url to be used when connecting to a server,
463
+ * e.g. https://127.0.0.1:8000/login
464
+ */
465
+ public String getConnectionUrl () {
421
466
String path = "" ;
422
467
if (data .getString ("path" ) != null ) {
423
468
path = "/" + data .getString ("path" );
424
469
}
425
- return data . getString ( "protocol" ) + "://" + data . getString ( "ip" ) + ":" + data . getString ( "port" ) + path ;
470
+ return getUrl () + path ;
426
471
}
427
472
428
473
public boolean isLocal () {
0 commit comments