IndexFromConfluence.java

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

  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.regex.Pattern;

  5. import org.jbehave.core.io.rest.RESTClient;
  6. import org.jbehave.core.io.rest.RESTClient.Type;
  7. import org.jbehave.core.io.rest.Resource;
  8. import org.jbehave.core.io.rest.ResourceIndexer;
  9. import org.jbehave.core.io.rest.confluence.Confluence.Page;

  10. public class IndexFromConfluence implements ResourceIndexer {

  11.     private static final String DISPLAY = "/display/";

  12.     private final Confluence confluence;

  13.     public IndexFromConfluence() {
  14.         this(null, null);
  15.     }

  16.     public IndexFromConfluence(String username, String password) {
  17.         this(new RESTClient(Type.XML, username, password));
  18.     }

  19.     public IndexFromConfluence(RESTClient client) {
  20.         this.confluence = new Confluence(client);
  21.     }

  22.     @Override
  23.     public Map<String, Resource> indexResources(String rootURI) {
  24.         return indexResources(rootURI, null);
  25.     }

  26.     @Override
  27.     public Map<String, Resource> indexResources(String rootURI, String rootPath, String syntax, String includes) {
  28.         return indexResources(rootURI, includes);
  29.     }

  30.     protected Map<String, Resource> indexResources(String rootURI, String includePattern) {
  31.         if (rootURI == null || !rootURI.contains(DISPLAY)) {
  32.             throw new RuntimeException("Root URI is not in correct format: " + rootURI);
  33.         }
  34.         String[] split = rootURI.split(DISPLAY);
  35.         String baseUrl = split[0];
  36.         if (split.length == 1) {
  37.             throw new RuntimeException("URI does not contain space and page: " + rootURI);
  38.         }
  39.         String[] searchTerms = split[1].split("/");
  40.         if (split.length != 2) {
  41.             throw new RuntimeException("URI does not contain space and page: " + rootURI);
  42.         }
  43.         return createResourceMap(baseUrl, searchTerms[0], searchTerms[1], includePattern);
  44.     }

  45.     private Map<String, Resource> createResourceMap(String baseUrl, String spaceKey, String pageName, String pattern) {
  46.         Map<String, Resource> result = new HashMap<>();
  47.         Page rootPage = confluence.loadRootPage(baseUrl, spaceKey, pageName);
  48.         addPage(result, rootPage.getSelfReference(), pattern);
  49.         return result;
  50.     }

  51.     private void addPage(Map<String, Resource> result, String href, String pattern) {
  52.         Page page = confluence.loadPage(href, true);
  53.         Resource resource = new Resource(page.getSelfReference(), page.getTitle());
  54.         resource.setContent(page.getBody());
  55.         if (pattern == null || (pattern != null && Pattern.matches(pattern, page.getTitle()))) {
  56.             result.put(page.getTitle(), resource);
  57.         }
  58.         if (page.hasChildren()) {
  59.             for (Page child : page.getChildren()) {
  60.                 addPage(result, child.getSelfReference(), pattern);
  61.             }
  62.         }
  63.     }

  64. }