Skip to content

Commit

Permalink
Merge pull request #1 from teebow1e/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
teebow1e authored Apr 18, 2024
2 parents 9dd3e2c + f44febb commit c58d80d
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 19 deletions.
1 change: 1 addition & 0 deletions credentials/cred.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
teebow1e,password,admin
55 changes: 55 additions & 0 deletions src/main/java/loganalyzer/Log.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package loganalyzer;

public class Log {
public Log(String ipAddress, String timeStamp, String method, String protocol, String requestPath, int statusCode, int contentLength, String userAgent) {
this.ipAddress = ipAddress;
this.timeStamp = timeStamp;
this.method = method;
this.protocol = protocol;
this.requestPath = requestPath;
this.statusCode = statusCode;
this.contentLength = contentLength;
this.userAgent = userAgent;
}

public String getIpAddress() {
return ipAddress;
}

public String getTimestamp() {
return timeStamp;
}

public String getMethod() {
return method;
}

public String getProtocol() {
return protocol;
}

public String getRequestPath() {
return requestPath;
}

public int getStatusCode() {
return statusCode;
}

public int getContentLength() {
return contentLength;
}

public String getUserAgent() {
return userAgent;
}

private final String ipAddress;
private final String timeStamp;
private final String method;
private final String protocol;
private final String requestPath;
private final int statusCode;
private final int contentLength;
private final String userAgent;
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,33 @@
package LogAnalyzer;
package loganalyzer;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

public class LogCrawl {
public class LogParser {
public static void main(String[] args) {
// Define the path to the log file
String logFilePath = "logs/apache/access_log_100.log";

// Create a Path object for the log file
Path logPath = Paths.get(logFilePath);

try {
// Check if the log file exists
if (Files.exists(logPath)) {
// Read the contents of the log file
List<String> lines = Files.readAllLines(logPath);

// Print the first 5 lines of the log file
System.out.println("First 5 lines of " + logPath.getFileName() + ":");
int lineCount = 0;
for (String line : lines) {
System.out.println(line);
lineCount++;
if (lineCount >= 5) {
break; // Exit the loop after reading 5 lines
break;
}
}
} else {
System.out.println("Log file does not exist: " + logPath);
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("Error reading log file: " + e.getMessage());
}
}
}
35 changes: 31 additions & 4 deletions src/main/java/ui/Controller.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
package ui;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import javafx.scene.control.*;
import usermanagement.User;
import usermanagement.UserManagement;

import javax.swing.JOptionPane;

import java.io.File;
import java.util.List;

public class Controller {
private Stage stage;
public List<User> userLists;

// Method to initialize the stage
public void init(Stage stage) {
String dbFilePath = System.getProperty("user.dir") + "\\credentials\\cred.txt";
File dbFile = new File(dbFilePath);
if (dbFile.exists()) {
System.out.println("exists: " + dbFile.getAbsolutePath());
userLists = UserManagement.readUserFile(dbFilePath);
} else {
System.out.println("not exists: " + dbFile.getAbsolutePath());
}
this.stage = stage;
}

Expand All @@ -22,6 +38,11 @@ public void init(Stage stage) {
@FXML
private TextField rePasswordField;

@FXML
private void onEnter(ActionEvent ae) throws Exception {
System.out.println("Key Enter clicked");
handleLoginAccount(null);
}
@FXML
private void handleCreateAccount(MouseEvent event) throws Exception{
SignUpForm signUpForm = new SignUpForm();
Expand Down Expand Up @@ -54,9 +75,15 @@ private void handleLoginAccount(MouseEvent event) throws Exception {

System.out.println("Username: " + username);
System.out.println("Password: " + password);

WebLogManager webLogManager = new WebLogManager();
webLogManager.start(stage);
boolean authenticated = UserManagement.authenticateUser(userLists, username, password);
if (authenticated) {
System.out.println("Login successful");
WebLogManager webLogManager = new WebLogManager();
webLogManager.start(stage);
} else {
System.out.println("Login failed");
JOptionPane.showMessageDialog(null, "Nhap sai password roi cu!");
}
}

}
1 change: 1 addition & 0 deletions src/main/java/ui/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
public class Main {
public static void main(String[] args) {
Application.launch(LoginForm.class, args);

// Application.launch(WebLogManager.class, args);
}
}
25 changes: 25 additions & 0 deletions src/main/java/usermanagement/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package usermanagement;

public class User {
private final String username;
private final String password;
private final String role;

public User(String username, String password, String role) {
this.username = username;
this.password = password;
this.role = role;
}

public String getUsername() {
return username;
}

public String getPassword() {
return password;
}

public String getRole() {
return role;
}
}
37 changes: 37 additions & 0 deletions src/main/java/usermanagement/UserManagement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package usermanagement;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class UserManagement {
public static List<User> readUserFile(String fileName) {
List<User> userList = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 3) {
String username = parts[0];
String password = parts[1];
String role = parts[2];
userList.add(new User(username, password, role));
}
}
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
}
return userList;
}

public static boolean authenticateUser(List<User> userList, String username, String password) {
for (User user : userList) {
if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
return true;
}
}
return false;
}
}
4 changes: 2 additions & 2 deletions src/main/resources/css/Form.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
}

.login-wrapper {

-fx-padding: 0 0 11px 0;
}

.login-label {
-fx-font-size: 28px;
-fx-font-size: 30px;
-fx-font-weight: bold;
-fx-text-fill: #000000;
-fx-alignment: center;
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/fxml/LoginForm.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
</VBox>

<VBox alignment="CENTER" spacing="10" styleClass="textbox-wrapper">
<TextField fx:id="usernameField" promptText="Enter username" styleClass="text-box"/>
<PasswordField fx:id="passwordField" promptText="Enter password" styleClass="text-box"/>
<TextField fx:id="usernameField" promptText="Enter username" styleClass="text-box" onAction="#onEnter"/>
<PasswordField fx:id="passwordField" promptText="Enter password" styleClass="text-box" onAction="#onEnter"/>
<Button text="Login" styleClass="button" onMouseClicked="#handleLoginAccount"/>
<Label text="Create Account" styleClass="create-account-label" onMouseClicked="#handleCreateAccount"/>
</VBox>
Expand Down

0 comments on commit c58d80d

Please sign in to comment.