1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.exchangesimple;
35
36 import info.magnolia.cms.core.ItemType;
37 import info.magnolia.cms.exchange.ActivationManagerFactory;
38 import info.magnolia.cms.exchange.ExchangeException;
39 import info.magnolia.cms.exchange.Subscriber;
40 import info.magnolia.cms.exchange.Subscription;
41 import info.magnolia.cms.security.SecurityUtil;
42
43 import java.io.IOException;
44 import java.net.MalformedURLException;
45 import java.net.URLConnection;
46 import java.util.ArrayList;
47 import java.util.Collection;
48 import java.util.Iterator;
49 import java.util.List;
50
51 import org.apache.commons.lang.StringUtils;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 import EDU.oswego.cs.dl.util.concurrent.CountDown;
56 import EDU.oswego.cs.dl.util.concurrent.Sync;
57
58
59
60
61
62
63 public class SimpleSyndicator extends BaseSyndicatorImpl {
64 private static final Logger log = LoggerFactory.getLogger(SimpleSyndicator.class);
65
66 @Override
67 public void activate(final ActivationContent activationContent, String nodePath) throws ExchangeException {
68 String nodeUUID = activationContent.getproperty(NODE_UUID);
69 final ExchangeTask task;
70
71 if (Boolean.parseBoolean(activationContent.getproperty(ItemType.DELETED_NODE_MIXIN))) {
72 task = getDeactivateTask(nodeUUID, nodePath);
73 } else {
74 task = getActivateTask(activationContent, nodePath);
75 }
76 List<Exception> errors = executeExchangeTask(task);
77
78
79 executeInPool(new Runnable() {
80 @Override
81 public void run() {
82 cleanTemporaryStore(activationContent);
83 }
84 });
85
86 handleErrors(errors);
87
88 }
89
90 protected void handleErrors(List<Exception> errors) throws ExchangeException {
91
92 if (!errors.isEmpty()) {
93 Exception e = errors.get(0);
94 log.error(e.getMessage(), e);
95 throw new ExchangeException("1 error detected: \n" + e.getMessage(), e);
96 }
97 }
98
99 private ExchangeTask getActivateTask(final ActivationContent activationContent, final String nodePath) {
100 ExchangeTask r = new ExchangeTask() {
101 @Override
102 public void runTask(Subscriber subscriber) throws ExchangeException {
103 activate(subscriber, activationContent, nodePath);
104 }
105 };
106 return r;
107 }
108
109 @Override
110 public void doDeactivate(String nodeUUID, String nodePath) throws ExchangeException {
111 List<Exception> errors = executeExchangeTask(getDeactivateTask(nodeUUID, nodePath));
112 handleErrors(errors);
113 }
114
115 private List<Exception> executeExchangeTask(ExchangeTask runnable) throws ExchangeException {
116 Collection<Subscriber> allSubscribers = ActivationManagerFactory.getActivationManager().getSubscribers();
117 Iterator<Subscriber> subscriberIterator = allSubscribers.iterator();
118 final Sync done = new CountDown(allSubscribers.size());
119 final List<Exception> errors = new ArrayList<Exception>();
120 int count = 0;
121 while (subscriberIterator.hasNext()) {
122 count++;
123 final Subscriber subscriber = subscriberIterator.next();
124 if (subscriber.isActive()) {
125
126 runnable.setErrors(errors);
127 runnable.setSubscriber(subscriber);
128 runnable.setSync(done);
129
130 executeInPool(runnable);
131 break;
132 } else {
133 done.release();
134 }
135 }
136
137
138 for (; count < allSubscribers.size(); count++) {
139 done.release();
140 }
141
142
143 acquireIgnoringInterruption(done);
144
145 return errors;
146 }
147
148 private ExchangeTask getDeactivateTask(final String nodeUUID, final String nodePath) {
149 ExchangeTask r = new ExchangeTask() {
150 @Override
151 public void runTask(Subscriber subscriber) throws ExchangeException {
152 doDeactivate(subscriber, nodeUUID, nodePath);
153 }
154 };
155 return r;
156 }
157
158
159
160
161
162
163
164 @Override
165 public String doDeactivate(Subscriber subscriber, String nodeUUID, String path) throws ExchangeException {
166 Subscription subscription = subscriber.getMatchedSubscription(path, this.repositoryName);
167 if (null != subscription) {
168 String urlString = getDeactivationURL(subscriber);
169 try {
170 URLConnection urlConnection = prepareConnection(subscriber, urlString);
171
172 this.addDeactivationHeaders(urlConnection, nodeUUID, null);
173 String status = urlConnection.getHeaderField(ACTIVATION_ATTRIBUTE_STATUS);
174
175 if (StringUtils.equals(status, ACTIVATION_HANDSHAKE)) {
176 String handshakeKey = urlConnection.getHeaderField(ACTIVATION_AUTH);
177
178 urlConnection.getContent();
179
180
181 urlConnection = prepareConnection(subscriber, getActivationURL(subscriber));
182
183 this.addDeactivationHeaders(urlConnection, nodeUUID, handshakeKey);
184 status = urlConnection.getHeaderField(ACTIVATION_ATTRIBUTE_STATUS);
185 }
186
187
188 if (StringUtils.equals(status, ACTIVATION_FAILED)) {
189 String message = urlConnection.getHeaderField(ACTIVATION_ATTRIBUTE_MESSAGE);
190 throw new ExchangeException("Message received from subscriber: " + message);
191 }
192
193 urlConnection.getContent();
194
195 } catch (MalformedURLException e) {
196 throw new ExchangeException("Incorrect URL for subscriber " + subscriber + "[" + SecurityUtil.stripPasswordFromUrl(urlString) + "]");
197 } catch (IOException e) {
198 throw new ExchangeException("Not able to send the deactivation request [" + SecurityUtil.stripPasswordFromUrl(urlString) + "]: " + e.getMessage());
199 } catch (Exception e) {
200 throw new ExchangeException(e);
201 }
202 }
203 return null;
204 }
205
206 }