001    /**
002     * 
003     */
004    package de.jw.cloud42.core.eventing.subscriptionProcessor;
005    
006    import java.net.URI;
007    import java.net.URISyntaxException;
008    import java.util.logging.Logger;
009    
010    import org.apache.axiom.om.OMElement;
011    import org.apache.axiom.om.util.UUIDGenerator;
012    import org.apache.axiom.soap.SOAPBody;
013    
014    import de.jw.cloud42.core.eventing.EventingException;
015    import de.jw.cloud42.core.eventing.storage.DefaultSubscriberStore;
016    import de.jw.cloud42.core.eventing.storage.SubscriberStore;
017    import de.jw.cloud42.core.eventing.subscription.Subscription;
018    
019    /**
020     * A generic implementation of a subscription processor.
021     * 
022     * Getting the subscriber form the subscription message is delegated to more
023     * specific implementations.
024     * 
025     * @author fbitzer
026     * 
027     */
028    public class GenericSubscriptionProcessor implements SubscriptionProcessor {
029    
030            /**
031             * Processes a subscription request message.
032             * 
033             * @param subscriptionMessage
034             * @return Id of subscription, if processed successfully.
035             * @throws EventingException
036             */
037            public String subscribe(OMElement subscriptionMessage, String topic)
038                            throws EventingException {
039                    SubscriberStore store = new DefaultSubscriberStore();
040    
041                    if (store == null)
042                            throw new EventingException("Subscription store not found");
043    
044                    Subscription subscription = getSubscriberFromMessage(subscriptionMessage);
045    
046                    //set ID for subsccription
047                    subscription.setId(generateId());
048                    
049                    //set topic
050                    subscription.setTopic(topic);
051                    
052                    //and store the subscription
053                    store.store(subscription);
054    
055                    return subscription.getId();
056            }
057    
058            /**
059             * Processes a unsubscribe request message.
060             * 
061             * @param subscriptionId
062             *            id of the subscription to cancel.
063             */
064            public void unsubscribe(String subscriptionId) throws EventingException {
065    
066                    SubscriberStore store = new DefaultSubscriberStore();
067    
068                    if (store == null)
069                            throw new EventingException("Subscription store not found");
070    
071                    store.delete(subscriptionId);
072    
073                    Logger.getAnonymousLogger()
074                                    .info(
075                                                    "Subscription with id " + subscriptionId
076                                                                    + " was unsubscribed.");
077    
078            }
079    
080            /**
081             * Read subscriber informations from subscription message. Inherited classes
082             * must implement this method by themselves.
083             * 
084             * @param body
085             *            the SOAP body of the subscription message.
086             * @return
087             * @throws EventingException
088             */
089            public Subscription getSubscriberFromMessage(OMElement message)
090                            throws EventingException {
091                    throw new UnsupportedOperationException(
092                                    "Method not supported in generic subscription processor.");
093            }
094    
095            /**
096             * Generates an Id for a subscription.
097             * 
098             * @return the generated Id as String
099             */
100            public static String generateId() throws EventingException {
101                    // generate an ID for this subscription
102                    String id = UUIDGenerator.getUUID();
103    
104                    try {
105                            URI uri = new URI(id);
106                            return uri.getSchemeSpecificPart();
107                    } catch (URISyntaxException e) {
108                            throw new EventingException(e);
109                    }
110                    
111            }
112    
113    }