UploadToXWiki.java

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

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

  5. import org.jbehave.core.io.rest.RESTClient.Type;
  6. import org.jbehave.core.io.rest.Resource;
  7. import org.jbehave.core.io.rest.UploadToREST;

  8. /**
  9.  * Uploads resource to XWiki pages using the REST API
  10.  */
  11. public class UploadToXWiki extends UploadToREST {

  12.     public UploadToXWiki(Type type) {
  13.         this(type, null, null);
  14.     }

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

  18.     @Override
  19.     protected String entity(Resource resource, Type type) {
  20.         Page page = new Page();
  21.         page.syntax = resource.hasSyntax() ? resource.getSyntax() : "xwiki/2.0";
  22.         page.title = resource.getName();
  23.         page.content = resource.getContent();
  24.         page.parent = resource.getParentName();
  25.         switch (type) {
  26.             case JSON:
  27.                 Gson gson = new Gson();
  28.                 return gson.toJson(page);
  29.             case XML:
  30.                 page.xmlns = "http://www.xwiki.org";
  31.                 XStream xstream = new XStream();
  32.                 XStream.setupDefaultSecurity(xstream);
  33.                 xstream.addPermission(AnyTypePermission.ANY);
  34.                 xstream.alias("page", Page.class);
  35.                 xstream.useAttributeFor(Page.class, "xmlns");
  36.                 xstream.aliasField("xmlns", Page.class, "xmlns");
  37.                 xstream.ignoreUnknownElements();
  38.                 return xstream.toXML(page);
  39.             default:
  40.                 return resource.getContent();
  41.         }
  42.     }

  43.     @SuppressWarnings("unused")
  44.     private static class Page {
  45.         private String xmlns;
  46.         private String title;
  47.         private String syntax;
  48.         private String content;
  49.         private String parent;
  50.     }

  51. }