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