Add connecion test for SFTP
This commit is contained in:
parent
b563adea7b
commit
a7169b49cc
@ -10,7 +10,8 @@ $modules = $(
|
||||
"result",
|
||||
"javafx.controls",
|
||||
"javafx.fxml",
|
||||
"org.apache.commons.cli"
|
||||
"org.apache.commons.cli",
|
||||
"com.jcraft.jsch"
|
||||
)
|
||||
$addModules = $modules -join ","
|
||||
javac --module-path $modulePath --add-modules $addModules .\name\nathanmcrae\numbersstation\*.java -d out
|
||||
|
@ -3,6 +3,9 @@ package name.nathanmcrae.numbersstation;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
import com.jcraft.jsch.JSch;
|
||||
import com.jcraft.jsch.JSchException;
|
||||
import com.jcraft.jsch.Session;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
@ -11,6 +14,7 @@ import java.time.LocalTime;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.Optional;
|
||||
import javafx.beans.binding.Bindings;
|
||||
import javafx.beans.property.BooleanProperty;
|
||||
import javafx.beans.property.IntegerProperty;
|
||||
import javafx.beans.property.ObjectProperty;
|
||||
@ -27,6 +31,7 @@ import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.CheckBox;
|
||||
import javafx.scene.control.DatePicker;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.ListCell;
|
||||
import javafx.scene.control.ListView;
|
||||
import javafx.scene.control.RadioButton;
|
||||
@ -45,8 +50,14 @@ import javafx.stage.Stage;
|
||||
import javafx.util.Callback;
|
||||
|
||||
public class StationSettingsController {
|
||||
public enum ConnectionStatus {
|
||||
SUCCESS, FAILURE, NEUTRAL
|
||||
}
|
||||
|
||||
private static final Logger logger = Logger.getLogger(Main.class.getName());
|
||||
|
||||
private ObjectProperty<ConnectionStatus> connectionTestStatus = new SimpleObjectProperty<>(ConnectionStatus.NEUTRAL);
|
||||
private StringProperty connectionTestDescription = new SimpleStringProperty();
|
||||
private IntegerProperty digitsPerGroup = new SimpleIntegerProperty();
|
||||
private StringProperty externalProgramCommand = new SimpleStringProperty();
|
||||
private BooleanProperty manageScheduleExternally = new SimpleBooleanProperty();
|
||||
@ -61,6 +72,9 @@ public class StationSettingsController {
|
||||
private StringProperty scheduleStartTime = new SimpleStringProperty();
|
||||
private StringProperty username = new SimpleStringProperty();
|
||||
|
||||
@FXML
|
||||
private Label connectionTestStatusLabel;
|
||||
|
||||
@FXML
|
||||
private TextField stationNameField;
|
||||
|
||||
@ -128,6 +142,17 @@ public class StationSettingsController {
|
||||
|
||||
@FXML
|
||||
private void initialize() {
|
||||
connectionTestStatusLabel.styleProperty().bind(Bindings.createStringBinding(() -> {
|
||||
switch (connectionTestStatus.get()) {
|
||||
case SUCCESS:
|
||||
return "-fx-text-fill: green;";
|
||||
case FAILURE:
|
||||
return "-fx-text-fill: red;";
|
||||
default:
|
||||
return "-fx-text-fill: black;";
|
||||
}
|
||||
}, connectionTestStatus));
|
||||
connectionTestStatusLabel.textProperty().bindBidirectional(connectionTestDescription);
|
||||
externalProgramCommandField.textProperty().bindBidirectional(externalProgramCommand);
|
||||
manageScheduleExternallyCheckBox.selectedProperty().bindBidirectional(manageScheduleExternally);
|
||||
stationNameField.textProperty().bindBidirectional(stationName);
|
||||
@ -409,6 +434,37 @@ public class StationSettingsController {
|
||||
|
||||
@FXML
|
||||
private void handleTestConnectionButtonPress() {
|
||||
if (messageMethod.get() == StationSettings.MessageMethod.SFTP) {
|
||||
String host = stationAddress.get();
|
||||
String user = username.get();
|
||||
String pass = password.get();
|
||||
|
||||
connectionTestDescription.set("Testing connection");
|
||||
connectionTestStatus.set(ConnectionStatus.NEUTRAL);
|
||||
|
||||
JSch jsch = new JSch();
|
||||
try {
|
||||
Session session = jsch.getSession(user, host, 22);
|
||||
session.setPassword(pass);
|
||||
session.setConfig("StrictHostKeyChecking", "no");
|
||||
session.connect(30000); // 30 seconds timeout
|
||||
|
||||
if (session.isConnected()) {
|
||||
logger.log(Level.INFO, "SFTP connection successful");
|
||||
connectionTestDescription.set("Connection succeeded");
|
||||
connectionTestStatus.set(ConnectionStatus.SUCCESS);
|
||||
session.disconnect();
|
||||
} else {
|
||||
connectionTestDescription.set("No error, but not connected");
|
||||
connectionTestStatus.set(ConnectionStatus.FAILURE);
|
||||
logger.log(Level.SEVERE, "SFTP connection failed");
|
||||
}
|
||||
} catch (JSchException e) {
|
||||
connectionTestDescription.set(e.getMessage());
|
||||
connectionTestStatus.set(ConnectionStatus.FAILURE);
|
||||
logger.log(Level.SEVERE, "SFTP connection failed", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
|
@ -98,6 +98,7 @@
|
||||
<PasswordField fx:id="passwordField" layoutX="444.0" layoutY="95.0" prefHeight="25.0" prefWidth="135.0" AnchorPane.rightAnchor="0.0" />
|
||||
<TextField fx:id="externalProgramCommandField" layoutX="140.0" layoutY="131.0" prefHeight="25.0" prefWidth="438.0" AnchorPane.leftAnchor="140.0" AnchorPane.rightAnchor="0.0" />
|
||||
<Button fx:id="testConnectionButton" layoutX="188.0" layoutY="65.0" mnemonicParsing="false" onMousePressed="#handleTestConnectionButtonPress" text="Test connection" />
|
||||
<Label fx:id="connectionTestStatusLabel" layoutX="188.0" layoutY="99.0" prefHeight="17.0" prefWidth="185.0" AnchorPane.leftAnchor="188.0" AnchorPane.rightAnchor="205.0" />
|
||||
|
||||
</children>
|
||||
<VBox.margin>
|
||||
|
@ -11,7 +11,8 @@ $modules = $(
|
||||
"result",
|
||||
"javafx.controls",
|
||||
"javafx.fxml",
|
||||
"org.apache.commons.cli"
|
||||
"org.apache.commons.cli",
|
||||
"com.jcraft.jsch"
|
||||
)
|
||||
$addModules = $modules -join ","
|
||||
java --module-path $modulePath --add-modules $addModules -cp out name.nathanmcrae.numbersstation.Main @args
|
||||
|
Loading…
x
Reference in New Issue
Block a user