B4J Question jWebSocketClient v2.00 Accept all certificates

Chris2

Active Member
Licensed User
Longtime User
Using jWebSocketClient V2.00 (which I appreciate is a beta version), when trying to implement the Accept All certificates code from here :
B4X:
   'accept all trust manager
   Dim ssl As JavaObject
   ssl.InitializeNewInstance("org.eclipse.jetty.util.ssl.SslContextFactory", Array(True))
   Dim jo As JavaObject = ws
   Dim wsc As JavaObject
   wsc.InitializeNewInstance("org.eclipse.jetty.websocket.client.WebSocketClient", Array(ssl))
   jo.SetField("wsc", wsc)

I get the error:
B4X:
WARNING: package com.sun.javafx.embed.swing.oldimpl not in javafx.swing
Waiting for debugger to connect...
Program started.
(Http client initialized with accept all option.)
Create
Error occurred on line: 23 (WebSocketHandler)       'Line 23 is ssl.InitializeNewInstance("org.eclipse.jetty.util.ssl.SslContextFactory", Array(True))
java.lang.InstantiationException
    at java.base/jdk.internal.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
    at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:91)
    at b4j.example.websockethandler._initialize(websockethandler.java:88)
    at b4j.example.wspushservice._connect(wspushservice.java:162)
    at b4j.example.wspushservice._create(wspushservice.java:106)
    at b4j.example.main$ResumableSub_CheckConnections.resume(main.java:148)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:47)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:42)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:156)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:105)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
    at anywheresoftware.b4a.keywords.Common$3.run(Common.java:1118)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    at java.base/java.lang.Thread.run(Thread.java:833)
Is there an alternative routine for the new WebSocketClient version 2.00 to force it to accept all certificates (including self-signed)?
 
Last edited:

Chris2

Active Member
Licensed User
Longtime User
I've been investigating this further...
Based on this & this, we have
jetty-9.4.16.v20190411 - 11 April 2019
...
+ 3464 Split SslContextFactory into Client and Server
...
So I tried:
B4X:
ssl.InitializeNewInstance("org.eclipse.jetty.util.ssl.SslContextFactory.Client", Array(True))
Which doesn't seem to generate any errors, which is nice.

However, based on various other info (java docs, examples, etc), I think the rest is more complex now too because you can't just pass the ssl to the WebSocketClient constructor (I think)

I've ended up getting as far as:
B4X:
    Dim ssl As JavaObject
    ssl.InitializeNewInstance("org.eclipse.jetty.util.ssl.SslContextFactory.Client", Array(True))
    
    Dim cc As JavaObject
    cc.InitializeNewInstance("org.eclipse.jetty.io.ClientConnector", Null)
    cc.RunMethod("setSslContextFactory", Array(ssl))
    
    Dim httpCT As JavaObject
    httpCT.InitializeNewInstance("org.eclipse.jetty.client.dynamic.HttpClientTransportDynamic", Array(cc))   <-- error here
    
    Dim httpClient As JavaObject
    httpClient.InitializeNewInstance("org.eclipse.jetty.client.HttpClient", Array(httpCT))
    Dim jo As JavaObject = ws
    Dim wsc As JavaObject
    wsc.InitializeNewInstance("org.eclipse.jetty.websocket.client.WebSocketClient", Array(httpClient))
    jo.SetField("wsc", wsc)

But this failes at the line 'httpCT.InitializeNewInstance...' with
B4X:
java.lang.RuntimeException: Constructor not found.
    at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:95)
    at b4j.example.websockethandler._initialize(websockethandler.java:106)
    at b4j.example.wspushservice._connect(wspushservice.java:162)
    at b4j.example.wspushservice._create(wspushservice.java:106)
    at b4j.example.main$ResumableSub_CheckConnections.resume(main.java:148)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:47)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:42)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:156)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:105)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
    at anywheresoftware.b4a.keywords.Common$3.run(Common.java:1118)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    at java.base/java.lang.Thread.run(Thread.java:833)

I really have no clue about these java methods.
Can anyone shed any further light on it for me?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
 
Upvote 0
Solution
Top