Merge "Add support for stream formatters"
diff --git a/BUCK b/BUCK
index 99a8eca..cccde03 100644
--- a/BUCK
+++ b/BUCK
@@ -2,7 +2,7 @@
 
 MODULE = 'com.googlesource.gerrit.plugins.xdocs.XDocs'
 
-ASCIIDOCTOR = '//lib/asciidoctor:asciidoc_lib' if __standalone_mode__ \
+ASCIIDOCTOR = '//lib/asciidoctor:asciidoc_lib' if STANDALONE_MODE \
   else '//plugins/x-docs/lib/asciidoctor:asciidoc_lib'
 
 gerrit_plugin(
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocServlet.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocServlet.java
index f557bee..e281ef3 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocServlet.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocServlet.java
@@ -130,9 +130,7 @@
       formatter = formatters.getRawFormatter();
     } else {
       formatter = formatters.get(state, key.file);
-      if (formatter == null
-          && !("image".equals(mimeType.getMediaType())
-              && fileTypeRegistry.isSafeInline(mimeType))) {
+      if (formatter == null && !isSafeImage(mimeType)) {
         Resource.NOT_FOUND.send(req, res);
         return;
       }
@@ -193,7 +191,7 @@
         Resource rsc;
         if (formatter != null) {
           rsc = docCache.get(formatter, key.project, key.file, revId);
-        } else if ("image".equals(mimeType.getMediaType())) {
+        } else if (isImage(mimeType)) {
           rsc = getImageResource(repo, revId, key.file);
         } else {
           rsc = Resource.NOT_FOUND;
@@ -236,8 +234,7 @@
         byte[] content = loader.getBytes(Integer.MAX_VALUE);
 
         MimeType mimeType = fileTypeRegistry.getMimeType(file, content);
-        if (!"image".equals(mimeType.getMediaType())
-            || !fileTypeRegistry.isSafeInline(mimeType)) {
+        if (!isSafeImage(mimeType)) {
           return Resource.NOT_FOUND;
         }
         return new SmallResource(content)
@@ -254,6 +251,14 @@
     }
   }
 
+  private boolean isSafeImage(MimeType mimeType) {
+    return isImage(mimeType) && fileTypeRegistry.isSafeInline(mimeType);
+  }
+
+  private static boolean isImage(MimeType mimeType) {
+    return "image".equals(mimeType.getMediaType());
+  }
+
   private static String computeETag(Project.NameKey project, ObjectId revId,
       String file) {
     return Hashing.md5().newHasher()