001/*
002 *  Copyright 2017 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.plugins.workspaces.documents;
017
018import java.util.Collections;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.commons.lang.StringUtils;
023
024import org.ametys.cms.transformation.URIResolver;
025import org.ametys.core.authentication.token.AuthenticationTokenManager;
026import org.ametys.core.util.URLEncoder;
027import org.ametys.plugins.explorer.resources.Resource;
028import org.ametys.plugins.explorer.resources.ResourceCollection;
029import org.ametys.plugins.repository.AmetysObject;
030import org.ametys.plugins.repository.UnknownAmetysObjectException;
031import org.ametys.plugins.repository.version.VersionableAmetysObject;
032import org.ametys.plugins.workspaces.project.objects.Project;
033
034/**
035 * {@link URIResolver} for webdav "project-resource". <br>
036 * These links point to a file from the resources of a project through webdav
037 */
038public class WebdavProjectResourceURIResolver extends ProjectResourceURIResolver
039{
040    /** The authentication token manager */
041    protected AuthenticationTokenManager _authenticationTokenManager;
042    
043    @Override
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        super.service(manager);
047        _authenticationTokenManager = (AuthenticationTokenManager) manager.lookup(AuthenticationTokenManager.ROLE);
048    }
049    
050    @Override
051    public String getType()
052    {
053        return "webdav-project-resource";
054    }
055    
056    // Override to single encoded instead of twice
057    @Override
058    protected String _resolve(String uri, boolean download, boolean absolute, boolean internal, String prefix, String suffix)
059    {
060        String version = null;
061        String path;
062        AmetysObject resource = null;
063        try
064        {
065            String resourceId = uri;
066            int i = uri.indexOf(";");
067            if (i != -1)
068            {
069                resourceId = uri.substring(0, i);
070                version = uri.substring(i + 1);
071            }
072            
073            resource = _resolver.resolveById(resourceId);
074            if (resource instanceof Resource)
075            {
076                path = getResourcePath((Resource) resource);
077            }
078            else if (resource instanceof ResourceCollection)
079            {
080                path = ((ResourceCollection) resource).getExplorerPath();
081            }
082            else
083            {
084                getLogger().error("Trying to access to something that is not a Resource or a ResourceCollection : " + resource.getId());
085                return "";
086            }
087        }
088        catch (UnknownAmetysObjectException e)
089        {
090            getLogger().warn("Link to unexisting resource " + uri);
091            return "";
092        }
093        
094        int i = path.lastIndexOf(".");
095        String extension = i != -1 ? path.substring(i) : null;
096        path = i != -1 ? path.substring(0, i) : path; 
097        
098        StringBuilder result = new StringBuilder();
099        
100        result.append(getUriPrefix(resource, download, absolute, internal));
101
102        String realPrefix = null;
103        if (resource instanceof Resource)
104        {
105            realPrefix = getRealPrefix((Resource) resource, prefix);
106        }
107
108        if (StringUtils.isNotEmpty(realPrefix))
109        {
110            result.append("/_").append(realPrefix);
111        }
112        
113        if (StringUtils.isNotEmpty(version) && resource instanceof VersionableAmetysObject)
114        {
115            result.append("/__version").append(version);
116        }
117        
118        result.append(path);
119        
120        if (suffix != null)
121        {
122            result.append(suffix);
123        }
124        
125        if (extension != null)
126        {
127            result.append(extension);
128        }
129        
130        return URLEncoder.encodeURI(result.toString(), download ? Collections.singletonMap("download", "true") : null);
131    }
132    
133    @Override
134    protected String getUriPrefix(AmetysObject object, boolean download, boolean absolute, boolean internal)
135    {
136        Project project = null;
137        String projectName = null;
138        String siteName = null;
139        
140        String token = _authenticationTokenManager.generateToken(60, "Workspaces-Webdav", null);
141        
142        project = _getProject(object);
143        projectName = project.getName();
144
145        siteName = project.getSites().iterator().next().getName();
146        
147        if (internal)
148        {
149            return "cocoon://_workspaces/dav/" + projectName + "/" + token;
150        }
151        else if (absolute)
152        {
153            return _webPrefixHandler.getAbsoluteUriPrefix(siteName) + "/_workspaces/dav/" + projectName + "/" + token;
154        }
155        else
156        {
157            return _webPrefixHandler.getUriPrefix(siteName) + "/_workspaces/dav/" + projectName + "/" + token;
158        }
159    }
160    
161    @Override
162    protected String getRealPrefix(Resource resource, String prefix)
163    {
164        return null;
165    }
166}