LoadFromXWiki.java

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

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

  3. import com.google.gson.Gson;
  4. import com.thoughtworks.xstream.XStream;
  5. import com.thoughtworks.xstream.security.AnyTypePermission;

  6. import org.jbehave.core.io.rest.LoadFromREST;
  7. import org.jbehave.core.io.rest.RESTClient.Type;

  8. /**
  9.  * Loads resource from XWiki pages using the REST API
  10.  */
  11. public class LoadFromXWiki extends LoadFromREST {

  12.     private static final String XWIKI_URI = "{0}?media={1}";

  13.     public LoadFromXWiki(Type type) {
  14.         this(type, null, null);
  15.     }

  16.     public LoadFromXWiki(Type type, String username, String password) {
  17.         super(type, username, password);
  18.     }

  19.     @Override
  20.     protected String uri(String resourcePath, Type type) {
  21.         return format(XWIKI_URI, resourcePath, type.name().toLowerCase());
  22.     }

  23.     @Override
  24.     protected String text(String entity, Type type) {
  25.         switch (type) {
  26.             case JSON:
  27.                 Gson gson = new Gson();
  28.                 return gson.fromJson(entity, Page.class).content;
  29.             case XML:
  30.                 XStream xstream = new XStream();
  31.                 XStream.setupDefaultSecurity(xstream);
  32.                 xstream.addPermission(AnyTypePermission.ANY);
  33.                 xstream.alias("page", Page.class);
  34.                 xstream.ignoreUnknownElements();
  35.                 return ((Page) xstream.fromXML(entity)).content;
  36.             default:
  37.                 return entity;
  38.         }
  39.     }

  40.     private static class Page {
  41.         String content;
  42.     }
  43. }