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.editor; 017 018import org.apache.avalon.framework.service.ServiceException; 019import org.apache.avalon.framework.service.ServiceManager; 020import org.apache.cocoon.components.ContextHelper; 021import org.apache.cocoon.environment.Request; 022import org.apache.commons.lang3.StringUtils; 023 024import org.ametys.cms.transformation.URIResolver; 025import org.ametys.plugins.repository.data.ametysobject.ModelAwareDataAwareAmetysObject; 026import org.ametys.web.URIPrefixHandler; 027import org.ametys.web.WebHelper; 028import org.ametys.web.renderingcontext.RenderingContext; 029import org.ametys.web.renderingcontext.RenderingContextHandler; 030import org.ametys.web.repository.site.Site; 031import org.ametys.web.repository.site.SiteManager; 032 033/** 034 * {@link URIResolver} for type "attribute".<br> 035 * These links or images point to a file from the attribute of the current Content. 036 */ 037public class AttributeURIResolver extends org.ametys.cms.transformation.AttributeURIResolver 038{ 039 /** The URI prefix handler */ 040 protected URIPrefixHandler _prefixHandler; 041 042 private RenderingContextHandler _renderingContexthandler; 043 private SiteManager _siteManager; 044 045 @Override 046 public void service(ServiceManager manager) throws ServiceException 047 { 048 super.service(manager); 049 _prefixHandler = (URIPrefixHandler) manager.lookup(URIPrefixHandler.ROLE); 050 _renderingContexthandler = (RenderingContextHandler) manager.lookup(RenderingContextHandler.ROLE); 051 _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE); 052 } 053 054 @Override 055 protected String getUri(String path, ModelAwareDataAwareAmetysObject object, boolean download, boolean absolute, boolean internal) 056 { 057 Request request = ContextHelper.getRequest(_context); 058 059 String siteName = WebHelper.getSiteName(request, object); 060 061 if (StringUtils.isEmpty(siteName)) 062 { 063 return super.getUri(path, object, download, absolute, internal); 064 } 065 066 if (internal) 067 { 068 return "cocoon://" + siteName + path; 069 } 070 else if (absolute) 071 { 072 if (_renderingContexthandler.getRenderingContext() == RenderingContext.FRONT) 073 { 074 Site site = _siteManager.getSite(siteName); 075 076 String[] aliases = site.getUrlAliases(); 077 return aliases[Math.abs(path.hashCode()) % aliases.length] + path; 078 } 079 080 return _prefixHandler.getAbsoluteUriPrefix() + "/" + siteName + path; 081 } 082 else 083 { 084 return _prefixHandler.getUriPrefix(siteName) + path; 085 } 086 } 087}