1 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 ; 26 import java.net.URLConnection ; 27 import java.util.Enumeration ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 31 import javax.jcr.RepositoryException; 32 import javax.servlet.http.HttpServletRequest ; 33 34 import org.apache.commons.lang.BooleanUtils; 35 import org.apache.commons.lang.StringUtils; 36 import org.apache.log4j.Logger; 37 38 39 44 public class Syndicator { 45 46 public static final String DEFAULT_CONTEXT = ContentRepository.WEBSITE; 47 48 public static final String DEFAULT_HANDLER = "ActivationHandler"; 50 51 public static final String ACTIVATE = "activate"; 53 public static final String DE_ACTIVATE = "deactivate"; 55 public static final String GET = "get"; 57 public static final String WORKING_CONTEXT = "context"; 59 public static final String PAGE = "page"; 61 public static final String PARENT = "parent"; 63 public static final String ACTION = "action"; 65 public static final String RECURSIVE = "recursive"; 67 public static final String REMOTE_PORT = "remote-port"; 69 public static final String SENDER_URL = "senderURL"; 71 public static final String SENDER_CONTEXT = "senderContext"; 73 public static final String OBJECT_TYPE = "objectType"; 75 public static final String GET_TYPE = "gettype"; 77 public static final String GET_TYPE_BINARY = "binary"; 79 public static final String GET_TYPE_SERIALIZED_OBJECT = "serializedObject"; 81 84 private static Logger log = Logger.getLogger(Syndicator.class); 85 86 private HttpServletRequest request; 87 88 private String context; 89 90 private String parent; 91 92 private String path; 93 94 private boolean recursive; 95 96 public Syndicator(HttpServletRequest request) { 97 this.request = request; 98 } 99 100 109 public synchronized void activate(String context, String parent, String path, boolean recursive) throws Exception { 110 this.parent = parent; 111 this.path = path; 112 this.recursive = recursive; 113 this.context = context; 114 this.activate(); 115 } 116 117 127 public synchronized void activate(Subscriber subscriber, String context, String parent, String path, 128 boolean recursive) throws Exception { 129 this.parent = parent; 130 this.path = path; 131 this.recursive = recursive; 132 this.context = context; 133 this.activate(subscriber); 134 } 135 136 139 public synchronized void activate(String parent, String path, boolean recursive) throws Exception { 140 this.parent = parent; 141 this.path = path; 142 this.recursive = recursive; 143 this.context = DEFAULT_CONTEXT; 144 this.activate(); 145 } 146 147 150 private synchronized void activate() throws Exception { 151 Enumeration 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 166 private synchronized void activate(Subscriber subscriber) throws Exception { 167 if (!isSubscribed(subscriber)) { 168 if (log.isDebugEnabled()) { 169 log.debug("Exchange : subscriber [ " + subscriber.getName() + " ] is not subscribed to " + this.path); } 171 return; 172 } 173 if (log.isDebugEnabled()) { 174 log.debug("Exchange : sending activation request to " + subscriber.getName()); log.debug("Exchange : user [ " + Authenticator.getUserId(this.request) + " ]"); } 177 String handle = getActivationURL(subscriber); 178 URL url = new URL (handle); 179 URLConnection urlConnection = url.openConnection(); 180 this.addActivationHeaders(urlConnection, subscriber); 181 urlConnection.getContent(); 182 log.info("Exchange : activation request received by " + subscriber.getName()); updateActivationDetails(); 184 } 185 186 private boolean isSubscribed(Subscriber subscriber) { 187 boolean isSubscribed = false; 188 List subscribedURIList = subscriber.getContext(this.context); 189 for (int i = 0; i < subscribedURIList.size(); i++) { 190 String uri = (String ) subscribedURIList.get(i); 191 if (this.path.equals(uri)) { 192 isSubscribed = true; 193 } 194 else if (this.path.startsWith(uri + "/")) { isSubscribed = true; 196 } 197 else if (uri.endsWith("/") && (this.path.startsWith(uri))) { isSubscribed = true; 199 } 200 } 201 return isSubscribed; 202 } 203 204 209 public synchronized void deActivate(String context, String path) throws Exception { 210 this.path = path; 211 this.context = context; 212 this.deActivate(); 213 } 214 215 221 public synchronized void deActivate(Subscriber subscriber, String context, String path) throws Exception { 222 this.path = path; 223 this.context = context; 224 this.deActivate(subscriber); 225 } 226 227 232 public synchronized void deActivate(String path) throws Exception { 233 this.path = path; 234 this.context = DEFAULT_CONTEXT; 235 this.deActivate(); 236 } 237 238 240 private synchronized void deActivate() { 241 Enumeration 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() + " ]"); } 251 deActivate(si); 252 } 253 catch (Exception e) { 254 log.error("Failed to remove [ " + this.path + " ] from [ " + si.getAddress() + " ]"); log.error(e.getMessage(), e); 256 } 257 } 258 } 259 260 263 private synchronized void deActivate(Subscriber subscriber) throws Exception { 264 if (!isSubscribed(subscriber)) { 265 return; 266 } 267 String handle = getDeactivationURL(subscriber); 268 URL url = new URL (handle); 269 URLConnection urlConnection = url.openConnection(); 270 this.addDeactivationHeaders(urlConnection); 271 urlConnection.getContent(); 272 updateDeActivationDetails(); 273 } 274 275 278 private String getDeactivationURL(Subscriber subscriberInfo) { 279 String handle = subscriberInfo.getProtocol() + "://" + subscriberInfo.getAddress() + "/" + DEFAULT_HANDLER; return handle; 281 } 282 283 private void addDeactivationHeaders(URLConnection connection) { 284 connection.setRequestProperty("Authorization", Authenticator.getCredentials(this.request)); connection.addRequestProperty("context", this.context); connection.addRequestProperty("page", this.path); connection.addRequestProperty("action", "deactivate"); } 289 290 293 private String getActivationURL(Subscriber subscriberInfo) { 294 String handle = subscriberInfo.getProtocol() + "://" + subscriberInfo.getAddress() + "/" + DEFAULT_HANDLER; return handle; 296 } 297 298 private void addActivationHeaders(URLConnection connection, Subscriber subscriber) throws AccessDeniedException { 299 connection.setRequestProperty("Authorization", Authenticator.getCredentials(this.request)); connection.addRequestProperty("context", this.context); connection.addRequestProperty("page", this.path); 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); log.error(re.getMessage(), re); 311 } 312 } 313 connection.addRequestProperty("parent", this.parent); 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"); connection.addRequestProperty("recursive", BooleanUtils.toStringTrueFalse(this.recursive)); 326 String senderURL = subscriber.getSenderURL(); 327 328 if (senderURL == null) { 329 String remotePort = (new Integer (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 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 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 365 private void updateTree(Content startPage, String type) { 366 Iterator 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 384 private void updateMetaData(Content page, String 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 |