Browse Source

license viewer refactoring

Jason Rivard 7 years ago
parent
commit
fcaddb4c46

+ 56 - 0
server/src/main/java/password/pwm/util/java/XmlUtil.java

@@ -22,7 +22,9 @@
 
 package password.pwm.util.java;
 
+import lombok.Value;
 import org.jdom2.Document;
+import org.jdom2.Element;
 import org.jdom2.input.SAXBuilder;
 import org.jdom2.output.Format;
 import org.jdom2.output.XMLOutputter;
@@ -38,6 +40,9 @@ import java.io.OutputStreamWriter;
 import java.io.Reader;
 import java.io.Writer;
 import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
 
 public class XmlUtil {
 
@@ -88,5 +93,56 @@ public class XmlUtil {
         return builder;
     }
 
+    @Value
+    public static class DependencyInfo {
+        private String projectUrl;
+        private String name;
+        private String artifactId;
+        private String version;
+        private String type;
+        private List<LicenseInfo> licenses;
+    }
+
+    @Value
+    public static class LicenseInfo {
+        private String licenseUrl;
+        private String licenseName;
+    }
+
+    public static List<DependencyInfo> getLicenseInfos() throws PwmUnrecoverableException {
+        final List<DependencyInfo> returnList = new ArrayList<>();
+
+        final InputStream attributionInputStream = XmlUtil.class.getResourceAsStream("/attribution.xml");
+
+        if (attributionInputStream != null) {
+            final Document document = XmlUtil.parseXml(attributionInputStream);
+            final Element dependencies = document.getRootElement().getChild("dependencies");
 
+            for (final Element dependency : dependencies.getChildren("dependency")) {
+                final String projectUrl = dependency.getChildText("projectUrl");
+                final String name = dependency.getChildText("name");
+                final String artifactId = dependency.getChildText("artifactId");
+                final String version = dependency.getChildText("version");
+                final String type = dependency.getChildText("type");
+
+                final List<LicenseInfo> licenseInfos = new ArrayList<>();
+                {
+                    final Element licenses = dependency.getChild("licenses");
+                    final List<Element> licenseList = licenses.getChildren("license");
+                    for (final Element license : licenseList) {
+                        final String licenseUrl = license.getChildText("url");
+                        final String licenseName = license.getChildText("name");
+                        final LicenseInfo licenseInfo = new LicenseInfo(licenseUrl, licenseName);
+                        licenseInfos.add(licenseInfo);
+                    }
+                }
+
+                final DependencyInfo dependencyInfo = new DependencyInfo(projectUrl, name, artifactId, version, type,
+                        Collections.unmodifiableList(licenseInfos));
+
+                returnList.add(dependencyInfo);
+            }
+        }
+        return Collections.unmodifiableList(returnList);
+    }
 }

+ 45 - 41
server/src/main/webapp/public/reference/license.jsp

@@ -5,6 +5,8 @@
 <%@ page import="java.util.List" %>
 <%@ page import="java.io.InputStream" %>
 <%@ page import="org.apache.commons.lang3.StringUtils" %>
+<%@ page import="password.pwm.util.java.StringUtil" %>
+<%@ page import="password.pwm.Permission" %>
 <%--
   ~ Password Management Servlets (PWM)
   ~ http://www.pwm-project.org
@@ -40,6 +42,7 @@
 <%@ include file="/WEB-INF/jsp/fragment/header.jsp" %>
 <body class="nihilo">
 <link href="<pwm:url url='/public/resources/referenceStyle.css' addContext="true"/>" rel="stylesheet" type="text/css"/>
+<% List<XmlUtil.DependencyInfo> dependencyInfos = XmlUtil.getLicenseInfos(); %>
 <div id="wrapper">
     <jsp:include page="../../WEB-INF/jsp/fragment/header-body.jsp">
         <jsp:param name="pwm.PageName" value="Software License Reference"/>
@@ -50,57 +53,58 @@
 
         <p class="page-title">Software License Reference</p>
 
-        <%
-        final InputStream attributionInputStream = getClass().getResourceAsStream("/attribution.xml");
+        <pwm:if test="<%=PwmIfTest.permission%>" permission="<%=Permission.PWMADMIN%>" negate="true">
+            <p>
+                Additional data is available for authenticated administrators.
+            </p>
+        </pwm:if>
 
-        if (attributionInputStream != null) {
-            final Document document = XmlUtil.parseXml(attributionInputStream);
-            final Element dependencies = document.getRootElement().getChild("dependencies");
+        <% if (dependencyInfos != null) { %>
+        <% for (final XmlUtil.DependencyInfo dependencyInfo : dependencyInfos) { %>
+        <div class="licenseBlock">
+            <div class="dependency-name"><%=StringUtil.escapeHtml(dependencyInfo.getName())%></div>
 
-            for (final Element dependency : dependencies.getChildren("dependency")) {
-                final String projectUrl = dependency.getChildText("projectUrl");
-            %>
-                <div class="licenseBlock">
-                    <div class="dependency-name"><%=dependency.getChildText("name") %></div>
-
-                    <% if (projectUrl != null) { %>
-                        <div class="dependency-url"><a href="<%=projectUrl%>" target="_blank"><%=projectUrl%></a></div>
-                    <% } else { %>
-                        <div class="dependency-url-not-provided">URL not provided</div>
-                    <% } %>
-
-                    <div class="dependency-file"><%=dependency.getChildText("artifactId") %>-<%=dependency.getChildText("version") %>.<%=dependency.getChildText("type") %></div>
-                    <%
-                    final Element licenses = dependency.getChild("licenses");
-                    final List<Element> licenseList = licenses.getChildren("license");
-                    for (final Element license : licenseList) { %>
-                    <div class="dependency-license"><a href="<%=license.getChildText("url")%>" target="_blank" class="license-link"><%=license.getChildText("name")%></a></div>
-                    <% } %>
+            <% if (dependencyInfo.getProjectUrl() != null) { %>
+            <div class="dependency-url">
+                <a href="<%=dependencyInfo.getProjectUrl()%>" target="_blank">
+                    <%=dependencyInfo.getProjectUrl()%>
+                </a>
+            </div>
+            <% } else { %>
+            <div class="dependency-url-not-provided">URL not provided</div>
+            <% } %>
+            <pwm:if test="<%=PwmIfTest.permission%>" permission="<%=Permission.PWMADMIN%>">
+                <div class="dependency-file">
+                    <%=dependencyInfo.getArtifactId() %>-<%=dependencyInfo.getVersion()%>.<%=dependencyInfo.getType() %>
                 </div>
+            </pwm:if>
+            <%
+                for (final XmlUtil.LicenseInfo licenseInfo : dependencyInfo.getLicenses()) { %>
+            <div class="dependency-license">
+                License:
+                <a href="<%=licenseInfo.getLicenseUrl()%>" target="_blank" class="license-link">
+                    <%=licenseInfo.getLicenseName()%>
+                </a>
+            </div>
             <% } %>
+        </div>
+        <% } %>
         <% } else { %>
-            <div class="licenseBlock">
-                Error: attribution file not found: attribution.xml
-            </div>
+        <div class="licenseBlock">
+            Error: attribution file not found: attribution.xml
+        </div>
         <% } %>
-        <p>
-        <form action="source.zip" method="get">
-            <button class="btn" type="submit">Download Source</button>
-        </form>
-        </p>
+        <pwm:if test="<%=PwmIfTest.permission%>" permission="<%=Permission.PWMADMIN%>">
+            <p>
+            <form action="source.zip" method="get">
+                <button class="btn" type="submit">Download Source</button>
+            </form>
+            </p>
+        </pwm:if>
         <span class="footnote">nanos gigantum humeris insidentes</span>
     </div>
     <div class="push"></div>
 </div>
-<pwm:script>
-<script type="text/javascript">
-    PWM_GLOBAL['startupFunctions'].push(function(){
-        require(["dojo/parser","dijit/TitlePane"],function(dojoParser){
-            dojoParser.parse();
-        });
-    });
-</script>
-</pwm:script>
 <%@ include file="/WEB-INF/jsp/fragment/footer.jsp" %>
 </body>
 </html>