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.io.IOException;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.avalon.framework.service.Serviceable;
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.environment.ObjectModelHelper;
025import org.apache.cocoon.environment.Request;
026import org.apache.cocoon.environment.Response;
027import org.apache.cocoon.generation.AbstractGenerator;
028import org.apache.cocoon.xml.XMLUtils;
029import org.xml.sax.SAXException;
030
031import org.ametys.cms.transformation.xslt.ResolveURIComponent;
032import org.ametys.core.user.CurrentUserProvider;
033import org.ametys.core.user.UserIdentity;
034import org.ametys.plugins.explorer.resources.ModifiableResource;
035import org.ametys.runtime.config.Config;
036
037/**
038 * Reader for WebDAV LOCK method
039 */
040public class WebdavLockGenerator extends AbstractGenerator implements Serviceable
041{
042    private CurrentUserProvider _currentUserProvider;
043    
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
047    }
048    
049    public void generate() throws IOException, SAXException, ProcessingException
050    {
051        Request request = ObjectModelHelper.getRequest(objectModel);
052        ModifiableResource resource = (ModifiableResource) request.getAttribute("resource");
053        
054        Response response = ObjectModelHelper.getResponse(objectModel);
055        response.setHeader("Lock-Token", "<" + resource.getId() + ">");
056        response.setHeader("Content-Type", "application/xml; charset=utf-8");
057        
058        contentHandler.startDocument();
059        
060        contentHandler.startPrefixMapping("d", "DAV:");
061
062        XMLUtils.startElement(contentHandler, "d:prop");
063        XMLUtils.startElement(contentHandler, "d:lockdiscovery");
064        XMLUtils.startElement(contentHandler, "d:activelock");
065        
066        // <D:locktype><D:write/></D:locktype> 
067        XMLUtils.startElement(contentHandler, "d:locktype");
068        XMLUtils.createElement(contentHandler, "d:write");
069        XMLUtils.endElement(contentHandler, "d:locktype");
070        
071        // <D:lockscope><D:exclusive/></D:lockscope>
072        XMLUtils.startElement(contentHandler, "d:lockscope");
073        XMLUtils.createElement(contentHandler, "d:exclusive");
074        XMLUtils.endElement(contentHandler, "d:lockscope");
075        
076        // <D:depth>infinity</D:depth>
077        XMLUtils.createElement(contentHandler, "d:depth", "infinity");
078        
079        XMLUtils.startElement(contentHandler, "d:locktoken");
080        XMLUtils.createElement(contentHandler, "d:href", resource.getId());
081        XMLUtils.endElement(contentHandler, "d:locktoken");
082        
083        boolean unlockActive = Config.getInstance().getValueAsBoolean("content.unlocktimer.activate");
084        long unlockTimer = Config.getInstance().getValueAsLong("content.unlocktimer.period");
085        
086        XMLUtils.createElement(contentHandler, "d:timeout", unlockActive ? "Second-" + String.valueOf(3600 * unlockTimer) : "Infinite");
087
088        XMLUtils.createElement(contentHandler, "d:owner", UserIdentity.userIdentityToString(_currentUserProvider.getUser()));
089        
090        String href = ResolveURIComponent.resolve("webdav-project-resource", resource.getId(), false, true);
091        XMLUtils.startElement(contentHandler, "d:lockroot");
092        XMLUtils.createElement(contentHandler, "d:href", href);
093        XMLUtils.endElement(contentHandler, "d:lockroot");
094        
095        XMLUtils.endElement(contentHandler, "d:activelock");
096        XMLUtils.endElement(contentHandler, "d:lockdiscovery");
097        XMLUtils.endElement(contentHandler, "d:prop");
098        
099        contentHandler.endDocument();
100    }
101}