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.ArrayList;
019import java.util.List;
020import java.util.Map;
021
022import javax.servlet.http.HttpServletRequest;
023
024import org.apache.avalon.framework.parameters.Parameters;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.avalon.framework.service.Serviceable;
028import org.apache.cocoon.acting.AbstractAction;
029import org.apache.cocoon.environment.ObjectModelHelper;
030import org.apache.cocoon.environment.Redirector;
031import org.apache.cocoon.environment.SourceResolver;
032import org.apache.cocoon.environment.http.HttpEnvironment;
033import org.apache.cocoon.environment.http.HttpResponse;
034
035import org.ametys.core.user.CurrentUserProvider;
036import org.ametys.plugins.explorer.resources.ModifiableResourceCollection;
037import org.ametys.plugins.explorer.resources.actions.ExplorerResourcesDAO;
038import org.ametys.plugins.repository.AmetysObjectResolver;
039import org.ametys.plugins.workspaces.project.objects.Project;
040
041/**
042 * Action for WebDAV PUT method
043 *
044 */
045public class WebdavMkcolAction extends AbstractAction implements Serviceable
046{
047    
048    private AmetysObjectResolver _resolver;
049
050    private ExplorerResourcesDAO _resourcesDAO;
051    
052    private CurrentUserProvider _currentUserProvider;
053
054    @Override
055    public void service(ServiceManager smanager) throws ServiceException
056    {
057        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
058        _resourcesDAO = (ExplorerResourcesDAO) smanager.lookup(ExplorerResourcesDAO.ROLE);
059        _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
060    }
061
062    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
063    {
064        HttpServletRequest request = (HttpServletRequest) objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT);
065
066        String path = parameters.getParameter("path");
067        Project project = (Project) request.getAttribute("project"); 
068        ModifiableResourceCollection parent = _resolver.resolveByPath(project.getPath() + "/ametys-internal:resources/");
069        
070        //list of possible errors, that will not be used because the only possible error is "lock", and they can not be locked
071        List<String> errors = new ArrayList<>();
072        
073        for (String folder : path.split("/"))
074        {
075            if (folder.isEmpty())
076            {
077                continue;
078            }
079            if (!parent.hasChild(folder))
080            {
081                _resourcesDAO.addResourceCollection(parent, folder, false, errors);
082            }
083            if (!errors.isEmpty())
084            {
085                getLogger().error("User '" + _currentUserProvider.getUser() + "' try create a collection named '" + folder + "' in parent '" + parent.getId() + "' but it failed with message : " + errors.get(0));
086                HttpResponse response = (HttpResponse) ObjectModelHelper.getResponse(objectModel);
087                response.setStatus(423); //locked
088                return EMPTY_MAP;
089            }
090            parent = parent.getChild(folder);
091        }
092        return EMPTY_MAP;
093    }
094}