001/* 002 * Copyright 2019 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.requesttime; 017 018import org.apache.avalon.framework.parameters.Parameters; 019import org.apache.cocoon.environment.Request; 020 021import org.ametys.web.WebConstants; 022import org.ametys.web.repository.page.ZoneItem; 023 024/** 025 * A class which sometimes set some request attibutes before {@link SearchServiceGenerator} is processed. 026 */ 027public class SearchServiceRequestAttributeSetter 028{ 029 private static final String __ENABLE_PARAMETER_NAME = "set-request-attributes"; 030 031 private SearchServiceGenerator _generator; 032 033 SearchServiceRequestAttributeSetter(SearchServiceGenerator generator) 034 { 035 _generator = generator; 036 } 037 038 private boolean _mustDo(Parameters parameters) 039 { 040 return parameters.getParameterAsBoolean(__ENABLE_PARAMETER_NAME, false); 041 } 042 043 /** 044 * If explicitly asked (The generator was defined with the parameter 'set-request-attributes'), 045 * set some request attributes (such as ZoneItem, site, skin name...). 046 * <br>This has to be done to handle URLs for AJAX requests for instance. 047 * @param request The request 048 * @param parameters The {@link SearchServiceGenerator} parameters 049 */ 050 public void setAttributes(Request request, Parameters parameters) 051 { 052 if (_mustDo(parameters)) 053 { 054 ZoneItem zoneItem = _setZoneItem(request, parameters); 055 _setSite(request, zoneItem); 056 _setSkin(request, zoneItem); 057 } 058 } 059 060 private ZoneItem _setZoneItem(Request request, Parameters parameters) 061 { 062 ZoneItem zoneItem = (ZoneItem) request.getAttribute(WebConstants.REQUEST_ATTR_ZONEITEM); 063 if (zoneItem == null) 064 { 065 String zoneItemId = parameters.getParameter(SearchServiceGenerator.__ZONE_ITEM_GENERATOR_PARAM_NAME, ""); 066 zoneItem = _generator._ametysObjectResolver.resolveById(zoneItemId); 067 request.setAttribute(WebConstants.REQUEST_ATTR_ZONEITEM, zoneItem); 068 _generator._logger.info("Request attribute '{}' was set to '{}'", WebConstants.REQUEST_ATTR_ZONEITEM, zoneItem); 069 } 070 return zoneItem; 071 } 072 073 private void _setSite(Request request, ZoneItem zoneItem) 074 { 075 String siteName = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SITE_NAME); 076 if (siteName == null) 077 { 078 siteName = zoneItem.getZone().getPage().getSite().getName(); 079 request.setAttribute(WebConstants.REQUEST_ATTR_SITE_NAME, siteName); 080 _generator._logger.info("Request attribute '{}' was set to '{}'", WebConstants.REQUEST_ATTR_SITE_NAME, siteName); 081 } 082 } 083 084 private void _setSkin(Request request, ZoneItem zoneItem) 085 { 086 String skinName = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SKIN_ID); 087 if (skinName == null) 088 { 089 skinName = zoneItem.getZone().getPage().getSite().getSkinId(); 090 request.setAttribute(WebConstants.REQUEST_ATTR_SKIN_ID, skinName); 091 _generator._logger.info("Request attribute '{}' was set to '{}'", WebConstants.REQUEST_ATTR_SKIN_ID, skinName); 092 } 093 } 094}