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.transformation.URIResolver;
024import org.ametys.plugins.repository.metadata.MetadataAwareAmetysObject;
025import org.ametys.web.URIPrefixHandler;
026import org.ametys.web.renderingcontext.RenderingContext;
027import org.ametys.web.renderingcontext.RenderingContextHandler;
028import org.ametys.web.repository.SiteAwareAmetysObject;
029import org.ametys.web.repository.site.Site;
030import org.ametys.web.repository.site.SiteManager;
031
032/**
033 * {@link URIResolver} for type "metadata".<br>
034 * These links or images point to a file from the metadata of the current Content.
035 */
036public class MetadataURIResolver extends org.ametys.cms.transformation.MetadataURIResolver
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, MetadataAwareAmetysObject object, boolean download, boolean absolute, boolean internal)
055    {
056        Request request = ContextHelper.getRequest(_context);
057        
058        String siteName = null;
059        if (object instanceof SiteAwareAmetysObject)
060        {
061            siteName = ((SiteAwareAmetysObject) object).getSiteName();
062        }
063        else
064        {
065            siteName = (String) request.getAttribute("siteName");
066            
067            if (siteName == null)
068            {
069                siteName = (String) request.getAttribute("site");
070            }
071        }
072        
073        if (internal)
074        {
075            return "cocoon://" + siteName + path;
076        }
077        else if (absolute)
078        {
079            if (_renderingContexthandler.getRenderingContext() == RenderingContext.FRONT)
080            {
081                Site site = _siteManager.getSite(siteName);
082                
083                String[] aliases = site.getUrlAliases();
084                return aliases[Math.abs(path.hashCode()) % aliases.length] + path;
085            }
086            
087            return _prefixHandler.getAbsoluteUriPrefix() + "/" + siteName + path; 
088        }
089        else
090        {
091            return _prefixHandler.getUriPrefix(siteName) + path;
092        }
093    }
094}