Fixed FELIX-2207 /License plugin should support Bundle-License manifest header/
https://issues.apache.org/jira/browse/FELIX-2207

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@934400 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/LicenseServlet.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/LicenseServlet.java
index dafb565..b69195e 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/LicenseServlet.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/LicenseServlet.java
@@ -32,6 +32,8 @@
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.commons.io.IOUtils;
+import org.apache.felix.utils.manifest.Clause;
+import org.apache.felix.utils.manifest.Parser;
 import org.apache.felix.webconsole.DefaultVariableResolver;
 import org.apache.felix.webconsole.SimpleWebConsolePlugin;
 import org.apache.felix.webconsole.WebConsoleUtil;
@@ -118,6 +120,7 @@
                 Bundle bundle = bundles[i];
 
                 JSONObject files = findResource(bundle, LICENSE_FILES);
+                addLicensesFromHeader(bundle, files);
                 if (files.length() > 0)
                 { // has resources
                     JSONObject data = new JSONObject();
@@ -141,6 +144,45 @@
         return path.substring( path.lastIndexOf( '/' ) + 1 );
     }
 
+    private static final JSONObject addLicensesFromHeader(Bundle bundle, JSONObject files)
+        throws JSONException
+    {
+        String target = (String) bundle.getHeaders("").get("Bundle-License");
+        if (target != null)
+        {
+            Clause[] licenses = Parser.parseHeader(target);
+            for (int i = 0; licenses != null && i < licenses.length; i++)
+            {
+                final String name = licenses[i].getName();
+                if (!"<<EXTERNAL>>".equals(name))
+                {
+                    final String link = licenses[i].getAttribute("link");
+                    final String path;
+                    final String url;
+                    if (link == null)
+                    {
+                        path = name;
+                        url = getName(name);
+                    }
+                    else
+                    {
+                        path = link;
+                        url = name;
+                    }
+
+                    // skip entry URL is bundle resources, but doesn't exists
+                    if (path.indexOf("://") == -1 && null == bundle.getEntry(path))
+                        continue;
+
+                    JSONObject entry = new JSONObject();
+                    entry.put("path", path);
+                    entry.put("url", url);
+                    files.append("__res__", entry);
+                }
+            }
+        }
+        return files;
+    }
 
     private static final JSONObject findResource( Bundle bundle, String[] patterns ) throws IOException, JSONException
     {
diff --git a/webconsole/src/main/resources/res/ui/license.css b/webconsole/src/main/resources/res/ui/license.css
index 98f7c7d..bf65987 100644
--- a/webconsole/src/main/resources/res/ui/license.css
+++ b/webconsole/src/main/resources/res/ui/license.css
@@ -37,3 +37,8 @@
 	width: 100%;
 	padding: 1em;
 }
+#licenseDetails iframe {
+	width: 100%;
+	height: 35em;
+	border: none;
+}
diff --git a/webconsole/src/main/resources/res/ui/license.js b/webconsole/src/main/resources/res/ui/license.js
index ca3744f..8fc2891 100644
--- a/webconsole/src/main/resources/res/ui/license.js
+++ b/webconsole/src/main/resources/res/ui/license.js
@@ -59,6 +59,7 @@
                     link += descr.jar + "!/"; // inner jar attribute
                 }
                 link += descr.path;
+				if (descr.path.indexOf('http:') == 0 || descr.path.indexOf('ftp:') == 0) link = descr.path;
 
 				buttons += '<a href="' + link + '">' + descr.url + '</a> ';
 
@@ -79,7 +80,7 @@
     }
     
     if (firstPage) {
-        $.get(firstPage, insertLicenseData);
+		openLicenseLink(firstPage);
     } else {
         licenseDetails.html("");
     }
@@ -88,11 +89,19 @@
 	$("#licenseLeft #" +bundleIndex).addClass('ui-state-default ui-corner-all');
 
     $('#licenseButtons a').click(function() {
-        $.get(this.href, insertLicenseData);
-       return false;
+		openLicenseLink(this.href);
+		return false;
     });
 }
 
+function openLicenseLink(uri) {
+	if (uri.indexOf(window.location.href) == 0 || uri.indexOf(pluginRoot) == 0) { // local URI
+        $.get(uri, insertLicenseData);
+	} else {
+		licenseDetails.html( '<iframe frameborder="0" src="' + uri+ '"></iframe>' );
+	}
+}
+
 
 $(document).ready(function() {
 	// init elements cache