use of com.google.gerrit.server.plugins.PluginContentScanner in project gerrit by GerritCodeReview.
the class HttpPluginServlet method sendAutoIndex.
private void sendAutoIndex(PluginContentScanner scanner, final String prefix, final String pluginName, PluginResourceKey cacheKey, HttpServletResponse res, long lastModifiedTime) throws IOException {
List<PluginEntry> cmds = new ArrayList<>();
List<PluginEntry> servlets = new ArrayList<>();
List<PluginEntry> restApis = new ArrayList<>();
List<PluginEntry> docs = new ArrayList<>();
PluginEntry about = null;
PluginEntry toc = null;
Predicate<PluginEntry> filter = 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) {
logger.atWarning().log("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;
}
return true;
}
return false;
};
List<PluginEntry> entries = scanner.entries().filter(filter).collect(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 {
logger.atWarning().log("Plugin %s: Multiple 'about' documents found; using %s", pluginName, about.getName().substring(prefix.length()));
}
} else if (name.startsWith("toc.")) {
if (toc == null) {
toc = entry;
} else {
logger.atWarning().log("Plugin %s: Multiple 'toc' documents found; using %s", pluginName, toc.getName().substring(prefix.length()));
}
} else {
docs.add(entry);
}
}
cmds.sort(PluginEntry.COMPARATOR_BY_NAME);
docs.sort(PluginEntry.COMPARATOR_BY_NAME);
StringBuilder md = new StringBuilder();
md.append(String.format("# Plugin %s #\n", pluginName));
md.append("\n");
appendPluginInfoTable(md, scanner.getManifest().getMainAttributes());
if (about != null) {
appendPageAsSection(scanner, about, "About", md);
}
if (toc != null) {
appendPageAsSection(scanner, toc, "Documentaion", md);
} else {
appendEntriesSection(scanner, docs, "Documentation", md, prefix, 0);
appendEntriesSection(scanner, servlets, "Servlets", md, prefix, "servlet-".length());
appendEntriesSection(scanner, restApis, "REST APIs", md, prefix, "rest-api-".length());
appendEntriesSection(scanner, cmds, "Commands", md, prefix, "cmd-".length());
}
sendMarkdownAsHtml(md.toString(), pluginName, cacheKey, res, lastModifiedTime);
}
use of com.google.gerrit.server.plugins.PluginContentScanner in project gerrit by GerritCodeReview.
the class HttpPluginServlet method onDefault.
private void onDefault(PluginHolder holder, HttpServletRequest req, HttpServletResponse res) throws IOException {
if (!"GET".equals(req.getMethod()) && !"HEAD".equals(req.getMethod())) {
CacheHeaders.setNotCacheable(res);
res.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
return;
}
String pathInfo = RequestUtil.getEncodedPathInfo(req);
if (pathInfo.length() < 1) {
Resource.NOT_FOUND.send(req, res);
return;
}
checkCors(req, res);
String file = pathInfo.substring(1);
PluginResourceKey key = PluginResourceKey.create(holder.plugin, file);
Resource rsc = resourceCache.getIfPresent(key);
if (rsc != null && req.getHeader(HttpHeaders.IF_MODIFIED_SINCE) == null) {
rsc.send(req, res);
return;
}
String uri = req.getRequestURI();
if ("".equals(file)) {
res.sendRedirect(uri + holder.docPrefix + "index.html");
return;
}
if (file.startsWith(holder.staticPrefix)) {
if (holder.plugin.getApiType() == ApiType.JS) {
sendJsPlugin(holder.plugin, key, req, res);
} else {
PluginContentScanner scanner = holder.plugin.getContentScanner();
Optional<PluginEntry> entry = scanner.getEntry(file);
if (entry.isPresent()) {
if (hasUpToDateCachedResource(rsc, entry.get().getTime())) {
rsc.send(req, res);
} else {
sendResource(scanner, entry.get(), key, res);
}
} else {
resourceCache.put(key, Resource.NOT_FOUND);
Resource.NOT_FOUND.send(req, res);
}
}
} else if (file.equals(holder.docPrefix.substring(0, holder.docPrefix.length() - 1))) {
res.sendRedirect(uri + "/index.html");
} else if (file.startsWith(holder.docPrefix) && file.endsWith("/")) {
res.sendRedirect(uri + "index.html");
} else if (file.startsWith(holder.docPrefix)) {
PluginContentScanner scanner = holder.plugin.getContentScanner();
Optional<PluginEntry> entry = scanner.getEntry(file);
if (!entry.isPresent()) {
entry = findSource(scanner, file);
}
if (!entry.isPresent() && file.endsWith("/index.html")) {
String pfx = file.substring(0, file.length() - "index.html".length());
long pluginLastModified = lastModified(holder.plugin.getSrcFile());
if (hasUpToDateCachedResource(rsc, pluginLastModified)) {
rsc.send(req, res);
} else {
sendAutoIndex(scanner, pfx, holder.plugin.getName(), key, res, pluginLastModified);
}
} else if (entry.isPresent() && entry.get().getName().endsWith(".md")) {
if (hasUpToDateCachedResource(rsc, entry.get().getTime())) {
rsc.send(req, res);
} else {
sendMarkdownAsHtml(scanner, entry.get(), holder.plugin.getName(), key, res);
}
} else if (entry.isPresent()) {
if (hasUpToDateCachedResource(rsc, entry.get().getTime())) {
rsc.send(req, res);
} else {
sendResource(scanner, entry.get(), key, res);
}
} else {
resourceCache.put(key, Resource.NOT_FOUND);
Resource.NOT_FOUND.send(req, res);
}
} else {
resourceCache.put(key, Resource.NOT_FOUND);
Resource.NOT_FOUND.send(req, res);
}
}
Aggregations