Allow to retrieve documents as raw unformatted text By setting the URL parameter 'raw' a document for which a formatter is registered can be retrieved as raw unformatted text, e.g.: /xdocs/project/external%2Fopenssl/rev/stable-1.3/docs%2Ffaq.md?raw The 'raw' parameter cannot be used for binary files. Change-Id: I9579b4302b1fd5a8efdb1661bd03c206d01f96f3 Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/Resources.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/Resources.java new file mode 100644 index 0000000..02e547b --- /dev/null +++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/Resources.java
@@ -0,0 +1,50 @@ +// Copyright (C) 2014 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.googlesource.gerrit.plugins.xdocs; + +import com.google.gerrit.httpd.resources.Resource; +import com.google.gwtexpui.server.CacheHeaders; + +import java.io.IOException; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class Resources { + public static final Resource METHOD_NOT_ALLOWED = new Resource() { + private static final long serialVersionUID = 1L; + + @Override + public int weigh() { + return 0; + } + + @Override + public void send(HttpServletRequest req, HttpServletResponse res) + throws IOException { + CacheHeaders.setNotCacheable(res); + res.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED); + } + + @Override + public boolean isUnchanged(long latestModifiedDate) { + return false; + } + + protected Object readResolve() { + return METHOD_NOT_ALLOWED; + } + }; +}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocLoader.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocLoader.java index 9fd8ac1..3705f33 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocLoader.java +++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocLoader.java
@@ -35,6 +35,7 @@ import com.googlesource.gerrit.plugins.xdocs.formatter.Formatters; import com.googlesource.gerrit.plugins.xdocs.formatter.Formatters.FormatterProvider; +import org.eclipse.jgit.diff.RawText; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectLoader; import org.eclipse.jgit.lib.Repository; @@ -97,10 +98,14 @@ } ObjectId objectId = tw.getObjectId(0); ObjectLoader loader = repo.open(objectId); - byte[] raw = loader.getBytes(Integer.MAX_VALUE); + byte[] bytes = loader.getBytes(Integer.MAX_VALUE); + if (formatter.getName().equals(Formatters.RAW_FORMATTER) + && RawText.isBinary(bytes)) { + return Resources.METHOD_NOT_ALLOWED; + } String html = formatter.get().format(formatterCfg, - replaceMacros(key.getProject(), raw)); + replaceMacros(key.getProject(), bytes)); return getAsHtmlResource(html, commit.getCommitTime()); } finally { tw.release();
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 70eb763..0c61366 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocServlet.java +++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocServlet.java
@@ -125,13 +125,19 @@ res.sendRedirect(getRedirectUrl(req, key, cfg)); return; } + MimeType mimeType = fileTypeRegistry.getMimeType(key.file, null); - FormatterProvider formatter = formatters.get(state, key.file); - if (formatter == null - && !("image".equals(mimeType.getMediaType()) - && fileTypeRegistry.isSafeInline(mimeType))) { - Resource.NOT_FOUND.send(req, res); - return; + FormatterProvider formatter; + if (req.getParameter("raw") != null) { + formatter = formatters.getRawFormatter(); + } else { + formatter = formatters.get(state, key.file); + if (formatter == null + && !("image".equals(mimeType.getMediaType()) + && fileTypeRegistry.isSafeInline(mimeType))) { + Resource.NOT_FOUND.send(req, res); + return; + } } try {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/Formatters.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/Formatters.java index efbde10..eb5f447 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/Formatters.java +++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/Formatters.java
@@ -43,6 +43,8 @@ @Singleton public class Formatters { + public static final String RAW_FORMATTER = "RAW"; + private final String pluginName; private final PluginConfigFactory pluginCfgFactory; private final FileTypeRegistry fileTypeRegistry; @@ -129,6 +131,11 @@ } public FormatterProvider getByName(String formatterName) { + if (formatterName.equals(RAW_FORMATTER)) { + return new FormatterProvider(RAW_FORMATTER, + getByName(PlainTextFormatter.NAME).formatter); + } + for (String pluginName : formatters.plugins()) { for (Entry<String, Provider<Formatter>> e : formatters.byPlugin(pluginName).entrySet()) { @@ -140,6 +147,10 @@ return null; } + public FormatterProvider getRawFormatter() { + return getByName(RAW_FORMATTER); + } + public static class FormatterProvider { private final String name; private final Provider<Formatter> formatter;
diff --git a/src/main/resources/Documentation/about.md b/src/main/resources/Documentation/about.md index 80195fb..531c7cc 100644 --- a/src/main/resources/Documentation/about.md +++ b/src/main/resources/Documentation/about.md
@@ -18,6 +18,15 @@ If the file name is omitted the plugin serves the `README.md` from the project if available. +By setting the URL parameter `raw` the document will be returned as raw +unformatted text. + +``` + /@PLUGIN@/project/external%2Fopenssl/rev/stable-1.3/docs%2Ffaq.md?raw +``` + +The `raw` parameter cannot be used for binary files. + *WARNING:* All HTML blocks as well as inline HTML tags are suppressed. Both will be accepted in the input but not be contained in the output.