This is an automated email from the ASF dual-hosted git repository. cziegeler pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git
The following commit(s) were added to refs/heads/master by this push: new 7852e6a6 Process directory in offline mode 7852e6a6 is described below commit 7852e6a6ecdb9e2df7c869d67395a7c8f88f0ed7 Author: Carsten Ziegeler <cziege...@apache.org> AuthorDate: Thu Dec 7 15:56:10 2023 +0100 Process directory in offline mode --- .../org/apache/sling/offline/impl/Starter.java | 98 +++++++++++++++------- 1 file changed, 70 insertions(+), 28 deletions(-) diff --git a/org.apache.sling.offline/src/main/java/org/apache/sling/offline/impl/Starter.java b/org.apache.sling.offline/src/main/java/org/apache/sling/offline/impl/Starter.java index 07c9544e..3b59781d 100644 --- a/org.apache.sling.offline/src/main/java/org/apache/sling/offline/impl/Starter.java +++ b/org.apache.sling.offline/src/main/java/org/apache/sling/offline/impl/Starter.java @@ -18,8 +18,9 @@ */ package org.apache.sling.offline.impl; -import java.io.FileNotFoundException; +import java.io.File; import java.io.IOException; +import java.nio.file.Files; import javax.servlet.ServletException; @@ -31,17 +32,22 @@ import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.resource.ResourceResolverFactory; import org.apache.sling.engine.SlingRequestProcessor; import org.osgi.framework.BundleContext; -import org.osgi.framework.FrameworkEvent; -import org.osgi.framework.FrameworkListener; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.ConfigurationPolicy; import org.osgi.service.component.annotations.Reference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -@Component(service = {}) +@Component(service = {}, configurationPolicy = ConfigurationPolicy.REQUIRE) public class Starter { + public @interface Config { + String input_path(); + + String output_path(); + } + private final Logger logger = LoggerFactory.getLogger(getClass()); @Reference @@ -50,39 +56,75 @@ public class Starter { @Reference private SlingRequestProcessor processor; + private volatile Config config; + @Activate - public void activate(final BundleContext ctx) { + public void activate(final BundleContext ctx, final Config config) { logger.info("Activating offliner..."); - ctx.addFrameworkListener(new FrameworkListener() { - @Override - public void frameworkEvent(final FrameworkEvent event) { - if (event.getType() == FrameworkEvent.STARTED) { - logger.info("Framework started, starting..."); - new Thread(Starter.this::run).start(); - } - } - }); + this.config = config; + final Thread t = new Thread(Starter.this::run); + t.setDaemon(true); + t.start(); + } + + private boolean ignore(final Resource r) { + return r.getName().startsWith("."); + } + + private boolean isWebPage(final Resource r) { + return r.getName().endsWith(".md"); } - private void process(final ResourceResolver resolver, final String path) throws IOException, ServletException { - logger.info("Getting {}", path); - final Resource resource = resolver.getResource(path); - if ( resource != null ) { - final SlingHttpServletRequest req = Builders.newRequestBuilder(resource) - .withExtension("html") - .build(); - final SlingHttpServletResponseResult resp = Builders.newResponseBuilder().build(); - processor.processRequest(req, resp, resolver); - - logger.info("Result for {} = {}", path, resp.getOutputAsString()); - } else { - throw new FileNotFoundException(path); + private void process(final ResourceResolver resolver, final Resource resource) throws IOException, ServletException { + if ( this.ignore(resource) ) { + return; } + if ( !this.isWebPage(resource) ) { + return; + } + logger.info("Processing {}", resource.getPath()); + final SlingHttpServletRequest req = Builders.newRequestBuilder(resource) + .withExtension("html") + .build(); + final SlingHttpServletResponseResult resp = Builders.newResponseBuilder().build(); + processor.processRequest(req, resp, resolver); + + final File output = new File(config.output_path(), resource.getPath().substring(this.config.input_path().length()).concat(".html")); + logger.info("Writing output to {}", output.getAbsolutePath()); + output.getParentFile().mkdirs(); + Files.writeString(output.toPath(), resp.getOutputAsString()); + } + + private Resource getResource(final ResourceResolver resolver, final String path) { + logger.info("Trying to get resource {}", path); + // 5 seconds to get the resource + final long endAt = System.currentTimeMillis() + 5000; + while ( System.currentTimeMillis() < endAt ) { + final Resource resource = resolver.getResource(path); + if ( resource != null ) { + return resource; + } + try { + Thread.sleep(100); + } catch ( final InterruptedException ie) { + // ignore + } + } + return null; } + public void run() { + new File(this.config.output_path()).mkdirs(); try ( final ResourceResolver resolver = factory.getAdministrativeResourceResolver(null) ) { - process(resolver, "/content/docs/README.md"); + final Resource root = this.getResource(resolver, config.input_path()); + if ( root == null ) { + logger.error("Unable to find root resource at {}", config.input_path()); + } else { + for(final Resource c : root.getChildren()) { + process(resolver, c); + } + } } catch ( final Exception e ) { throw new RuntimeException(e); }