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.HashSet;
019import java.util.Set;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.commons.lang.StringUtils;
024
025import org.ametys.cms.transformation.URIResolver;
026import org.ametys.core.authentication.token.AuthenticationTokenManager;
027import org.ametys.core.user.CurrentUserProvider;
028import org.ametys.core.user.UserIdentity;
029import org.ametys.plugins.explorer.resources.Resource;
030import org.ametys.plugins.explorer.resources.ResourceCollection;
031import org.ametys.plugins.repository.AmetysObject;
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    /** Current User Provider */
044    protected CurrentUserProvider _currentUserProvider;
045    
046    @Override
047    public void service(ServiceManager manager) throws ServiceException
048    {
049        super.service(manager);
050        _authenticationTokenManager = (AuthenticationTokenManager) manager.lookup(AuthenticationTokenManager.ROLE);
051        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
052    }
053    
054    @Override
055    public String getType()
056    {
057        return "webdav-project-resource";
058    }
059    
060    @Override
061    protected String getUriPrefix(AmetysObject object, boolean download, boolean absolute, boolean internal)
062    {
063        Project project = null;
064        String projectName = null;
065        String siteName = null;
066
067        UserIdentity user = _currentUserProvider.getUser();
068        long duration = 12 * 60 * 60; // 12h
069        boolean autoRenew = true; // Auto Renew the token to be able to continue to edit it
070        
071        Set<String> contexts = new HashSet<>();
072        String path = getPath(object); // Token only usable on this resource
073        path = StringUtils.removeStart(path, "/"); // / at the beginning is removed because it is used in a uri path
074        contexts.add(path);
075        
076        if (object instanceof Resource)
077        {
078            String parentPath = getPath(object.getParent());
079            parentPath = StringUtils.removeStart(parentPath, "/").concat("/"); // / at the beginning is removed because it is used in a uri path, / at the end to avoid identical folders
080            contexts.add(parentPath);
081        }
082        String type = "Workspaces-Webdav";
083        String comment = "Token created to modify content : '" + object.getId() + "'";
084        String token = _authenticationTokenManager.generateToken(user, duration, autoRenew, null, contexts, type, comment);
085        
086        project = _getProject(object);
087        projectName = project.getName();
088
089        siteName = project.getSite().getName();
090        
091        if (internal)
092        {
093            return "cocoon://_workspaces/dav/" + projectName + "/" + token;
094        }
095        else if (absolute)
096        {
097            return _webPrefixHandler.getAbsoluteUriPrefix(siteName) + "/_workspaces/dav/" + projectName + "/" + token;
098        }
099        else
100        {
101            return _webPrefixHandler.getUriPrefix(siteName) + "/_workspaces/dav/" + projectName + "/" + token;
102        }
103    }
104    
105    /**
106     * Get the path of a {@link Resource} or a {@link ResourceCollection}
107     * @param resource the resource to work with
108     * @return the path
109     */
110    protected String getPath(AmetysObject resource)
111    {
112        String path = null;
113        if (resource instanceof Resource)
114        {
115            path = getResourcePath((Resource) resource);
116        }
117        else if (resource instanceof ResourceCollection)
118        {
119            path = ((ResourceCollection) resource).getExplorerPath();
120        }
121        else
122        {
123            getLogger().error("Trying to access to something that is not a Resource or a ResourceCollection : " + resource.getId());
124        }
125        return path;
126    }
127    
128    @Override
129    protected String getRealPrefix(Resource resource, String prefix)
130    {
131        return null;
132    }
133}