1 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 ; 20 import java.net.URL ; 21 import java.net.URLConnection ; 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 32 public class SimpleSyndicator extends BaseSyndicatorImpl { 33 34 37 private static Logger log = LoggerFactory.getLogger(SimpleSyndicator.class); 38 39 42 public SimpleSyndicator() { 43 44 } 45 46 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 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()); log.debug("Exchange : user [{}]", this.user.getName()); } 74 String handle = getActivationURL(subscriber); 75 try { 76 URL url = new URL (handle); 77 URLConnection urlConnection = url.openConnection(); 78 this.addActivationHeaders(urlConnection, activationContent); 79 80 Transporter.transport(urlConnection, activationContent); 81 82 String status = urlConnection.getHeaderField(SimpleSyndicator.ACTIVATION_ATTRIBUTE_STATUS); 83 84 if (StringUtils.equals(status, SimpleSyndicator.ACTIVATION_FAILED)) { 86 String 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()); } 92 catch (ExchangeException e) { 93 throw e; 94 } 95 catch (MalformedURLException 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 e) { 102 throw new ExchangeException(e); 103 } 104 } 105 106 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()); } 115 doDeActivate(si); 116 } 117 } 118 119 124 public synchronized void doDeActivate(Subscriber subscriber) throws ExchangeException { 125 if (!isSubscribed(subscriber)) { 126 return; 127 } 128 String handle = getDeactivationURL(subscriber); 129 try { 130 URL url = new URL (handle); 131 URLConnection urlConnection = url.openConnection(); 132 this.addDeactivationHeaders(urlConnection); 133 urlConnection.getContent(); 134 } 135 catch (MalformedURLException 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 e) { 142 throw new ExchangeException(e); 143 } 144 } 145 146 } 147 | Popular Tags |