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.webdav.util.IndexWriter;
61 import org.apache.maven.archiva.webdav.util.MimeTypes;
62 import org.joda.time.DateTime;
63 import org.joda.time.format.DateTimeFormatter;
64 import org.joda.time.format.ISODateTimeFormat;
68 public class ArchivaDavResource
69 implements DavResource
71 public static final String HIDDEN_PATH_PREFIX = ".";
73 private final ArchivaDavResourceLocator locator;
75 private final DavResourceFactory factory;
77 private final File localResource;
79 private final String logicalResource;
81 private DavPropertySet properties = null;
83 private LockManager lockManager;
85 private final DavSession session;
87 private String remoteAddr;
89 private final ManagedRepositoryConfiguration repository;
91 private final RepositoryContentConsumers consumers;
93 private final MimeTypes mimeTypes;
95 private List<AuditListener> auditListeners;
97 private String principal;
99 public static final String COMPLIANCE_CLASS = "1, 2";
101 public ArchivaDavResource( String localResource, String logicalResource, ManagedRepositoryConfiguration repository,
102 DavSession session, ArchivaDavResourceLocator locator, DavResourceFactory factory,
103 MimeTypes mimeTypes, List<AuditListener> auditListeners,
104 RepositoryContentConsumers consumers )
106 this.localResource = new File( localResource );
107 this.logicalResource = logicalResource;
108 this.locator = locator;
109 this.factory = factory;
110 this.session = session;
112 // TODO: push into locator as well as moving any references out of the resource factory
113 this.repository = repository;
115 // TODO: these should be pushed into the repository layer, along with the physical file operations in this class
116 this.mimeTypes = mimeTypes;
117 this.consumers = consumers;
118 this.auditListeners = auditListeners;
121 public ArchivaDavResource( String localResource, String logicalResource, ManagedRepositoryConfiguration repository,
122 String remoteAddr, String principal, DavSession session, ArchivaDavResourceLocator locator,
123 DavResourceFactory factory, MimeTypes mimeTypes, List<AuditListener> auditListeners,
124 RepositoryContentConsumers consumers )
126 this( localResource, logicalResource, repository, session, locator, factory, mimeTypes, auditListeners,
129 this.remoteAddr = remoteAddr;
130 this.principal = principal;
133 public String getComplianceClass()
135 return COMPLIANCE_CLASS;
138 public String getSupportedMethods()
143 public boolean exists()
145 return localResource.exists();
148 public boolean isCollection()
150 return localResource.isDirectory();
153 public String getDisplayName()
155 String resPath = getResourcePath();
156 return ( resPath != null ) ? Text.getName( resPath ) : resPath;
159 public DavResourceLocator getLocator()
164 public File getLocalResource()
166 return localResource;
169 public String getResourcePath()
171 return locator.getResourcePath();
174 public String getHref()
176 return locator.getHref( isCollection() );
179 public long getModificationTime()
181 return localResource.lastModified();
184 public void spool( OutputContext outputContext )
187 if ( !isCollection())
189 outputContext.setContentLength( localResource.length() );
190 outputContext.setContentType( mimeTypes.getMimeType( localResource.getName() ) );
193 if ( !isCollection() && outputContext.hasStream() )
195 FileInputStream is = null;
198 // Write content to stream
199 is = new FileInputStream( localResource );
200 IOUtils.copy( is, outputContext.getOutputStream() );
204 IOUtils.closeQuietly( is );
207 else if (outputContext.hasStream())
209 IndexWriter writer = new IndexWriter( this, localResource, logicalResource );
210 writer.write( outputContext );
214 public DavPropertyName[] getPropertyNames()
216 return getProperties().getPropertyNames();
219 public DavProperty getProperty( DavPropertyName name )
221 return getProperties().get( name );
224 public DavPropertySet getProperties()
226 return initProperties();
229 public void setProperty( DavProperty property )
234 public void removeProperty( DavPropertyName propertyName )
239 public MultiStatusResponse alterProperties( DavPropertySet setProperties, DavPropertyNameSet removePropertyNames )
245 @SuppressWarnings("unchecked")
246 public MultiStatusResponse alterProperties( List changeList )
252 public DavResource getCollection()
254 DavResource parent = null;
255 if ( getResourcePath() != null && !getResourcePath().equals( "/" ) )
257 String parentPath = Text.getRelativeParent( getResourcePath(), 1 );
258 if ( parentPath.equals( "" ) )
262 DavResourceLocator parentloc = locator.getFactory().createResourceLocator( locator.getPrefix(), parentPath );
265 parent = factory.createResource( parentloc, session );
267 catch ( DavException e )
275 public void addMember( DavResource resource, InputContext inputContext )
278 File localFile = new File( localResource, resource.getDisplayName() );
279 boolean exists = localFile.exists();
281 if ( isCollection() && inputContext.hasStream() ) // New File
283 FileOutputStream stream = null;
286 stream = new FileOutputStream( localFile );
287 IOUtils.copy( inputContext.getInputStream(), stream );
289 catch ( IOException e )
291 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
295 IOUtils.closeQuietly( stream );
298 if ( inputContext.getContentLength() != localFile.length() )
300 FileUtils.deleteQuietly( localFile );
302 throw new DavException( HttpServletResponse.SC_BAD_REQUEST, "Content Header length was " +
303 inputContext.getContentLength() + " but was " + localFile.length() );
306 // Just-in-time update of the index and database by executing the consumers for this artifact
307 consumers.executeConsumers( repository, localFile );
309 triggerAuditEvent( resource, exists ? AuditEvent.MODIFY_FILE : AuditEvent.CREATE_FILE );
311 else if ( !inputContext.hasStream() && isCollection() ) // New directory
315 triggerAuditEvent( resource, AuditEvent.CREATE_DIR );
319 throw new DavException( HttpServletResponse.SC_BAD_REQUEST, "Could not write member " +
320 resource.getResourcePath() + " at " + getResourcePath() );
324 public DavResourceIterator getMembers()
326 List<DavResource> list = new ArrayList<DavResource>();
327 if ( exists() && isCollection() )
329 for ( String item : localResource.list() )
333 if ( !item.startsWith( HIDDEN_PATH_PREFIX ) )
335 String path = locator.getResourcePath() + '/' + item;
336 DavResourceLocator resourceLocator =
337 locator.getFactory().createResourceLocator( locator.getPrefix(), path );
338 DavResource resource = factory.createResource( resourceLocator, session );
339 if ( resource != null )
341 list.add( resource );
345 catch ( DavException e )
351 return new DavResourceIteratorImpl( list );
354 public void removeMember( DavResource member )
357 File resource = checkDavResourceIsArchivaDavResource( member ).getLocalResource();
359 if ( resource.exists() )
363 if ( resource.isDirectory() )
365 FileUtils.deleteDirectory( resource );
367 triggerAuditEvent( member, AuditEvent.REMOVE_DIR );
371 if ( !resource.delete() )
373 throw new IOException( "Could not remove file" );
376 triggerAuditEvent( member, AuditEvent.REMOVE_FILE );
379 catch ( IOException e )
381 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
386 throw new DavException( HttpServletResponse.SC_NOT_FOUND );
390 private void triggerAuditEvent( DavResource member, String event ) throws DavException
392 String path = logicalResource + "/" + member.getDisplayName();
394 triggerAuditEvent( checkDavResourceIsArchivaDavResource( member ).remoteAddr, locator.getRepositoryId(), path,
398 public void move( DavResource destination )
403 throw new DavException( HttpServletResponse.SC_NOT_FOUND, "Resource to copy does not exist." );
408 ArchivaDavResource resource = checkDavResourceIsArchivaDavResource( destination );
409 if ( isCollection() )
411 FileUtils.moveDirectory( getLocalResource(), resource.getLocalResource() );
413 triggerAuditEvent( remoteAddr, locator.getRepositoryId(), logicalResource, AuditEvent.MOVE_DIRECTORY );
417 FileUtils.moveFile( getLocalResource(), resource.getLocalResource() );
419 triggerAuditEvent( remoteAddr, locator.getRepositoryId(), logicalResource, AuditEvent.MOVE_FILE );
422 catch ( IOException e )
424 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
428 public void copy( DavResource destination, boolean shallow )
433 throw new DavException( HttpServletResponse.SC_NOT_FOUND, "Resource to copy does not exist." );
436 if ( shallow && isCollection() )
438 throw new DavException( DavServletResponse.SC_FORBIDDEN, "Unable to perform shallow copy for collection" );
443 ArchivaDavResource resource = checkDavResourceIsArchivaDavResource( destination );
444 if ( isCollection() )
446 FileUtils.copyDirectory( getLocalResource(), resource.getLocalResource() );
448 triggerAuditEvent( remoteAddr, locator.getRepositoryId(), logicalResource, AuditEvent.COPY_DIRECTORY );
452 FileUtils.copyFile( getLocalResource(), resource.getLocalResource() );
454 triggerAuditEvent( remoteAddr, locator.getRepositoryId(), logicalResource, AuditEvent.COPY_FILE );
457 catch ( IOException e )
459 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
463 public boolean isLockable( Type type, Scope scope )
465 return Type.WRITE.equals(type) && Scope.EXCLUSIVE.equals(scope);
468 public boolean hasLock( Type type, Scope scope )
470 return getLock(type, scope) != null;
473 public ActiveLock getLock( Type type, Scope scope )
475 ActiveLock lock = null;
476 if (exists() && Type.WRITE.equals(type) && Scope.EXCLUSIVE.equals(scope))
478 lock = lockManager.getLock(type, scope, this);
483 public ActiveLock[] getLocks()
485 ActiveLock writeLock = getLock(Type.WRITE, Scope.EXCLUSIVE);
486 return (writeLock != null) ? new ActiveLock[]{writeLock} : new ActiveLock[0];
489 public ActiveLock lock( LockInfo lockInfo )
492 ActiveLock lock = null;
493 if (isLockable(lockInfo.getType(), lockInfo.getScope()))
495 lock = lockManager.createLock(lockInfo, this);
499 throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED, "Unsupported lock type or scope.");
504 public ActiveLock refreshLock( LockInfo lockInfo, String lockToken )
508 throw new DavException(DavServletResponse.SC_NOT_FOUND);
510 ActiveLock lock = getLock(lockInfo.getType(), lockInfo.getScope());
512 throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED, "No lock with the given type/scope present on resource " + getResourcePath());
515 lock = lockManager.refreshLock(lockInfo, lockToken, this);
520 public void unlock( String lockToken )
523 ActiveLock lock = getLock(Type.WRITE, Scope.EXCLUSIVE);
526 throw new DavException(HttpServletResponse.SC_PRECONDITION_FAILED);
528 else if (lock.isLockedByToken(lockToken))
530 lockManager.releaseLock(lockToken, this);
534 throw new DavException(DavServletResponse.SC_LOCKED);
538 public void addLockManager( LockManager lockManager )
540 this.lockManager = lockManager;
543 public DavResourceFactory getFactory()
548 public DavSession getSession()
554 * Fill the set of properties
556 protected DavPropertySet initProperties()
560 properties = new DavPropertySet();
563 if ( properties != null )
568 DavPropertySet properties = new DavPropertySet();
570 // set (or reset) fundamental properties
571 if ( getDisplayName() != null )
573 properties.add( new DefaultDavProperty( DavPropertyName.DISPLAYNAME, getDisplayName() ) );
575 if ( isCollection() )
577 properties.add( new ResourceType( ResourceType.COLLECTION ) );
578 // Windows XP support
579 properties.add( new DefaultDavProperty( DavPropertyName.ISCOLLECTION, "1" ) );
583 properties.add( new ResourceType( ResourceType.DEFAULT_RESOURCE ) );
585 // Windows XP support
586 properties.add( new DefaultDavProperty( DavPropertyName.ISCOLLECTION, "0" ) );
589 // Need to get the ISO8601 date for properties
590 DateTime dt = new DateTime( localResource.lastModified() );
591 DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
592 String modifiedDate = fmt.print( dt );
594 properties.add( new DefaultDavProperty( DavPropertyName.GETLASTMODIFIED, modifiedDate ) );
596 properties.add( new DefaultDavProperty( DavPropertyName.CREATIONDATE, modifiedDate ) );
598 properties.add( new DefaultDavProperty( DavPropertyName.GETCONTENTLENGTH, localResource.length() ) );
600 this.properties = properties;
605 private ArchivaDavResource checkDavResourceIsArchivaDavResource( DavResource resource )
608 if ( !( resource instanceof ArchivaDavResource ) )
610 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
611 "DavResource is not instance of ArchivaDavResource" );
613 return (ArchivaDavResource) resource;
616 private void triggerAuditEvent( String remoteIP, String repositoryId, String resource, String action )
618 AuditEvent event = new AuditEvent( repositoryId, principal, resource, action );
619 event.setRemoteIP( remoteIP );
621 for ( AuditListener listener : auditListeners )
623 listener.auditEvent( event );