1 package org.apache.maven.archiva.webdav;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.List;
29 import javax.servlet.http.HttpServletResponse;
31 import org.apache.commons.io.FileUtils;
32 import org.apache.commons.io.IOUtils;
33 import org.apache.jackrabbit.util.Text;
34 import org.apache.jackrabbit.webdav.DavException;
35 import org.apache.jackrabbit.webdav.DavResource;
36 import org.apache.jackrabbit.webdav.DavResourceFactory;
37 import org.apache.jackrabbit.webdav.DavResourceIterator;
38 import org.apache.jackrabbit.webdav.DavResourceIteratorImpl;
39 import org.apache.jackrabbit.webdav.DavResourceLocator;
40 import org.apache.jackrabbit.webdav.DavServletResponse;
41 import org.apache.jackrabbit.webdav.DavSession;
42 import org.apache.jackrabbit.webdav.MultiStatusResponse;
43 import org.apache.jackrabbit.webdav.io.InputContext;
44 import org.apache.jackrabbit.webdav.io.OutputContext;
45 import org.apache.jackrabbit.webdav.lock.ActiveLock;
46 import org.apache.jackrabbit.webdav.lock.LockInfo;
47 import org.apache.jackrabbit.webdav.lock.LockManager;
48 import org.apache.jackrabbit.webdav.lock.Scope;
49 import org.apache.jackrabbit.webdav.lock.Type;
50 import org.apache.jackrabbit.webdav.property.DavProperty;
51 import org.apache.jackrabbit.webdav.property.DavPropertyName;
52 import org.apache.jackrabbit.webdav.property.DavPropertyNameSet;
53 import org.apache.jackrabbit.webdav.property.DavPropertySet;
54 import org.apache.jackrabbit.webdav.property.DefaultDavProperty;
55 import org.apache.jackrabbit.webdav.property.ResourceType;
56 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
57 import org.apache.maven.archiva.repository.audit.AuditEvent;
58 import org.apache.maven.archiva.repository.audit.AuditListener;
59 import org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers;
60 import org.apache.maven.archiva.security.ArchivaXworkUser;
61 import org.apache.maven.archiva.webdav.util.IndexWriter;
62 import org.apache.maven.archiva.webdav.util.MimeTypes;
63 import org.joda.time.DateTime;
64 import org.joda.time.format.DateTimeFormatter;
65 import org.joda.time.format.ISODateTimeFormat;
67 import com.opensymphony.xwork.ActionContext;
70 * @author <a href="mailto:james@atlassian.com">James William Dumay</a> Portions from the Apache Jackrabbit Project
72 public class ArchivaDavResource
73 implements DavResource
75 public static final String HIDDEN_PATH_PREFIX = ".";
77 private final ArchivaDavResourceLocator locator;
79 private final DavResourceFactory factory;
81 private final File localResource;
83 private final String logicalResource;
85 private DavPropertySet properties = null;
87 private LockManager lockManager;
89 private final DavSession session;
91 private String remoteAddr;
93 private final ManagedRepositoryConfiguration repository;
95 private final RepositoryContentConsumers consumers;
97 private final MimeTypes mimeTypes;
99 private List<AuditListener> auditListeners;
101 private ArchivaXworkUser archivaXworkUser;
103 public ArchivaDavResource( String localResource, String logicalResource, ManagedRepositoryConfiguration repository,
104 DavSession session, ArchivaDavResourceLocator locator, DavResourceFactory factory,
105 MimeTypes mimeTypes, List<AuditListener> auditListeners,
106 RepositoryContentConsumers consumers, ArchivaXworkUser archivaXworkUser )
108 this.localResource = new File( localResource );
109 this.logicalResource = logicalResource;
110 this.locator = locator;
111 this.factory = factory;
112 this.session = session;
113 this.archivaXworkUser = archivaXworkUser;
115 // TODO: push into locator as well as moving any references out of the resource factory
116 this.repository = repository;
118 // TODO: these should be pushed into the repository layer, along with the physical file operations in this class
119 this.mimeTypes = mimeTypes;
120 this.consumers = consumers;
121 this.auditListeners = auditListeners;
124 public ArchivaDavResource( String localResource, String logicalResource, ManagedRepositoryConfiguration repository,
125 String remoteAddr, DavSession session, ArchivaDavResourceLocator locator,
126 DavResourceFactory factory, MimeTypes mimeTypes, List<AuditListener> auditListeners,
127 RepositoryContentConsumers consumers, ArchivaXworkUser archivaXworkUser )
129 this( localResource, logicalResource, repository, session, locator, factory, mimeTypes, auditListeners,
130 consumers, archivaXworkUser );
132 this.remoteAddr = remoteAddr;
135 public String getComplianceClass()
137 return COMPLIANCE_CLASS;
140 public String getSupportedMethods()
145 public boolean exists()
147 return localResource.exists();
150 public boolean isCollection()
152 return localResource.isDirectory();
155 public String getDisplayName()
157 String resPath = getResourcePath();
158 return ( resPath != null ) ? Text.getName( resPath ) : resPath;
161 public DavResourceLocator getLocator()
166 public File getLocalResource()
168 return localResource;
171 public String getResourcePath()
173 return locator.getResourcePath();
176 public String getHref()
178 return locator.getHref( isCollection() );
181 public long getModificationTime()
183 return localResource.lastModified();
186 public void spool( OutputContext outputContext )
189 if ( !isCollection())
191 outputContext.setContentLength( localResource.length() );
192 outputContext.setContentType( mimeTypes.getMimeType( localResource.getName() ) );
195 if ( !isCollection() && outputContext.hasStream() )
197 FileInputStream is = null;
200 // Write content to stream
201 is = new FileInputStream( localResource );
202 IOUtils.copy( is, outputContext.getOutputStream() );
206 IOUtils.closeQuietly( is );
209 else if (outputContext.hasStream())
211 IndexWriter writer = new IndexWriter( this, localResource, logicalResource );
212 writer.write( outputContext );
216 public DavPropertyName[] getPropertyNames()
218 return getProperties().getPropertyNames();
221 public DavProperty getProperty( DavPropertyName name )
223 return getProperties().get( name );
226 public DavPropertySet getProperties()
228 return initProperties();
231 public void setProperty( DavProperty property )
236 public void removeProperty( DavPropertyName propertyName )
241 public MultiStatusResponse alterProperties( DavPropertySet setProperties, DavPropertyNameSet removePropertyNames )
247 @SuppressWarnings("unchecked")
248 public MultiStatusResponse alterProperties( List changeList )
254 public DavResource getCollection()
256 DavResource parent = null;
257 if ( getResourcePath() != null && !getResourcePath().equals( "/" ) )
259 String parentPath = Text.getRelativeParent( getResourcePath(), 1 );
260 if ( parentPath.equals( "" ) )
264 DavResourceLocator parentloc = locator.getFactory().createResourceLocator( locator.getPrefix(), parentPath );
267 parent = factory.createResource( parentloc, session );
269 catch ( DavException e )
277 public void addMember( DavResource resource, InputContext inputContext )
280 File localFile = new File( localResource, resource.getDisplayName() );
281 boolean exists = localFile.exists();
283 if ( isCollection() && inputContext.hasStream() ) // New File
285 FileOutputStream stream = null;
288 stream = new FileOutputStream( localFile );
289 IOUtils.copy( inputContext.getInputStream(), stream );
291 catch ( IOException e )
293 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
297 IOUtils.closeQuietly( stream );
300 if ( inputContext.getContentLength() != localFile.length() )
302 FileUtils.deleteQuietly( localFile );
304 throw new DavException( HttpServletResponse.SC_BAD_REQUEST, "Content Header length was " +
305 inputContext.getContentLength() + " but was " + localFile.length() );
308 // Just-in-time update of the index and database by executing the consumers for this artifact
309 consumers.executeConsumers( repository, localFile );
311 triggerAuditEvent( resource, exists ? AuditEvent.MODIFY_FILE : AuditEvent.CREATE_FILE );
313 else if ( !inputContext.hasStream() && isCollection() ) // New directory
317 triggerAuditEvent( resource, AuditEvent.CREATE_DIR );
321 throw new DavException( HttpServletResponse.SC_BAD_REQUEST, "Could not write member " +
322 resource.getResourcePath() + " at " + getResourcePath() );
326 public DavResourceIterator getMembers()
328 List<DavResource> list = new ArrayList<DavResource>();
329 if ( exists() && isCollection() )
331 for ( String item : localResource.list() )
335 if ( !item.startsWith( HIDDEN_PATH_PREFIX ) )
337 String path = locator.getResourcePath() + '/' + item;
338 DavResourceLocator resourceLocator =
339 locator.getFactory().createResourceLocator( locator.getPrefix(), path );
340 DavResource resource = factory.createResource( resourceLocator, session );
341 if ( resource != null )
343 list.add( resource );
347 catch ( DavException e )
353 return new DavResourceIteratorImpl( list );
356 public void removeMember( DavResource member )
359 File resource = checkDavResourceIsArchivaDavResource( member ).getLocalResource();
361 if ( resource.exists() )
365 if ( resource.isDirectory() )
367 FileUtils.deleteDirectory( resource );
369 triggerAuditEvent( member, AuditEvent.REMOVE_DIR );
373 if ( !resource.delete() )
375 throw new IOException( "Could not remove file" );
378 triggerAuditEvent( member, AuditEvent.REMOVE_FILE );
381 catch ( IOException e )
383 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
388 throw new DavException( HttpServletResponse.SC_NOT_FOUND );
392 private void triggerAuditEvent( DavResource member, String event ) throws DavException
394 String path = logicalResource + "/" + member.getDisplayName();
396 triggerAuditEvent( checkDavResourceIsArchivaDavResource( member ).remoteAddr, locator.getRepositoryId(), path,
400 public void move( DavResource destination )
405 throw new DavException( HttpServletResponse.SC_NOT_FOUND, "Resource to copy does not exist." );
410 ArchivaDavResource resource = checkDavResourceIsArchivaDavResource( destination );
411 if ( isCollection() )
413 FileUtils.moveDirectory( getLocalResource(), resource.getLocalResource() );
415 triggerAuditEvent( remoteAddr, locator.getRepositoryId(), logicalResource, AuditEvent.MOVE_DIRECTORY );
419 FileUtils.moveFile( getLocalResource(), resource.getLocalResource() );
421 triggerAuditEvent( remoteAddr, locator.getRepositoryId(), logicalResource, AuditEvent.MOVE_FILE );
424 catch ( IOException e )
426 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
430 public void copy( DavResource destination, boolean shallow )
435 throw new DavException( HttpServletResponse.SC_NOT_FOUND, "Resource to copy does not exist." );
438 if ( shallow && isCollection() )
440 throw new DavException( DavServletResponse.SC_FORBIDDEN, "Unable to perform shallow copy for collection" );
445 ArchivaDavResource resource = checkDavResourceIsArchivaDavResource( destination );
446 if ( isCollection() )
448 FileUtils.copyDirectory( getLocalResource(), resource.getLocalResource() );
450 triggerAuditEvent( remoteAddr, locator.getRepositoryId(), logicalResource, AuditEvent.COPY_DIRECTORY );
454 FileUtils.copyFile( getLocalResource(), resource.getLocalResource() );
456 triggerAuditEvent( remoteAddr, locator.getRepositoryId(), logicalResource, AuditEvent.COPY_FILE );
459 catch ( IOException e )
461 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
465 public boolean isLockable( Type type, Scope scope )
467 return Type.WRITE.equals(type) && Scope.EXCLUSIVE.equals(scope);
470 public boolean hasLock( Type type, Scope scope )
472 return getLock(type, scope) != null;
475 public ActiveLock getLock( Type type, Scope scope )
477 ActiveLock lock = null;
478 if (exists() && Type.WRITE.equals(type) && Scope.EXCLUSIVE.equals(scope))
480 lock = lockManager.getLock(type, scope, this);
485 public ActiveLock[] getLocks()
487 ActiveLock writeLock = getLock(Type.WRITE, Scope.EXCLUSIVE);
488 return (writeLock != null) ? new ActiveLock[]{writeLock} : new ActiveLock[0];
491 public ActiveLock lock( LockInfo lockInfo )
494 ActiveLock lock = null;
495 if (isLockable(lockInfo.getType(), lockInfo.getScope()))
497 lock = lockManager.createLock(lockInfo, this);
501 throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED, "Unsupported lock type or scope.");
506 public ActiveLock refreshLock( LockInfo lockInfo, String lockToken )
510 throw new DavException(DavServletResponse.SC_NOT_FOUND);
512 ActiveLock lock = getLock(lockInfo.getType(), lockInfo.getScope());
514 throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED, "No lock with the given type/scope present on resource " + getResourcePath());
517 lock = lockManager.refreshLock(lockInfo, lockToken, this);
522 public void unlock( String lockToken )
525 ActiveLock lock = getLock(Type.WRITE, Scope.EXCLUSIVE);
528 throw new DavException(HttpServletResponse.SC_PRECONDITION_FAILED);
530 else if (lock.isLockedByToken(lockToken))
532 lockManager.releaseLock(lockToken, this);
536 throw new DavException(DavServletResponse.SC_LOCKED);
540 public void addLockManager( LockManager lockManager )
542 this.lockManager = lockManager;
545 public DavResourceFactory getFactory()
550 public DavSession getSession()
556 * Fill the set of properties
558 protected DavPropertySet initProperties()
562 properties = new DavPropertySet();
565 if ( properties != null )
570 DavPropertySet properties = new DavPropertySet();
572 // set (or reset) fundamental properties
573 if ( getDisplayName() != null )
575 properties.add( new DefaultDavProperty( DavPropertyName.DISPLAYNAME, getDisplayName() ) );
577 if ( isCollection() )
579 properties.add( new ResourceType( ResourceType.COLLECTION ) );
580 // Windows XP support
581 properties.add( new DefaultDavProperty( DavPropertyName.ISCOLLECTION, "1" ) );
585 properties.add( new ResourceType( ResourceType.DEFAULT_RESOURCE ) );
587 // Windows XP support
588 properties.add( new DefaultDavProperty( DavPropertyName.ISCOLLECTION, "0" ) );
591 // Need to get the ISO8601 date for properties
592 DateTime dt = new DateTime( localResource.lastModified() );
593 DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
594 String modifiedDate = fmt.print( dt );
596 properties.add( new DefaultDavProperty( DavPropertyName.GETLASTMODIFIED, modifiedDate ) );
598 properties.add( new DefaultDavProperty( DavPropertyName.CREATIONDATE, modifiedDate ) );
600 properties.add( new DefaultDavProperty( DavPropertyName.GETCONTENTLENGTH, localResource.length() ) );
602 this.properties = properties;
607 private ArchivaDavResource checkDavResourceIsArchivaDavResource( DavResource resource )
610 if ( !( resource instanceof ArchivaDavResource ) )
612 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
613 "DavResource is not instance of ArchivaDavResource" );
615 return (ArchivaDavResource) resource;
618 private void triggerAuditEvent( String remoteIP, String repositoryId, String resource, String action )
620 String activePrincipal = archivaXworkUser.getActivePrincipal( ActionContext.getContext().getSession() );
621 AuditEvent event = new AuditEvent( repositoryId, activePrincipal, resource, action );
622 event.setRemoteIP( remoteIP );
624 for ( AuditListener listener : auditListeners )
626 listener.auditEvent( event );