Merge "HttpPluginServlet: Refactor generation of documentation index entries"
diff --git a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/plugins/HttpPluginServlet.java b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/plugins/HttpPluginServlet.java
index 2190fe0..4059ac1 100644
--- a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/plugins/HttpPluginServlet.java
+++ b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/plugins/HttpPluginServlet.java
@@ -21,9 +21,11 @@
 
 import com.google.common.base.CharMatcher;
 import com.google.common.base.Optional;
+import com.google.common.base.Predicate;
 import com.google.common.base.Splitter;
 import com.google.common.base.Strings;
 import com.google.common.cache.Cache;
+import com.google.common.collect.FluentIterable;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import com.google.common.io.ByteStreams;
@@ -68,7 +70,6 @@
 import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -369,7 +370,7 @@
   }
 
   private void sendAutoIndex(PluginContentScanner scanner,
-      String prefix, String pluginName,
+      final String prefix, final String pluginName,
       PluginResourceKey cacheKey, HttpServletResponse res,long lastModifiedTime)
       throws IOException {
     List<PluginEntry> cmds = new ArrayList<>();
@@ -377,30 +378,54 @@
     List<PluginEntry> restApis = new ArrayList<>();
     List<PluginEntry> docs = new ArrayList<>();
     PluginEntry about = null;
-    Enumeration<PluginEntry> entries = scanner.entries();
-    while (entries.hasMoreElements()) {
-      PluginEntry entry = entries.nextElement();
-      String name = entry.getName();
-      Optional<Long> size = entry.getSize();
-      if (name.startsWith(prefix)
-          && (name.endsWith(".md")
-              || name.endsWith(".html"))
-              && size.isPresent()
-          && 0 < size.get() && size.get() <= SMALL_RESOURCE) {
-        name = name.substring(prefix.length());
-        if (name.startsWith("cmd-")) {
-          cmds.add(entry);
-        } else if (name.startsWith("servlet-")) {
-          servlets.add(entry);
-        } else if (name.startsWith("rest-api-")) {
-          restApis.add(entry);
-        } else if (name.startsWith("about.")) {
-          if (about == null) {
-            about = entry;
+
+    Predicate<PluginEntry> filter = new Predicate<PluginEntry>() {
+      @Override
+      public boolean apply(PluginEntry entry) {
+        String name = entry.getName();
+        Optional<Long> size = entry.getSize();
+        if (name.startsWith(prefix)
+            && (name.endsWith(".md") || name.endsWith(".html"))
+            && size.isPresent()) {
+          if (size.get() <= 0 || size.get() > SMALL_RESOURCE) {
+            log.warn(String.format(
+                "Plugin %s: %s omitted from document index. "
+                  + "Size %d out of range (0,%d).",
+                pluginName,
+                name.substring(prefix.length()),
+                size.get(),
+                SMALL_RESOURCE));
+            return false;
           }
-        } else {
-          docs.add(entry);
+          return true;
         }
+        return false;
+      }
+    };
+
+    List<PluginEntry> entries = FluentIterable
+        .from(Collections.list(scanner.entries()))
+        .filter(filter)
+        .toList();
+    for (PluginEntry entry: entries) {
+      String name = entry.getName().substring(prefix.length());
+      if (name.startsWith("cmd-")) {
+        cmds.add(entry);
+      } else if (name.startsWith("servlet-")) {
+        servlets.add(entry);
+      } else if (name.startsWith("rest-api-")) {
+        restApis.add(entry);
+      } else if (name.startsWith("about.")) {
+        if (about == null) {
+          about = entry;
+        } else {
+          log.warn(String.format(
+              "Plugin %s: Multiple 'about' documents found; using %s",
+              pluginName,
+              about.getName().substring(prefix.length())));
+        }
+      } else {
+        docs.add(entry);
       }
     }