1111import java .util .List ;
1212import java .util .Map ;
1313
14+ import javax .net .ssl .SSLContext ;
15+ import javax .net .ssl .SSLParameters ;
16+ import javax .net .ssl .TrustManager ;
17+ import javax .net .ssl .X509TrustManager ;
18+
1419import org .json .JSONArray ;
1520import org .json .JSONException ;
1621import org .json .JSONObject ;
1722
1823
1924
20- public class GenerateCNESReport {
25+ public class GenerateSonarReport {
26+
27+ private static HttpClient createUnsafeHttpClient () {
28+ try {
29+ TrustManager [] trustAll = new TrustManager []{
30+ new X509TrustManager () {
31+ public java .security .cert .X509Certificate [] getAcceptedIssuers () { return new java .security .cert .X509Certificate [0 ]; }
32+ public void checkClientTrusted (java .security .cert .X509Certificate [] certs , String authType ) {}
33+ public void checkServerTrusted (java .security .cert .X509Certificate [] certs , String authType ) {}
34+ }
35+ };
36+
37+ SSLContext sslContext = SSLContext .getInstance ("TLS" );
38+ sslContext .init (null , trustAll , new java .security .SecureRandom ());
39+
40+ SSLParameters sslParams = sslContext .getDefaultSSLParameters ();
41+ sslParams .setEndpointIdentificationAlgorithm (null );
42+
43+ return HttpClient .newBuilder ()
44+ .sslContext (sslContext )
45+ .sslParameters (sslParams )
46+ .build ();
47+
48+ } catch (java .security .NoSuchAlgorithmException | java .security .KeyManagementException e ) {
49+ throw new IllegalStateException ("The insecure HttpClient couldn't be created" , e );
50+ }
51+ }
2152
2253 public static JSONObject fetchDataFromURL (String url , String call , String token , String projectKey ) throws IOException , InterruptedException {
23- HttpClient client = HttpClient . newHttpClient ();
54+ HttpClient client = createUnsafeHttpClient ();
2455 String encodedProjectKey = URLEncoder .encode (projectKey , StandardCharsets .UTF_8 );
2556 String fullURL = String .format ("%s%s%s" , url , call , encodedProjectKey );
57+
2658
2759 HttpRequest request = HttpRequest .newBuilder ()
2860 .uri (URI .create (fullURL ))
2961 .header ("Authorization" , "Bearer " + token )
3062 .build ();
31-
63+
64+
3265 HttpResponse <String > response = client .send (request , HttpResponse .BodyHandlers .ofString ());
3366
3467 JSONObject json = new JSONObject (response .body ());
3568
3669 if (response .statusCode () >= 200 && response .statusCode () < 300 ) {
3770 return json ;
3871 } else {
39- throw new IOException ("Error al obtener datos de la URL: Código de estado " + response .statusCode () + ", Cuerpo : " + response .body ());
72+ throw new IOException ("Error at obtaining data from the URL: Status Code " + response .statusCode () + ", Body : " + response .body ());
4073 }
4174 }
4275
@@ -49,14 +82,13 @@ public static void main(String[] args) throws IOException {
4982 String apiUrl = args [0 ];
5083 String authToken = args [1 ];
5184 String project = args [2 ];
52-
85+
5386 // We create the initial pdf
5487 PDFReportWriter pdf = new PDFReportWriter ();
5588 JSONObject data = null ;
5689 JSONArray dataArray = null ;
5790 try {
58-
59- data = fetchDataFromURL (apiUrl , "api/navigation/component?component=" , authToken , project );
91+ data = fetchDataFromURL (apiUrl , "/api/navigation/component?component=" , authToken , project );
6092 } catch (IOException | InterruptedException e ) {
6193 System .err .println ("Error at doing the HTTP petition: " + e .getMessage ());
6294 }
@@ -115,7 +147,7 @@ public static void main(String[] args) throws IOException {
115147 data = null ;
116148 try {
117149
118- data = fetchDataFromURL (apiUrl , "api/measures/component?metricKeys=reliability_rating,software_quality_maintainability_rating,security_rating,security_review_rating&component=" , authToken , project );
150+ data = fetchDataFromURL (apiUrl , "/ api/measures/component?metricKeys=reliability_rating,software_quality_maintainability_rating,security_rating,security_review_rating&component=" , authToken , project );
119151 data = data .getJSONObject ("component" );
120152
121153 } catch (IOException | InterruptedException e ) {
@@ -149,7 +181,7 @@ public static void main(String[] args) throws IOException {
149181
150182 try {
151183
152- data = fetchDataFromURL (apiUrl , "api/qualitygates/project_status?projectKey=" , authToken , project );
184+ data = fetchDataFromURL (apiUrl , "/ api/qualitygates/project_status?projectKey=" , authToken , project );
153185 data = data .getJSONObject ("projectStatus" );
154186
155187 } catch (IOException | InterruptedException e ) {
@@ -166,7 +198,7 @@ public static void main(String[] args) throws IOException {
166198
167199 try {
168200
169- data = fetchDataFromURL (apiUrl , "api/measures/component?metricKeys=duplicated_lines_density,comment_lines_density,ncloc,complexity,cognitive_complexity,coverage&component=" , authToken , project );
201+ data = fetchDataFromURL (apiUrl , "/ api/measures/component?metricKeys=duplicated_lines_density,comment_lines_density,ncloc,complexity,cognitive_complexity,coverage&component=" , authToken , project );
170202 data = data .getJSONObject ("component" );
171203
172204 } catch (IOException | InterruptedException e ) {
@@ -185,6 +217,7 @@ public static void main(String[] args) throws IOException {
185217 metricIndex .put ("cognitive_complexity" , 5 );
186218 measuresList = data .getJSONArray ("measures" );
187219 measures = new String [6 ];
220+ Arrays .fill (measures , "0" );
188221
189222 for (int i = 0 ; i < measuresList .length (); i ++) {
190223 JSONObject measure = measuresList .getJSONObject (i );
@@ -211,7 +244,7 @@ public static void main(String[] args) throws IOException {
211244
212245 try {
213246
214- data = fetchDataFromURL (apiUrl , "api/measures/component?metricKeys=duplicated_lines_density,comment_lines_density,ncloc,complexity,cognitive_complexity,coverage&component=" , authToken , project );
247+ data = fetchDataFromURL (apiUrl , "/ api/measures/component?metricKeys=duplicated_lines_density,comment_lines_density,ncloc,complexity,cognitive_complexity,coverage&component=" , authToken , project );
215248 data = data .getJSONObject ("component" );
216249
217250 } catch (IOException | InterruptedException e ) {
@@ -256,7 +289,7 @@ public static void main(String[] args) throws IOException {
256289
257290 try {
258291
259- data = fetchDataFromURL (apiUrl , "api/measures/component?metricKeys=reliability_remediation_effort,security_remediation_effort,sqale_index&component=" , authToken , project );
292+ data = fetchDataFromURL (apiUrl , "/ api/measures/component?metricKeys=reliability_remediation_effort,security_remediation_effort,sqale_index&component=" , authToken , project );
260293 data = data .getJSONObject ("component" );
261294
262295 } catch (IOException | InterruptedException e ) {
@@ -298,7 +331,7 @@ public static void main(String[] args) throws IOException {
298331
299332 try {
300333
301- data = fetchDataFromURL (apiUrl , "api/measures/component?metricKeys=ncloc_language_distribution&component=" , authToken , project );
334+ data = fetchDataFromURL (apiUrl , "/ api/measures/component?metricKeys=ncloc_language_distribution&component=" , authToken , project );
302335 data = data .getJSONObject ("component" );
303336
304337 } catch (IOException | InterruptedException e ) {
@@ -334,7 +367,7 @@ public static void main(String[] args) throws IOException {
334367
335368 try {
336369
337- data = fetchDataFromURL (apiUrl , "api/security_reports/show?standard=sonarsourceSecurity&project=" , authToken , project );
370+ data = fetchDataFromURL (apiUrl , "/ api/security_reports/show?standard=sonarsourceSecurity&project=" , authToken , project );
338371 dataArray = data .getJSONArray ("categories" );
339372
340373 } catch (IOException | InterruptedException e ) {
@@ -405,7 +438,7 @@ public static void main(String[] args) throws IOException {
405438 while ((pageIndex - 1 ) * 500 < total ) {
406439 data = fetchDataFromURL (
407440 apiUrl ,
408- String .format ("api/hotspots/search?status=TO_REVIEW&ps=500&pageIndex=%d&project=" , pageIndex ),
441+ String .format ("/ api/hotspots/search?status=TO_REVIEW&ps=500&pageIndex=%d&project=" , pageIndex ),
409442 authToken ,
410443 project
411444 );
@@ -485,7 +518,7 @@ public static void main(String[] args) throws IOException {
485518
486519 try {
487520
488- data = fetchDataFromURL (apiUrl , "api/issues/search?types=BUG&facets=severities&componentKeys=" , authToken , project );
521+ data = fetchDataFromURL (apiUrl , "/ api/issues/search?types=BUG&facets=severities&componentKeys=" , authToken , project );
489522
490523 } catch (IOException | InterruptedException e ) {
491524 System .err .println ("Error at doing the HTTP petition: " + e .getMessage ());
@@ -497,7 +530,7 @@ public static void main(String[] args) throws IOException {
497530
498531 try {
499532
500- data = fetchDataFromURL (apiUrl , "api/issues/search?types=VULNERABILITY&facets=severities&componentKeys=" , authToken , project );
533+ data = fetchDataFromURL (apiUrl , "/ api/issues/search?types=VULNERABILITY&facets=severities&componentKeys=" , authToken , project );
501534
502535 } catch (IOException | InterruptedException e ) {
503536 System .err .println ("Error at doing the HTTP petition: " + e .getMessage ());
@@ -509,7 +542,7 @@ public static void main(String[] args) throws IOException {
509542
510543 try {
511544
512- data = fetchDataFromURL (apiUrl , "api/issues/search?types=CODE_SMELL&facets=severities&componentKeys=" , authToken , project );
545+ data = fetchDataFromURL (apiUrl , "/ api/issues/search?types=CODE_SMELL&facets=severities&componentKeys=" , authToken , project );
513546
514547 } catch (IOException | InterruptedException e ) {
515548 System .err .println ("Error at doing the HTTP petition: " + e .getMessage ());
@@ -532,7 +565,7 @@ public static void main(String[] args) throws IOException {
532565 while ((pageIndex - 1 ) * 500 < total ) {
533566 data = fetchDataFromURL (
534567 apiUrl ,
535- String .format ("api/issues/search?issueStatuses=OPEN&ps=500&pageIndex=%d&componentKeys=" , pageIndex ),
568+ String .format ("/ api/issues/search?issueStatuses=OPEN&ps=500&pageIndex=%d&componentKeys=" , pageIndex ),
536569 authToken ,
537570 project
538571 );
0 commit comments