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.xwork2.ActionContext;
71 public class ArchivaDavResource
72 implements DavResource
74 public static final String HIDDEN_PATH_PREFIX = ".";
76 private final ArchivaDavResourceLocator locator;
78 private final DavResourceFactory factory;
80 private final File localResource;
82 private final String logicalResource;
84 private DavPropertySet properties = null;
86 private LockManager lockManager;
88 private final DavSession session;
90 private String remoteAddr;
92 private final ManagedRepositoryConfiguration repository;
94 private final RepositoryContentConsumers consumers;
96 private final MimeTypes mimeTypes;
98 private List<AuditListener> auditListeners;
100 private ArchivaXworkUser archivaXworkUser;
102 public static final String COMPLIANCE_CLASS = "1, 2";
104 public ArchivaDavResource( String localResource, String logicalResource, ManagedRepositoryConfiguration repository,
105 DavSession session, ArchivaDavResourceLocator locator, DavResourceFactory factory,
106 MimeTypes mimeTypes, List<AuditListener> auditListeners,
107 RepositoryContentConsumers consumers, ArchivaXworkUser archivaXworkUser )
109 this.localResource = new File( localResource );
110 this.logicalResource = logicalResource;
111 this.locator = locator;
112 this.factory = factory;
113 this.session = session;
114 this.archivaXworkUser = archivaXworkUser;
116 // TODO: push into locator as well as moving any references out of the resource factory
117 this.repository = repository;
119 // TODO: these should be pushed into the repository layer, along with the physical file operations in this class
120 this.mimeTypes = mimeTypes;
121 this.consumers = consumers;
122 this.auditListeners = auditListeners;
125 public ArchivaDavResource( String localResource, String logicalResource, ManagedRepositoryConfiguration repository,
126 String remoteAddr, DavSession session, ArchivaDavResourceLocator locator,
127 DavResourceFactory factory, MimeTypes mimeTypes, List<AuditListener> auditListeners,
128 RepositoryContentConsumers consumers, ArchivaXworkUser archivaXworkUser )
130 this( localResource, logicalResource, repository, session, locator, factory, mimeTypes, auditListeners,
131 consumers, archivaXworkUser );
133 this.remoteAddr = remoteAddr;
136 public String getComplianceClass()
138 return COMPLIANCE_CLASS;
141 public String getSupportedMethods()
146 public boolean exists()
148 return localResource.exists();
151 public boolean isCollection()
153 return localResource.isDirectory();
156 public String getDisplayName()
158 String resPath = getResourcePath();
159 return ( resPath != null ) ? Text.getName( resPath ) : resPath;
162 public DavResourceLocator getLocator()
167 public File getLocalResource()
169 return localResource;
172 public String getResourcePath()
174 return locator.getResourcePath();
177 public String getHref()
179 return locator.getHref( isCollection() );
182 public long getModificationTime()
184 return localResource.lastModified();
187 public void spool( OutputContext outputContext )
190 if ( !isCollection())
192 outputContext.setContentLength( localResource.length() );
193 outputContext.setContentType( mimeTypes.getMimeType( localResource.getName() ) );
196 if ( !isCollection() && outputContext.hasStream() )
198 FileInputStream is = null;
201 // Write content to stream
202 is = new FileInputStream( localResource );
203 IOUtils.copy( is, outputContext.getOutputStream() );
207 IOUtils.closeQuietly( is );
210 else if (outputContext.hasStream())
212 IndexWriter writer = new IndexWriter( this, localResource, logicalResource );
213 writer.write( outputContext );
217 public DavPropertyName[] getPropertyNames()
219 return getProperties().getPropertyNames();
222 public DavProperty getProperty( DavPropertyName name )
224 return getProperties().get( name );
227 public DavPropertySet getProperties()
229 return initProperties();
232 public void setProperty( DavProperty property )
237 public void removeProperty( DavPropertyName propertyName )
242 public MultiStatusResponse alterProperties( DavPropertySet setProperties, DavPropertyNameSet removePropertyNames )
248 @SuppressWarnings("unchecked")
249 public MultiStatusResponse alterProperties( List changeList )
255 public DavResource getCollection()
257 DavResource parent = null;
258 if ( getResourcePath() != null && !getResourcePath().equals( "/" ) )
260 String parentPath = Text.getRelativeParent( getResourcePath(), 1 );
261 if ( parentPath.equals( "" ) )
265 DavResourceLocator parentloc = locator.getFactory().createResourceLocator( locator.getPrefix(), parentPath );
268 parent = factory.createResource( parentloc, session );
270 catch ( DavException e )
278 public void addMember( DavResource resource, InputContext inputContext )
281 File localFile = new File( localResource, resource.getDisplayName() );
282 boolean exists = localFile.exists();
284 if ( isCollection() && inputContext.hasStream() ) // New File
286 FileOutputStream stream = null;
289 stream = new FileOutputStream( localFile );
290 IOUtils.copy( inputContext.getInputStream(), stream );
292 catch ( IOException e )
294 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
298 IOUtils.closeQuietly( stream );
301 if ( inputContext.getContentLength() != localFile.length() )
303 FileUtils.deleteQuietly( localFile );
305 throw new DavException( HttpServletResponse.SC_BAD_REQUEST, "Content Header length was " +
306 inputContext.getContentLength() + " but was " + localFile.length() );
309 // Just-in-time update of the index and database by executing the consumers for this artifact
310 consumers.executeConsumers( repository, localFile );
312 triggerAuditEvent( resource, exists ? AuditEvent.MODIFY_FILE : AuditEvent.CREATE_FILE );
314 else if ( !inputContext.hasStream() && isCollection() ) // New directory
318 triggerAuditEvent( resource, AuditEvent.CREATE_DIR );
322 throw new DavException( HttpServletResponse.SC_BAD_REQUEST, "Could not write member " +
323 resource.getResourcePath() + " at " + getResourcePath() );
327 public DavResourceIterator getMembers()
329 List<DavResource> list = new ArrayList<DavResource>();
330 if ( exists() && isCollection() )
332 for ( String item : localResource.list() )
336 if ( !item.startsWith( HIDDEN_PATH_PREFIX ) )
338 String path = locator.getResourcePath() + '/' + item;
339 DavResourceLocator resourceLocator =
340 locator.getFactory().createResourceLocator( locator.getPrefix(), path );
341 DavResource resource = factory.createResource( resourceLocator, session );
342 if ( resource != null )
344 list.add( resource );
348 catch ( DavException e )
354 return new DavResourceIteratorImpl( list );
357 public void removeMember( DavResource member )
360 File resource = checkDavResourceIsArchivaDavResource( member ).getLocalResource();
362 if ( resource.exists() )
366 if ( resource.isDirectory() )
368 FileUtils.deleteDirectory( resource );
370 triggerAuditEvent( member, AuditEvent.REMOVE_DIR );
374 if ( !resource.delete() )
376 throw new IOException( "Could not remove file" );
379 triggerAuditEvent( member, AuditEvent.REMOVE_FILE );
382 catch ( IOException e )
384 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
389 throw new DavException( HttpServletResponse.SC_NOT_FOUND );
393 private void triggerAuditEvent( DavResource member, String event ) throws DavException
395 String path = logicalResource + "/" + member.getDisplayName();
397 triggerAuditEvent( checkDavResourceIsArchivaDavResource( member ).remoteAddr, locator.getRepositoryId(), path,
401 public void move( DavResource destination )
406 throw new DavException( HttpServletResponse.SC_NOT_FOUND, "Resource to copy does not exist." );
411 ArchivaDavResource resource = checkDavResourceIsArchivaDavResource( destination );
412 if ( isCollection() )
414 FileUtils.moveDirectory( getLocalResource(), resource.getLocalResource() );
416 triggerAuditEvent( remoteAddr, locator.getRepositoryId(), logicalResource, AuditEvent.MOVE_DIRECTORY );
420 FileUtils.moveFile( getLocalResource(), resource.getLocalResource() );
422 triggerAuditEvent( remoteAddr, locator.getRepositoryId(), logicalResource, AuditEvent.MOVE_FILE );
425 catch ( IOException e )
427 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
431 public void copy( DavResource destination, boolean shallow )
436 throw new DavException( HttpServletResponse.SC_NOT_FOUND, "Resource to copy does not exist." );
439 if ( shallow && isCollection() )
441 throw new DavException( DavServletResponse.SC_FORBIDDEN, "Unable to perform shallow copy for collection" );
446 ArchivaDavResource resource = checkDavResourceIsArchivaDavResource( destination );
447 if ( isCollection() )
449 FileUtils.copyDirectory( getLocalResource(), resource.getLocalResource() );
451 triggerAuditEvent( remoteAddr, locator.getRepositoryId(), logicalResource, AuditEvent.COPY_DIRECTORY );
455 FileUtils.copyFile( getLocalResource(), resource.getLocalResource() );
457 triggerAuditEvent( remoteAddr, locator.getRepositoryId(), logicalResource, AuditEvent.COPY_FILE );
460 catch ( IOException e )
462 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
466 public boolean isLockable( Type type, Scope scope )
468 return Type.WRITE.equals(type) && Scope.EXCLUSIVE.equals(scope);
471 public boolean hasLock( Type type, Scope scope )
473 return getLock(type, scope) != null;
476 public ActiveLock getLock( Type type, Scope scope )
478 ActiveLock lock = null;
479 if (exists() && Type.WRITE.equals(type) && Scope.EXCLUSIVE.equals(scope))
481 lock = lockManager.getLock(type, scope, this);
486 public ActiveLock[] getLocks()
488 ActiveLock writeLock = getLock(Type.WRITE, Scope.EXCLUSIVE);
489 return (writeLock != null) ? new ActiveLock[]{writeLock} : new ActiveLock[0];
492 public ActiveLock lock( LockInfo lockInfo )
495 ActiveLock lock = null;
496 if (isLockable(lockInfo.getType(), lockInfo.getScope()))
498 lock = lockManager.createLock(lockInfo, this);
502 throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED, "Unsupported lock type or scope.");
507 public ActiveLock refreshLock( LockInfo lockInfo, String lockToken )
511 throw new DavException(DavServletResponse.SC_NOT_FOUND);
513 ActiveLock lock = getLock(lockInfo.getType(), lockInfo.getScope());
515 throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED, "No lock with the given type/scope present on resource " + getResourcePath());
518 lock = lockManager.refreshLock(lockInfo, lockToken, this);
523 public void unlock( String lockToken )
526 ActiveLock lock = getLock(Type.WRITE, Scope.EXCLUSIVE);
529 throw new DavException(HttpServletResponse.SC_PRECONDITION_FAILED);
531 else if (lock.isLockedByToken(lockToken))
533 lockManager.releaseLock(lockToken, this);
537 throw new DavException(DavServletResponse.SC_LOCKED);
541 public void addLockManager( LockManager lockManager )
543 this.lockManager = lockManager;
546 public DavResourceFactory getFactory()
551 public DavSession getSession()
557 * Fill the set of properties
559 protected DavPropertySet initProperties()
563 properties = new DavPropertySet();
566 if ( properties != null )
571 DavPropertySet properties = new DavPropertySet();
573 // set (or reset) fundamental properties
574 if ( getDisplayName() != null )
576 properties.add( new DefaultDavProperty( DavPropertyName.DISPLAYNAME, getDisplayName() ) );
578 if ( isCollection() )
580 properties.add( new ResourceType( ResourceType.COLLECTION ) );
581 // Windows XP support
582 properties.add( new DefaultDavProperty( DavPropertyName.ISCOLLECTION, "1" ) );
586 properties.add( new ResourceType( ResourceType.DEFAULT_RESOURCE ) );
588 // Windows XP support
589 properties.add( new DefaultDavProperty( DavPropertyName.ISCOLLECTION, "0" ) );
592 // Need to get the ISO8601 date for properties
593 DateTime dt = new DateTime( localResource.lastModified() );
594 DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
595 String modifiedDate = fmt.print( dt );
597 properties.add( new DefaultDavProperty( DavPropertyName.GETLASTMODIFIED, modifiedDate ) );
599 properties.add( new DefaultDavProperty( DavPropertyName.CREATIONDATE, modifiedDate ) );
601 properties.add( new DefaultDavProperty( DavPropertyName.GETCONTENTLENGTH, localResource.length() ) );
603 this.properties = properties;
608 private ArchivaDavResource checkDavResourceIsArchivaDavResource( DavResource resource )
611 if ( !( resource instanceof ArchivaDavResource ) )
613 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
614 "DavResource is not instance of ArchivaDavResource" );
616 return (ArchivaDavResource) resource;
619 private void triggerAuditEvent( String remoteIP, String repositoryId, String resource, String action )
621 String activePrincipal = archivaXworkUser.getActivePrincipal( ActionContext.getContext().getSession() );
622 AuditEvent event = new AuditEvent( repositoryId, activePrincipal, resource, action );
623 event.setRemoteIP( remoteIP );
625 for ( AuditListener listener : auditListeners )
627 listener.auditEvent( event );