Skip Headers

Oracle® Application Server Syndication Services Developer’s and Administrator’s Guide
10g (9.0.4)
Part No. B10667-01
Go To Documentation Library
Home
Go To Product List
Solution Area
Go To Table Of Contents
Contents
Go To Index
Index

Previous Next

C Sample Syndication Client

This appendix provides a listing of the SampleSyndicationClient.java program. See Section C.1.

C.1 Listing of the SampleSyndicationClient.java Program

A listing of the SampleSyndicationClient.java program follows:

import java.util.*;

import oracle.syndicate.*;
import oracle.syndicate.client.*;
import oracle.syndicate.client.handler.file.*;

import oracle.syndicate.message.*;
import oracle.syndicate.message.pkg.*;
import oracle.syndicate.message.offer.*;
import oracle.syndicate.message.response.*;
import oracle.syndicate.message.response.event.*;

/**
 *  @version $Header: SampleSyndicationClient.java 07-mar-2003.19:12:51 mcarrer Exp $
 *  @author  mcarrer 
 *  @since   release specific (what release of product did this appear in)
 */
public class SampleSyndicationClient
{
  private SyndicateConnection _sc;

  public SampleSyndicationClient()
  {
  }

  public static void main(String args[])
    throws Exception
  {
    if (args.length < 3) {
      System.err.println("java SampleSyndicationClient ossurl username password");
      System.exit(0);
    }

    SampleSyndicationClient ssc = new SampleSyndicationClient();

    //
    // Step 0: Initialize syndication connection
    // and ping to the server to make sure it
    // up and running, and we can authenticate.
    //
    ssc.initConnection(args);
    ssc.ping();

    //
    // Step 1: Get the catalog of offers available
    // and select the first offer appearing in the catalog.
    //
    Catalog cat = ssc.getCatalog();
    Offer   off = ssc.getFirstOffer(cat);

    //
    // Step 2: Subscribe to the selected offer.
    //
    Subscription sbt = ssc.subscribe(off);

    //    // Step 3: Get the content package associated 
    // with the new subscription. If necessary,
    // confirm to the syndication server the 
    // receipt of the content package.
    //
    //SyndicatePackage pkg = ssc.getPackage(sbt);
    //ssc.confirmPackage(pkg);

    //
    // Step 4: Verify the subscription status by
    // checking subscription expiration criteria,
    // such as the quantity remaining.
    //
    ssc.status(sbt);
    
    //
    // Step 5: Get session events.
    //
    ssc.events(sbt);

    //
    // Step 6: Cancel subscription.
    //
    ssc.cancel(sbt);
  }
  

  /**
   * Creates a SyndicateConnection using
   * an XML state handler storing client state 
   * in the synd-client.xml file in the current directory.
   */
  private void initConnection(String argv[])
    throws SyndicateException
  {
    // Acquire a SyndicateConnectionFactory, 
    // optionally set the connection parameters,
    // such as proxy info, timeout, and credentials.
    SyndicateConnectionFactory scf = SyndicateConnectionFactory.getInstance();
    // scf.setTimeout(1000);
    // scf.setProxy("myproxyhost", iMyProxtPort);

    // Create a default XML state handler storing subscription state in a file.
    SyndicateClientStateHandler scsh =
 scf.getDefaultSyndicateClientStateHandler("synd-client.xml");
    _sc = scf.createSyndicateConnection(argv[0],  // url of the oss server
                                        argv[1],  // username
                                        argv[2],  // password
                                        scsh);
  }

  private void ping()
    throws SyndicateException
  {
    Response resp = _sc.ping();
    System.out.println("ping: "+resp.getCode().getPhrase());
  }

  private Catalog getCatalog()
    throws SyndicateException
  {
    return _sc.getCatalog();
  }

  private Offer getFirstOffer(Catalog cat)
    throws SyndicateException
  {
    // Iterates through the catalog and returns the
    // first offer encountered.
    Offer   off = null;
    Iterator it = cat.getCatalogItems();
    while (it.hasNext() && (off == null)) {
      CatalogItem item = (CatalogItem)it.next();
      if (item.getCatalogItemType() == CatalogItem.OFFER) {

        off = (Offer) item; 
      }
      else {
        // You got an offer group.
        off = getFirstOffer((OfferGroup) item);
      }
    }
    
    // Print a few offer details.
    System.out.println("offer: "+off.getID()+" - "+off.getDescription());
    DeliveryPolicy dp = off.getDeliveryPolicy();
    if (dp.getStartDate() != null) {
      System.out.println("\t start date: "+dp.getStartDate());
    }
    if (dp.getStopDate() != null) {
      System.out.println("\t  stop date: "+dp.getStopDate());
    }
    Iterator dlrs = dp.getDeliveryRules();
    while (dlrs.hasNext()) {
      DeliveryRule dlr = (DeliveryRule) dlrs.next();
      System.out.println("\t  dlr mode: "+dlr.getMode());
    }
    return off;
  }

  
  private Offer getFirstOffer(OfferGroup offgrp) 
    throws SyndicateException
  {
    Offer   off = null;
    Iterator it = offgrp.getCatalogItems();
    while (it.hasNext() && (off == null)) {

      CatalogItem item = (CatalogItem)it.next();
      if (item.getCatalogItemType() == CatalogItem.OFFER) {

        off = (Offer) item; 
      }
      else {
        // You got an offer group.
        off = getFirstOffer((OfferGroup) item);
      }
    }
    return off;
  }

  private Subscription subscribe(Offer off)
    throws SyndicateException
  {
    // If the offer contains a push delivery rule,
    // you can use the offer APIs to set the destination URL.
    DeliveryPolicy dp = off.getDeliveryPolicy();
    Iterator   itdlrs = dp.getDeliveryRules();
    while (itdlrs.hasNext()) {

      DeliveryRule dlr = (DeliveryRule) itdlrs.next();
      if (DeliveryRule.DELIVERY_RULE_MODE_PUSH.equals(dlr.getMode())) {
        
        dlr.setURL("http://mysyndicationclient.com/syndclient/listener");
        // Optionaly set username/password.
        // dlr.setPushAuthUsername("me");
        // dlr.setPushAuthUsername("pwd");
        // If raw content is requested (for example, a mailto or 
        // ftp url has been supplied), set the raw content flag.
        // dlr.setRawFormatSupport(true);
      }
    }
    
    Subscription sbt = _sc.subscribe(off);
    System.out.println("subscription: "+sbt.getSubscriptionID());
    return sbt;
  }

  private SyndicatePackage getPackage(Subscription sbt)
    throws SyndicateException
  {
    // Use a FileSAXPackageHandler so that
    // syndicate content will be stored in
    // the local file system /tmp directory.
    FileSAXPackageHandler fsph = FileSAXPackageHandler.getInstance("/tmp/");
    
    // This will get content incremented since the last update.
    // The current subscriber state will be fetched from 
    // the SyndicateClientStateHandler used by this connection.
    // To request a full update of the content versus an incremental
    // one, use the following syntax:
    // _sc.getPackage(sbt.getSubscriptionID(), 
    //                SyndicateSubscription.STATE_ICE_INITIAL, 
    //                null, 
    //                fsph);    
    SyndicatePackage sp = _sc.getPackage(sbt.getSubscriptionID(), null, fsph);    
    return sp;
  }

  private void confirmPackage(SyndicatePackage pkg)
    throws SyndicateException
  {
    // Check if the package requires confirmation.
    // If so, confirm it.
    if (pkg.hasConfirmation()) {
      
      Response resp = _sc.confirm(pkg.getID());
      System.out.println("confirmed "+pkg.getID()+":
 "+resp.getCode().getPhrase());
    }
  }

  private void status(Subscription sbt)
    throws SyndicateException
  {
    Status sts = _sc.getStatus(sbt.getSubscriptionID());
    
    Subscription sbtNew = (Subscription) sts.getSubscriptions().next();
    System.out.println(" sbt "+sbtNew.getSubscriptionID()+":");
    System.out.println("     expiration priority: "+
                       Subscription.EXPIRATION_PRIORITIES[sbtNew.getExpirationPriority()]);
    System.out.println("     expiration date: "+sbtNew.getExpirationDate());
    System.out.println("     quantity remaining: "+sbtNew.getQuantityRemaining());
  }

  private void cancel(Subscription sbt)
    throws SyndicateException
  {
    Cancellation canc = _sc.cancelSubscription(sbt.getSubscriptionID(),
                                               "boring", "en_us");

    System.out.println("cancelled: "+canc.getCancellationID());
  }
  

  private void events(Subscription sbt)
    throws SyndicateException
  {
    Events evt = _sc.getEvents(null, null, sbt.getSubscriptionID());
    EventLog evtlog = evt.getEventLog();
    Iterator itEvts = evtlog.getEventItems();
    while (itEvts.hasNext()) {

      EventItem ei = (EventItem) itEvts.next();
      if (ei.getType() ==  EventItem.EVENT_TYPE_MSG) {

        EventMsg evtmsg = (EventMsg) ei;
        System.out.println(evtmsg.getRequestStart()+" "
                           +evtmsg.getRequest()+" "+
                           evtmsg.getResponseType());
      }
    }
  }
}