001/* 002 * Copyright 2010 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.newsletter.subscribe; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.parameters.Parameters; 024import org.apache.cocoon.environment.ObjectModelHelper; 025import org.apache.cocoon.environment.Redirector; 026import org.apache.cocoon.environment.Request; 027import org.apache.cocoon.environment.SourceResolver; 028 029import org.ametys.plugins.newsletter.category.Category; 030import org.ametys.plugins.newsletter.daos.Subscriber; 031import org.ametys.web.WebConstants; 032import org.ametys.web.repository.page.SitemapElement; 033import org.ametys.web.repository.page.ZoneItem; 034 035/** 036 * This action subscribes an email address to a newsletter 037 * 038 */ 039public class SubscribeAction extends AbstractSubscribeAction 040{ 041 @Override 042 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 043 { 044 Map<String, String> result = new HashMap<>(); 045 046 Request request = ObjectModelHelper.getRequest(objectModel); 047 boolean subscribe = request.getParameter("subscribe") != null; 048 049 String email = request.getParameter("email"); 050 result.put("email", email); 051 052 ZoneItem currentZoneItem = (ZoneItem) request.getAttribute(WebConstants.REQUEST_ATTR_ZONEITEM); 053 String zoneItemId = request.getParameter("zoneitem-id"); 054 055 if (subscribe && (zoneItemId == null || currentZoneItem.getId().equals(zoneItemId))) // Handle zoneitem id null for legacy purpose 056 { 057 try 058 { 059 String siteName = request.getParameter("siteName"); 060 String[] categories = request.getParameterValues("category"); 061 062 // Validate email 063 if (!_validEmail(email)) 064 { 065 result.put("msg", "invalid-email"); 066 return result; 067 } 068 069 070 // Validate captcha 071 SitemapElement sitemapElement; 072 if (currentZoneItem == null) 073 { 074 sitemapElement = _resolver.resolveById(request.getParameter("page-id")); 075 } 076 else 077 { 078 sitemapElement = currentZoneItem.getZone().getSitemapElement(); 079 } 080 if (!_validCaptcha(request, sitemapElement)) 081 { 082 result.put("msg", "invalid-captcha"); 083 return result; 084 } 085 086 // Validate categories 087 if (!_validCategory(categories)) 088 { 089 result.put("msg", "invalid-category"); 090 return result; 091 } 092 093 094 List<String> subscribeTo = new ArrayList<>(); 095 List<String> alreadySubscribeTo = new ArrayList<>(); 096 for (String categoryID : categories) 097 { 098 if (_subscribersDao.getSubscriber(email, siteName, categoryID) == null) 099 { 100 Category category = _getCategory(categoryID); 101 if (category != null) 102 { 103 Subscriber subscriber = _createSubscritpion(email, siteName, categoryID); 104 _subscribersDao.subscribe(subscriber); 105 106 getLogger().info("The user with email '" + email + "' subscribed to the newsletter with the token " + subscriber.getToken()); 107 108 subscribeTo.add(categoryID); 109 } 110 } 111 else 112 { 113 alreadySubscribeTo.add(categoryID); 114 } 115 } 116 117 result.put("msg", "success"); 118 result.put("subscribeTo", String.join(",", subscribeTo)); 119 result.put("alreadySubscribeTo", String.join(",", alreadySubscribeTo)); 120 } 121 catch (Exception e) 122 { 123 result.put("msg", "failure"); 124 getLogger().error("An error occurred during the subscription for the email '" + email + "'", e); 125 } 126 } 127 return result; 128 } 129 130 /** 131 * Determines if the categories are valid 132 * @param categories The categories id 133 * @return true if the categories are valid 134 */ 135 protected boolean _validCategory (String[] categories) 136 { 137 return categories != null && categories.length > 0; 138 } 139}