| 1 | |
package org.jbehave.core.io.rest.redmine; |
| 2 | |
|
| 3 | |
import static java.text.MessageFormat.format; |
| 4 | |
import static org.apache.commons.lang.StringUtils.substringAfterLast; |
| 5 | |
|
| 6 | |
import org.jbehave.core.io.rest.RESTClient.Type; |
| 7 | |
import org.jbehave.core.io.rest.UploadToREST; |
| 8 | |
|
| 9 | |
import com.google.gson.Gson; |
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
public class UploadToRedmine extends UploadToREST { |
| 15 | |
|
| 16 | |
private static final String REDMINE_URI = "{0}.{1}"; |
| 17 | |
|
| 18 | |
public UploadToRedmine(Type type) { |
| 19 | 2 | this(type, null, null); |
| 20 | 2 | } |
| 21 | |
|
| 22 | |
public UploadToRedmine(Type type, String username, String password) { |
| 23 | 2 | super(type, username, password); |
| 24 | 2 | } |
| 25 | |
|
| 26 | |
protected String uri(String resourcePath, Type type) { |
| 27 | 1 | return format(REDMINE_URI, resourcePath, type.name().toLowerCase()); |
| 28 | |
} |
| 29 | |
|
| 30 | |
protected String entity(String resourcePath, String text, Type type) { |
| 31 | 1 | switch (type) { |
| 32 | |
case JSON: |
| 33 | 1 | Gson gson = new Gson(); |
| 34 | 1 | WikiPage page = new WikiPage(); |
| 35 | 1 | page.title = substringAfterLast(resourcePath, "/"); |
| 36 | 1 | page.text = text; |
| 37 | 1 | Entity entity = new Entity(); |
| 38 | 1 | entity.wiki_page = page; |
| 39 | 1 | return gson.toJson(entity); |
| 40 | |
case XML: |
| 41 | |
default: |
| 42 | 0 | return text; |
| 43 | |
} |
| 44 | |
} |
| 45 | |
|
| 46 | 3 | private static class Entity { |
| 47 | |
private WikiPage wiki_page; |
| 48 | |
} |
| 49 | |
|
| 50 | 4 | private static class WikiPage { |
| 51 | |
private String title; |
| 52 | |
private String text; |
| 53 | |
} |
| 54 | |
|
| 55 | |
} |