REST API:PackageSubscribeClient

From LongJump Support Wiki
Revision as of 01:30, 3 December 2010 by imported>Aeric
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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
{
    static String sessionId = ""; 
    
    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();
        sessionId = subscribeClient.login(); 
        try
        {
           Resource resource = getResource(packageUrl);
           resource.header("Cookie", "JSESSIONID=" + sessionId);

           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();
        }
    }
}