UploadToREST.java

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

  2. import org.jbehave.core.io.InvalidStoryResource;
  3. import org.jbehave.core.io.rest.RESTClient.Type;

  4. /**
  5.  * Uploads resource to REST
  6.  */
  7. public class UploadToREST implements ResourceUploader {

  8.     private RESTClient client;
  9.    
  10.     public UploadToREST(Type type) {
  11.         this(type, null, null);
  12.     }

  13.     public UploadToREST(Type type, String username, String password) {
  14.         this.client = new RESTClient(type, username, password);
  15.     }
  16.    
  17.     public UploadToREST(RESTClient client) {
  18.         this.client = client;
  19.     }
  20.    
  21.     @Override
  22.     public void uploadResource(Resource resource) {
  23.         try {
  24.             Type type = client.getType();
  25.             put(uri(resource.getURI(), type), entity(resource, type));
  26.         } catch (Exception cause) {
  27.             throw new InvalidStoryResource(resource.toString(), cause);
  28.         }
  29.     }

  30.     protected String uri(String resourcePath, Type type) {
  31.         return resourcePath;
  32.     }

  33.     protected String entity(Resource resource, Type type) {
  34.         return resource.getContent();
  35.     }

  36.     private void put(String uri, String entity) {
  37.         client.put(uri, entity);
  38.     }

  39. }