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.plugins.repository.data.holder.ModelAwareDataHolder; 033import org.ametys.plugins.ugc.UGCConstants; 034import org.ametys.runtime.model.DefinitionContext; 035import org.ametys.web.WebConstants; 036import org.ametys.web.cache.PageHelper; 037import org.ametys.web.content.FOContentCreationHelper; 038import org.ametys.web.repository.page.Page; 039import org.ametys.web.repository.page.ZoneItem; 040 041/** 042 * UGC service generator 043 */ 044public class UGCGenerator extends ServiceableGenerator 045{ 046 /** The {@link ContentType} manager */ 047 protected ContentTypeExtensionPoint _cTypeExtPt; 048 /** The FO content creation helper */ 049 protected FOContentCreationHelper _foContentCreationHelper; 050 /** The page helper */ 051 protected PageHelper _pageHelper; 052 053 @Override 054 public void service(ServiceManager smanager) throws ServiceException 055 { 056 super.service(smanager); 057 _cTypeExtPt = (ContentTypeExtensionPoint) smanager.lookup(ContentTypeExtensionPoint.ROLE); 058 _foContentCreationHelper = (FOContentCreationHelper) smanager.lookup(FOContentCreationHelper.ROLE); 059 _pageHelper = (PageHelper) smanager.lookup(PageHelper.ROLE); 060 } 061 062 @Override 063 public void generate() throws IOException, SAXException, ProcessingException 064 { 065 Request request = ObjectModelHelper.getRequest(objectModel); 066 067 Page page = (Page) request.getAttribute(WebConstants.REQUEST_ATTR_PAGE); 068 ZoneItem zoneItem = (ZoneItem) request.getAttribute(WebConstants.REQUEST_ATTR_ZONEITEM); 069 070 ModelAwareDataHolder serviceParameters = zoneItem.getServiceParameters(); 071 String cType = (String) serviceParameters.getValue("content-type"); 072 if (StringUtils.isBlank(cType)) 073 { 074 throw new IllegalArgumentException("A content type must be defined"); 075 } 076 077 contentHandler.startDocument(); 078 079 XMLUtils.startElement(contentHandler, "ugc"); 080 081 // SAX inputs for UGC mixin 082 XMLUtils.startElement(contentHandler, "mixin"); 083 ContentType ugcMixin = _cTypeExtPt.getExtension(UGCConstants.UGC_MIXIN_TYPE); 084 ugcMixin.getView("main").toSAX(contentHandler, DefinitionContext.newInstance()); 085 XMLUtils.endElement(contentHandler, "mixin"); 086 087 // SAX view 088 XMLUtils.startElement(contentHandler, "content-type"); 089 ContentType contentType = _cTypeExtPt.getExtension(cType); 090 contentType.getView("main").toSAX(contentHandler, DefinitionContext.newInstance()); 091 XMLUtils.endElement(contentHandler, "content-type"); 092 093 // SAX contents values for metadata of type CONTENT 094 _foContentCreationHelper.saxContentValues(contentHandler, contentType, "items", page.getSitemapName()); 095 096 XMLUtils.createElement(contentHandler, "has-captcha", String.valueOf(_pageHelper.isCaptchaRequired(page))); 097 098 XMLUtils.endElement(contentHandler, "ugc"); 099 100 contentHandler.endDocument(); 101 } 102}