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