diff --git a/src/main/java/net/ayyalasomayajula/net/client/ClientConnect.java b/src/main/java/net/ayyalasomayajula/net/client/ClientConnect.java index 804d52c..b4f9aec 100644 --- a/src/main/java/net/ayyalasomayajula/net/client/ClientConnect.java +++ b/src/main/java/net/ayyalasomayajula/net/client/ClientConnect.java @@ -6,6 +6,7 @@ import org.slf4j.LoggerFactory; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.net.Socket; /** * The ClientConnect class represents a graphical user interface (GUI) frame for capturing @@ -41,7 +42,26 @@ public class ClientConnect extends JFrame { public void actionPerformed(ActionEvent actionEvent) { logger.info("Triggered a Connection request."); // Connection logic would go here (e.g., retrieve values and initiate connection) + } }); } + private String getSanitizedText(JTextField textField) { + // Get the text from the JTextField and trim leading/trailing whitespace + String sanitizedText = textField.getText().trim(); + + // Remove unwanted characters (single quotes, double quotes, non-alphanumeric characters) + sanitizedText = sanitizedText.replaceAll("[^a-zA-Z0-9]", ""); // Remove all non-alphanumeric characters + + // Optionally, remove single quotes and double quotes + sanitizedText = sanitizedText.replaceAll("[\'\"]", ""); + + // If the text is empty after sanitization, set a default value + if (sanitizedText.isEmpty()) { + sanitizedText = "default"; // or you can return an empty string, or throw an exception, depending on the case + } + + return sanitizedText; + } + } diff --git a/src/main/java/net/ayyalasomayajula/net/server/ServerDaemon.java b/src/main/java/net/ayyalasomayajula/net/server/ServerDaemon.java index f691a2c..18a1120 100644 --- a/src/main/java/net/ayyalasomayajula/net/server/ServerDaemon.java +++ b/src/main/java/net/ayyalasomayajula/net/server/ServerDaemon.java @@ -73,47 +73,59 @@ public class ServerDaemon implements Runnable { * @param path The root path to initialize. * @return True if initialization is successful, false otherwise. */ + private boolean initRootPath(Path path) { // Create the lock file String lockFile = "lock.lock"; // Set your desired file name + Path fullPath = path.resolve(lockFile); // Combine the base path and file name - // Combine the base path and file name - Path fullPath = path.resolve(lockFile); - - // Create the file File file = fullPath.toFile(); try { // Create the parent directories if they don't exist - file.getParentFile().mkdirs(); + if (!file.getParentFile().exists() && !file.getParentFile().mkdirs()) { + logger.error("Failed to create directories for lock file."); + return false; + } - // Create the file - if (file.createNewFile()) { - logger.info("File created: " + file.getName()); + // Create the lock file if it doesn't already exist + if (!file.exists()) { + if (file.createNewFile()) { + logger.info("Lock file created: " + file.getName()); - // Write a random byte to the file - try (FileOutputStream fos = new FileOutputStream(file)) { - Random random = new Random(); - byte randomByte = (byte) random.nextInt(256); // Generate random byte - fos.write(randomByte); - logger.info("Random byte written to file."); + // Write a random byte to the file + try (FileOutputStream fos = new FileOutputStream(file)) { + Random random = new Random(); + byte randomByte = (byte) random.nextInt(256); // Generate random byte + fos.write(randomByte); + logger.info("Random byte written to lock file."); + } + } else { + logger.error("Failed to create lock file."); + return false; } } else { - System.out.println("File already exists."); + logger.info("Lock file already exists."); } + + // Create the patient directory + String patientEhrs = "patients/"; + Path patientPath = path.resolve(patientEhrs); + File dir = patientPath.toFile(); + + if (!dir.exists() && !dir.mkdirs()) { + logger.error("Failed to create patient directory."); + return false; + } + + return true; } catch (IOException e) { - System.err.println("An error occurred: " + e.getMessage()); + logger.error("An error occurred during lock file creation: {}", e.getMessage()); + return false; } - - // Create patient directory - String patientEhrs = "patients/"; - Path patientPath = path.resolve(patientEhrs); - File dir = patientPath.toFile(); - dir.mkdir(); - - return true; } + /** * Checks whether the root path has already been initialized by checking if the lock file exists. * diff --git a/target/classes/net/ayyalasomayajula/net/ClientServerSelect$1.class b/target/classes/net/ayyalasomayajula/net/ClientServerSelect$1.class index 2c8c4e4..2213331 100644 Binary files a/target/classes/net/ayyalasomayajula/net/ClientServerSelect$1.class and b/target/classes/net/ayyalasomayajula/net/ClientServerSelect$1.class differ diff --git a/target/classes/net/ayyalasomayajula/net/ClientServerSelect$2.class b/target/classes/net/ayyalasomayajula/net/ClientServerSelect$2.class index 5e9e48d..6ec6494 100644 Binary files a/target/classes/net/ayyalasomayajula/net/ClientServerSelect$2.class and b/target/classes/net/ayyalasomayajula/net/ClientServerSelect$2.class differ diff --git a/target/classes/net/ayyalasomayajula/net/ClientServerSelect.class b/target/classes/net/ayyalasomayajula/net/ClientServerSelect.class index 6faf9ec..89ada10 100644 Binary files a/target/classes/net/ayyalasomayajula/net/ClientServerSelect.class and b/target/classes/net/ayyalasomayajula/net/ClientServerSelect.class differ diff --git a/target/classes/net/ayyalasomayajula/net/client/ClientConnect$1.class b/target/classes/net/ayyalasomayajula/net/client/ClientConnect$1.class index 906bb9a..34781ca 100644 Binary files a/target/classes/net/ayyalasomayajula/net/client/ClientConnect$1.class and b/target/classes/net/ayyalasomayajula/net/client/ClientConnect$1.class differ diff --git a/target/classes/net/ayyalasomayajula/net/client/ClientConnect.class b/target/classes/net/ayyalasomayajula/net/client/ClientConnect.class index bf945a1..5ce142b 100644 Binary files a/target/classes/net/ayyalasomayajula/net/client/ClientConnect.class and b/target/classes/net/ayyalasomayajula/net/client/ClientConnect.class differ diff --git a/target/classes/net/ayyalasomayajula/net/server/ServerDaemon$1.class b/target/classes/net/ayyalasomayajula/net/server/ServerDaemon$1.class new file mode 100644 index 0000000..e5546d6 Binary files /dev/null and b/target/classes/net/ayyalasomayajula/net/server/ServerDaemon$1.class differ diff --git a/target/classes/net/ayyalasomayajula/net/server/ServerDaemon.class b/target/classes/net/ayyalasomayajula/net/server/ServerDaemon.class new file mode 100644 index 0000000..cb3931a Binary files /dev/null and b/target/classes/net/ayyalasomayajula/net/server/ServerDaemon.class differ