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