ExportFromFilesystem.java

  1. package org.jbehave.core.io.rest.filesystem;

  2. import static org.jbehave.core.io.rest.filesystem.FilesystemUtils.asFile;

  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.nio.charset.StandardCharsets;
  6. import java.util.Map;

  7. import org.apache.commons.io.FileUtils;
  8. import org.jbehave.core.io.rest.Resource;
  9. import org.jbehave.core.io.rest.ResourceExporter;
  10. import org.jbehave.core.io.rest.ResourceIndexer;
  11. import org.jbehave.core.io.rest.ResourceUploader;

  12. /**
  13.  * <p>Implementation that reads from filesystem the resources and uploads them.</p>
  14.  *
  15.  * <p>An include pattern of the resources may be provided.</p>
  16.  *
  17.  * <p>The exporter requires an instance of a {@link ResourceIndexer} and of a {@link ResourceUploader}.</p>
  18.  */
  19. public class ExportFromFilesystem implements ResourceExporter {

  20.     private final ResourceIndexer indexer;
  21.     private final ResourceUploader uploader;
  22.     private final String sourcePath;
  23.     private final String sourceExt;
  24.     private String syntax;
  25.     private String includes;

  26.     public ExportFromFilesystem(ResourceIndexer indexer,
  27.             ResourceUploader uploader, String sourcePath, String sourceExt,
  28.             String syntax, String includes) {
  29.         this.indexer = indexer;
  30.         this.uploader = uploader;
  31.         this.sourcePath = sourcePath;
  32.         this.sourceExt = sourceExt;
  33.         this.syntax = syntax;
  34.         this.includes = includes;
  35.     }

  36.     @Override
  37.     public void exportResources(String rootURI) {
  38.         Map<String, Resource> index = indexer.indexResources(rootURI,
  39.                 sourcePath, syntax, includes);
  40.         readResources(index, sourcePath, sourceExt);
  41.         uploadResources(index);
  42.     }

  43.     private void uploadResources(Map<String, Resource> index) {
  44.         for (String name : index.keySet()) {
  45.             Resource resource = index.get(name);
  46.             uploader.uploadResource(resource);
  47.         }
  48.     }

  49.     private void readResources(Map<String, Resource> index, String sourcePath,
  50.             String sourceExt) {
  51.         for (String name : index.keySet()) {
  52.             Resource resource = index.get(name);
  53.             readResource(resource, asFile(resource, sourcePath, sourceExt));
  54.         }
  55.     }

  56.     private void readResource(Resource resource, File file) {
  57.         if (file.isDirectory() || !file.exists()) {
  58.             return;
  59.         }
  60.         try {
  61.             String text = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
  62.             resource.setContent(text);
  63.         } catch (IOException e) {
  64.             throw new RuntimeException("Failed to read resource " + resource
  65.                     + " from file " + file, e);
  66.         }
  67.     }

  68. }