001/* 002 * Copyright 2010 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; 017 018import java.util.ArrayList; 019import java.util.List; 020import java.util.Locale; 021import java.util.Map; 022 023import org.apache.cocoon.environment.Request; 024import org.apache.commons.lang.StringUtils; 025 026import org.ametys.cms.content.GetContentAction; 027import org.ametys.core.observation.Event; 028import org.ametys.core.user.population.PopulationContextHelper; 029import org.ametys.plugins.repository.AmetysObject; 030import org.ametys.web.repository.SiteAwareAmetysObject; 031import org.ametys.web.repository.page.SitemapElement; 032import org.ametys.web.repository.page.ZoneItem; 033import org.ametys.web.repository.site.Site; 034 035/** 036 * Helper for web 037 */ 038public final class WebHelper 039{ 040 private WebHelper() 041 { 042 // Empty 043 } 044 045 /** 046 * Get the site name from the request. 047 * The site name is searched in the following order: the 'siteName' request parameter, the 'site' request attribute, the 'siteName' request attribute. 048 * @param request the request. Cannot be null. 049 * @return the site name or <code>null</code> if not found 050 */ 051 public static String getSiteName(Request request) 052 { 053 return getSiteName(request, null); 054 } 055 056 /** 057 * Get the site name from a object or current request. 058 * The site name is searched in the following order: the Ametys object, the 'siteName' request parameter, the 'site' request attribute, the 'siteName' request attribute. 059 * @param request the request. Cannot be null. 060 * @param object the object. Can be null. 061 * @return the site name or <code>null</code> if not found 062 */ 063 public static String getSiteName(Request request, AmetysObject object) 064 { 065 String siteName = null; 066 if (object != null && object instanceof SiteAwareAmetysObject) 067 { 068 siteName = ((SiteAwareAmetysObject) object).getSiteName(); 069 } 070 else 071 { 072 siteName = request.getParameter("siteName"); 073 074 if (siteName == null) 075 { 076 siteName = (String) request.getAttribute("siteName"); // From FO request 077 } 078 079 if (siteName == null) 080 { 081 siteName = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SITE_NAME); 082 } 083 084 if (siteName == null) 085 { 086 Site site = (Site) request.getAttribute(WebConstants.REQUEST_ATTR_SITE); 087 if (site != null) 088 { 089 siteName = site.getName(); 090 } 091 } 092 } 093 094 return siteName; 095 } 096 097 /** 098 * Retrieves the site from an observation {@link Event}.<br> 099 * This method search in the events' arguments to find :<br> 100 * <ul> 101 * <li><code>ObservationConstants.ARGS_SITE</code> 102 * <li><code>ObservationConstants.ARGS_PAGE</code> 103 * <li><code>ObservationConstants.ARGS_ZONE_ITEM</code> 104 * <li><code>ObservationConstants.ARGS_ACL_CONTEXT</code> 105 * <li><code>object</code> if it's instance of {@link SiteAwareAmetysObject} 106 * </ul> 107 * @param event the {@link Event}. 108 * @return the current site or null if not found. 109 */ 110 public static Site findSite(Event event) 111 { 112 Map<String, Object> args = event.getArguments(); 113 114 if (args.containsKey(ObservationConstants.ARGS_SITE)) 115 { 116 return (Site) args.get(ObservationConstants.ARGS_SITE); 117 } 118 else if (args.containsKey(ObservationConstants.ARGS_SITEMAP_ELEMENT)) 119 { 120 return ((SitemapElement) args.get(ObservationConstants.ARGS_SITEMAP_ELEMENT)).getSite(); 121 } 122 else if (args.containsKey(ObservationConstants.ARGS_ZONE_ITEM)) 123 { 124 return ((ZoneItem) args.get(ObservationConstants.ARGS_ZONE_ITEM)).getZone().getSitemapElement().getSite(); 125 } 126 else if (args.containsKey(org.ametys.core.ObservationConstants.ARGS_ACL_CONTEXT)) 127 { 128 Object object = args.get(org.ametys.core.ObservationConstants.ARGS_ACL_CONTEXT); 129 if (object != null && object instanceof SiteAwareAmetysObject) 130 { 131 return ((SiteAwareAmetysObject) object).getSite(); 132 } 133 } 134 else 135 { 136 AmetysObject ao = (AmetysObject) args.get("object"); 137 if (ao != null && ao instanceof SiteAwareAmetysObject) 138 { 139 return ((SiteAwareAmetysObject) ao).getSite(); 140 } 141 } 142 143 return null; 144 } 145 146 /** 147 * Set the population context attribute from the given site in the request 148 * @param request the request 149 * @param site the site. Can be null. In this case, take the site in the request 150 */ 151 public static void setPopulationContextAttribute(Request request, Site site) 152 { 153 String siteName = site != null ? site.getName() : getSiteName(request); 154 if (StringUtils.isNotBlank(siteName)) 155 { 156 // Set the site name into the request for the other components to be able to retrieve it. 157 request.setAttribute("siteName", siteName); 158 159 List<String> populationContexts = new ArrayList<>(); 160 161 // Set the population contexts to be able to get allowed users 162 populationContexts.add("/sites/" + siteName); 163 populationContexts.add("/sites-fo/" + siteName); 164 165 request.setAttribute(PopulationContextHelper.POPULATION_CONTEXTS_REQUEST_ATTR, populationContexts); 166 } 167 } 168 169 /** 170 * Find the language to use from request or default 171 * @param request The request 172 * @return The language found in "lang" request parameter of default language 173 */ 174 public static String findLanguage(Request request) 175 { 176 String parameter = request.getParameter("lang"); 177 178 if (StringUtils.isBlank(parameter)) 179 { 180 Object attributeLang = request.getAttribute("lang"); 181 182 if (attributeLang != null) 183 { 184 parameter = (String) attributeLang; 185 } 186 else 187 { 188 attributeLang = request.getAttribute(WebConstants.REQUEST_ATTR_SITEMAP_NAME); 189 if (attributeLang != null) 190 { 191 parameter = (String) attributeLang; 192 } 193 else 194 { 195 attributeLang = request.getAttribute(GetContentAction.RESULT_RENDERINGLANGUAGE); 196 if (attributeLang != null) 197 { 198 parameter = (String) attributeLang; 199 } 200 else 201 { 202 parameter = Locale.getDefault().getLanguage(); 203 } 204 } 205 } 206 } 207 208 return parameter; 209 } 210}