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.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.parameters.Parameters;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025import org.apache.cocoon.acting.AbstractAction;
026import org.apache.cocoon.environment.ObjectModelHelper;
027import org.apache.cocoon.environment.Redirector;
028import org.apache.cocoon.environment.Request;
029import org.apache.cocoon.environment.SourceResolver;
030
031import org.ametys.core.util.URIUtils;
032import org.ametys.plugins.repository.AmetysObject;
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        // Mostly unnecessary, except for the ';' character
054        // {@see org.ametys.plugins.workspaces.documents.WebdavProjectResourceURIResolver}
055        String decodedPath = URIUtils.decode(path);
056        
057        result.put("path", decodedPath);
058        
059        Request request = ObjectModelHelper.getRequest(objectModel);
060        Project project = (Project) request.getAttribute("project"); 
061        
062        try
063        {
064            AmetysObject resource = _resolver.resolveByPath(project.getPath() + "/ametys-internal:resources/" + decodedPath);
065            request.setAttribute("resource", resource);
066        }
067        catch (Exception e)
068        {
069            // does nothing
070        }
071        
072        return result;
073    }
074}