KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > exchange > simple > SimpleSyndicator


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.exchange.simple;
14
15 import info.magnolia.cms.beans.config.Subscriber;
16 import info.magnolia.cms.exchange.ActivationContent;
17 import info.magnolia.cms.exchange.ExchangeException;
18
19 import java.net.MalformedURLException JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.net.URLConnection JavaDoc;
22 import java.io.*;
23
24 import org.apache.commons.lang.StringUtils;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28
29 /**
30  * @author Sameer Charles $Id: SimpleSyndicator.java 7258 2006-11-08 14:31:15Z philipp $
31  */

32 public class SimpleSyndicator extends BaseSyndicatorImpl {
33
34     /**
35      * Logger.
36      */

37     private static Logger log = LoggerFactory.getLogger(SimpleSyndicator.class);
38
39     /**
40      *
41      */

42     public SimpleSyndicator() {
43
44     }
45
46     /**
47      * @throws ExchangeException
48      */

49     public synchronized void activate(ActivationContent activationContent) throws ExchangeException {
50         Subscriber si = Subscriber.getSubscriber();
51         if (si.isActive()) {
52             activate(si, activationContent);
53         }
54     }
55
56     /**
57      * Send activation request if subscribed to the activated URI
58      * @param subscriber
59      * @param activationContent
60      * @throws ExchangeException
61      */

62     public synchronized void activate(Subscriber subscriber, ActivationContent activationContent)
63         throws ExchangeException {
64         if (!isSubscribed(subscriber)) {
65             if (log.isDebugEnabled()) {
66                 log.debug("Exchange : subscriber [{}] is not subscribed to {}", subscriber.getName(), this.path);
67             }
68             return;
69         }
70         if (log.isDebugEnabled()) {
71             log.debug("Exchange : sending activation request to {}", subscriber.getName()); //$NON-NLS-1$
72
log.debug("Exchange : user [{}]", this.user.getName()); //$NON-NLS-1$
73
}
74         String JavaDoc handle = getActivationURL(subscriber);
75         try {
76             URL JavaDoc url = new URL JavaDoc(handle);
77             URLConnection JavaDoc urlConnection = url.openConnection();
78             this.addActivationHeaders(urlConnection, activationContent);
79
80             Transporter.transport(urlConnection, activationContent);
81
82             String JavaDoc status = urlConnection.getHeaderField(SimpleSyndicator.ACTIVATION_ATTRIBUTE_STATUS);
83
84             // check if the activation failed
85
if (StringUtils.equals(status, SimpleSyndicator.ACTIVATION_FAILED)) {
86                 String JavaDoc message = urlConnection.getHeaderField(SimpleSyndicator.ACTIVATION_ATTRIBUTE_MESSAGE);
87                 throw new ExchangeException("Message received from subscriber: " + message);
88             }
89             urlConnection.getContent();
90             log.debug("Exchange : activation to subscriber {} succeeded", subscriber.getName()); //$NON-NLS-1$
91
}
92         catch (ExchangeException e) {
93             throw e;
94         }
95         catch (MalformedURLException JavaDoc e) {
96             throw new ExchangeException("Incorrect URL for subscriber " + subscriber + "[" + handle + "]");
97         }
98         catch (IOException e) {
99             throw new ExchangeException("Not able to send the activation request [" + handle + "]: " + e.getMessage());
100         }
101         catch (Exception JavaDoc e) {
102             throw new ExchangeException(e);
103         }
104     }
105
106     /**
107      * @throws ExchangeException
108      */

109     public synchronized void doDeActivate() throws ExchangeException {
110         Subscriber si = Subscriber.getSubscriber();
111         if (si.isActive()) {
112             if (log.isDebugEnabled()) {
113                 log.debug("Removing [{}] from [{}]", this.path, si.getURL()); //$NON-NLS-1$
114
}
115             doDeActivate(si);
116         }
117     }
118
119     /**
120      * deactivate from a specified subscriber
121      * @param subscriber
122      * @throws ExchangeException
123      */

124     public synchronized void doDeActivate(Subscriber subscriber) throws ExchangeException {
125         if (!isSubscribed(subscriber)) {
126             return;
127         }
128         String JavaDoc handle = getDeactivationURL(subscriber);
129         try {
130             URL JavaDoc url = new URL JavaDoc(handle);
131             URLConnection JavaDoc urlConnection = url.openConnection();
132             this.addDeactivationHeaders(urlConnection);
133             urlConnection.getContent();
134         }
135         catch (MalformedURLException JavaDoc e) {
136             throw new ExchangeException("Incorrect URL for subscriber " + subscriber + "[" + handle + "]");
137         }
138         catch (IOException e) {
139             throw new ExchangeException("Not able to send the deactivation request [" + handle + "]: " + e.getMessage());
140         }
141         catch (Exception JavaDoc e) {
142             throw new ExchangeException(e);
143         }
144     }
145
146 }
147
Popular Tags