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; 017 018import java.io.IOException; 019import java.util.HashSet; 020import java.util.Set; 021import java.util.regex.Pattern; 022 023import javax.jcr.Node; 024import javax.jcr.RepositoryException; 025import javax.jcr.Value; 026 027import org.apache.avalon.framework.service.ServiceException; 028import org.apache.avalon.framework.service.ServiceManager; 029import org.apache.cocoon.ProcessingException; 030import org.apache.cocoon.environment.ObjectModelHelper; 031import org.apache.cocoon.environment.Request; 032import org.apache.cocoon.generation.ServiceableGenerator; 033import org.apache.cocoon.xml.AttributesImpl; 034import org.apache.cocoon.xml.XMLUtils; 035import org.xml.sax.SAXException; 036 037import org.ametys.core.user.CurrentUserProvider; 038import org.ametys.core.user.User; 039import org.ametys.core.user.UserIdentity; 040import org.ametys.core.user.UserManager; 041import org.ametys.plugins.repository.AmetysObjectResolver; 042import org.ametys.plugins.repository.RepositoryConstants; 043import org.ametys.plugins.repository.jcr.JCRAmetysObject; 044import org.ametys.web.repository.page.Page; 045 046/** 047 * Generate user info about a page 048 */ 049public class InfoSubscriberGenerator extends ServiceableGenerator 050{ 051 /** Constant for the attachment node name. */ 052 public static final String SUBSCRIBERS_PROPERTY_NAME = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":subscriberMails"; 053 054 /** The pattern to check emails */ 055 protected static final Pattern EMAIL_VALIDATOR = Pattern.compile("^([a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-zA-Z0-9]{2,})?$"); 056 057 /** The pattern to check text input */ 058 protected static final Pattern TEXT_VALIDATOR = Pattern.compile("^\\s*$"); 059 060 /** Repository content */ 061 protected AmetysObjectResolver _resolver; 062 063 /** FO user manager */ 064 protected UserManager _foUserManager; 065 066 /** The current user provider */ 067 protected CurrentUserProvider _currentUserProvider; 068 069 @Override 070 public void service(ServiceManager smanager) throws ServiceException 071 { 072 super.service(smanager); 073 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 074 _foUserManager = (UserManager) smanager.lookup(UserManager.ROLE); 075 _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE); 076 } 077 078 public void generate() throws IOException, SAXException, ProcessingException 079 { 080 Request request = ObjectModelHelper.getRequest(objectModel); 081 082 UserIdentity userIdentity = _currentUserProvider.getUser(); 083 User user = _foUserManager.getUser(userIdentity.getPopulationId(), userIdentity.getLogin()); 084 085 contentHandler.startDocument(); 086 AttributesImpl attr = new AttributesImpl(); 087 if (user == null) 088 { 089 attr.addCDATAAttribute("isLogged", "false"); 090 XMLUtils.createElement(contentHandler, "user", attr); 091 } 092 else 093 { 094 attr.addCDATAAttribute("isLogged", "true"); 095 String email = user.getEmail(); 096 097 String pageId = request.getParameter("page-id"); 098 if (pageId == null) 099 { 100 throw new IllegalArgumentException("Unable to subscribe or unsubscribe to the page: cannot determine the current page"); 101 } 102 else 103 { 104 Page page = _resolver.resolveById(pageId); 105 if (page instanceof JCRAmetysObject) 106 { 107 Set<String> subscribers = _getSubscribers((JCRAmetysObject) page); 108 attr.addCDATAAttribute("isRegistered", Boolean.toString(subscribers.contains(email))); 109 } 110 else 111 { 112 throw new ProcessingException("Unable to subscribe or unsubscribe to the page: the page is not subscribable"); 113 } 114 } 115 XMLUtils.createElement(contentHandler, "user", attr, email); 116 } 117 contentHandler.endDocument(); 118 } 119 120 private Set<String> _getSubscribers (JCRAmetysObject page) 121 { 122 Set<String> subscriberMails = new HashSet<>(); 123 124 try 125 { 126 Node node = page.getNode(); 127 if (node.hasProperty(SUBSCRIBERS_PROPERTY_NAME)) 128 { 129 Value[] values = node.getProperty(SUBSCRIBERS_PROPERTY_NAME).getValues(); 130 for (Value value : values) 131 { 132 subscriberMails.add(value.getString()); 133 } 134 } 135 } 136 catch (RepositoryException e) 137 { 138 getLogger().error("Unable to retrieve subscribers for page of id'" + page.getId() + "'", e); 139 } 140 141 return subscriberMails; 142 } 143 144}