Confluence.java

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

  2. import static java.text.MessageFormat.format;

  3. import java.util.List;

  4. import com.thoughtworks.xstream.XStream;
  5. import com.thoughtworks.xstream.security.AnyTypePermission;

  6. import org.jbehave.core.io.rest.RESTClient;

  7. public class Confluence {

  8.     private static final String SEARCH_PAGE = "{0}/rest/prototype/1/search/name?query={2}&type=page&spaceKey={1}";
  9.     private static final String EXPAND_PAGE = "{0}?expand=children";
  10.     private static final String REGULAR_PAGE = "{0}";

  11.     private final RESTClient client;

  12.     public Confluence(RESTClient client) {
  13.         this.client = client;
  14.     }

  15.     public Page loadRootPage(String baseUrl, String spaceKey, String pageName) {
  16.         String searchResult = client.get(format(SEARCH_PAGE, baseUrl, spaceKey, pageName));
  17.         XStream parse = configureXStream();
  18.         Results results = (Results) parse.fromXML(searchResult);
  19.         return results.getGroup().getResult();
  20.     }

  21.     public Page loadPage(String pageUrl, boolean expanded) {
  22.         String pattern = expanded ? EXPAND_PAGE : REGULAR_PAGE;
  23.         String content = client.get(format(pattern, pageUrl));
  24.         XStream parse = configureXStream();
  25.         return (Page) parse.fromXML(content);
  26.     }

  27.     protected XStream configureXStream() {
  28.         XStream xstream = new XStream();
  29.         XStream.setupDefaultSecurity(xstream);
  30.         xstream.addPermission(AnyTypePermission.ANY);
  31.         xstream.addImplicitCollection(Page.class, "link");
  32.         xstream.alias("results", Results.class);
  33.         xstream.alias("result", Page.class);
  34.         xstream.alias("content", Page.class);
  35.         xstream.alias("link", Link.class);
  36.         xstream.useAttributeFor(Link.class, "rel");
  37.         xstream.useAttributeFor(Link.class, "href");
  38.         xstream.ignoreUnknownElements();
  39.         return xstream;
  40.     }

  41.     public static class Results {

  42.         private Group group;

  43.         public Group getGroup() {
  44.             return group;
  45.         }

  46.         public void setGroup(Group group) {
  47.             this.group = group;
  48.         }

  49.     }

  50.     public static class Group {

  51.         private Page result;

  52.         public Page getResult() {
  53.             return result;
  54.         }

  55.         public void setResult(Page result) {
  56.             this.result = result;
  57.         }

  58.     }

  59.     public static class Page {

  60.         private List<Link> link;
  61.         private String title;
  62.         private String body;
  63.         private List<Page> children;

  64.         public String getSelfReference() {
  65.             for (Link candidate : link) {
  66.                 if ("self".equals(candidate.getRel())) {
  67.                     return candidate.getHref();
  68.                 }
  69.             }
  70.             throw new RuntimeException("Page does not contain self-reference");
  71.         }

  72.         public boolean hasChildren() {
  73.             return children != null;
  74.         }

  75.         public List<Link> getLink() {
  76.             return link;
  77.         }

  78.         public void setLink(List<Link> link) {
  79.             this.link = link;
  80.         }

  81.         public String getTitle() {
  82.             return title;
  83.         }

  84.         public void setTitle(String title) {
  85.             this.title = title;
  86.         }

  87.         public List<Page> getChildren() {
  88.             return children;
  89.         }

  90.         public void setChildren(List<Page> children) {
  91.             this.children = children;
  92.         }

  93.         public String getBody() {
  94.             return body;
  95.         }

  96.         public void setBody(String body) {
  97.             this.body = body;
  98.         }

  99.     }

  100.     public static class Link {

  101.         private String rel;
  102.         private String href;

  103.         public String getRel() {
  104.             return rel;
  105.         }

  106.         public void setRel(String rel) {
  107.             this.rel = rel;
  108.         }

  109.         public String getHref() {
  110.             return href;
  111.         }

  112.         public void setHref(String href) {
  113.             this.href = href;
  114.         }

  115.     }
  116. }