KickJava   Java API By Example, From Geeks To Geeks.

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


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-2005 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.ContentRepository;
16 import info.magnolia.cms.beans.config.Subscriber;
17 import info.magnolia.cms.core.Content;
18 import info.magnolia.cms.core.HierarchyManager;
19 import info.magnolia.cms.core.ItemType;
20 import info.magnolia.cms.core.MetaData;
21 import info.magnolia.cms.security.AccessDeniedException;
22 import info.magnolia.cms.security.Authenticator;
23 import info.magnolia.cms.security.SessionAccessControl;
24
25 import java.net.URL JavaDoc;
26 import java.net.URLConnection JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 import javax.jcr.RepositoryException;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33
34 import org.apache.commons.lang.BooleanUtils;
35 import org.apache.commons.lang.StringUtils;
36 import org.apache.log4j.Logger;
37
38
39 /**
40  * Date: May 7, 2004 Time: 05:15:20 PM
41  * @author Sameer Charles
42  * @version 1.5
43  */

44 public class Syndicator {
45
46     public static final String JavaDoc DEFAULT_CONTEXT = ContentRepository.WEBSITE;
47
48     public static final String JavaDoc DEFAULT_HANDLER = "ActivationHandler"; //$NON-NLS-1$
49

50     /* request headers */
51     public static final String JavaDoc ACTIVATE = "activate"; //$NON-NLS-1$
52

53     public static final String JavaDoc DE_ACTIVATE = "deactivate"; //$NON-NLS-1$
54

55     public static final String JavaDoc GET = "get"; //$NON-NLS-1$
56

57     public static final String JavaDoc WORKING_CONTEXT = "context"; //$NON-NLS-1$
58

59     public static final String JavaDoc PAGE = "page"; //$NON-NLS-1$
60

61     public static final String JavaDoc PARENT = "parent"; //$NON-NLS-1$
62

63     public static final String JavaDoc ACTION = "action"; //$NON-NLS-1$
64

65     public static final String JavaDoc RECURSIVE = "recursive"; //$NON-NLS-1$
66

67     public static final String JavaDoc REMOTE_PORT = "remote-port"; //$NON-NLS-1$
68

69     public static final String JavaDoc SENDER_URL = "senderURL"; //$NON-NLS-1$
70

71     public static final String JavaDoc SENDER_CONTEXT = "senderContext"; //$NON-NLS-1$
72

73     public static final String JavaDoc OBJECT_TYPE = "objectType"; //$NON-NLS-1$
74

75     public static final String JavaDoc GET_TYPE = "gettype"; //$NON-NLS-1$
76

77     public static final String JavaDoc GET_TYPE_BINARY = "binary"; //$NON-NLS-1$
78

79     public static final String JavaDoc GET_TYPE_SERIALIZED_OBJECT = "serializedObject"; //$NON-NLS-1$
80

81     /**
82      * Logger.
83      */

84     private static Logger log = Logger.getLogger(Syndicator.class);
85
86     private HttpServletRequest JavaDoc request;
87
88     private String JavaDoc context;
89
90     private String JavaDoc parent;
91
92     private String JavaDoc path;
93
94     private boolean recursive;
95
96     public Syndicator(HttpServletRequest JavaDoc request) {
97         this.request = request;
98     }
99
100     /**
101      * <p>
102      * this will activate specifies page (sub pages) to all configured subscribers
103      * </p>
104      * @param context repository ID as configured
105      * @param parent parent under which this page will be activated
106      * @param path page to be activated
107      * @param recursive
108      */

109     public synchronized void activate(String JavaDoc context, String JavaDoc parent, String JavaDoc path, boolean recursive) throws Exception JavaDoc {
110         this.parent = parent;
111         this.path = path;
112         this.recursive = recursive;
113         this.context = context;
114         this.activate();
115     }
116
117     /**
118      * <p>
119      * this will activate specifies page (sub pages) to the specified subscribers
120      * </p>
121      * @param subscriber
122      * @param context repository ID as configured
123      * @param parent parent under which this page will be activated
124      * @param path page to be activated
125      * @param recursive
126      */

127     public synchronized void activate(Subscriber subscriber, String JavaDoc context, String JavaDoc parent, String JavaDoc path,
128         boolean recursive) throws Exception JavaDoc {
129         this.parent = parent;
130         this.path = path;
131         this.recursive = recursive;
132         this.context = context;
133         this.activate(subscriber);
134     }
135
136     /**
137      * @deprecated use activate(String context, String parent, String path, boolean recursive) instead
138      */

139     public synchronized void activate(String JavaDoc parent, String JavaDoc path, boolean recursive) throws Exception JavaDoc {
140         this.parent = parent;
141         this.path = path;
142         this.recursive = recursive;
143         this.context = DEFAULT_CONTEXT;
144         this.activate();
145     }
146
147     /**
148      * @throws Exception
149      */

150     private synchronized void activate() throws Exception JavaDoc {
151         Enumeration JavaDoc en = Subscriber.getList();
152         while (en.hasMoreElements()) {
153             Subscriber si = (Subscriber) en.nextElement();
154             if (si.isActive()) {
155                 activate(si);
156             }
157         }
158     }
159
160     /**
161      * <p>
162      * send activation request only if subscribed to the activated URI
163      * </p>
164      * @throws Exception
165      */

166     private synchronized void activate(Subscriber subscriber) throws Exception JavaDoc {
167         if (!isSubscribed(subscriber)) {
168             if (log.isDebugEnabled()) {
169                 log.debug("Exchange : subscriber [ " + subscriber.getName() + " ] is not subscribed to " + this.path); //$NON-NLS-1$ //$NON-NLS-2$
170
}
171             return;
172         }
173         if (log.isDebugEnabled()) {
174             log.debug("Exchange : sending activation request to " + subscriber.getName()); //$NON-NLS-1$
175
log.debug("Exchange : user [ " + Authenticator.getUserId(this.request) + " ]"); //$NON-NLS-1$ //$NON-NLS-2$
176
}
177         String JavaDoc handle = getActivationURL(subscriber);
178         URL JavaDoc url = new URL JavaDoc(handle);
179         URLConnection JavaDoc urlConnection = url.openConnection();
180         this.addActivationHeaders(urlConnection, subscriber);
181         urlConnection.getContent();
182         log.info("Exchange : activation request received by " + subscriber.getName()); //$NON-NLS-1$
183
updateActivationDetails();
184     }
185
186     private boolean isSubscribed(Subscriber subscriber) {
187         boolean isSubscribed = false;
188         List JavaDoc subscribedURIList = subscriber.getContext(this.context);
189         for (int i = 0; i < subscribedURIList.size(); i++) {
190             String JavaDoc uri = (String JavaDoc) subscribedURIList.get(i);
191             if (this.path.equals(uri)) {
192                 isSubscribed = true;
193             }
194             else if (this.path.startsWith(uri + "/")) { //$NON-NLS-1$
195
isSubscribed = true;
196             }
197             else if (uri.endsWith("/") && (this.path.startsWith(uri))) { //$NON-NLS-1$
198
isSubscribed = true;
199             }
200         }
201         return isSubscribed;
202     }
203
204     /**
205      * @param path , to deactivate
206      * @param context
207      * @throws Exception
208      */

209     public synchronized void deActivate(String JavaDoc context, String JavaDoc path) throws Exception JavaDoc {
210         this.path = path;
211         this.context = context;
212         this.deActivate();
213     }
214
215     /**
216      * @param path , to deactivate
217      * @param context
218      * @param subscriber
219      * @throws Exception
220      */

221     public synchronized void deActivate(Subscriber subscriber, String JavaDoc context, String JavaDoc path) throws Exception JavaDoc {
222         this.path = path;
223         this.context = context;
224         this.deActivate(subscriber);
225     }
226
227     /**
228      * @param path , to deactivate
229      * @throws Exception
230      * @deprecated use deActivate(String context, String path)
231      */

232     public synchronized void deActivate(String JavaDoc path) throws Exception JavaDoc {
233         this.path = path;
234         this.context = DEFAULT_CONTEXT;
235         this.deActivate();
236     }
237
238     /**
239      */

240     private synchronized void deActivate() {
241         Enumeration JavaDoc en = Subscriber.getList();
242         while (en.hasMoreElements()) {
243             Subscriber si = (Subscriber) en.nextElement();
244             if (!si.isActive()) {
245                 continue;
246             }
247             try {
248                 if (log.isDebugEnabled()) {
249                     log.debug("Removing [ " + this.path + " ] from [ " + si.getAddress() + " ]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
250
}
251                 deActivate(si);
252             }
253             catch (Exception JavaDoc e) {
254                 log.error("Failed to remove [ " + this.path + " ] from [ " + si.getAddress() + " ]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
255
log.error(e.getMessage(), e);
256             }
257         }
258     }
259
260     /**
261      * @throws Exception
262      */

263     private synchronized void deActivate(Subscriber subscriber) throws Exception JavaDoc {
264         if (!isSubscribed(subscriber)) {
265             return;
266         }
267         String JavaDoc handle = getDeactivationURL(subscriber);
268         URL JavaDoc url = new URL JavaDoc(handle);
269         URLConnection JavaDoc urlConnection = url.openConnection();
270         this.addDeactivationHeaders(urlConnection);
271         urlConnection.getContent();
272         updateDeActivationDetails();
273     }
274
275     /**
276      *
277      */

278     private String JavaDoc getDeactivationURL(Subscriber subscriberInfo) {
279         String JavaDoc handle = subscriberInfo.getProtocol() + "://" + subscriberInfo.getAddress() + "/" + DEFAULT_HANDLER; //$NON-NLS-1$ //$NON-NLS-2$
280
return handle;
281     }
282
283     private void addDeactivationHeaders(URLConnection JavaDoc connection) {
284         connection.setRequestProperty("Authorization", Authenticator.getCredentials(this.request)); //$NON-NLS-1$
285
connection.addRequestProperty("context", this.context); //$NON-NLS-1$
286
connection.addRequestProperty("page", this.path); //$NON-NLS-1$
287
connection.addRequestProperty("action", "deactivate"); //$NON-NLS-1$ //$NON-NLS-2$
288
}
289
290     /**
291      * @return activation handle
292      */

293     private String JavaDoc getActivationURL(Subscriber subscriberInfo) {
294         String JavaDoc handle = subscriberInfo.getProtocol() + "://" + subscriberInfo.getAddress() + "/" + DEFAULT_HANDLER; //$NON-NLS-1$ //$NON-NLS-2$
295
return handle;
296     }
297
298     private void addActivationHeaders(URLConnection JavaDoc connection, Subscriber subscriber) throws AccessDeniedException {
299         connection.setRequestProperty("Authorization", Authenticator.getCredentials(this.request)); //$NON-NLS-1$
300
connection.addRequestProperty("context", this.context); //$NON-NLS-1$
301
connection.addRequestProperty("page", this.path); //$NON-NLS-1$
302
HierarchyManager hm = SessionAccessControl.getHierarchyManager(this.request, this.context);
303         if (StringUtils.isEmpty(this.parent)) {
304             try {
305                 Content page = hm.getContent(this.path);
306                 this.parent = page.getParent().getHandle();
307             }
308             catch (RepositoryException re) {
309                 log.error("failed to build parent path for - " + this.path); //$NON-NLS-1$
310
log.error(re.getMessage(), re);
311             }
312         }
313         connection.addRequestProperty("parent", this.parent); //$NON-NLS-1$
314
if (hm.isPage(this.path)) {
315             connection.addRequestProperty(Syndicator.OBJECT_TYPE, ItemType.CONTENT.getSystemName());
316         }
317         else if (hm.isNodeType(this.path, ItemType.CONTENTNODE.getSystemName())) {
318             connection.addRequestProperty(Syndicator.OBJECT_TYPE, ItemType.CONTENTNODE.getSystemName());
319         }
320         else if (hm.isNodeData(this.path)) {
321             connection.addRequestProperty(Syndicator.OBJECT_TYPE, ItemType.NT_NODEDATA);
322         }
323         connection.addRequestProperty("action", "activate"); //$NON-NLS-1$ //$NON-NLS-2$
324
connection.addRequestProperty("recursive", BooleanUtils.toStringTrueFalse(this.recursive)); //$NON-NLS-1$
325

326         String JavaDoc senderURL = subscriber.getSenderURL();
327
328         if (senderURL == null) {
329             // todo remove remotePort property once its tested together with apache
330
String JavaDoc remotePort = (new Integer JavaDoc(this.request.getServerPort())).toString();
331             connection.addRequestProperty(REMOTE_PORT, remotePort);
332             connection.addRequestProperty(SENDER_CONTEXT, this.request.getContextPath());
333         }
334         else {
335             connection.addRequestProperty(SENDER_URL, senderURL);
336         }
337     }
338
339     /**
340      *
341      */

342     private void updateActivationDetails() throws RepositoryException {
343         HierarchyManager hm = SessionAccessControl.getHierarchyManager(this.request, this.context);
344         Content page = hm.getContent(this.path);
345         updateMetaData(page, Syndicator.ACTIVATE);
346         if (this.recursive) {
347             this.updateTree(page, Syndicator.ACTIVATE);
348         }
349         page.save();
350     }
351
352     /**
353      */

354     private void updateDeActivationDetails() throws RepositoryException {
355         HierarchyManager hm = SessionAccessControl.getHierarchyManager(this.request, this.context);
356         Content page = hm.getContent(this.path);
357         updateMetaData(page, Syndicator.DE_ACTIVATE);
358         this.updateTree(page, Syndicator.DE_ACTIVATE);
359         page.save();
360     }
361
362     /**
363      * @param startPage
364      */

365     private void updateTree(Content startPage, String JavaDoc type) {
366         Iterator JavaDoc children = startPage.getChildren().iterator();
367         while (children.hasNext()) {
368             Content page = (Content) children.next();
369             try {
370                 updateMetaData(page, type);
371             }
372             catch (AccessDeniedException e) {
373                 log.error(e.getMessage(), e);
374             }
375             if (page.hasChildren()) {
376                 updateTree(page, type);
377             }
378         }
379     }
380
381     /**
382      * @param page
383      */

384     private void updateMetaData(Content page, String JavaDoc type) throws AccessDeniedException {
385         MetaData md = page.getMetaData(MetaData.ACTIVATION_INFO);
386         if (type.equals(Syndicator.ACTIVATE)) {
387             md.setActivated();
388         }
389         else {
390             md.setUnActivated();
391         }
392         md.setActivatorId(Authenticator.getUserId(this.request));
393         md.setLastActivationActionDate();
394         md = null;
395     }
396 }
397
Popular Tags