Search in sources :

Example 1 with Plugin

use of com.google.gerrit.server.plugins.Plugin 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;
    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) {
                log.warn(String.format("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 = Collections.list(scanner.entries()).stream().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 {
                log.warn(String.format("Plugin %s: Multiple 'about' documents found; using %s", pluginName, about.getName().substring(prefix.length())));
            }
        } else {
            docs.add(entry);
        }
    }
    Collections.sort(cmds, PluginEntry.COMPARATOR_BY_NAME);
    Collections.sort(docs, 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) {
        InputStreamReader isr = new InputStreamReader(scanner.getInputStream(about), UTF_8);
        StringBuilder aboutContent = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(isr)) {
            String line;
            while ((line = reader.readLine()) != null) {
                line = line.trim();
                if (line.isEmpty()) {
                    aboutContent.append("\n");
                } else {
                    aboutContent.append(line).append("\n");
                }
            }
        }
        // Only append the About section if there was anything in it
        if (aboutContent.toString().trim().length() > 0) {
            md.append("## About ##\n");
            md.append("\n").append(aboutContent);
        }
    }
    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) ServletException(javax.servlet.ServletException) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) PluginsCollection(com.google.gerrit.server.plugins.PluginsCollection) 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) ServletConfig(javax.servlet.ServletConfig) 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) ServletResponse(javax.servlet.ServletResponse) ReloadPluginListener(com.google.gerrit.server.plugins.ReloadPluginListener) ByteStreams(com.google.common.io.ByteStreams) Optional(java.util.Optional) RegistrationHandle(com.google.gerrit.extensions.registration.RegistrationHandle) Pattern(java.util.regex.Pattern) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Singleton(com.google.inject.Singleton) PluginContentScanner(com.google.gerrit.server.plugins.PluginContentScanner) HashMap(java.util.HashMap) CacheHeaders(com.google.gwtexpui.server.CacheHeaders) 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) Lists(com.google.common.collect.Lists) Charset(java.nio.charset.Charset) Plugin(com.google.gerrit.server.plugins.Plugin) ATTR_CHARACTER_ENCODING(com.google.gerrit.server.plugins.PluginEntry.ATTR_CHARACTER_ENCODING) OutputStream(java.io.OutputStream) RequestUtil(com.google.gerrit.util.http.RequestUtil) ServletRequest(javax.servlet.ServletRequest) IO(org.eclipse.jgit.util.IO) Logger(org.slf4j.Logger) 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) Collections(java.util.Collections) InputStream(java.io.InputStream) Optional(java.util.Optional) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) PluginEntry(com.google.gerrit.server.plugins.PluginEntry)

Example 2 with Plugin

use of com.google.gerrit.server.plugins.Plugin in project gerrit by GerritCodeReview.

the class HttpPluginServlet method init.

@Override
public synchronized void init(ServletConfig config) throws ServletException {
    super.init(config);
    wrapper = new ContextMapper(config.getServletContext().getContextPath());
    for (Plugin plugin : pending) {
        install(plugin);
    }
    pending = null;
}
Also used : Plugin(com.google.gerrit.server.plugins.Plugin)

Example 3 with Plugin

use of com.google.gerrit.server.plugins.Plugin in project gerrit by GerritCodeReview.

the class LfsPluginServlet method init.

@Override
public synchronized void init(ServletConfig config) throws ServletException {
    super.init(config);
    for (Plugin plugin : pending) {
        install(plugin);
    }
    pending = null;
}
Also used : Plugin(com.google.gerrit.server.plugins.Plugin)

Example 4 with Plugin

use of com.google.gerrit.server.plugins.Plugin in project gerrit by GerritCodeReview.

the class AllRequestFilterFilterProxyTest method dynamicUnloading.

@Test
public void dynamicUnloading() throws Exception {
    EasyMockSupport ems = new EasyMockSupport();
    FilterConfig config = ems.createMock(FilterConfig.class);
    HttpServletRequest req1 = new FakeHttpServletRequest();
    HttpServletRequest req2 = new FakeHttpServletRequest();
    HttpServletRequest req3 = new FakeHttpServletRequest();
    HttpServletResponse res1 = new FakeHttpServletResponse();
    HttpServletResponse res2 = new FakeHttpServletResponse();
    HttpServletResponse res3 = new FakeHttpServletResponse();
    Plugin plugin = ems.createMock(Plugin.class);
    IMocksControl mockControl = ems.createStrictControl();
    FilterChain chain = mockControl.createMock("chain", FilterChain.class);
    Capture<FilterChain> capturedChainA1 = new Capture<>();
    Capture<FilterChain> capturedChainB1 = new Capture<>();
    Capture<FilterChain> capturedChainB2 = new Capture<>();
    AllRequestFilter filterA = mockControl.createMock("filterA", AllRequestFilter.class);
    AllRequestFilter filterB = mockControl.createMock("filterB", AllRequestFilter.class);
    filterA.init(config);
    filterB.init(config);
    filterA.doFilter(eq(req1), eq(res1), capture(capturedChainA1));
    filterB.doFilter(eq(req1), eq(res1), capture(capturedChainB1));
    chain.doFilter(req1, res1);
    // Cleaning up of filterA after it got unloaded
    filterA.destroy();
    filterB.doFilter(eq(req2), eq(res2), capture(capturedChainB2));
    chain.doFilter(req2, res2);
    // Cleaning up of filterA after it got unloaded
    filterB.destroy();
    chain.doFilter(req3, res3);
    ems.replayAll();
    AllRequestFilter.FilterProxy filterProxy = getFilterProxy();
    ReloadableRegistrationHandle<AllRequestFilter> handleFilterA = addFilter(filterA);
    ReloadableRegistrationHandle<AllRequestFilter> handleFilterB = addFilter(filterB);
    filterProxy.init(config);
    // Request #1 with filterA and filterB
    filterProxy.doFilter(req1, res1, chain);
    capturedChainA1.getValue().doFilter(req1, res1);
    capturedChainB1.getValue().doFilter(req1, res1);
    // Unloading filterA
    handleFilterA.remove();
    filterProxy.onStopPlugin(plugin);
    // Request #1 only with filterB
    filterProxy.doFilter(req2, res2, chain);
    capturedChainA1.getValue().doFilter(req2, res2);
    // Unloading filterB
    handleFilterB.remove();
    filterProxy.onStopPlugin(plugin);
    // Request #1 with no additional filters
    filterProxy.doFilter(req3, res3, chain);
    filterProxy.destroy();
    ems.verifyAll();
}
Also used : EasyMockSupport(org.easymock.EasyMockSupport) FakeHttpServletRequest(com.google.gerrit.util.http.testutil.FakeHttpServletRequest) FilterChain(javax.servlet.FilterChain) FakeHttpServletResponse(com.google.gerrit.util.http.testutil.FakeHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) Capture(org.easymock.Capture) HttpServletRequest(javax.servlet.http.HttpServletRequest) FakeHttpServletRequest(com.google.gerrit.util.http.testutil.FakeHttpServletRequest) IMocksControl(org.easymock.IMocksControl) FakeHttpServletResponse(com.google.gerrit.util.http.testutil.FakeHttpServletResponse) FilterConfig(javax.servlet.FilterConfig) Plugin(com.google.gerrit.server.plugins.Plugin) Test(org.junit.Test)

Aggregations

Plugin (com.google.gerrit.server.plugins.Plugin)4 CharMatcher (com.google.common.base.CharMatcher)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 ByteStreams (com.google.common.io.ByteStreams)1 HttpHeaders (com.google.common.net.HttpHeaders)1 FileUtil.lastModified (com.google.gerrit.common.FileUtil.lastModified)1 RegistrationHandle (com.google.gerrit.extensions.registration.RegistrationHandle)1 Resource (com.google.gerrit.httpd.resources.Resource)1 ResourceKey (com.google.gerrit.httpd.resources.ResourceKey)1 SmallResource (com.google.gerrit.httpd.resources.SmallResource)1 RestApiServlet (com.google.gerrit.httpd.restapi.RestApiServlet)1 CanonicalWebUrl (com.google.gerrit.server.config.CanonicalWebUrl)1 MarkdownFormatter (com.google.gerrit.server.documentation.MarkdownFormatter)1 MimeUtilFileTypeRegistry (com.google.gerrit.server.mime.MimeUtilFileTypeRegistry)1 ApiType (com.google.gerrit.server.plugins.Plugin.ApiType)1 PluginContentScanner (com.google.gerrit.server.plugins.PluginContentScanner)1