Repository: porscheinformatik/sonarqube-licensecheck (java)

Issue:
Add default mappings for Maven licenses and dependencies
Currently there are only two default mappings for Maven dependencies and none for license texts. There should be a sane list of default mappings.

Pull request description:
Added default Maven license mappings, closes #15

Patch:
diff --git a/src/main/java/at/porscheinformatik/sonarqube/licensecheck/license/LicenseSettingsService.java b/src/main/java/at/porscheinformatik/sonarqube/licensecheck/license/LicenseSettingsService.java
index bed5b10..3fdbcd9 100644
--- a/src/main/java/at/porscheinformatik/sonarqube/licensecheck/license/LicenseSettingsService.java
+++ b/src/main/java/at/porscheinformatik/sonarqube/licensecheck/license/LicenseSettingsService.java
@@ -2,9 +2,7 @@ package at.porscheinformatik.sonarqube.licensecheck.license;
 
 import static at.porscheinformatik.sonarqube.licensecheck.LicenseCheckPropertyKeys.LICENSE_KEY;
 
-import java.io.BufferedReader;
 import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -15,6 +13,8 @@ import org.sonar.api.config.Settings;
 import org.sonar.api.server.ServerSide;
 import org.sonar.server.platform.PersistentSettings;
 
+import at.porscheinformatik.sonarqube.licensecheck.utils.IOUtils;
+
 @ServerSide
 public class LicenseSettingsService
 {
@@ -137,29 +137,16 @@ public class LicenseSettingsService
     {
         String licenseJson = settings.getString(LICENSE_KEY);
 
-        if ((licenseJson != null) && !licenseJson.isEmpty())
+        if (licenseJson != null && !licenseJson.isEmpty())
         {
             return;
         }
 
-        try
+        try(InputStream inputStream = LicenseSettingsService.class.getResourceAsStream("spdx_license_list.json");)
         {
-            InputStream inputStream = LicenseService.class.getResourceAsStream("spdx_license_list.json");
-            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
-            StringBuilder out = new StringBuilder();
-            String line;
-
-            while ((line = reader.readLine()) != null)
-            {
-                out.append(line);
-            }
-
-            String newJsonLicense = out.toString();
-            settings.setProperty(LICENSE_KEY, newJsonLicense);
-            persistentSettings.saveProperty(LICENSE_KEY, newJsonLicense);
-
-            reader.close();
-            inputStream.close();
+            String spdxLicenseListJson = IOUtils.readToString(inputStream);
+            settings.setProperty(LICENSE_KEY, spdxLicenseListJson);
+            persistentSettings.saveProperty(LICENSE_KEY, spdxLicenseListJson);
         }
         catch (Exception e)
         {
diff --git a/src/main/java/at/porscheinformatik/sonarqube/licensecheck/mavenlicense/MavenLicenseSettingsService.java b/src/main/java/at/porscheinformatik/sonarqube/licensecheck/mavenlicense/MavenLicenseSettingsService.java
index add1486..0f6b125 100644
--- a/src/main/java/at/porscheinformatik/sonarqube/licensecheck/mavenlicense/MavenLicenseSettingsService.java
+++ b/src/main/java/at/porscheinformatik/sonarqube/licensecheck/mavenlicense/MavenLicenseSettingsService.java
@@ -2,17 +2,24 @@ package at.porscheinformatik.sonarqube.licensecheck.mavenlicense;
 
 import static at.porscheinformatik.sonarqube.licensecheck.LicenseCheckPropertyKeys.LICENSE_REGEX;
 
+import java.io.InputStream;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.sonar.api.config.Settings;
 import org.sonar.api.server.ServerSide;
 import org.sonar.server.platform.PersistentSettings;
 
+import at.porscheinformatik.sonarqube.licensecheck.utils.IOUtils;
+
 @ServerSide
 public class MavenLicenseSettingsService
 {
+    private static final Logger LOGGER = LoggerFactory.getLogger(MavenLicenseSettingsService.class);
+
     /** This is not official API */
     private final PersistentSettings persistentSettings;
     private final Settings settings;
@@ -25,6 +32,28 @@ public class MavenLicenseSettingsService
         this.persistentSettings = persistentSettings;
         this.settings = settings;
         this.mavenLicenseService = mavenLicenseService;
+        initMavenLicenses();
+    }
+
+    private void initMavenLicenses()
+    {
+        String mavenLicenseListString = settings.getString(LICENSE_REGEX);
+
+        if (mavenLicenseListString != null && !mavenLicenseListString.isEmpty())
+        {
+            return;
+        }
+
+        try(InputStream in = MavenLicenseSettingsService.class.getResourceAsStream("default_license_mapping.json"))
+        {
+            mavenLicenseListString = IOUtils.readToString(in);
+            settings.setProperty(LICENSE_REGEX, mavenLicenseListString);
+            persistentSettings.saveProperty(LICENSE_REGEX, mavenLicenseListString);
+        }
+        catch(Exception e)
+        {
+            LOGGER.error("Could not load default_license_mapping.json", e);
+        }
     }
 
     public boolean addMavenLicense(String regex, String key)
diff --git a/src/main/java/at/porscheinformatik/sonarqube/licensecheck/utils/IOUtils.java b/src/main/java/at/porscheinformatik/sonarqube/licensecheck/utils/IOUtils.java
new file mode 100644
index 0000000..467e543
--- /dev/null
+++ b/src/main/java/at/porscheinformatik/sonarqube/licensecheck/utils/IOUtils.java
@@ -0,0 +1,28 @@
+package at.porscheinformatik.sonarqube.licensecheck.utils;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.StringWriter;
+import java.nio.charset.StandardCharsets;
+
+public class IOUtils
+{
+    private IOUtils()
+    {
+    }
+
+    public static String readToString(InputStream input) throws IOException 
+    {
+        Reader in = new InputStreamReader(input, StandardCharsets.UTF_8);
+        StringWriter out = new StringWriter();
+        int n = 0;
+        char[] buffer = new char[4096];
+        while ((n = in.read(buffer)) != -1)
+        {
+            out.write(buffer, 0, n);
+        }
+        return out.toString();
+    }
+}
diff --git a/src/main/resources/at/porscheinformatik/sonarqube/licensecheck/mavenlicense/default_license_mapping.json b/src/main/resources/at/porscheinformatik/sonarqube/licensecheck/mavenlicense/default_license_mapping.json
new file mode 100644
index 0000000..621b5d6
--- /dev/null
+++ b/src/main/resources/at/porscheinformatik/sonarqube/licensecheck/mavenlicense/default_license_mapping.json
@@ -0,0 +1,31 @@
+[
+  { "regex": "Apple License", "license": "AML" },
+  { "regex": ".*Apache.*1\\.1.*", "license": "Apache-1.1" },
+  { "regex": ".*AL.*2.*", "license": "Apache-2.0" },
+  { "regex": ".*ASF.*2.*", "license": "Apache-2.0" },
+  { "regex": ".*ASL.*", "license": "Apache-2.0" },
+  { "regex": ".*Apache.*2.*", "license": "Apache-2.0" },
+  { "regex": ".*BSD.*", "license": "BSD-3-Clause" },
+  { "regex": "^CC0.*$", "license": "CC0-1.0" },
+  { "regex": ".*CDDL.*", "license": "CDDL-1.0" },
+  { "regex": ".*CDDL.*GPL.*", "license": "(CDDL-1.0 OR GPL-2.0)" },
+  { "regex": ".*CDDL.*1.1.*", "license": "CDDL-1.1" },
+  {
+    "regex": ".*Common Development and Distribution License.*",
+    "license": "CDDL-1.1"
+  },
+  { "regex": ".*Common Public License.*", "license": "CPL-1.0" },
+  { "regex": ".*(EPL|Eclipse Public License).*", "license": "EPL-1.0" },
+  { "regex": ".*LGPL.*2\\.1.*", "license": "LGPL-2.1" },
+  { "regex": ".*Lesser.*Public License.*2\\.1.*", "license": "LGPL-2.1" },
+  { "regex": ".*LGPL.*", "license": "LGPL-3.0" },
+  { "regex": ".*Lesser Public License.*", "license": "LGPL-3.0" },
+  { "regex": ".*MIT.*", "license": "MIT" },
+  { "regex": ".*MPL.*1\\.1.*", "license": "MPL-1.1" },
+  { "regex": ".*Mozilla Public License.*", "license": "MPL-1.1" },
+  { "regex": ".*PostgreSQL License.*", "license": "PostgreSQL" },
+  { "regex": ".*Eclipse.Distribution.License.*.1.0.*", "license": "BSD-3-Clause" },
+  { "regex": ".*GPL2.*CPE.*", "license": "GPL-2.0-with-classpath-exception" },
+  { "regex": ".*JSON.*License.*", "license": "JSON" },
+  { "regex": ".*W3C.Software.*License.*", "license": "W3C"}
+]


Test patch:
diff --git a/src/test/java/at/porscheinformatik/sonarqube/licensecheck/utils/IOUtilsTest.java b/src/test/java/at/porscheinformatik/sonarqube/licensecheck/utils/IOUtilsTest.java
new file mode 100644
index 0000000..6b6b3bd
--- /dev/null
+++ b/src/test/java/at/porscheinformatik/sonarqube/licensecheck/utils/IOUtilsTest.java
@@ -0,0 +1,18 @@
+package at.porscheinformatik.sonarqube.licensecheck.utils;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.junit.Assert.assertThat;
+
+import java.io.IOException;
+
+import org.junit.Test;
+
+public class IOUtilsTest
+{
+    @Test
+    public void testLoadFile() throws IOException
+    {
+        String packageJson = IOUtils.readToString(IOUtilsTest.class.getResourceAsStream("/package.json"));
+        assertThat(packageJson, containsString("\"name\": \"test\""));
+    }
+}