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.InputStream; 019import java.util.Date; 020import java.util.HashMap; 021import java.util.Map; 022 023import javax.jcr.Node; 024import javax.jcr.lock.Lock; 025import javax.jcr.lock.LockManager; 026import javax.servlet.http.HttpServletRequest; 027 028import org.apache.avalon.framework.parameters.Parameters; 029import org.apache.avalon.framework.service.ServiceException; 030import org.apache.avalon.framework.service.ServiceManager; 031import org.apache.avalon.framework.service.Serviceable; 032import org.apache.cocoon.acting.AbstractAction; 033import org.apache.cocoon.environment.ObjectModelHelper; 034import org.apache.cocoon.environment.Redirector; 035import org.apache.cocoon.environment.SourceResolver; 036import org.apache.cocoon.environment.http.HttpEnvironment; 037import org.apache.cocoon.environment.http.HttpResponse; 038 039import org.ametys.core.observation.Event; 040import org.ametys.core.observation.ObservationManager; 041import org.ametys.core.user.CurrentUserProvider; 042import org.ametys.core.user.UserIdentity; 043import org.ametys.plugins.explorer.ObservationConstants; 044import org.ametys.plugins.explorer.resources.ModifiableResource; 045import org.ametys.plugins.repository.AmetysObject; 046import org.ametys.plugins.repository.RepositoryConstants; 047import org.ametys.plugins.repository.jcr.JCRAmetysObject; 048import org.ametys.plugins.repository.lock.LockHelper; 049import org.ametys.plugins.repository.version.VersionableAmetysObject; 050 051/** 052 * Action for WebDAV PUT method 053 * 054 */ 055public class WebdavPutAction extends AbstractAction implements Serviceable 056{ 057 /** The current user provider */ 058 protected CurrentUserProvider _currentUserProvider; 059 /** Observer manager. */ 060 protected ObservationManager _observationManager; 061 062 @Override 063 public void service(ServiceManager smanager) throws ServiceException 064 { 065 _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE); 066 _observationManager = (ObservationManager) smanager.lookup(ObservationManager.ROLE); 067 } 068 069 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 070 { 071 HttpServletRequest request = (HttpServletRequest) objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT); 072 073 ModifiableResource resource = (ModifiableResource) request.getAttribute("resource"); 074 075 if (resource.isLocked() && !LockHelper.isLockOwner(resource, _currentUserProvider.getUser())) 076 { 077 HttpResponse response = (HttpResponse) ObjectModelHelper.getResponse(objectModel); 078 response.setStatus(423); 079 return null; 080 } 081 082 if (resource instanceof JCRAmetysObject) 083 { 084 Node node = ((JCRAmetysObject) resource).getNode(); 085 086 if (node.isLocked()) 087 { 088 LockManager lockManager = node.getSession().getWorkspace().getLockManager(); 089 090 Lock lock = lockManager.getLock(node.getPath()); 091 Node lockHolder = lock.getNode(); 092 093 lockManager.addLockToken(lockHolder.getProperty(RepositoryConstants.METADATA_LOCKTOKEN).getString()); 094 } 095 } 096 097 InputStream is = request.getInputStream(); 098 099 UserIdentity author = _currentUserProvider.getUser(); 100 String mimeType = resource.getMimeType(); 101 mimeType = mimeType == null ? "application/unknown" : mimeType; 102 103 resource.setData(is, mimeType, new Date(), author); 104 105 resource.saveChanges(); 106 _checkpoint(resource); 107 108 Map<String, Object> eventParams = new HashMap<>(); 109 110 eventParams.put(ObservationConstants.ARGS_ID, resource.getId()); 111 eventParams.put(ObservationConstants.ARGS_NAME, resource.getName()); 112 eventParams.put(ObservationConstants.ARGS_PATH, resource.getPath()); 113 eventParams.put(ObservationConstants.ARGS_RESOURCE_PATH, resource.getResourcePath()); 114 AmetysObject parent = resource.getParent(); 115 if (parent != null) 116 { 117 eventParams.put(ObservationConstants.ARGS_PARENT_ID, parent.getId()); 118 } 119 _observationManager.notify(new Event(ObservationConstants.EVENT_RESOURCE_UPDATED, _currentUserProvider.getUser(), eventParams)); 120 121 return EMPTY_MAP; 122 } 123 124 private void _checkpoint(ModifiableResource resource) 125 { 126 if (resource instanceof VersionableAmetysObject) 127 { 128 // Create first version 129 ((VersionableAmetysObject) resource).checkpoint(); 130 } 131 } 132}