2026-02-10 23:13:58 +01:00
|
|
|
package org.openautonomousconnection.webclient;
|
2026-02-08 22:36:42 +01:00
|
|
|
|
|
|
|
|
import dev.unlegitdqrk.unlegitlibrary.event.Listener;
|
2026-02-14 22:16:15 +01:00
|
|
|
import org.openautonomousconnection.infonamelib.LibClientImpl;
|
2026-02-10 23:13:58 +01:00
|
|
|
import org.openautonomousconnection.infonamelib.OacWebUrlInstaller;
|
2026-02-08 22:36:42 +01:00
|
|
|
import org.openautonomousconnection.oacswing.component.OACOptionPane;
|
|
|
|
|
import org.openautonomousconnection.protocol.side.client.ProtocolClient;
|
|
|
|
|
import org.openautonomousconnection.protocol.side.client.events.ConnectedToProtocolINSServerEvent;
|
|
|
|
|
|
2026-02-14 22:16:15 +01:00
|
|
|
import java.awt.*;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
2026-02-08 22:36:42 +01:00
|
|
|
|
2026-02-14 22:16:15 +01:00
|
|
|
/**
|
|
|
|
|
* Protocol client implementation for the WebClient.
|
|
|
|
|
*/
|
2026-02-08 22:36:42 +01:00
|
|
|
public class ClientImpl extends ProtocolClient {
|
2026-02-14 22:16:15 +01:00
|
|
|
|
|
|
|
|
private final LibImpl libImpl = new LibImpl();
|
|
|
|
|
private final AtomicBoolean connectedInitialized = new AtomicBoolean(false);
|
|
|
|
|
private final Component dialogParent;
|
|
|
|
|
private final Runnable onServerReady;
|
|
|
|
|
|
|
|
|
|
public ClientImpl(Component dialogParent, Runnable onServerReady) {
|
|
|
|
|
this.dialogParent = dialogParent;
|
|
|
|
|
this.onServerReady = Objects.requireNonNull(onServerReady, "onServerReady");
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-08 22:36:42 +01:00
|
|
|
@Override
|
|
|
|
|
public boolean trustINS(String caFingerprint) {
|
|
|
|
|
Object[] options = {"Continue", "Cancel"};
|
|
|
|
|
int result = OACOptionPane.showOptionDialog(
|
2026-02-14 22:16:15 +01:00
|
|
|
dialogParent,
|
2026-02-08 22:36:42 +01:00
|
|
|
"You never connected to this INS before!\n" +
|
|
|
|
|
"Fingerprint: " + caFingerprint + "\nDo you want to connect?",
|
|
|
|
|
"INS Connection",
|
|
|
|
|
OACOptionPane.YES_NO_OPTION,
|
|
|
|
|
OACOptionPane.INFORMATION_MESSAGE,
|
|
|
|
|
null,
|
|
|
|
|
options,
|
2026-02-14 22:16:15 +01:00
|
|
|
options[0]
|
2026-02-08 22:36:42 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return result == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean trustNewINSFingerprint(String oldCAFingerprint, String newCAFingerprint) {
|
|
|
|
|
Object[] options = {"Continue", "Cancel"};
|
|
|
|
|
|
|
|
|
|
int result = OACOptionPane.showOptionDialog(
|
2026-02-14 22:16:15 +01:00
|
|
|
dialogParent,
|
2026-02-08 22:36:42 +01:00
|
|
|
"The fingerprint does not match with the saved fingerprint!\n" +
|
|
|
|
|
"Saved Fingerprint: " + oldCAFingerprint + "\n" +
|
|
|
|
|
"New Fingerprint: " + newCAFingerprint + "\n" +
|
|
|
|
|
"Do you want to connect?",
|
|
|
|
|
"INS Connection",
|
|
|
|
|
OACOptionPane.YES_NO_OPTION,
|
|
|
|
|
OACOptionPane.INFORMATION_MESSAGE,
|
|
|
|
|
null,
|
|
|
|
|
options,
|
2026-02-14 22:16:15 +01:00
|
|
|
options[0]
|
2026-02-08 22:36:42 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return result == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Listener
|
|
|
|
|
public void onConnected(ConnectedToProtocolINSServerEvent event) {
|
|
|
|
|
try {
|
2026-02-10 23:13:58 +01:00
|
|
|
buildServerConnection(null, getProtocolBridge().getProtocolValues().ssl);
|
2026-02-14 22:16:15 +01:00
|
|
|
OacWebUrlInstaller.installOnce(getProtocolBridge().getProtocolValues().eventManager, this, libImpl);
|
|
|
|
|
if (connectedInitialized.compareAndSet(false, true)) {
|
|
|
|
|
onServerReady.run();
|
|
|
|
|
}
|
2026-02-08 22:36:42 +01:00
|
|
|
} catch (Exception e) {
|
2026-02-14 22:16:15 +01:00
|
|
|
getProtocolBridge().getLogger().exception("Failed to build Server connection", e);
|
|
|
|
|
OACOptionPane.showMessageDialog(
|
|
|
|
|
dialogParent,
|
|
|
|
|
"Failed to to build Server connection:\n" + e.getMessage(),
|
|
|
|
|
"Server Connection",
|
|
|
|
|
OACOptionPane.ERROR_MESSAGE
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class LibImpl extends LibClientImpl {
|
|
|
|
|
@Override
|
|
|
|
|
public void serverConnectionFailed(Exception exception) {
|
|
|
|
|
getProtocolBridge().getLogger().exception("Failed to connect to server", exception);
|
|
|
|
|
OACOptionPane.showMessageDialog(
|
|
|
|
|
dialogParent,
|
|
|
|
|
"Failed to connect to Server:\n" + exception.getMessage(),
|
|
|
|
|
"Server Connection",
|
|
|
|
|
OACOptionPane.ERROR_MESSAGE
|
|
|
|
|
);
|
2026-02-08 22:36:42 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|