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.Map;
019
020import org.apache.avalon.framework.parameters.Parameters;
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024import org.apache.cocoon.acting.AbstractAction;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.environment.Redirector;
027import org.apache.cocoon.environment.Request;
028import org.apache.cocoon.environment.SourceResolver;
029import org.apache.cocoon.environment.http.HttpResponse;
030
031import org.ametys.core.user.CurrentUserProvider;
032import org.ametys.core.user.UserIdentity;
033import org.ametys.plugins.explorer.resources.ModifiableResource;
034import org.ametys.plugins.repository.AmetysObjectResolver;
035
036/**
037 * Action for WebDAV UNLOCK method
038 */
039public class WebdavUnlockAction extends AbstractAction implements Serviceable
040{
041    /** The Ametys object resolver */
042    protected AmetysObjectResolver _resolver;
043    
044    /** The current user provider */
045    protected CurrentUserProvider _currentUserProvider;
046    
047    @Override
048    public void service(ServiceManager smanager) throws ServiceException
049    {
050        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
051        _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
052    }
053    
054    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
055    {
056        Request request = ObjectModelHelper.getRequest(objectModel);
057        ModifiableResource resource = (ModifiableResource) request.getAttribute("resource");
058        
059        UserIdentity lockOwner = resource.getLockOwner();
060        UserIdentity currentUser = _currentUserProvider.getUser();
061        
062        HttpResponse response = (HttpResponse) ObjectModelHelper.getResponse(objectModel);
063        
064        if (!resource.isLocked())
065        {
066            response.setStatus(409);
067            return null;
068        }
069        else if (!currentUser.equals(lockOwner))
070        {
071            response.setStatus(403);
072            return null;
073        }
074        else
075        {
076            response.setStatus(204);
077            resource.unlock();
078        }
079
080        return EMPTY_MAP;
081    }
082}