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.web.frontoffice.search.metamodel.impl;
017
018import java.io.IOException;
019import java.util.Collection;
020
021import org.apache.cocoon.components.ContextHelper;
022import org.apache.cocoon.environment.Request;
023import org.slf4j.Logger;
024import org.xml.sax.ContentHandler;
025import org.xml.sax.SAXException;
026
027import org.ametys.cms.contenttype.ContentTypesHelper;
028import org.ametys.cms.repository.Content;
029import org.ametys.plugins.repository.AmetysObject;
030import org.ametys.web.WebConstants;
031import org.ametys.web.content.GetSiteAction;
032import org.ametys.web.frontoffice.search.metamodel.ReturnableSaxer;
033import org.ametys.web.frontoffice.search.requesttime.SearchComponentArguments;
034import org.ametys.web.repository.site.Site;
035
036/**
037 * {@link ReturnableSaxer} for {@link ContentReturnable}
038 */
039public class ContentSaxer implements ReturnableSaxer
040{
041    /** The associated returnable on contents */
042    protected AbstractContentBasedReturnable _contentReturnable;
043    /** The view for SAXing contents */
044    protected String _view;
045    /** the {@link ContentTypesHelper}  */
046    protected ContentTypesHelper _cTypesHelper;
047    /** the associated content types */
048    protected Collection<String> _contentTypes;
049    
050    /**
051     * Constructor
052     * @param contentReturnable The associated returnable on contents
053     * @param view The view for SAXing contents
054     * @param cTypesHelper the {@link ContentTypesHelper}
055     * @param contentTypes the allowed content types
056     */
057    public ContentSaxer(AbstractContentBasedReturnable contentReturnable, String view, ContentTypesHelper cTypesHelper, Collection<String> contentTypes)
058    {
059        _contentReturnable = contentReturnable;
060        _view = view;
061        _cTypesHelper = cTypesHelper;
062        _contentTypes = contentTypes;
063    }
064    
065    @Override
066    public boolean canSax(AmetysObject hit, Logger logger, SearchComponentArguments args)
067    {
068        if (!(hit instanceof Content))
069        {
070            return false;
071        }
072        
073        if (_contentTypes == null || _contentTypes.isEmpty())
074        {
075            return true;
076        }
077        
078        return _contentTypes.stream().anyMatch(type -> _cTypesHelper.isInstanceOf((Content) hit, type));
079    }
080
081    @Override
082    public void sax(ContentHandler contentHandler, AmetysObject hit, Logger logger, SearchComponentArguments args) throws SAXException
083    {
084        Content content = (Content) hit;
085        
086        Request request = ContextHelper.getRequest(_contentReturnable._context);
087        String currentSiteName = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SITE_NAME);
088        Site currentSite = (Site) request.getAttribute(WebConstants.REQUEST_ATTR_SITE);
089        String currentSkinName = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SKIN_ID);
090        
091        try
092        {
093            // Content of other sites should be rendered with the current skin
094            request.setAttribute(GetSiteAction.OVERRIDE_SITE_REQUEST_ATTR, currentSiteName);
095            request.setAttribute(GetSiteAction.OVERRIDE_SKIN_REQUEST_ATTR, currentSkinName);
096            
097            _contentReturnable._contentFilterHelper.saxContent(contentHandler, content, _view, false);
098        }
099        catch (IOException e)
100        {
101            logger.error("The content '{}' could not be saxed.", content.getId(), e);
102        }
103        finally
104        {
105            // Need to do this as contentFilterHelper redirects to a Cocoon URL that gets through 'GetSiteAction' which changes some request attributes
106            request.removeAttribute(GetSiteAction.OVERRIDE_SITE_REQUEST_ATTR);
107            request.removeAttribute(GetSiteAction.OVERRIDE_SKIN_REQUEST_ATTR);
108            request.setAttribute(WebConstants.REQUEST_ATTR_SITE_NAME, currentSiteName);
109            request.setAttribute(WebConstants.REQUEST_ATTR_SITE, currentSite);
110            request.setAttribute("siteName", currentSiteName);
111            request.setAttribute(WebConstants.REQUEST_ATTR_SKIN_ID, currentSkinName);
112        }
113    }
114}