001/* 002 * Copyright 2018 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.ugc.generators; 017 018import java.io.IOException; 019 020import org.apache.avalon.framework.service.ServiceException; 021import org.apache.avalon.framework.service.ServiceManager; 022import org.apache.cocoon.ProcessingException; 023import org.apache.cocoon.environment.ObjectModelHelper; 024import org.apache.cocoon.environment.Request; 025import org.apache.cocoon.generation.ServiceableGenerator; 026import org.apache.cocoon.xml.XMLUtils; 027import org.apache.commons.lang.StringUtils; 028import org.xml.sax.SAXException; 029 030import org.ametys.cms.contenttype.ContentType; 031import org.ametys.cms.contenttype.ContentTypeExtensionPoint; 032import org.ametys.core.right.RightManager; 033import org.ametys.core.user.CurrentUserProvider; 034import org.ametys.core.user.UserIdentity; 035import org.ametys.plugins.repository.data.holder.ModelAwareDataHolder; 036import org.ametys.runtime.authentication.AccessDeniedException; 037import org.ametys.web.WebConstants; 038import org.ametys.web.content.FOContentCreationHelper; 039import org.ametys.web.repository.page.Page; 040import org.ametys.web.repository.page.ZoneItem; 041 042/** 043 * UGC service generator 044 */ 045public class UGCGenerator extends ServiceableGenerator 046{ 047 /** The {@link ContentType} manager */ 048 protected ContentTypeExtensionPoint _cTypeExtPt; 049 /** The right manager */ 050 protected RightManager _rightManager; 051 /** The current user provider */ 052 protected CurrentUserProvider _currentUserProvider; 053 /** The FO content creation helper */ 054 protected FOContentCreationHelper _foContentCreationHelper; 055 056 @Override 057 public void service(ServiceManager smanager) throws ServiceException 058 { 059 super.service(smanager); 060 _cTypeExtPt = (ContentTypeExtensionPoint) smanager.lookup(ContentTypeExtensionPoint.ROLE); 061 _rightManager = (RightManager) smanager.lookup(RightManager.ROLE); 062 _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE); 063 _foContentCreationHelper = (FOContentCreationHelper) smanager.lookup(FOContentCreationHelper.ROLE); 064 } 065 066 @Override 067 public void generate() throws IOException, SAXException, ProcessingException 068 { 069 Request request = ObjectModelHelper.getRequest(objectModel); 070 071 Page page = (Page) request.getAttribute(WebConstants.REQUEST_ATTR_PAGE); 072 UserIdentity user = _currentUserProvider.getUser(); 073 if (_rightManager.hasAnonymousReadAccess(page) || user == null) 074 { 075 // Anonymous mode or no user connected ... can't propose content 076 throw new AccessDeniedException("The UGC service can not be used on an unlimited page"); 077 } 078 079 ZoneItem zoneItem = (ZoneItem) request.getAttribute(WebConstants.REQUEST_ATTR_ZONEITEM); 080 ModelAwareDataHolder serviceParameters = zoneItem.getServiceParameters(); 081 String cType = (String) serviceParameters.getValue("content-type"); 082 if (StringUtils.isBlank(cType)) 083 { 084 throw new IllegalArgumentException("A content type must be defined"); 085 } 086 087 contentHandler.startDocument(); 088 089 XMLUtils.startElement(contentHandler, "ugc"); 090 091 // SAX view 092 ContentType contentType = _cTypeExtPt.getExtension(cType); 093 _foContentCreationHelper.saxViewIfExists(contentHandler, contentType, "content-type", "main"); 094 095 // SAX contents values for metadata of type CONTENT 096 _foContentCreationHelper.saxContentValues(contentHandler, contentType, "items", page.getSitemapName()); 097 098 XMLUtils.endElement(contentHandler, "ugc"); 099 100 contentHandler.endDocument(); 101 } 102 103 104 105}