1 | |
package org.jbehave.core.io.rest.xwiki; |
2 | |
|
3 | |
import static java.text.MessageFormat.format; |
4 | |
|
5 | |
import org.jbehave.core.io.rest.LoadFromREST; |
6 | |
import org.jbehave.core.io.rest.RESTClient.Type; |
7 | |
|
8 | |
import com.google.gson.Gson; |
9 | |
import com.thoughtworks.xstream.XStream; |
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
public class LoadFromXWiki extends LoadFromREST { |
15 | |
|
16 | |
private static final String XWIKI_URI = "{0}?media={1}"; |
17 | |
|
18 | |
public LoadFromXWiki(Type type) { |
19 | 4 | this(type, null, null); |
20 | 4 | } |
21 | |
|
22 | |
public LoadFromXWiki(Type type, String username, String password) { |
23 | 4 | super(type, username, password); |
24 | 4 | } |
25 | |
|
26 | |
protected String uri(String resourcePath, Type type) { |
27 | 2 | return format(XWIKI_URI, resourcePath, type.name().toLowerCase()); |
28 | |
} |
29 | |
|
30 | |
protected String text(String entity, Type type) { |
31 | 1 | switch (type) { |
32 | |
case JSON: |
33 | 1 | Gson gson = new Gson(); |
34 | 1 | return gson.fromJson(entity, Page.class).content; |
35 | |
case XML: |
36 | 1 | XStream xstream = new XStream(); |
37 | 1 | xstream.alias("page", Page.class); |
38 | 1 | xstream.ignoreUnknownElements(); |
39 | 1 | return ((Page) xstream.fromXML(entity)).content; |
40 | |
default: |
41 | 0 | return entity; |
42 | |
} |
43 | |
} |
44 | |
|
45 | 1 | private static class Page { |
46 | |
String content; |
47 | |
} |
48 | |
} |