import java.util.List;
import org.apache.commons.httpclient.HttpClient;
import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;
import org.jboss.resteasy.client.ProxyFactory;
import org.jboss.resteasy.client.core.executors.ApacheHttpClientExecutor;
import org.jboss.resteasy.plugins.providers.RegisterBuiltin;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.zanata.common.LocaleId;
import org.zanata.common.ResourceType;
import org.zanata.rest.client.ITranslationResources;
import org.zanata.rest.client.IFixedTranslationResources;
import org.zanata.rest.dto.resource.Resource;
import org.zanata.rest.dto.resource.ResourceMeta;
import org.zanata.rest.dto.resource.TextFlow;
import org.zanata.rest.dto.resource.TextFlowTarget;
import org.zanata.rest.dto.resource.TranslationsResource;
public class Main
{
public static void main(String[] args)
{
RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
//getTranslations();
//getTranslationResources();
getResources();
//postTranslationResources();
}
private static void getTranslations()
{
final String server = "http://zanata-fortitude.lab.eng.bne.redhat.com:8080";
final String project = "demo";
final String version = "1.0";
final String URI = server + "/seam/resource/restv1/projects/p/" + project + "/iterations/i/" + version + "/r";
final IFixedTranslationResources client = ProxyFactory.create(IFixedTranslationResources.class, URI);
final ClientResponse<TranslationsResource> response = client.getTranslations("test", LocaleId.DE);
if (response.getStatus() == 200)
{
final TranslationsResource entity = response.getEntity();
final List<TextFlowTarget> targets = entity.getTextFlowTargets();
for (final TextFlowTarget target : targets)
System.out.println(target.getContent());
}
else
{
System.out.println("Return code: " + response.getStatus());
}
}
private static void getResources()
{
final String server = "http://zanata-fortitude.lab.eng.bne.redhat.com:8080";
final String project = "skynet";
final String version = "1";
final String URI = server + "/seam/resource/restv1/projects/p/" + project + "/iterations/i/" + version + "/r";
final IFixedTranslationResources client = ProxyFactory.create(IFixedTranslationResources.class, URI);
final ClientResponse<List<ResourceMeta>> response = client.getResourceList();
final List<ResourceMeta> entity = response.getEntity();
for (final ResourceMeta meta : entity)
{
final String name = meta.getName();
System.out.println(meta.getName());
System.out.println(meta.getType().toString());
if (name.endsWith("null"))
{
System.out.println("Deleting " + name);
final ClientResponse<String> deleteResponse = client.deleteResource("admin", "b6d7044e9ee3b2447c28fb7c50d86d98", name);
final String deleteEntity = deleteResponse.getEntity();
System.out.println(deleteEntity);
}
}
}
private static void getTranslationResources()
{
try
{
final String server = "http://zanata-fortitude.lab.eng.bne.redhat.com:8080";
final String project = "demo";
final String version = "1.0";
final String URI = server + "/seam/resource/restv1/projects/p/" + project + "/iterations/i/" + version + "/r";
final IFixedTranslationResources client = ProxyFactory.create(IFixedTranslationResources.class, URI);
final ClientResponse<Resource> response = client.getResource("test");
if (response.getStatus() == 200)
{
final Resource entity = response.getEntity();
System.out.println(entity.getName());
for (final TextFlow textFlow : entity.getTextFlows())
System.out.println(textFlow.getContent());
}
else
{
System.out.println("Return code: " + response.getStatus());
}
}
catch (final Exception ex)
{
System.out.println(ex.toString());
}
}
private static void postTranslationResources()
{
try
{
final String server = "http://zanata-fortitude.lab.eng.bne.redhat.com:8080";
final String project = "demo";
final String version = "1.0";
final String URI = server + "/seam/resource/restv1/projects/p/" + project + "/iterations/i/" + version + "/r";
final Resource resource = new Resource();
resource.setLang(LocaleId.EN_US);
resource.setName("test");
resource.setRevision(1);
resource.setType(ResourceType.FILE);
final TextFlow textFlow = new TextFlow();
textFlow.setId("id");
textFlow.setContent("Some text to be translated");
textFlow.setLang(LocaleId.EN_US);
textFlow.setRevision(1);
resource.getTextFlows().add(textFlow);
/* create a http client to supply the credentials */
final HttpClient httpClient = new HttpClient();
final ApacheHttpClientExecutor executor = new ApacheHttpClientExecutor(httpClient)
{
@Override
public ClientResponse execute(ClientRequest request) throws Exception
{
request.header("X-Auth-User", "admin");
request.header("X-Auth-Token", "b6d7044e9ee3b2447c28fb7c50d86d98");
return super.execute(request);
}
};
final IFixedTranslationResources client = ProxyFactory.create(IFixedTranslationResources.class, URI);
final ClientResponse<String> response = client.createResource("admin", "b6d7044e9ee3b2447c28fb7c50d86d98", resource);
final String entity = response.getEntity();
System.out.println(entity);
}
catch (final Exception ex)
{
System.out.println(ex.toString());
}
}
}