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    protected String getUriPrefix (AmetysObject object, boolean download, boolean absolute, boolean internal)
054    {
055        Request request = ContextHelper.getRequest(_context);
056        
057        String siteName = WebHelper.getSiteName(request, object);
058        
059        String initialSiteName = (String) ContextHelper.getRequest(_context).getAttribute("initialSiteName");
060        boolean isInitialSite = initialSiteName == null || initialSiteName.equals(siteName);
061        
062        if (internal)
063        {
064            return "cocoon://" + siteName;
065        }
066        else if (absolute || !isInitialSite)
067        {
068            return _webPrefixHandler.getAbsoluteUriPrefix(siteName);
069        }
070        else
071        {
072            return _webPrefixHandler.getUriPrefix(siteName);
073        }
074    }
075}