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; 031 032/** 033 * {@link ReturnableSaxer} for {@link ContentReturnable} 034 */ 035public class ContentSaxer implements ReturnableSaxer 036{ 037 /** The associated returnable on contents */ 038 protected AbstractContentBasedReturnable _contentReturnable; 039 /** The view for SAXing contents */ 040 protected String _view; 041 042 /** 043 * Constructor 044 * @param contentReturnable The associated returnable on contents 045 * @param view The view for SAXing contents 046 */ 047 public ContentSaxer(AbstractContentBasedReturnable contentReturnable, String view) 048 { 049 _contentReturnable = contentReturnable; 050 _view = view; 051 } 052 053 @Override 054 public boolean canSax(AmetysObject hit, Logger logger, SearchComponentArguments args) 055 { 056 return hit instanceof Content; 057 } 058 059 @Override 060 public void sax(ContentHandler contentHandler, AmetysObject hit, Logger logger, SearchComponentArguments args) throws SAXException 061 { 062 Content content = (Content) hit; 063 String currentSiteName = null; 064 try 065 { 066 currentSiteName = _getCurrentSiteName(); 067 _contentReturnable._contentFilterHelper.saxContent(contentHandler, content, _view, false); 068 } 069 catch (IOException e) 070 { 071 logger.error("The content '{}' could not be saxed.", content.getId(), e); 072 } 073 finally 074 { 075 // Need to do this as contentFilterHelper redirects to a Cocoon URL that gets through 'GetSiteAction' which changes some request attributes 076 if (currentSiteName != null) 077 { 078 _restoreSiteName(currentSiteName); 079 } 080 } 081 } 082 083 private String _getCurrentSiteName() 084 { 085 Request request = ContextHelper.getRequest(_contentReturnable._context); 086 return (String) request.getAttribute(WebConstants.REQUEST_ATTR_SITE_NAME); 087 } 088 089 private void _restoreSiteName(String currentSiteName) 090 { 091 Request request = ContextHelper.getRequest(_contentReturnable._context); 092 request.setAttribute(WebConstants.REQUEST_ATTR_SITE_NAME, currentSiteName); 093 } 094}