Difference between revisions of "REST API:PackageSubscribeClient"

From LongJump Support Wiki
imported>Aeric
 
imported>Aeric
 
Line 21: Line 21:
public class PackageSubscribeClient extends BaseClient
public class PackageSubscribeClient extends BaseClient
{
{
    static String sessionId = "";
 
   
     public static void main(String[] args)
     public static void main(String[] args)
     {
     {
Line 35: Line 34:
         // Instantiate BaseClient.client and get sessionId
         // Instantiate BaseClient.client and get sessionId
         PackageSubscribeClient subscribeClient = new PackageSubscribeClient();
         PackageSubscribeClient subscribeClient = new PackageSubscribeClient();
         sessionId = subscribeClient.login();  
         cookieString = subscribeClient.login();  
         try
         try
         {
         {
           Resource resource = getResource(packageUrl);
           Resource resource = getResource(packageUrl);
           resource.header("Cookie", "JSESSIONID=" + sessionId);
           resource.header("Cookie", cookieString);


           ClientResponse response =
           ClientResponse response =

Latest revision as of 20:05, 3 February 2012

The PackageSubscribeClient demo program extends the REST API:BaseClient class to subscribe to a package file (a zip file downloaded from the platform). With minor modifications, it can be used to subscribe to a package hosted on the platform.

package demo.rest;

import java.io.File;

import javax.ws.rs.core.MediaType;

import org.apache.wink.client.ClientResponse;
import org.apache.wink.client.ClientWebException;
import org.apache.wink.client.Resource;
import org.apache.wink.common.internal.utils.MediaTypeUtils;

/**
 * "Subscribe" to (install or update) a package contained in a zip file. 
 * This program extends demo.rest.BaseClient.java 
 * which in turn makes use of demo.rest.BaseUtil.java to handle
 * logging in, logging out, and message formatting.
 */
public class PackageSubscribeClient extends BaseClient
{
   
    public static void main(String[] args)
    {
        String packageUrl = baseUrl + "/package/operation/subscribe"
               + "?fileName=orders.zip";         // Subscribe from a file
        File f = new File("c:\\orders.zip");

        //Other options:
        //     + "?packageId=123123123";       // Subscribe from platform
        //     + "?packageId=123123123&version=1.2";  //  Specify version

        // Instantiate BaseClient.client and get sessionId
        PackageSubscribeClient subscribeClient = new PackageSubscribeClient();
        cookieString = subscribeClient.login(); 
        try
        {
           Resource resource = getResource(packageUrl);
           resource.header("Cookie", cookieString);

           ClientResponse response =
                resource.contentType(MediaTypeUtils.ZIP).accept(
                    MediaType.APPLICATION_XML).post(ClientResponse.class, f);
            String responseXml = response.getEntity(String.class);
            echoResponse(responseXml);
        }
        catch (ClientWebException webException)
        {
            echoResponse(webException);
        }
        catch (Exception e)
        {
           System.out.println(e.getMessage());
           e.printStackTrace();
        }
        finally
        {
            client.logout();
        }
    }
}