Search in sources :

Example 1 with PluginContentScanner

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);
}
Also used : RestApiServlet(com.google.gerrit.httpd.restapi.RestApiServlet) FileUtil.lastModified(com.google.gerrit.common.FileUtil.lastModified) FilterChain(javax.servlet.FilterChain) ResourceKey(com.google.gerrit.httpd.resources.ResourceKey) ACCESS_CONTROL_ALLOW_METHODS(com.google.common.net.HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS) ServletException(javax.servlet.ServletException) Inject(com.google.inject.Inject) StringUtils(org.apache.commons.lang3.StringUtils) PluginsCollection(com.google.gerrit.server.plugins.PluginsCollection) Config(org.eclipse.jgit.lib.Config) ACCESS_CONTROL_ALLOW_ORIGIN(com.google.common.net.HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN) SshInfo(com.google.gerrit.server.ssh.SshInfo) Matcher(java.util.regex.Matcher) HttpHeaders(com.google.common.net.HttpHeaders) Locale(java.util.Locale) Map(java.util.Map) Splitter(com.google.common.base.Splitter) StartPluginListener(com.google.gerrit.server.plugins.StartPluginListener) Path(java.nio.file.Path) ORIGIN(com.google.common.net.HttpHeaders.ORIGIN) ServletConfig(javax.servlet.ServletConfig) GerritServerConfig(com.google.gerrit.server.config.GerritServerConfig) HttpServlet(javax.servlet.http.HttpServlet) Predicate(java.util.function.Predicate) ApiType(com.google.gerrit.server.plugins.Plugin.ApiType) RawParseUtils(org.eclipse.jgit.util.RawParseUtils) MarkdownFormatter(com.google.gerrit.server.documentation.MarkdownFormatter) GuiceFilter(com.google.inject.servlet.GuiceFilter) Attributes(java.util.jar.Attributes) Resource(com.google.gerrit.httpd.resources.Resource) List(java.util.List) ReloadPluginListener(com.google.gerrit.server.plugins.ReloadPluginListener) ByteStreams(com.google.common.io.ByteStreams) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) UnsupportedEncodingException(java.io.UnsupportedEncodingException) FluentLogger(com.google.common.flogger.FluentLogger) ACCESS_CONTROL_ALLOW_CREDENTIALS(com.google.common.net.HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS) Joiner(com.google.common.base.Joiner) Singleton(com.google.inject.Singleton) PluginContentScanner(com.google.gerrit.server.plugins.PluginContentScanner) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) Strings(com.google.common.base.Strings) CanonicalWebUrl(com.google.gerrit.server.config.CanonicalWebUrl) HttpServletRequest(javax.servlet.http.HttpServletRequest) VARY(com.google.common.net.HttpHeaders.VARY) Lists(com.google.common.collect.Lists) Charset(java.nio.charset.Charset) Plugin(com.google.gerrit.server.plugins.Plugin) CacheHeaders(com.google.gerrit.util.http.CacheHeaders) ATTR_CHARACTER_ENCODING(com.google.gerrit.server.plugins.PluginEntry.ATTR_CHARACTER_ENCODING) OutputStream(java.io.OutputStream) RequestUtil(com.google.gerrit.util.http.RequestUtil) IO(org.eclipse.jgit.util.IO) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) CharMatcher(com.google.common.base.CharMatcher) SmallResource(com.google.gerrit.httpd.resources.SmallResource) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ATTR_CONTENT_TYPE(com.google.gerrit.server.plugins.PluginEntry.ATTR_CONTENT_TYPE) Maps(com.google.common.collect.Maps) InputStreamReader(java.io.InputStreamReader) PluginEntry(com.google.gerrit.server.plugins.PluginEntry) Collectors.toList(java.util.stream.Collectors.toList) Provider(com.google.inject.Provider) Named(com.google.inject.name.Named) ServletContext(javax.servlet.ServletContext) BufferedReader(java.io.BufferedReader) Cache(com.google.common.cache.Cache) MimeUtilFileTypeRegistry(com.google.gerrit.server.mime.MimeUtilFileTypeRegistry) InputStream(java.io.InputStream) Optional(java.util.Optional) ArrayList(java.util.ArrayList) PluginEntry(com.google.gerrit.server.plugins.PluginEntry)

Example 2 with PluginContentScanner

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);
    }
}
Also used : Optional(java.util.Optional) PluginContentScanner(com.google.gerrit.server.plugins.PluginContentScanner) Resource(com.google.gerrit.httpd.resources.Resource) SmallResource(com.google.gerrit.httpd.resources.SmallResource) PluginEntry(com.google.gerrit.server.plugins.PluginEntry)

Aggregations

Resource (com.google.gerrit.httpd.resources.Resource)2 SmallResource (com.google.gerrit.httpd.resources.SmallResource)2 PluginContentScanner (com.google.gerrit.server.plugins.PluginContentScanner)2 PluginEntry (com.google.gerrit.server.plugins.PluginEntry)2 Optional (java.util.Optional)2 CharMatcher (com.google.common.base.CharMatcher)1 Joiner (com.google.common.base.Joiner)1 Splitter (com.google.common.base.Splitter)1 Strings (com.google.common.base.Strings)1 Cache (com.google.common.cache.Cache)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 FluentLogger (com.google.common.flogger.FluentLogger)1 ByteStreams (com.google.common.io.ByteStreams)1 HttpHeaders (com.google.common.net.HttpHeaders)1 ACCESS_CONTROL_ALLOW_CREDENTIALS (com.google.common.net.HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS)1 ACCESS_CONTROL_ALLOW_METHODS (com.google.common.net.HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS)1 ACCESS_CONTROL_ALLOW_ORIGIN (com.google.common.net.HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)1 ORIGIN (com.google.common.net.HttpHeaders.ORIGIN)1 VARY (com.google.common.net.HttpHeaders.VARY)1