jimm.twice.subscriber
Class TwICESubscriber

java.lang.Object
  extended byjimm.twice.ice.IceMachine
      extended byjimm.twice.ice.Subscriber
          extended byjimm.twice.subscriber.TwICESubscriber

public class TwICESubscriber
extends Subscriber

A subscriber that can deal with subscriptions, persistence, and HTTP.

Author:
Jim Menard, jimm@io.com

Field Summary
protected static int BUFSIZ
           
protected  java.util.ArrayList confirmationsList
           
protected  java.lang.String cstoreDir
           
protected  java.text.SimpleDateFormat dateFormatter
           
protected  boolean dateTimeStamps
           
protected  java.util.ArrayList itemContents
           
protected  org.apache.axis.MessageContext msgContext
           
protected  java.lang.String pstoreDir
           
protected  java.util.ArrayList remainingOffers
           
protected static java.lang.String SAVED_FILE_DATE_FORMAT
           
protected  IceSyndicatorFullPortType syndicator
           
protected  java.net.URL syndicatorURL
           
protected  boolean uniqueFileNames
           
 
Fields inherited from class jimm.twice.ice.Subscriber
LOG_FILE_PREFIX, offers, subscriptions
 
Fields inherited from class jimm.twice.ice.IceMachine
DEFAULT_LOGGER_PREFIX, domain, loggerPrefix, party, PING_RESPONSE_STRING
 
Constructor Summary
TwICESubscriber()
          Constructor.
 
Method Summary
 void addSubscription(Subscription sub)
          Add a subscription.
protected  void buildDirectoryHierarchy(java.lang.StringBuffer buf, java.lang.String dirPrefix, java.io.File dir)
          Recursively builds a string representation of a directory structure.
 java.lang.String cancelSelectedSubscriptions(HttpServletRequest request)
          Cancels selected subscriptions and returns HTML for display.
 java.lang.String cancelSubscription(java.lang.String subId)
          Requests a subscription cancellation.
 OfferType findOffer(java.lang.String id)
          Returns the offer with the given id.
static TwICESubscriber findOrCreate(HttpSession session)
          Either finds the current subscriber or creates a new one.
 java.lang.String getListenerURL()
           
protected  java.lang.String getSubscriptionFileContents(java.lang.String subId, java.lang.String contentFilename)
           
 void handlePing()
           
 boolean hasOffers()
          Returns true if there are any offers.
protected  void load()
          Loads subscriber and subscription information from permanent storage.
 java.util.List offerList()
          For testing.
 java.util.Iterator offers()
          Returns an iterator over the offer id strings.
 java.lang.String pingSyndicator()
          Pings the syndicator and returns the response.
 OK receiveCancellation(Cancellation cancellationResp)
           
 PackageConfirmationsType receivePackage(PackageType p)
           
 PackageConfirmationsType receivePackages(Packages p)
           
 OK receiveStatus(StatusType statusResp)
           
 OK receiveSubscription(SubscriptionType subscriptionResp)
           
 void removeAllSubscriptions()
          Removes all subscriptions.
protected  OfferType removeUnwantedTransports(OfferType offer, int offerNum, HttpServletRequest request)
          Given an offer, remove the transports that are not selected by the user.
 void requestAndParseCatalog()
          Requests a catalog and parses the results, saving a list of outstanding offers.
 java.lang.String[] requestSubscriptionContents(HttpServletRequest request)
          Requests the contents of all specified "pull" subscriptions from the syndicator.
 java.lang.String[] requestSubscriptionStatus(java.lang.String subId)
          Requests the status of the specified subscription from the syndicator.
 java.lang.String retrieveSubscriptionContents(java.lang.String subId)
          Retrieves and returns the contents of the subscription with the specified subscription id.
protected  void sendDeliveryConfirmations()
          Sends a delivery confirmations message to the syndicator
 void setMessageContext(org.apache.axis.MessageContext msgContext)
           
protected  void store()
          Stores subscriber and subscription information to permanent storage.
 void subscribe(HttpServletRequest request)
          Subscribes to offers selected in HTTP form by sending one subscription request to the syndicator for each offer.
 void subscribeDirect(java.util.Collection wanted)
          Subscribes to offers contained in a list by building and sending a subscription request to the syndicator.
 void subscribeFull(java.util.Collection wanted)
          Subscribes to offers contained in a list by building and sending a subscription request to the syndicator.
protected  java.lang.String subscriptionContentsAsString(SubscriptionType sub)
          Returns the contents of a subscription as a string.
 
Methods inherited from class jimm.twice.ice.Subscriber
findSubscription, hasSubscription, hasSubscriptions, subscriptions
 
Methods inherited from class jimm.twice.ice.IceMachine
createIceHeader, getDomain, getLocationURL, getName, getParty, getRole, getUuid
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BUFSIZ

protected static final int BUFSIZ
See Also:
Constant Field Values

SAVED_FILE_DATE_FORMAT

protected static final java.lang.String SAVED_FILE_DATE_FORMAT
See Also:
Constant Field Values

pstoreDir

protected java.lang.String pstoreDir

cstoreDir

protected java.lang.String cstoreDir

syndicatorURL

protected java.net.URL syndicatorURL

dateTimeStamps

protected boolean dateTimeStamps

uniqueFileNames

protected boolean uniqueFileNames

dateFormatter

protected java.text.SimpleDateFormat dateFormatter

syndicator

protected IceSyndicatorFullPortType syndicator

msgContext

protected org.apache.axis.MessageContext msgContext

confirmationsList

protected java.util.ArrayList confirmationsList

remainingOffers

protected java.util.ArrayList remainingOffers

itemContents

protected java.util.ArrayList itemContents
Constructor Detail

TwICESubscriber

public TwICESubscriber()
Constructor. Reads subscriptions from persistent store.

Method Detail

findOrCreate

public static TwICESubscriber findOrCreate(HttpSession session)
Either finds the current subscriber or creates a new one.

Returns:
a subscriber

setMessageContext

public void setMessageContext(org.apache.axis.MessageContext msgContext)

getListenerURL

public java.lang.String getListenerURL()

offers

public java.util.Iterator offers()
Returns an iterator over the offer id strings. Requests a catalog from the syndicator if necessary.

Returns:
an iterator over offer id strings

hasOffers

public boolean hasOffers()
Returns true if there are any offers. Requests a catalog from the syndicator if necessary.

Returns:
an iterator over offer id strings

findOffer

public OfferType findOffer(java.lang.String id)
Returns the offer with the given id. Request a catalog from the syndicator if necessary.

Returns:
an offer or null if not found

offerList

public java.util.List offerList()
For testing. Does not automatically retrieve offers.


requestAndParseCatalog

public void requestAndParseCatalog()
Requests a catalog and parses the results, saving a list of outstanding offers. The list may be empty, but it will not be null.


subscribe

public void subscribe(HttpServletRequest request)
Subscribes to offers selected in HTTP form by sending one subscription request to the syndicator for each offer. Looks for subscription ids in HTTP request form fields that correspond to offer ids.

Parameters:
request - the http servlet request

removeUnwantedTransports

protected OfferType removeUnwantedTransports(OfferType offer,
                                             int offerNum,
                                             HttpServletRequest request)
                                      throws org.apache.axis.types.URI.MalformedURIException
Given an offer, remove the transports that are not selected by the user. Also specifies push delivery endpoint.

Parameters:
offer - an offer
offerNum - offer's number in list of HTTP fields; not same as id
request - the http servlet request
Returns:
modified offer if changed, otherwise null
Throws:
org.apache.axis.types.URI.MalformedURIException

subscribeDirect

public void subscribeDirect(java.util.Collection wanted)
Subscribes to offers contained in a list by building and sending a subscription request to the syndicator. The offer is being accepted as presented, so we use the short cut of the offer-id attribute

Parameters:
wanted - list of subscription offers

subscribeFull

public void subscribeFull(java.util.Collection wanted)
Subscribes to offers contained in a list by building and sending a subscription request to the syndicator. In this case, the subscriber is specifying delivery style selections or a push endpoint, so we include the full offer within the subscribe message.

Parameters:
wanted - list of subscription offers

addSubscription

public void addSubscription(Subscription sub)
Add a subscription. Only used for unit testing.


removeAllSubscriptions

public void removeAllSubscriptions()
Removes all subscriptions. Only used for unit testing.


cancelSelectedSubscriptions

public java.lang.String cancelSelectedSubscriptions(HttpServletRequest request)
Cancels selected subscriptions and returns HTML for display.

Returns:
HTML ready for display

cancelSubscription

public java.lang.String cancelSubscription(java.lang.String subId)
Requests a subscription cancellation. If successful, removes the subscription from our list. Returns either an error string or the string "OK".

Parameters:
subId - a subscription id
Returns:
an error string or the string "OK"

retrieveSubscriptionContents

public java.lang.String retrieveSubscriptionContents(java.lang.String subId)
Retrieves and returns the contents of the subscription with the specified subscription id. This does not send a request to the syndicator, it only retrieves stored contents that have already been pulled or pushed.

Parameters:
subId - a subscription id

getSubscriptionFileContents

protected java.lang.String getSubscriptionFileContents(java.lang.String subId,
                                                       java.lang.String contentFilename)
                                                throws java.io.IOException
Throws:
java.io.IOException

sendDeliveryConfirmations

protected void sendDeliveryConfirmations()
Sends a delivery confirmations message to the syndicator


requestSubscriptionContents

public java.lang.String[] requestSubscriptionContents(HttpServletRequest request)
Requests the contents of all specified "pull" subscriptions from the syndicator.


requestSubscriptionStatus

public java.lang.String[] requestSubscriptionStatus(java.lang.String subId)
Requests the status of the specified subscription from the syndicator. If no subscription is specified, requests status from all subscriptions.

Parameters:
subId - subscription ID

handlePing

public void handlePing()
                throws StatusCode
Specified by:
handlePing in class Subscriber
Throws:
StatusCode

receivePackage

public PackageConfirmationsType receivePackage(PackageType p)
                                        throws StatusCode
Specified by:
receivePackage in class Subscriber
Throws:
StatusCode

receivePackages

public PackageConfirmationsType receivePackages(Packages p)
                                         throws StatusCode
Specified by:
receivePackages in class Subscriber
Throws:
StatusCode

receiveCancellation

public OK receiveCancellation(Cancellation cancellationResp)
                       throws StatusCode
Specified by:
receiveCancellation in class Subscriber
Throws:
StatusCode

receiveSubscription

public OK receiveSubscription(SubscriptionType subscriptionResp)
                       throws StatusCode
Specified by:
receiveSubscription in class Subscriber
Throws:
StatusCode

receiveStatus

public OK receiveStatus(StatusType statusResp)
                 throws StatusCode
Specified by:
receiveStatus in class Subscriber
Throws:
StatusCode

subscriptionContentsAsString

protected java.lang.String subscriptionContentsAsString(SubscriptionType sub)
Returns the contents of a subscription as a string. If the subscription contents directory contains a single file whose name is the subscription id, return the contents of that file. Else the subscription must contain directory structure; return a string representation of the file hierarchy.

Parameters:
sub - a subscription
Returns:
the contents of the subscription

buildDirectoryHierarchy

protected void buildDirectoryHierarchy(java.lang.StringBuffer buf,
                                       java.lang.String dirPrefix,
                                       java.io.File dir)
Recursively builds a string representation of a directory structure.

Parameters:
buf - output goes into this string buffer
dirPrefix - a string representing the directories above this one
dir - a directory

pingSyndicator

public java.lang.String pingSyndicator()
Pings the syndicator and returns the response.


load

protected void load()
Loads subscriber and subscription information from permanent storage.


store

protected void store()
Stores subscriber and subscription information to permanent storage.