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.explorer.resources.Resource;
027import org.ametys.plugins.repository.AmetysObject;
028import org.ametys.plugins.repository.AmetysObjectResolver;
029import org.ametys.web.URIPrefixHandler;
030import org.ametys.web.WebHelper;
031import org.ametys.web.explorer.ResourceHelper;
032import org.ametys.web.repository.site.Site;
033
034/**
035 * {@link URIResolver} for type "explorer". <br>
036 * These links point to a file from the resources explorer.
037 */
038public class ResourceURIResolver extends org.ametys.cms.transformation.ResourceURIResolver
039{
040    /** The URI prefix handler */
041    protected URIPrefixHandler _webPrefixHandler;
042    
043    @Override
044    public void contextualize(Context context) throws ContextException
045    {
046        _context = context;
047    }
048
049    @Override
050    public void service(ServiceManager manager) throws ServiceException
051    {
052        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
053        _webPrefixHandler = (URIPrefixHandler) manager.lookup(URIPrefixHandler.ROLE);
054    }
055    
056    @Override
057    protected String getUriPrefix (AmetysObject object, boolean download, boolean absolute, boolean internal)
058    {
059        String siteName = null;
060        if (object instanceof Resource)
061        {
062            Resource resource = (Resource) object;
063            siteName = _getSiteName(resource);
064        }
065        
066        String initialSiteName = (String) ContextHelper.getRequest(_context).getAttribute("initialSiteName");
067        boolean isInitialSite = initialSiteName == null || initialSiteName.equals(siteName);
068        
069        if (internal)
070        {
071            return "cocoon://" + siteName;
072        }
073        else if (absolute || !isInitialSite)
074        {
075            return _webPrefixHandler.getAbsoluteUriPrefix(siteName);
076        }
077        else
078        {
079            return _webPrefixHandler.getUriPrefix(siteName);
080        }
081    }
082    
083    @Override
084    protected String getResourcePath(Resource resource)
085    {
086        String fullPath = resource.getPath();
087        
088        if (fullPath.startsWith(ResourceHelper.SHARED_RESOURCE_PATH))
089        {
090            return fullPath.substring(ResourceHelper.SHARED_RESOURCE_PATH.length());
091        }
092        else
093        {
094            return super.getResourcePath(resource);
095        }
096    }
097    
098    @Override
099    protected String getRealPrefix (Resource resource, String prefix)
100    {
101        String fullPath = resource.getPath();
102        if (fullPath.startsWith(ResourceHelper.SHARED_RESOURCE_PATH))
103        {
104            // shared resource, URI should begin with "shared-"
105            // the resource path should also contain the shared collection's name
106            return "shared-" + prefix;
107        }
108        else
109        {
110            return prefix;
111        }
112    }
113    
114    /**
115     * Get the site name from resource
116     * @param resource the resource
117     * @return the site name
118     */
119    protected String _getSiteName (Resource resource)
120    {
121        Request request = ContextHelper.getRequest(_context);
122        
123        String fullPath = resource.getPath();
124        if (fullPath.startsWith(ResourceHelper.SHARED_RESOURCE_PATH))
125        {
126            return WebHelper.getSiteName(request);
127        }
128        else
129        {
130            AmetysObject parent = resource.getParent();
131            while (parent != null)
132            {
133                if (parent instanceof Site)
134                {
135                    return ((Site) parent).getName();
136                }
137                
138                parent = parent.getParent();
139            }
140            return null;
141        }
142    }
143   
144}