1 | |
package org.jbehave.core.io.rest.confluence; |
2 | |
|
3 | |
import static java.text.MessageFormat.format; |
4 | |
|
5 | |
import java.util.List; |
6 | |
|
7 | |
import org.jbehave.core.io.rest.RESTClient; |
8 | |
|
9 | |
import com.thoughtworks.xstream.XStream; |
10 | |
|
11 | |
public class Confluence { |
12 | |
|
13 | |
private static final String SEARCH_PAGE = "{0}/rest/prototype/1/search/name?query={2}&type=page&spaceKey={1}"; |
14 | |
private static final String EXPAND_PAGE = "{0}?expand=children"; |
15 | |
private static final String REGULAR_PAGE = "{0}"; |
16 | |
|
17 | |
private final RESTClient client; |
18 | |
|
19 | 2 | public Confluence(RESTClient client) { |
20 | 2 | this.client = client; |
21 | 2 | } |
22 | |
|
23 | |
public Page loadRootPage(String baseUrl, String spaceKey, String pageName) { |
24 | 1 | String searchResult = client.get(format(SEARCH_PAGE, baseUrl, spaceKey, pageName)); |
25 | 1 | XStream parse = configureXStream(); |
26 | 1 | Results results = (Results) parse.fromXML(searchResult); |
27 | 1 | return results.getGroup().getResult(); |
28 | |
} |
29 | |
|
30 | |
public Page loadPage(String pageUrl, boolean expanded) { |
31 | 3 | String pattern = expanded ? EXPAND_PAGE : REGULAR_PAGE; |
32 | 3 | String content = client.get(format(pattern, pageUrl)); |
33 | 3 | XStream parse = configureXStream(); |
34 | 3 | Page page = (Page) parse.fromXML(content); |
35 | 3 | return page; |
36 | |
} |
37 | |
|
38 | |
protected XStream configureXStream() { |
39 | 4 | XStream stream = new XStream(); |
40 | 4 | stream.addImplicitCollection(Page.class, "link"); |
41 | 4 | stream.alias("results", Results.class); |
42 | 4 | stream.alias("result", Page.class); |
43 | 4 | stream.alias("content", Page.class); |
44 | 4 | stream.alias("link", Link.class); |
45 | 4 | stream.useAttributeFor(Link.class, "rel"); |
46 | 4 | stream.useAttributeFor(Link.class, "href"); |
47 | 4 | stream.ignoreUnknownElements(); |
48 | 4 | return stream; |
49 | |
} |
50 | |
|
51 | 0 | public static class Results { |
52 | |
|
53 | |
private Group group; |
54 | |
|
55 | |
public Group getGroup() { |
56 | 1 | return group; |
57 | |
} |
58 | |
|
59 | |
public void setGroup(Group group) { |
60 | 0 | this.group = group; |
61 | 0 | } |
62 | |
|
63 | |
} |
64 | |
|
65 | 0 | public static class Group { |
66 | |
|
67 | |
private Page result; |
68 | |
|
69 | |
public Page getResult() { |
70 | 1 | return result; |
71 | |
} |
72 | |
|
73 | |
public void setResult(Page result) { |
74 | 0 | this.result = result; |
75 | 0 | } |
76 | |
|
77 | |
} |
78 | |
|
79 | 0 | public static class Page { |
80 | |
|
81 | |
private List<Link> link; |
82 | |
private String title; |
83 | |
private String body; |
84 | |
private List<Page> children; |
85 | |
|
86 | |
public String getSelfReference() { |
87 | 4 | for (Link candidate : link) { |
88 | 12 | if ("self".equals(candidate.getRel())) { |
89 | 4 | return candidate.getHref(); |
90 | |
} |
91 | 8 | } |
92 | 0 | throw new RuntimeException("Page does not contain self-reference"); |
93 | |
} |
94 | |
|
95 | |
public boolean hasChildren() { |
96 | 2 | return children != null; |
97 | |
} |
98 | |
|
99 | |
public List<Link> getLink() { |
100 | 0 | return link; |
101 | |
} |
102 | |
|
103 | |
public void setLink(List<Link> link) { |
104 | 0 | this.link = link; |
105 | 0 | } |
106 | |
|
107 | |
public String getTitle() { |
108 | 4 | return title; |
109 | |
} |
110 | |
|
111 | |
public void setTitle(String title) { |
112 | 0 | this.title = title; |
113 | 0 | } |
114 | |
|
115 | |
public List<Page> getChildren() { |
116 | 2 | return children; |
117 | |
} |
118 | |
|
119 | |
public void setChildren(List<Page> children) { |
120 | 0 | this.children = children; |
121 | 0 | } |
122 | |
|
123 | |
public String getBody() { |
124 | 3 | return body; |
125 | |
} |
126 | |
|
127 | |
public void setBody(String body) { |
128 | 0 | this.body = body; |
129 | 0 | } |
130 | |
|
131 | |
} |
132 | |
|
133 | 0 | public static class Link { |
134 | |
|
135 | |
private String rel; |
136 | |
private String href; |
137 | |
|
138 | |
public String getRel() { |
139 | 12 | return rel; |
140 | |
} |
141 | |
|
142 | |
public void setRel(String rel) { |
143 | 0 | this.rel = rel; |
144 | 0 | } |
145 | |
|
146 | |
public String getHref() { |
147 | 4 | return href; |
148 | |
} |
149 | |
|
150 | |
public void setHref(String href) { |
151 | 0 | this.href = href; |
152 | 0 | } |
153 | |
|
154 | |
} |
155 | |
} |