Search in sources :

Example 1 with Docs

use of io.nosqlbench.docapi.Docs in project nosqlbench by nosqlbench.

the class NBWebServer method run.

public void run() {
    // new InetSocketAddress("")
    Server server = new Server(bindPort);
    handlers = new HandlerList();
    if (this.basePaths.size() == 0 && this.servletClasses.size() == 0) {
        logger.warn("No service endpoints or doc paths have been added. Loading dynamically.");
    }
    RewriteHandler rh = new RewriteHandler();
    // rh.addRule(new RedirectRegexRule("/","/docs/"));
    // rh.addRule(new RedirectPatternRule("/","/docs/"));
    handlers.addHandler(rh);
    // Favicon
    for (Path basePath : basePaths) {
        Path icon = basePath.resolve("/favicon.ico");
        if (Files.exists(icon)) {
            FavIconHandler favIconHandler = new FavIconHandler(basePaths.get(0) + "/favicon.ico", false);
            handlers.addHandler(favIconHandler);
            break;
        }
    }
    if (basePaths.size() == 0) {
        Docs docs = new Docs();
        // Load static path contexts which are published within the runtime.
        docs.merge(DocsysPathLoader.loadStaticPaths());
        // default static copy of the docs app
        if (!docs.getPathMap().containsKey("docsys-app")) {
            docs.merge(new DocsysDefaultAppPath().getDocs());
        }
        basePaths.addAll(docs.getPaths());
    }
    for (Path basePath : basePaths) {
        logger.info("Adding path to server: " + basePath.toString());
        ResourceHandler resourceHandler = new ResourceHandler();
        resourceHandler.setDirAllowed(true);
        resourceHandler.setAcceptRanges(true);
        resourceHandler.setWelcomeFiles(new String[] { "index.html" });
        resourceHandler.setRedirectWelcome(false);
        Resource baseResource = new PathResource(basePath);
        if (basePath.toUri().toString().startsWith("jar:")) {
            try {
                baseResource = JarResource.newResource(basePath.toUri());
            } catch (MalformedURLException e) {
                throw new RuntimeException(e);
            }
        }
        resourceHandler.setBaseResource(baseResource);
        resourceHandler.setCacheControl("no-cache");
        handlers.addHandler(resourceHandler);
    }
    // ResourceConfig statusResourceCfg = new ResourceConfig(DocServerStatusEndpoint.class);
    // statusResourceCfg.property("server", this);
    // ServletContainer statusResourceContainer = new ServletContainer(statusResourceCfg);
    // ServletHolder statusResourceServletHolder = new ServletHolder(statusResourceContainer);
    // getContextHandler().addServlet(statusResourceServletHolder, "/_");
    logger.info("adding " + servletClasses.size() + " context handlers...");
    loadDynamicEndpoints();
    ResourceConfig rc = new ResourceConfig();
    rc.addProperties(contextParams);
    rc.property("server", this);
    ServletContainer container = new ServletContainer(rc);
    ServletHolder servlets = new ServletHolder(container);
    String classnames = this.servletClasses.stream().map(Class::getCanonicalName).collect(Collectors.joining(","));
    rc.property(ServerProperties.PROVIDER_CLASSNAMES, classnames);
    // servlets.setInitParameter(ServerProperties.PROVIDER_CLASSNAMES,
    // classnames
    // );
    ServletContextHandler sch = new ServletContextHandler();
    sch.setContextPath("/*");
    sch.addServlet(servlets, "/*");
    FilterHolder filter = new FilterHolder();
    filter.setInitParameter("allowedOrigins", "*");
    filter.setInitParameter("allowedMethods", "POST,GET,OPTIONS,PUT,DELETE,HEAD");
    filter.setInitParameter("allowedHeaders", "X-PINGOTHER, Origin, X-Requested-With, Content-Type, Accept");
    filter.setInitParameter("preflightMaxAge", "1800");
    filter.setInitParameter("allowCredentials", "true");
    CrossOriginFilter corsFilter = new CrossOriginFilter();
    filter.setFilter(corsFilter);
    FilterMapping filterMapping = new FilterMapping();
    filterMapping.setDispatcherTypes(EnumSet.of(DispatcherType.REQUEST));
    filterMapping.setPathSpec("/*");
    filterMapping.setServletName("cross-origin");
    sch.addFilter(filter, "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
    handlers.addHandler(sch);
    // Show contexts
    DefaultHandler defaultHandler = new DefaultHandler();
    defaultHandler.setShowContexts(true);
    defaultHandler.setServeIcon(false);
    handlers.addHandler(defaultHandler);
    // FilterMapping corsMapping = new FilterMapping();
    // corsMapping.set
    // 
    // ServletHandler CorsHandler = new ServletHandler();
    // CorsHandler.setH
    server.setHandler(handlers);
    for (Connector connector : server.getConnectors()) {
        if (connector instanceof AbstractConnector) {
            logger.debug("Setting idle timeout for " + connector + " to 300,000ms");
            ((AbstractConnector) connector).setIdleTimeout(300000);
        }
    }
    try {
        List<Connector> connectors = new ArrayList<>();
        if (bindScheme.equals("http")) {
            ServerConnector sc = new ServerConnector(server);
            sc.setPort(bindPort);
            sc.setHost(bindHost);
            // sc.setDefaultProtocol(bindScheme);
            connectors.add(sc);
        } else if (bindScheme.equals("https")) {
            SslContextFactory.Server server1 = new SslContextFactory.Server();
            ServerConnector sc = new ServerConnector(server, server1);
            sc.setPort(bindPort);
            sc.setHost(bindHost);
            // sc.setDefaultProtocol(bindScheme);
            connectors.add(sc);
        }
        server.setConnectors(connectors.toArray(new Connector[0]));
        server.start();
        System.out.println("Started documentation server at " + bindScheme + "://" + bindHost + ":" + bindPort + "/");
        if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
            System.out.println("Browsing to documentation server at " + bindScheme + "://" + bindHost + ":" + bindPort + "/");
            Desktop.getDesktop().browse(new URI(bindScheme + "://" + bindHost + ":" + bindPort + "/"));
            System.out.println("If the docs app did not open automatically in your browser, open to the the url above.");
        }
        server.join();
    } catch (Exception e) {
        logger.error("error while starting doc server", e);
        e.printStackTrace(System.out);
        System.exit(2);
    }
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) MalformedURLException(java.net.MalformedURLException) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) DocsysDefaultAppPath(io.nosqlbench.docsys.DocsysDefaultAppPath) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) FilterMapping(org.eclipse.jetty.servlet.FilterMapping) URI(java.net.URI) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) RewriteHandler(org.eclipse.jetty.rewrite.handler.RewriteHandler) Path(java.nio.file.Path) DocsysDefaultAppPath(io.nosqlbench.docsys.DocsysDefaultAppPath) PathResource(org.eclipse.jetty.util.resource.PathResource) JarResource(org.eclipse.jetty.util.resource.JarResource) Resource(org.eclipse.jetty.util.resource.Resource) CrossOriginFilter(org.eclipse.jetty.servlets.CrossOriginFilter) FavIconHandler(io.nosqlbench.docsys.handlers.FavIconHandler) MalformedURLException(java.net.MalformedURLException) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) PathResource(org.eclipse.jetty.util.resource.PathResource) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) Docs(io.nosqlbench.docapi.Docs) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 2 with Docs

use of io.nosqlbench.docapi.Docs in project nosqlbench by nosqlbench.

the class BundledDriverAdapterDocs method getDocs.

@Override
public DocsBinder getDocs() {
    Docs docs = new Docs().namespace("adapter-docs");
    SimpleServiceLoader<DriverAdapter> loader = new SimpleServiceLoader<>(DriverAdapter.class, Maturity.Any);
    List<SimpleServiceLoader.Component<? extends DriverAdapter>> namedProviders = loader.getNamedProviders();
    for (SimpleServiceLoader.Component<? extends DriverAdapter> namedProvider : namedProviders) {
        DriverAdapter driverAdapter = namedProvider.provider.get();
        DocsBinder bundledDocs = driverAdapter.getBundledDocs();
        docs.merge(bundledDocs);
    }
    return docs;
}
Also used : SimpleServiceLoader(io.nosqlbench.nb.api.spi.SimpleServiceLoader) Docs(io.nosqlbench.docapi.Docs) DocsBinder(io.nosqlbench.docapi.DocsBinder)

Example 3 with Docs

use of io.nosqlbench.docapi.Docs in project nosqlbench by nosqlbench.

the class DriverAdapter method getBundledDocs.

/**
 * The standard way to provide docs for a driver adapter is to put them in one of two common places:
 * <ul>
 *     <li>&lt;resources&gt;/&lt;adaptername&gt;.md - A single markdown file which is the named top-level
 *     markdown file for this driver adapter.</li>
 *     <li>&lt;resources&gt;/docs/&lt;adaptername&gt;/ - A directory containing any type of file which
 *     is to be included in docs under the adapter name, otherwise known as the {@link Service#selector()}</li>
 * </ul>
 * path &lt;resources&gt;/docs/&lt;adaptername&gt;. Specifically, the file
 *
 * @return A {@link DocsBinder} which describes docs to include for a given adapter.
 */
default DocsBinder getBundledDocs() {
    Docs docs = new Docs().namespace("adapter-" + this.getAdapterName());
    String dev_docspath = "adapter-" + this.getAdapterName() + "/src/main/resources/docs/" + this.getAdapterName();
    String cp_docspath = "docs/" + this.getAdapterName();
    Optional<Content<?>> bundled_docs = NBIO.local().name(dev_docspath, cp_docspath).first();
    bundled_docs.map(Content::asPath).ifPresent(docs::addContentsOf);
    Optional<Content<?>> maindoc = NBIO.local().name("/src/main/resources/" + this.getAdapterName() + ".md", this.getAdapterName() + ".md").first();
    maindoc.map(Content::asPath).ifPresent(docs::addPath);
    return docs.asDocsBinder();
}
Also used : Content(io.nosqlbench.nb.api.content.Content) Docs(io.nosqlbench.docapi.Docs)

Example 4 with Docs

use of io.nosqlbench.docapi.Docs in project nosqlbench by nosqlbench.

the class DocsysMarkdownEndpoint method enable.

private void enable(Set<String> enabled) {
    for (DocsNameSpace nsinfo : docsinfo) {
        // add namespaces which are neither enabled nor disabled to the default group
        if (nsinfo.isEnabledByDefault()) {
            if (disabled != null && disabled.getPathMap().containsKey(nsinfo.getName())) {
                continue;
            }
            enables.add(nsinfo.getName());
        }
    }
    if (enabled.isEmpty()) {
        // Nothing is enabled or enabled by default, so enable everything
        this.enabled = new Docs().merge(docsinfo);
        this.disabled = new Docs().asDocsBinder();
    } else {
        // At least one thing was enabled by default, or previously enabled specifically
        this.disabled = new Docs().merge(docsinfo);
        this.enabled = disabled.remove(enabled);
    }
}
Also used : DocsNameSpace(io.nosqlbench.docapi.DocsNameSpace) Docs(io.nosqlbench.docapi.Docs)

Aggregations

Docs (io.nosqlbench.docapi.Docs)4 DocsBinder (io.nosqlbench.docapi.DocsBinder)1 DocsNameSpace (io.nosqlbench.docapi.DocsNameSpace)1 DocsysDefaultAppPath (io.nosqlbench.docsys.DocsysDefaultAppPath)1 FavIconHandler (io.nosqlbench.docsys.handlers.FavIconHandler)1 Content (io.nosqlbench.nb.api.content.Content)1 SimpleServiceLoader (io.nosqlbench.nb.api.spi.SimpleServiceLoader)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 Path (java.nio.file.Path)1 RewriteHandler (org.eclipse.jetty.rewrite.handler.RewriteHandler)1 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)1 HandlerList (org.eclipse.jetty.server.handler.HandlerList)1 ResourceHandler (org.eclipse.jetty.server.handler.ResourceHandler)1 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)1 FilterMapping (org.eclipse.jetty.servlet.FilterMapping)1 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)1 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)1 CrossOriginFilter (org.eclipse.jetty.servlets.CrossOriginFilter)1 JarResource (org.eclipse.jetty.util.resource.JarResource)1