Refactor XDocServlet: Move formatter retrieval into helper method

Change-Id: Ie6683f32d475a746fa670f5d1b791501f427cf82
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
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 0bf2747..fcdc08f 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocServlet.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocServlet.java
@@ -120,15 +120,9 @@
       }
 
       MimeType mimeType = fileTypeRegistry.getMimeType(key.file, null);
-      FormatterProvider formatter;
-      if (req.getParameter("raw") != null) {
-        formatter = formatters.getRawFormatter();
-      } else {
-        formatter = formatters.get(state, key.file);
-        if (formatter == null && !isSafeImage(mimeType)) {
-          Resource.NOT_FOUND.send(req, res);
-          return;
-        }
+      FormatterProvider formatter = getFormatter(req, key);
+      if (formatter == null && !isSafeImage(mimeType)) {
+        throw new ResourceNotFoundException();
       }
 
       ProjectControl projectControl = projectControlFactory.validateFor(key.project);
@@ -263,6 +257,15 @@
     return state;
   }
 
+  private FormatterProvider getFormatter(HttpServletRequest req, ResourceKey key)
+      throws ResourceNotFoundException {
+    if (req.getParameter("raw") != null) {
+      return formatters.getRawFormatter();
+    } else {
+      return formatters.get(getProject(key), key.file);
+    }
+  }
+
   private boolean isSafeImage(MimeType mimeType) {
     return isImage(mimeType) && fileTypeRegistry.isSafeInline(mimeType);
   }