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