11package  org .owasp .benchmark .report .sonarqube ;
22
3+ import  static  java .lang .String .join ;
4+ import  static  java .nio .charset .Charset .defaultCharset ;
5+ import  static  org .apache .commons .io .FileUtils .writeStringToFile ;
6+ import  static  org .apache .commons .io .IOUtils .readLines ;
7+ 
38import  com .fasterxml .jackson .core .JsonProcessingException ;
49import  com .fasterxml .jackson .databind .ObjectMapper ;
5- import  org .owasp .benchmark .report .sonarqube .dto .SonarQubeResult ;
6- 
7- import  javax .xml .parsers .DocumentBuilderFactory ;
810import  java .io .File ;
911import  java .io .IOException ;
1012import  java .net .HttpURLConnection ;
1517import  java .util .List ;
1618import  java .util .Set ;
1719import  java .util .function .Consumer ;
18- 
19- import  static  java .lang .String .join ;
20- import  static  java .nio .charset .Charset .defaultCharset ;
21- import  static  org .apache .commons .io .FileUtils .writeStringToFile ;
22- import  static  org .apache .commons .io .IOUtils .readLines ;
20+ import  javax .xml .parsers .DocumentBuilderFactory ;
21+ import  org .owasp .benchmark .report .sonarqube .dto .SonarQubeResult ;
2322
2423public  class  SonarReport  {
2524    private  static  final  String  SONAR_USER  = "admin" ;
@@ -30,9 +29,8 @@ public class SonarReport {
3029
3130    private  static  final  int  PAGE_SIZE  = 500 ;
3231
33-     private  static  final  String  sonarAuth  = Base64 .getEncoder ()
34-         .encodeToString ((SONAR_USER  + ":"  + SONAR_PASSWORD )
35-             .getBytes ());
32+     private  static  final  String  sonarAuth  =
33+             Base64 .getEncoder ().encodeToString ((SONAR_USER  + ":"  + SONAR_PASSWORD ).getBytes ());
3634
3735    private  static  final  ObjectMapper  objectMapper  = new  ObjectMapper ();
3836
@@ -42,55 +40,56 @@ public static void main(String[] args) throws Exception {
4240        List <String > hotspots  = new  ArrayList <>();
4341
4442        forAllPagesAt (
45-             "issues/search?componentKeys="  + SONAR_PROJECT  + "&types=VULNERABILITY&&rules="  + allJavaRules ,
46-             (result  -> issues .addAll (result .issues ))
47-         );
43+                 "issues/search?componentKeys=" 
44+                         + SONAR_PROJECT 
45+                         + "&types=VULNERABILITY&&rules=" 
46+                         + allJavaRules ,
47+                 (result  -> issues .addAll (result .issues )));
4848        forAllPagesAt (
49-             "hotspots/search?projectKey="  + SONAR_PROJECT ,
50-             (result  -> hotspots .addAll (result .hotspots ))
51-         );
49+                 "hotspots/search?projectKey="  + SONAR_PROJECT ,
50+                 (result  -> hotspots .addAll (result .hotspots )));
5251
5352        writeStringToFile (
54-             new  File ("results/"  + resultFilename () + ".json" ),
55-             formattedJson (issues , hotspots ),
56-             defaultCharset ()
57-         );
53+                 new  File ("results/"  + resultFilename () + ".json" ),
54+                 formattedJson (issues , hotspots ),
55+                 defaultCharset ());
5856    }
5957
6058    private  static  String  resultFilename () throws  Exception  {
6159        return  "Benchmark_"  + benchmarkVersion () + "-sonarqube-v"  + apiCall ("server/version" );
6260    }
6361
6462    private  static  String  benchmarkVersion () throws  Exception  {
65-         return  DocumentBuilderFactory 
66-             .newInstance ()
67-             .newDocumentBuilder ()
68-             .parse (new  File ("pom.xml" ))
69-             .getElementsByTagName ("version" )
70-             .item (0 )
71-             .getTextContent ();
63+         return  DocumentBuilderFactory .newInstance ()
64+                 .newDocumentBuilder ()
65+                 .parse (new  File ("pom.xml" ))
66+                 .getElementsByTagName ("version" )
67+                 .item (0 )
68+                 .getTextContent ();
7269    }
7370
7471    private  static  Set <String > allJavaRules () throws  IOException  {
7572        Set <String > javaRuleIds  = new  HashSet <>();
7673
77-         forAllPagesAt ("rules/search" , (result ) -> result 
78-             .rules 
79-             .stream ().filter (rule  -> rule .ruleId .startsWith ("java:" ))
80-             .forEach (rule  -> javaRuleIds .add (rule .ruleId )));
74+         forAllPagesAt (
75+                 "rules/search" ,
76+                 (result ) ->
77+                         result .rules .stream ()
78+                                 .filter (rule  -> rule .ruleId .startsWith ("java:" ))
79+                                 .forEach (rule  -> javaRuleIds .add (rule .ruleId )));
8180
8281        return  javaRuleIds ;
8382    }
8483
85-     private  static  void  forAllPagesAt (String  apiPath , Consumer <SonarQubeResult > pageHandlerCallback ) throws  IOException  {
84+     private  static  void  forAllPagesAt (String  apiPath , Consumer <SonarQubeResult > pageHandlerCallback )
85+             throws  IOException  {
8686        int  pages ;
8787        int  page  = 1 ;
8888
8989        do  {
90-             SonarQubeResult  result  = objectMapper .readValue (
91-                 apiCall (apiPath  + pagingSuffix (page , apiPath )),
92-                 SonarQubeResult .class 
93-             );
90+             SonarQubeResult  result  =
91+                     objectMapper .readValue (
92+                             apiCall (apiPath  + pagingSuffix (page , apiPath )), SonarQubeResult .class );
9493
9594            pages  = (result .paging .resultCount  / PAGE_SIZE ) + 1 ;
9695
@@ -114,11 +113,17 @@ private static String apiCall(String apiPath) throws IOException {
114113        return  join ("\n " , readLines (connection .getInputStream (), defaultCharset ()));
115114    }
116115
117-     private  static  String  formattedJson (List <String > issues , List <String > hotspots ) throws  JsonProcessingException  {
118-         String  sb  = "{\" issues\" :["  + join ("," , issues ) + "],\" hotspots\" :["  + join ("," , hotspots ) + "]}" ;
116+     private  static  String  formattedJson (List <String > issues , List <String > hotspots )
117+             throws  JsonProcessingException  {
118+         String  sb  =
119+                 "{\" issues\" :[" 
120+                         + join ("," , issues )
121+                         + "],\" hotspots\" :[" 
122+                         + join ("," , hotspots )
123+                         + "]}" ;
119124
120125        return  objectMapper 
121-             .writerWithDefaultPrettyPrinter ()
122-             .writeValueAsString (objectMapper .readValue (sb , Object .class ));
126+                  .writerWithDefaultPrettyPrinter ()
127+                  .writeValueAsString (objectMapper .readValue (sb , Object .class ));
123128    }
124129}
0 commit comments