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.context.Context;
019import org.apache.avalon.framework.context.ContextException;
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.cocoon.components.ContextHelper;
023import org.apache.cocoon.environment.Request;
024
025import org.ametys.cms.transformation.URIResolver;
026import org.ametys.plugins.repository.AmetysObject;
027import org.ametys.plugins.repository.AmetysObjectResolver;
028import org.ametys.web.URIPrefixHandler;
029import org.ametys.web.repository.SiteAwareAmetysObject;
030
031/**
032 * {@link URIResolver} for type "attachment-content".<br>
033 * These links point to a file from the attachments of the current Content.
034 */
035public class AttachmentURIResolver extends org.ametys.cms.transformation.AttachmentURIResolver
036{
037    /** The URI prefix handler */
038    protected URIPrefixHandler _webPrefixHandler;
039    
040    @Override
041    public void contextualize(Context context) throws ContextException
042    {
043        _context = context;
044    }
045
046    @Override
047    public void service(ServiceManager manager) throws ServiceException
048    {
049        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
050        _webPrefixHandler = (URIPrefixHandler) manager.lookup(URIPrefixHandler.ROLE);
051    }
052    
053    @Override
054    public String getType()
055    {
056        return "attachment-content";
057    }
058    
059    @Override
060    protected String getUriPrefix (AmetysObject object, boolean download, boolean absolute, boolean internal)
061    {
062        Request request = ContextHelper.getRequest(_context);
063        
064        String siteName = null;
065        if (object instanceof SiteAwareAmetysObject)
066        {
067            siteName = ((SiteAwareAmetysObject) object).getSiteName();
068        }
069        else
070        {
071            siteName = (String) request.getAttribute("siteName");
072            
073            if (siteName == null)
074            {
075                siteName = (String) request.getAttribute("site");
076            }
077        }
078        
079        String initialSiteName = (String) ContextHelper.getRequest(_context).getAttribute("initialSiteName");
080        boolean isInitialSite = initialSiteName == null || initialSiteName.equals(siteName);
081        
082        if (internal)
083        {
084            return "cocoon://" + siteName;
085        }
086        else if (absolute || !isInitialSite)
087        {
088            return _webPrefixHandler.getAbsoluteUriPrefix(siteName);
089        }
090        else
091        {
092            return _webPrefixHandler.getUriPrefix(siteName);
093        }
094    }
095}