1 | |
package org.jbehave.core.io.rest.xwiki; |
2 | |
|
3 | |
import static java.text.MessageFormat.format; |
4 | |
|
5 | |
import java.util.Collection; |
6 | |
import java.util.HashMap; |
7 | |
import java.util.Map; |
8 | |
|
9 | |
import org.jbehave.core.io.rest.IndexWithBreadcrumbs; |
10 | |
import org.jbehave.core.io.rest.RESTClient; |
11 | |
import org.jbehave.core.io.rest.RESTClient.Type; |
12 | |
import org.jbehave.core.io.rest.Resource; |
13 | |
import org.jbehave.core.io.rest.ResourceNameResolver; |
14 | |
|
15 | |
import com.google.gson.Gson; |
16 | |
import com.google.gson.JsonParser; |
17 | |
import com.google.gson.reflect.TypeToken; |
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
public class IndexFromXWiki extends IndexWithBreadcrumbs { |
23 | |
|
24 | |
private static final String INDEX_URI = "{0}?media=json"; |
25 | |
private static final String PAGE_URI = "{0}/{1}"; |
26 | |
|
27 | |
public IndexFromXWiki() { |
28 | 3 | this(null, null); |
29 | 3 | } |
30 | |
|
31 | |
public IndexFromXWiki(String username, String password) { |
32 | 3 | this(username, password, new ToLowerCase()); |
33 | 3 | } |
34 | |
|
35 | |
public IndexFromXWiki(String username, String password, ResourceNameResolver nameResolver) { |
36 | 3 | super(new RESTClient(Type.JSON, username, password), nameResolver); |
37 | 3 | } |
38 | |
|
39 | |
protected Map<String, Resource> createIndexFromEntity(String rootURI, String entity) { |
40 | 1 | Collection<Page> pages = parse(entity); |
41 | 1 | Map<String, Resource> index = new HashMap<String, Resource>(); |
42 | 1 | for (Page page : pages) { |
43 | 4 | String parentName = (page.parent != null ? resolveName(page.parent) : null); |
44 | 4 | String uri = format(PAGE_URI, rootURI, page.name); |
45 | 4 | Resource resource = new Resource(uri, resolveName(page.name), parentName); |
46 | 4 | index.put(resource.getName(), resource); |
47 | 4 | } |
48 | 1 | return index; |
49 | |
} |
50 | |
|
51 | |
protected String uri(String rootPath) { |
52 | 0 | return format(INDEX_URI, rootPath); |
53 | |
} |
54 | |
|
55 | |
private Collection<Page> parse(String entity) { |
56 | 1 | Gson gson = new Gson(); |
57 | 1 | return gson.<Collection<Page>> fromJson( |
58 | |
jsonMember(entity, "pageSummaries"), |
59 | 1 | new TypeToken<Collection<Page>>() { |
60 | |
}.getType()); |
61 | |
} |
62 | |
|
63 | |
private String jsonMember(String entity, String memberName) { |
64 | 1 | return new JsonParser().parse(entity).getAsJsonObject().get(memberName) |
65 | |
.toString(); |
66 | |
} |
67 | |
|
68 | 20 | private static class Page { |
69 | |
private String name; |
70 | |
private String parent; |
71 | |
} |
72 | |
|
73 | |
} |