public class LogParser implements IPQuery { private Path logDir; public LogParser(Path logDir) { this.logDir = logDir; } @Override public int getNumberOfUniqueIPs(Date after, Date before) { return getUniqueIPs(after, before).size(); } @Override public Set<String> getUniqueIPs(Date after, Date before) { Set<String> setIp = new HashSet<>(); List<String> listAllInformation = getAllFiles(logDir); for (String s : listAllInformation) { String[] strings = s.split("\t"); Date date = findDate(strings); if (checkDate(after, before, date)) { String ip = findIP(strings); setIp.add(ip); } } return setIp; } @Override public Set<String> getIPsForUser(String user, Date after, Date before) { Set<String> userIps = new HashSet<>(); List<String> listAllInformation = getAllFiles(logDir); for (String s : listAllInformation) { String[] strings = s.split("\t"); Date date = findDate(strings); if (checkDate(after, before, date)) { String ipUser = findUserIp(user, strings); if (ipUser != null) { userIps.add(ipUser); } } } return userIps; } @Override public Set<String> getIPsForEvent(Event event, Date after, Date before) { Set<String> eventIps = new HashSet<>(); List<String> listAllInformation = getAllFiles(logDir); for (String s : listAllInformation) { String[] strings = s.split("\t"); Date date = findDate(strings); if (checkDate(after, before, date)) { String ipEvent = findEventIP(event, strings); if (ipEvent != null) { eventIps.add(ipEvent); } } } return eventIps; } @Override public Set<String> getIPsForStatus(Status status, Date after, Date before) { Set<String> statusIps = new HashSet<>(); List<String> listAllInformation = getAllFiles(logDir); for (String s : listAllInformation) { String[] strings = s.split("\t"); Date date = findDate(strings); if (checkDate(after, before, date)) { String ipStatus = findStatusIP(status, strings); if (ipStatus != null) { statusIps.add(ipStatus); } } } return statusIps; } public List<String> getAllFiles(Path path) { List<String> fileList = new ArrayList<>(); File file = path.toFile(); for (File f : file.listFiles()) { if (f.isDirectory()) { getAllFiles(f.toPath()); } else if (f.isFile() & f.getName().endsWith("log")) { FileReader fileReader; try { fileReader = new FileReader(f); BufferedReader buff = new BufferedReader(fileReader); String line; while ((line = buff.readLine()) != null) { fileList.add(line); } fileReader.close(); buff.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } return fileList; } public String findIP(String[] strings) { String ip = ""; Pattern p = Pattern.compile("(\\d{0,3}\\.){3}\\d{0,3}"); for (String s : strings) { Matcher m = p.matcher(s); if (m.find()) { ip = m.group(); break; } } return ip; } public Date findDate(String[] lines) { Date date = null; SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); Pattern p = Pattern.compile("\\d{1,2}\\.\\d{1,2}\\.\\d{4,} \\d{2}:\\d{2}:\\d{2}"); for (String line : lines) { Matcher m = p.matcher(line); if (m.find()) { try { date = sdf.parse(line); break; } catch (ParseException e) { e.printStackTrace(); } } } return date; } public boolean checkDate(Date after, Date before, Date date) { DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); Date getAfter = null; Date getBefore = null; try { if (after == null) { after = new Date(0); getAfter = dateFormat.parse(dateFormat.format(after)); } else getAfter = dateFormat.parse(dateFormat.format(after)); if (before == null) { before = new Date(Long.MAX_VALUE); getBefore = dateFormat.parse(dateFormat.format(before)); } else getBefore = dateFormat.parse(dateFormat.format(before)); } catch (ParseException e) { } return date.after(getAfter) && date.before(getBefore); } public String findUserIp(String user, String[] strings) { String userIp = null; for (String s : strings) { if (s.contains(user)) { userIp = findIP(strings); break; } } return userIp; } public String findEventIP(Event event, String[] strings) { String eventIp = null; for (String s : strings) { if (s.contains(event.toString())) { eventIp = findIP(strings); break; } } return eventIp; } public String findStatusIP(Status status, String[] strings) { String statusIp = null; for (String s : strings) { if (s.contains(status.toString())) { statusIp = findIP(strings); break; } } return statusIp; } }