LoadFromREST.java

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

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

  5. /**
  6.  * Loads resource from REST
  7.  */
  8. public class LoadFromREST implements ResourceLoader {

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

  14.     public LoadFromREST(Type type, String username, String password) {
  15.         this.client = new RESTClient(type, username, password);
  16.     }
  17.    
  18.     public LoadFromREST(RESTClient client) {
  19.         this.client = client;
  20.     }
  21.    
  22.     @Override
  23.     public String loadResourceAsText(String resourcePath) {
  24.         try {
  25.             Type type = client.getType();
  26.             return text(get(uri(resourcePath, type)), type);
  27.         } catch (Exception cause) {
  28.             throw new InvalidStoryResource(resourcePath, cause);
  29.         }
  30.     }

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

  34.     protected String text(String entity, Type type) {
  35.         return entity;
  36.     }

  37.     private String get(String uri) {
  38.         return client.get(uri);
  39.     }

  40. }