001/* 002 * Copyright 2014 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.pagesubscription.helpers; 017 018import java.util.HashSet; 019import java.util.Set; 020 021import javax.jcr.Node; 022import javax.jcr.RepositoryException; 023import javax.jcr.Value; 024 025import org.apache.avalon.framework.component.Component; 026import org.apache.avalon.framework.context.Context; 027import org.apache.avalon.framework.context.ContextException; 028import org.apache.avalon.framework.context.Contextualizable; 029import org.apache.avalon.framework.logger.LogEnabled; 030import org.apache.avalon.framework.logger.Logger; 031import org.apache.cocoon.components.ContextHelper; 032import org.apache.cocoon.environment.Request; 033 034import org.ametys.plugins.repository.RepositoryConstants; 035import org.ametys.plugins.repository.jcr.JCRAmetysObject; 036import org.ametys.web.repository.page.Page; 037 038/** 039 * Helper component to be used from XSL stylesheets. 040 */ 041public class PageSubscriptionXSLTHelper implements LogEnabled, Component, Contextualizable 042{ 043 /** Constant for the attachment node name. */ 044 public static final String SUBSCRIBERS_PROPERTY_NAME = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":subscriberMails"; 045 046 private static Logger _logger; 047 private static Context _context; 048 049 @Override 050 public void contextualize(Context context) throws ContextException 051 { 052 _context = context; 053 } 054 055 @Override 056 public void enableLogging(Logger logger) 057 { 058 _logger = logger; 059 } 060 061 /** 062 * Determines is the given email is registered as subscriber on current page 063 * @param email The email 064 * @return true if the given email is registered as subscriber on current page 065 * @throws IllegalArgumentException if the current page is not a JCRPage 066 */ 067 public static boolean isPageSubscriber(String email) throws IllegalArgumentException 068 { 069 Request request = ContextHelper.getRequest(_context); 070 Page page = (Page) request.getAttribute(Page.class.getName()); 071 072 if (page instanceof JCRAmetysObject) 073 { 074 Set<String> subscribers = _getSubscribers((JCRAmetysObject) page); 075 return subscribers.contains(email); 076 } 077 else 078 { 079 throw new IllegalArgumentException("Unable to subscribe or unsubscribe to the page: the page is not subscribable"); 080 } 081 } 082 083 /** 084 * Get the page's subscribers 085 * @param page the page 086 * @return the registered emails 087 */ 088 protected static Set<String> _getSubscribers (JCRAmetysObject page) 089 { 090 Set<String> subscriberMails = new HashSet<>(); 091 092 try 093 { 094 Node node = page.getNode(); 095 if (node.hasProperty(SUBSCRIBERS_PROPERTY_NAME)) 096 { 097 Value[] values = node.getProperty(SUBSCRIBERS_PROPERTY_NAME).getValues(); 098 for (Value value : values) 099 { 100 subscriberMails.add(value.getString()); 101 } 102 } 103 } 104 catch (RepositoryException e) 105 { 106 _logger.error("Unable to retrieve subscribers for page of id'" + page.getId() + "'", e); 107 } 108 109 return subscriberMails; 110 } 111 112}