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; 019 020import org.apache.cocoon.components.ContextHelper; 021import org.apache.cocoon.environment.Request; 022import org.slf4j.Logger; 023import org.xml.sax.ContentHandler; 024import org.xml.sax.SAXException; 025 026import org.ametys.cms.repository.Content; 027import org.ametys.plugins.repository.AmetysObject; 028import org.ametys.web.WebConstants; 029import org.ametys.web.frontoffice.search.metamodel.ReturnableSaxer; 030import org.ametys.web.frontoffice.search.requesttime.SearchComponentArguments; 031import org.ametys.web.repository.site.Site; 032 033/** 034 * {@link ReturnableSaxer} for {@link ContentReturnable} 035 */ 036public class ContentSaxer implements ReturnableSaxer 037{ 038 /** The associated returnable on contents */ 039 protected AbstractContentBasedReturnable _contentReturnable; 040 /** The view for SAXing contents */ 041 protected String _view; 042 043 /** 044 * Constructor 045 * @param contentReturnable The associated returnable on contents 046 * @param view The view for SAXing contents 047 */ 048 public ContentSaxer(AbstractContentBasedReturnable contentReturnable, String view) 049 { 050 _contentReturnable = contentReturnable; 051 _view = view; 052 } 053 054 @Override 055 public boolean canSax(AmetysObject hit, Logger logger, SearchComponentArguments args) 056 { 057 return hit instanceof Content; 058 } 059 060 @Override 061 public void sax(ContentHandler contentHandler, AmetysObject hit, Logger logger, SearchComponentArguments args) throws SAXException 062 { 063 Content content = (Content) hit; 064 065 Request request = ContextHelper.getRequest(_contentReturnable._context); 066 String currentSiteName = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SITE_NAME); 067 Site currentSite = (Site) request.getAttribute(WebConstants.REQUEST_ATTR_SITE); 068 String currentSkinName = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SKIN_ID); 069 070 try 071 { 072 _contentReturnable._contentFilterHelper.saxContent(contentHandler, content, _view, false); 073 } 074 catch (IOException e) 075 { 076 logger.error("The content '{}' could not be saxed.", content.getId(), e); 077 } 078 finally 079 { 080 // Need to do this as contentFilterHelper redirects to a Cocoon URL that gets through 'GetSiteAction' which changes some request attributes 081 request.setAttribute(WebConstants.REQUEST_ATTR_SITE_NAME, currentSiteName); 082 request.setAttribute(WebConstants.REQUEST_ATTR_SITE, currentSite); 083 request.setAttribute("siteName", currentSiteName); 084 request.setAttribute(WebConstants.REQUEST_ATTR_SKIN_ID, currentSkinName); 085 } 086 } 087}