Skip to content

Commit

Permalink
fix(*): fix code to comply with SonarQube fix
Browse files Browse the repository at this point in the history
  • Loading branch information
teebow1e committed Apr 18, 2024
1 parent b956277 commit 0293862
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
11 changes: 7 additions & 4 deletions src/main/java/loganalyzer/LogParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
import java.nio.file.Paths;
import java.util.List;
import java.util.regex.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class LogParser {
private static final Pattern ipAddrPattern = Pattern.compile("(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})");
private static final Pattern timestampPattern = Pattern.compile("\\[(\\d{2}/[A-Za-z]{3}/\\d{4}:\\d{2}:\\d{2}:\\d{2} [+\\-]\\d{4})]");
private static final Pattern userAgentPattern = Pattern.compile("\"([^\"]*)\"[^\"]*$");
private static final Pattern allInOnePattern = Pattern.compile("\"(GET|HEAD|POST|PUT|DELETE|CONNECT|OPTIONS|TRACE|PATCH) \\/[a-zA-Z0-9.\\-_~!$&'()*+,;=:@]+ (HTTP|HTTPS)\\/\\d+(\\.\\d+)*\" \\d{3}(\\.\\d+)? \\d+");
public static void main(String[] args) {
String logFilePath = System.getProperty("user.dir") + "/logs/apache_nginx/access_log_10000.log";
Logger logger = Logger.getLogger(LogParser.class.getName());
String logFilePath = System.getProperty("user.dir") + "/logs/apache_nginx/access_log_50000.log";
Path logPath = Paths.get(logFilePath);

try {
Expand All @@ -35,15 +38,15 @@ public static void main(String[] args) {
}
System.out.println("------------------------------------------------");
lineCount++;
if (lineCount >= 10000) {
if (lineCount >= 50000) {
break;
}
}
} else {
System.out.println("Log file does not exist: " + logPath);
logger.log(Level.SEVERE, "Log file not found at location " + logFilePath);
}
} catch (IOException e) {
System.out.println("Error reading log file: " + e.getMessage());
logger.log(Level.SEVERE, "Error reading log file: " + e.getMessage());
}
}

Expand Down
16 changes: 10 additions & 6 deletions src/main/java/ui/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@

import java.io.File;
import java.util.List;
import java.util.logging.Logger;
import java.util.logging.Level;

public class Controller {
private Stage stage;
public List<User> userLists;
private List<User> userLists;
// Not sure if declare logger here is correct
private Logger logger;

public void init(Stage stage) {
Logger logger = new Logger.getLogger(Controller.class.getName());
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());
logger.log(Level.SEVERE, "DB file not exists: " + dbFile.getAbsolutePath());
}
this.stage = stage;
}
Expand All @@ -40,7 +44,7 @@ public void init(Stage stage) {

@FXML
private void onEnter(ActionEvent ae) throws Exception {
System.out.println("Key Enter clicked");
logger.log(Level.INFO, "Enter key pressed");
handleLoginAccount(null);
}
@FXML
Expand Down Expand Up @@ -77,11 +81,11 @@ private void handleLoginAccount(MouseEvent event) throws Exception {
System.out.println("Password: " + password);
boolean authenticated = UserManagement.authenticateUser(userLists, username, password);
if (authenticated) {
System.out.println("Login successful");
logger.log(Level.INFO, "Login successful");
WebLogManager webLogManager = new WebLogManager();
webLogManager.start(stage);
} else {
System.out.println("Login failed");
logger.log(Level.INFO, "Login failed");
JOptionPane.showMessageDialog(null, "Nhap sai password roi cu!");
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/ui/SidebarController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import javafx.scene.Node;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import javafx.scene.control.*;

public class SidebarController {

private Stage stage;

public void init(Stage stage) {
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/usermanagement/UserManagement.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class UserManagement {
private static final Logger logger = Logger.getLogger(UserManagement.class.getName());
private UserManagement() {
throw new IllegalStateException("Utility class");
}
public static List<User> readUserFile(String fileName) {
List<User> userList = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
Expand All @@ -21,7 +27,7 @@ public static List<User> readUserFile(String fileName) {
}
}
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
logger.log(Level.SEVERE, "An error occurred while reading the user file", e);
}
return userList;
}
Expand Down

0 comments on commit 0293862

Please sign in to comment.