| 1 | |
package org.jbehave.core.io.rest; |
| 2 | |
|
| 3 | |
import org.jbehave.core.io.InvalidStoryResource; |
| 4 | |
import org.jbehave.core.io.rest.RESTClient.Type; |
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
public class UploadToREST implements ResourceUploader { |
| 10 | |
|
| 11 | |
private RESTClient client; |
| 12 | |
|
| 13 | |
public UploadToREST(Type type) { |
| 14 | 0 | this(type, null, null); |
| 15 | 0 | } |
| 16 | |
|
| 17 | 2 | public UploadToREST(Type type, String username, String password) { |
| 18 | 2 | this.client = new RESTClient(type, username, password); |
| 19 | 2 | } |
| 20 | |
|
| 21 | 0 | public UploadToREST(RESTClient client) { |
| 22 | 0 | this.client = client; |
| 23 | 0 | } |
| 24 | |
|
| 25 | |
public void uploadResourceAsText(String resourcePath, String text) { |
| 26 | |
try { |
| 27 | 0 | Type type = client.getType(); |
| 28 | 0 | put(uri(resourcePath, type), entity(resourcePath,text, type)); |
| 29 | 0 | } catch (Exception cause) { |
| 30 | 0 | throw new InvalidStoryResource(resourcePath, cause); |
| 31 | 0 | } |
| 32 | 0 | } |
| 33 | |
|
| 34 | |
protected String uri(String resourcePath, Type type) { |
| 35 | 0 | return resourcePath; |
| 36 | |
} |
| 37 | |
|
| 38 | |
protected String entity(String resourcePath, String text, Type type) { |
| 39 | 0 | return text; |
| 40 | |
} |
| 41 | |
|
| 42 | |
private void put(String uri, String entity) { |
| 43 | 0 | client.put(uri, entity); |
| 44 | 0 | } |
| 45 | |
|
| 46 | |
} |