@@ -21,26 +21,39 @@ private ApacheParser() {
2121 throw new IllegalStateException ("Utility class" );
2222 }
2323 private static final Pattern ipAddrPattern = Pattern .compile ("((\\ d{1,3}\\ .\\ d{1,3}\\ .\\ d{1,3}\\ .\\ d{1,3})|([0-9a-fA-F]{1,4}(:[0-9a-fA-F]{1,4}){7}))" );
24+ private static final Pattern ipv6AddrPattern = Pattern .compile ("([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\ \\ .){3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\ \\ .){3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])" );
2425 private static final Pattern timestampPattern = Pattern .compile ("\\ [(\\ d{2}/[A-Za-z]{3}/\\ d{4}:\\ d{2}:\\ d{2}:\\ d{2} [+\\ -]\\ d{4})]" );
2526 private static final Pattern userAgentPattern = Pattern .compile ("\" ([^\" ]*)\" [^\" ]*$" );
2627 private static final Logger logger = Logger .getLogger (ApacheParser .class .getName ());
2728 public static String parseIpAddress (String logLine ) {
28- return findFirstMatch (logLine , ipAddrPattern );
29+ if (findFirstMatch (logLine , ipAddrPattern ) == null ) {
30+ return findFirstMatch (logLine , ipv6AddrPattern );
31+ } else {
32+ return findFirstMatch (logLine , ipAddrPattern );
33+ }
2934 }
3035 public static String parseTimestamp (String logLine ) {
3136 String parsedLog = findFirstMatch (logLine , timestampPattern );
3237 if (parsedLog != null ) {
33- return parsedLog .substring (1 , parsedLog .length () - 1 );
38+ try {
39+ return parsedLog .substring (1 , parsedLog .length () - 1 );
40+ } catch (Exception e ) {
41+ return Config .NOT_AVAILABLE_TEXT ;
42+ }
3443 } else {
35- return null ;
44+ return Config . NOT_AVAILABLE_TEXT ;
3645 }
3746 }
3847 public static String parseUserAgent (String logLine ) {
3948 String parsedLog = findFirstMatch (logLine , userAgentPattern );
4049 if (parsedLog != null ) {
41- return parsedLog .substring (1 , parsedLog .length () - 1 );
50+ try {
51+ return parsedLog .substring (1 , parsedLog .length () - 1 );
52+ } catch (Exception e ) {
53+ return Config .NOT_AVAILABLE_TEXT ;
54+ }
4255 } else {
43- return null ;
56+ return Config . NOT_AVAILABLE_TEXT ;
4457 }
4558 }
4659
@@ -51,39 +64,60 @@ public static String[] parseAllInOne(String logLine) {
5164 public static String parseMethod (String [] aioArr ) {
5265 String tempMethodValue = getElementSafely (aioArr , 5 );
5366 if (tempMethodValue != null ) {
54- return tempMethodValue .replace ("\" " , "" );
67+ try {
68+ return tempMethodValue .replace ("\" " , "" );
69+ } catch (Exception e ) {
70+ return Config .NOT_AVAILABLE_TEXT ;
71+ }
5572 }
56- return null ;
73+ return Config . NOT_AVAILABLE_TEXT ;
5774 }
5875
5976 public static String parseProtocol (String [] aioArr ) {
6077 String tempMethodValue = getElementSafely (aioArr , 7 );
6178 if (tempMethodValue != null ) {
62- return tempMethodValue .replace ("\" " , "" );
79+ try {
80+ return tempMethodValue .replace ("\" " , "" );
81+ } catch (Exception e ) {
82+ return Config .NOT_AVAILABLE_TEXT ;
83+ }
6384 }
64- return null ;
85+ return Config . NOT_AVAILABLE_TEXT ;
6586 }
6687
6788 public static String parseRequestPath (String [] aioArr ) {
6889 String tempMethodValue = getElementSafely (aioArr , 6 );
6990 if (tempMethodValue != null ) {
70- return tempMethodValue .replace ("\" " , "" );
91+ try {
92+ return tempMethodValue .replace ("\" " , "" );
93+ } catch (Exception e ) {
94+ return Config .NOT_AVAILABLE_TEXT ;
95+ }
7196 }
72- return null ;
97+ return Config . NOT_AVAILABLE_TEXT ;
7398 }
7499
75100 public static int parseStatusCode (String [] aioArr ) {
76101 String tempMethodValue = getElementSafely (aioArr , 8 );
77102 if (tempMethodValue != null ) {
78- return Integer .parseInt (tempMethodValue .replace ("\" " , "" ));
103+ try {
104+ return Integer .parseInt (tempMethodValue .replace ("\" " , "" ));
105+ } catch (Exception e ) {
106+ return 0 ;
107+ }
108+
79109 }
80110 return 0 ;
81111 }
82112
83113 public static int parseContentLength (String [] aioArr ) {
84114 String tempMethodValue = getElementSafely (aioArr , 9 );
85115 if (tempMethodValue != null ) {
86- return Integer .parseInt (tempMethodValue .replace ("\" " , "" ));
116+ try {
117+ return Integer .parseInt (tempMethodValue .replace ("\" " , "" ));
118+ } catch (Exception e ) {
119+ return 0 ;
120+ }
87121 }
88122 return 0 ;
89123 }
@@ -102,8 +136,7 @@ public static Apache parseLogLine(String line) {
102136 parseUserAgent (line )
103137 );
104138 } catch (Exception e ) {
105- System .out .println (e );
106- System .out .println (line );
139+ logger .log (Level .WARNING , "An error occurred during parsing phase: {0}" , e );
107140 }
108141 return null ;
109142 }
0 commit comments