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.dav;
017
018import java.net.URLDecoder;
019import java.util.HashMap;
020import java.util.Map;
021
022import org.apache.avalon.framework.parameters.Parameters;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026import org.apache.cocoon.acting.AbstractAction;
027import org.apache.cocoon.environment.ObjectModelHelper;
028import org.apache.cocoon.environment.Redirector;
029import org.apache.cocoon.environment.Request;
030import org.apache.cocoon.environment.SourceResolver;
031
032import org.ametys.plugins.explorer.resources.ModifiableResource;
033import org.ametys.plugins.repository.AmetysObjectResolver;
034import org.ametys.plugins.workspaces.project.objects.Project;
035
036/**
037 * Decode the path to get the resource and set the resource in request attributes
038 *
039 */
040public class DecodePathAction extends AbstractAction implements Serviceable
041{
042    private AmetysObjectResolver _resolver;
043    
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
047    }
048    
049    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String path, Parameters parameters) throws Exception
050    {
051        Map<String, String> result = new HashMap<>();
052        
053        StringBuffer sb = new StringBuffer();
054        
055        String[] parts = path.split("/");
056        for (String part : parts)
057        {
058            // Should be unnecessary : the webdav url are encoded only once (@see org.ametys.plugins.workspaces.documents.WebdavProjectResourceURIResolver}
059            sb.append("/");
060            sb.append(URLDecoder.decode(part, "utf-8"));
061        }
062        
063        String decodedPath = sb.toString();
064        
065        result.put("path", decodedPath);
066        
067        Request request = ObjectModelHelper.getRequest(objectModel);
068        Project project = (Project) request.getAttribute("project"); 
069        
070        try
071        {
072            ModifiableResource resource = _resolver.resolveByPath(project.getPath() + "/ametys-internal:resources/" + decodedPath);
073            request.setAttribute("resource", resource);
074        }
075        catch (Exception e)
076        {
077            // does nothing
078        }
079        
080        return result;
081    }
082}