Coverage Report - org.apache.maven.archiva.webdav.ArchivaDavResource
 
Classes in this File Line Coverage Branch Coverage Complexity
ArchivaDavResource
0%
0/225
0%
0/106
0
 
 1  
 package org.apache.maven.archiva.webdav;
 2  
 
 3  
 /*
 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
 11  
  *
 12  
  *  http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 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
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.File;
 23  
 import java.io.FileInputStream;
 24  
 import java.io.FileOutputStream;
 25  
 import java.io.IOException;
 26  
 import java.util.ArrayList;
 27  
 import java.util.Calendar;
 28  
 import java.util.List;
 29  
 
 30  
 import javax.servlet.http.HttpServletResponse;
 31  
 
 32  
 import org.apache.commons.io.FileUtils;
 33  
 import org.apache.commons.io.IOUtils;
 34  
 import org.apache.jackrabbit.util.Text;
 35  
 import org.apache.jackrabbit.webdav.DavException;
 36  
 import org.apache.jackrabbit.webdav.DavResource;
 37  
 import org.apache.jackrabbit.webdav.DavResourceFactory;
 38  
 import org.apache.jackrabbit.webdav.DavResourceIterator;
 39  
 import org.apache.jackrabbit.webdav.DavResourceIteratorImpl;
 40  
 import org.apache.jackrabbit.webdav.DavResourceLocator;
 41  
 import org.apache.jackrabbit.webdav.DavServletResponse;
 42  
 import org.apache.jackrabbit.webdav.DavSession;
 43  
 import org.apache.jackrabbit.webdav.MultiStatusResponse;
 44  
 import org.apache.jackrabbit.webdav.io.InputContext;
 45  
 import org.apache.jackrabbit.webdav.io.OutputContext;
 46  
 import org.apache.jackrabbit.webdav.lock.ActiveLock;
 47  
 import org.apache.jackrabbit.webdav.lock.LockInfo;
 48  
 import org.apache.jackrabbit.webdav.lock.LockManager;
 49  
 import org.apache.jackrabbit.webdav.lock.Scope;
 50  
 import org.apache.jackrabbit.webdav.lock.Type;
 51  
 import org.apache.jackrabbit.webdav.property.DavProperty;
 52  
 import org.apache.jackrabbit.webdav.property.DavPropertyName;
 53  
 import org.apache.jackrabbit.webdav.property.DavPropertyNameSet;
 54  
 import org.apache.jackrabbit.webdav.property.DavPropertySet;
 55  
 import org.apache.jackrabbit.webdav.property.DefaultDavProperty;
 56  
 import org.apache.jackrabbit.webdav.property.ResourceType;
 57  
 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
 58  
 import org.apache.maven.archiva.database.ArchivaAuditLogsDao;
 59  
 import org.apache.maven.archiva.model.ArchivaAuditLogs;
 60  
 import org.apache.maven.archiva.repository.audit.AuditEvent;
 61  
 import org.apache.maven.archiva.repository.audit.AuditListener;
 62  
 import org.apache.maven.archiva.scheduled.ArchivaTaskScheduler;
 63  
 import org.apache.maven.archiva.scheduled.tasks.RepositoryTask;
 64  
 import org.apache.maven.archiva.scheduled.tasks.TaskCreator;
 65  
 import org.apache.maven.archiva.webdav.util.IndexWriter;
 66  
 import org.apache.maven.archiva.webdav.util.MimeTypes;
 67  
 import org.codehaus.plexus.taskqueue.TaskQueueException;
 68  
 import org.joda.time.DateTime;
 69  
 import org.joda.time.format.DateTimeFormatter;
 70  
 import org.joda.time.format.ISODateTimeFormat;
 71  
 import org.slf4j.Logger;
 72  
 import org.slf4j.LoggerFactory;
 73  
 
 74  
 /**
 75  
  */
 76  
 public class ArchivaDavResource
 77  
     implements DavResource
 78  
 {
 79  
     public static final String HIDDEN_PATH_PREFIX = ".";
 80  
 
 81  
     private final ArchivaDavResourceLocator locator;
 82  
 
 83  
     private final DavResourceFactory factory;
 84  
 
 85  
     private final File localResource;
 86  
 
 87  
     private final String logicalResource;
 88  
 
 89  0
     private DavPropertySet properties = null;
 90  
 
 91  
     private LockManager lockManager;
 92  
 
 93  
     private final DavSession session;
 94  
 
 95  
     private String remoteAddr;
 96  
 
 97  
     private final ManagedRepositoryConfiguration repository;
 98  
 
 99  
     private final MimeTypes mimeTypes;
 100  
 
 101  
     private List<AuditListener> auditListeners;
 102  
 
 103  
     private String principal;
 104  
 
 105  
     public static final String COMPLIANCE_CLASS = "1, 2";
 106  
 
 107  
     private ArchivaTaskScheduler scheduler;
 108  
 
 109  0
     private Logger log = LoggerFactory.getLogger( ArchivaDavResource.class );
 110  
     
 111  
     private ArchivaAuditLogsDao auditLogsDao;
 112  
 
 113  
     public ArchivaDavResource( String localResource, String logicalResource, ManagedRepositoryConfiguration repository,
 114  
                                DavSession session, ArchivaDavResourceLocator locator, DavResourceFactory factory,
 115  
                                MimeTypes mimeTypes, List<AuditListener> auditListeners,
 116  
                                ArchivaTaskScheduler scheduler, ArchivaAuditLogsDao auditLogsDao )
 117  0
     {
 118  0
         this.localResource = new File( localResource );
 119  0
         this.logicalResource = logicalResource;
 120  0
         this.locator = locator;
 121  0
         this.factory = factory;
 122  0
         this.session = session;
 123  
 
 124  
         // TODO: push into locator as well as moving any references out of the resource factory
 125  0
         this.repository = repository;
 126  
 
 127  
         // TODO: these should be pushed into the repository layer, along with the physical file operations in this class
 128  0
         this.mimeTypes = mimeTypes;
 129  0
         this.auditListeners = auditListeners;
 130  0
         this.scheduler = scheduler;
 131  0
         this.auditLogsDao = auditLogsDao;
 132  0
     }
 133  
 
 134  
     public ArchivaDavResource( String localResource, String logicalResource, ManagedRepositoryConfiguration repository,
 135  
                                String remoteAddr, String principal, DavSession session, ArchivaDavResourceLocator locator,
 136  
                                DavResourceFactory factory, MimeTypes mimeTypes, List<AuditListener> auditListeners,
 137  
                                ArchivaTaskScheduler scheduler, ArchivaAuditLogsDao auditLogsDao )
 138  
     {
 139  0
         this( localResource, logicalResource, repository, session, locator, factory, mimeTypes, auditListeners,
 140  
               scheduler, auditLogsDao );
 141  
 
 142  0
         this.remoteAddr = remoteAddr;
 143  0
         this.principal = principal;
 144  0
     }
 145  
 
 146  
     public String getComplianceClass()
 147  
     {
 148  0
         return COMPLIANCE_CLASS;
 149  
     }
 150  
 
 151  
     public String getSupportedMethods()
 152  
     {
 153  0
         return METHODS;
 154  
     }
 155  
 
 156  
     public boolean exists()
 157  
     {
 158  0
         return localResource.exists();
 159  
     }
 160  
 
 161  
     public boolean isCollection()
 162  
     {
 163  0
         return localResource.isDirectory();
 164  
     }
 165  
 
 166  
     public String getDisplayName()
 167  
     {
 168  0
         String resPath = getResourcePath();
 169  0
         return ( resPath != null ) ? Text.getName( resPath ) : resPath;
 170  
     }
 171  
 
 172  
     public DavResourceLocator getLocator()
 173  
     {
 174  0
         return locator;
 175  
     }
 176  
 
 177  
     public File getLocalResource()
 178  
     {
 179  0
         return localResource;
 180  
     }
 181  
 
 182  
     public String getResourcePath()
 183  
     {
 184  0
         return locator.getResourcePath();
 185  
     }
 186  
 
 187  
     public String getHref()
 188  
     {
 189  0
         return locator.getHref( isCollection() );
 190  
     }
 191  
 
 192  
     public long getModificationTime()
 193  
     {
 194  0
         return localResource.lastModified();
 195  
     }
 196  
 
 197  
     public void spool( OutputContext outputContext )
 198  
         throws IOException
 199  
     {
 200  0
         if ( !isCollection() )
 201  
         {
 202  0
             outputContext.setContentLength( localResource.length() );
 203  0
             outputContext.setContentType( mimeTypes.getMimeType( localResource.getName() ) );
 204  
         }
 205  
 
 206  0
         if ( !isCollection() && outputContext.hasStream() )
 207  
         {
 208  0
             FileInputStream is = null;
 209  
             try
 210  
             {
 211  
                 // Write content to stream
 212  0
                 is = new FileInputStream( localResource );
 213  0
                 IOUtils.copy( is, outputContext.getOutputStream() );
 214  
             }
 215  
             finally
 216  
             {
 217  0
                 IOUtils.closeQuietly( is );
 218  0
             }
 219  0
         }
 220  0
         else if ( outputContext.hasStream() )
 221  
         {
 222  0
             IndexWriter writer = new IndexWriter( this, localResource, logicalResource );
 223  0
             writer.write( outputContext );
 224  
         }
 225  0
     }
 226  
 
 227  
     public DavPropertyName[] getPropertyNames()
 228  
     {
 229  0
         return getProperties().getPropertyNames();
 230  
     }
 231  
 
 232  
     public DavProperty getProperty( DavPropertyName name )
 233  
     {
 234  0
         return getProperties().get( name );
 235  
     }
 236  
 
 237  
     public DavPropertySet getProperties()
 238  
     {
 239  0
         return initProperties();
 240  
     }
 241  
 
 242  
     public void setProperty( DavProperty property )
 243  
         throws DavException
 244  
     {
 245  0
     }
 246  
 
 247  
     public void removeProperty( DavPropertyName propertyName )
 248  
         throws DavException
 249  
     {
 250  0
     }
 251  
 
 252  
     public MultiStatusResponse alterProperties( DavPropertySet setProperties, DavPropertyNameSet removePropertyNames )
 253  
         throws DavException
 254  
     {
 255  0
         return null;
 256  
     }
 257  
 
 258  
     @SuppressWarnings( "unchecked" )
 259  
     public MultiStatusResponse alterProperties( List changeList )
 260  
         throws DavException
 261  
     {
 262  0
         return null;
 263  
     }
 264  
 
 265  
     public DavResource getCollection()
 266  
     {
 267  0
         DavResource parent = null;
 268  0
         if ( getResourcePath() != null && !getResourcePath().equals( "/" ) )
 269  
         {
 270  0
             String parentPath = Text.getRelativeParent( getResourcePath(), 1 );
 271  0
             if ( parentPath.equals( "" ) )
 272  
             {
 273  0
                 parentPath = "/";
 274  
             }
 275  0
             DavResourceLocator parentloc = locator.getFactory().createResourceLocator( locator.getPrefix(),
 276  
                                                                                        parentPath );
 277  
             try
 278  
             {
 279  0
                 parent = factory.createResource( parentloc, session );
 280  
             }
 281  0
             catch ( DavException e )
 282  
             {
 283  
                 // should not occur
 284  0
             }
 285  
         }
 286  0
         return parent;
 287  
     }
 288  
 
 289  
     public void addMember( DavResource resource, InputContext inputContext )
 290  
         throws DavException
 291  
     {
 292  0
         File localFile = new File( localResource, resource.getDisplayName() );
 293  0
         boolean exists = localFile.exists();
 294  
 
 295  0
         if ( isCollection() && inputContext.hasStream() ) // New File
 296  
         {
 297  0
             FileOutputStream stream = null;
 298  
             try
 299  
             {
 300  0
                 stream = new FileOutputStream( localFile );
 301  0
                 IOUtils.copy( inputContext.getInputStream(), stream );
 302  
             }
 303  0
             catch ( IOException e )
 304  
             {
 305  0
                 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
 306  
             }
 307  
             finally
 308  
             {
 309  0
                 IOUtils.closeQuietly( stream );
 310  0
             }
 311  
 
 312  
             // TODO: a bad deployment shouldn't delete an existing file - do we need to write to a temporary location first?
 313  0
             long expectedContentLength = inputContext.getContentLength();
 314  0
             long actualContentLength = localFile.length();
 315  
             // length of -1 is given for a chunked request or unknown length, in which case we accept what was uploaded
 316  0
             if ( expectedContentLength >= 0 && expectedContentLength != actualContentLength )
 317  
             {
 318  0
                 String msg =
 319  
                     "Content Header length was " + expectedContentLength + " but was " + actualContentLength;
 320  0
                 log.debug( "Upload failed: " + msg );
 321  
 
 322  0
                 FileUtils.deleteQuietly( localFile );
 323  0
                 throw new DavException( HttpServletResponse.SC_BAD_REQUEST, msg );
 324  
             }
 325  
 
 326  0
             queueRepositoryTask( localFile );
 327  
 
 328  0
             log.debug(
 329  
                 "File '" + resource.getDisplayName() + ( exists ? "' modified " : "' created " ) + "(current user '" +
 330  
                     this.principal + "')" );
 331  
 
 332  0
             triggerAuditEvent( resource, exists ? AuditEvent.MODIFY_FILE : AuditEvent.CREATE_FILE );
 333  0
         }
 334  0
         else if ( !inputContext.hasStream() && isCollection() ) // New directory
 335  
         {
 336  0
             localFile.mkdir();
 337  
 
 338  0
             log.debug( "Directory '" + resource.getDisplayName() + "' (current user '" + this.principal + "')" );
 339  
 
 340  0
             triggerAuditEvent( resource, AuditEvent.CREATE_DIR );
 341  
         }
 342  
         else
 343  
         {
 344  0
             String msg = "Could not write member " + resource.getResourcePath() + " at " + getResourcePath() +
 345  
                 " as this is not a DAV collection";
 346  0
             log.debug( msg );
 347  0
             throw new DavException( HttpServletResponse.SC_BAD_REQUEST, msg );
 348  
         }
 349  0
     }
 350  
 
 351  
     public DavResourceIterator getMembers()
 352  
     {
 353  0
         List<DavResource> list = new ArrayList<DavResource>();
 354  0
         if ( exists() && isCollection() )
 355  
         {
 356  0
             for ( String item : localResource.list() )
 357  
             {
 358  
                 try
 359  
                 {
 360  0
                     if ( !item.startsWith( HIDDEN_PATH_PREFIX ) )
 361  
                     {
 362  0
                         String path = locator.getResourcePath() + '/' + item;
 363  0
                         DavResourceLocator resourceLocator = locator.getFactory().createResourceLocator(
 364  
                             locator.getPrefix(), path );
 365  0
                         DavResource resource = factory.createResource( resourceLocator, session );
 366  
 
 367  0
                         if ( resource != null )
 368  
                         {
 369  0
                             list.add( resource );
 370  
                         }
 371  0
                         log.debug( "Resource '" + item + "' retrieved by '" + this.principal + "'" );
 372  
                     }
 373  
                 }
 374  0
                 catch ( DavException e )
 375  
                 {
 376  
                     // Should not occur
 377  0
                 }
 378  
             }
 379  
         }
 380  0
         return new DavResourceIteratorImpl( list );
 381  
     }
 382  
 
 383  
     public void removeMember( DavResource member )
 384  
         throws DavException
 385  
     {
 386  0
         File resource = checkDavResourceIsArchivaDavResource( member ).getLocalResource();
 387  
 
 388  0
         if ( resource.exists() )
 389  
         {
 390  
             try
 391  
             {
 392  0
                 if ( resource.isDirectory() )
 393  
                 {
 394  0
                     FileUtils.deleteDirectory( resource );
 395  
 
 396  0
                     triggerAuditEvent( member, AuditEvent.REMOVE_DIR );
 397  
                 }
 398  
                 else
 399  
                 {
 400  0
                     if ( !resource.delete() )
 401  
                     {
 402  0
                         throw new IOException( "Could not remove file" );
 403  
                     }
 404  
 
 405  0
                     triggerAuditEvent( member, AuditEvent.REMOVE_FILE );
 406  
                 }
 407  0
                 log.debug( ( resource.isDirectory() ? "Directory '" : "File '" ) + member.getDisplayName() +
 408  
                     "' removed (current user '" + this.principal + "')" );
 409  
             }
 410  0
             catch ( IOException e )
 411  
             {
 412  0
                 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
 413  0
             }
 414  
         }
 415  
         else
 416  
         {
 417  0
             throw new DavException( HttpServletResponse.SC_NOT_FOUND );
 418  
         }
 419  0
     }
 420  
 
 421  
     private void triggerAuditEvent( DavResource member, String event )
 422  
         throws DavException
 423  
     {
 424  0
         String path = logicalResource + "/" + member.getDisplayName();
 425  
 
 426  0
         ArchivaDavResource resource = checkDavResourceIsArchivaDavResource( member );
 427  0
         triggerAuditEvent( resource.remoteAddr, resource.principal, locator.getRepositoryId(), path,
 428  
                            event );
 429  0
     }
 430  
 
 431  
     public void move( DavResource destination )
 432  
         throws DavException
 433  
     {
 434  0
         if ( !exists() )
 435  
         {
 436  0
             throw new DavException( HttpServletResponse.SC_NOT_FOUND, "Resource to copy does not exist." );
 437  
         }
 438  
 
 439  
         try
 440  
         {
 441  0
             ArchivaDavResource resource = checkDavResourceIsArchivaDavResource( destination );
 442  0
             if ( isCollection() )
 443  
             {
 444  0
                 FileUtils.moveDirectory( getLocalResource(), resource.getLocalResource() );
 445  
 
 446  0
                 triggerAuditEvent( remoteAddr, principal, locator.getRepositoryId(), logicalResource, AuditEvent.MOVE_DIRECTORY );
 447  
             }
 448  
             else
 449  
             {
 450  0
                 FileUtils.moveFile( getLocalResource(), resource.getLocalResource() );
 451  
 
 452  0
                 triggerAuditEvent( remoteAddr, principal, locator.getRepositoryId(), logicalResource, AuditEvent.MOVE_FILE );
 453  
             }
 454  
 
 455  0
             log.debug( ( isCollection() ? "Directory '" : "File '" ) + getLocalResource().getName() + "' moved to '" +
 456  
                 destination + "' (current user '" + this.principal + "')" );
 457  
         }
 458  0
         catch ( IOException e )
 459  
         {
 460  0
             throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
 461  0
         }
 462  0
     }
 463  
 
 464  
     public void copy( DavResource destination, boolean shallow )
 465  
         throws DavException
 466  
     {
 467  0
         if ( !exists() )
 468  
         {
 469  0
             throw new DavException( HttpServletResponse.SC_NOT_FOUND, "Resource to copy does not exist." );
 470  
         }
 471  
 
 472  0
         if ( shallow && isCollection() )
 473  
         {
 474  0
             throw new DavException( DavServletResponse.SC_FORBIDDEN, "Unable to perform shallow copy for collection" );
 475  
         }
 476  
 
 477  
         try
 478  
         {
 479  0
             ArchivaDavResource resource = checkDavResourceIsArchivaDavResource( destination );
 480  0
             if ( isCollection() )
 481  
             {
 482  0
                 FileUtils.copyDirectory( getLocalResource(), resource.getLocalResource() );
 483  
 
 484  0
                 triggerAuditEvent( remoteAddr, principal, locator.getRepositoryId(), logicalResource, AuditEvent.COPY_DIRECTORY );
 485  
             }
 486  
             else
 487  
             {
 488  0
                 FileUtils.copyFile( getLocalResource(), resource.getLocalResource() );
 489  
 
 490  0
                 triggerAuditEvent( remoteAddr, principal, locator.getRepositoryId(), logicalResource, AuditEvent.COPY_FILE );
 491  
             }
 492  0
             log.debug( ( isCollection() ? "Directory '" : "File '" ) + getLocalResource().getName() + "' copied to '" +
 493  
                 destination + "' (current user '" + this.principal + "')" );
 494  
         }
 495  0
         catch ( IOException e )
 496  
         {
 497  0
             throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
 498  0
         }
 499  0
     }
 500  
 
 501  
     public boolean isLockable( Type type, Scope scope )
 502  
     {
 503  0
         return Type.WRITE.equals( type ) && Scope.EXCLUSIVE.equals( scope );
 504  
     }
 505  
 
 506  
     public boolean hasLock( Type type, Scope scope )
 507  
     {
 508  0
         return getLock( type, scope ) != null;
 509  
     }
 510  
 
 511  
     public ActiveLock getLock( Type type, Scope scope )
 512  
     {
 513  0
         ActiveLock lock = null;
 514  0
         if ( exists() && Type.WRITE.equals( type ) && Scope.EXCLUSIVE.equals( scope ) )
 515  
         {
 516  0
             lock = lockManager.getLock( type, scope, this );
 517  
         }
 518  0
         return lock;
 519  
     }
 520  
 
 521  
     public ActiveLock[] getLocks()
 522  
     {
 523  0
         ActiveLock writeLock = getLock( Type.WRITE, Scope.EXCLUSIVE );
 524  0
         return ( writeLock != null ) ? new ActiveLock[]{writeLock} : new ActiveLock[0];
 525  
     }
 526  
 
 527  
     public ActiveLock lock( LockInfo lockInfo )
 528  
         throws DavException
 529  
     {
 530  0
         ActiveLock lock = null;
 531  0
         if ( isLockable( lockInfo.getType(), lockInfo.getScope() ) )
 532  
         {
 533  0
             lock = lockManager.createLock( lockInfo, this );
 534  
         }
 535  
         else
 536  
         {
 537  0
             throw new DavException( DavServletResponse.SC_PRECONDITION_FAILED, "Unsupported lock type or scope." );
 538  
         }
 539  0
         return lock;
 540  
     }
 541  
 
 542  
     public ActiveLock refreshLock( LockInfo lockInfo, String lockToken )
 543  
         throws DavException
 544  
     {
 545  0
         if ( !exists() )
 546  
         {
 547  0
             throw new DavException( DavServletResponse.SC_NOT_FOUND );
 548  
         }
 549  0
         ActiveLock lock = getLock( lockInfo.getType(), lockInfo.getScope() );
 550  0
         if ( lock == null )
 551  
         {
 552  0
             throw new DavException( DavServletResponse.SC_PRECONDITION_FAILED,
 553  
                                     "No lock with the given type/scope present on resource " + getResourcePath() );
 554  
         }
 555  
 
 556  0
         lock = lockManager.refreshLock( lockInfo, lockToken, this );
 557  
 
 558  0
         return lock;
 559  
     }
 560  
 
 561  
     public void unlock( String lockToken )
 562  
         throws DavException
 563  
     {
 564  0
         ActiveLock lock = getLock( Type.WRITE, Scope.EXCLUSIVE );
 565  0
         if ( lock == null )
 566  
         {
 567  0
             throw new DavException( HttpServletResponse.SC_PRECONDITION_FAILED );
 568  
         }
 569  0
         else if ( lock.isLockedByToken( lockToken ) )
 570  
         {
 571  0
             lockManager.releaseLock( lockToken, this );
 572  
         }
 573  
         else
 574  
         {
 575  0
             throw new DavException( DavServletResponse.SC_LOCKED );
 576  
         }
 577  0
     }
 578  
 
 579  
     public void addLockManager( LockManager lockManager )
 580  
     {
 581  0
         this.lockManager = lockManager;
 582  0
     }
 583  
 
 584  
     public DavResourceFactory getFactory()
 585  
     {
 586  0
         return factory;
 587  
     }
 588  
 
 589  
     public DavSession getSession()
 590  
     {
 591  0
         return session;
 592  
     }
 593  
 
 594  
     /**
 595  
      * Fill the set of properties
 596  
      */
 597  
     protected DavPropertySet initProperties()
 598  
     {
 599  0
         if ( !exists() )
 600  
         {
 601  0
             properties = new DavPropertySet();
 602  
         }
 603  
 
 604  0
         if ( properties != null )
 605  
         {
 606  0
             return properties;
 607  
         }
 608  
 
 609  0
         DavPropertySet properties = new DavPropertySet();
 610  
 
 611  
         // set (or reset) fundamental properties
 612  0
         if ( getDisplayName() != null )
 613  
         {
 614  0
             properties.add( new DefaultDavProperty( DavPropertyName.DISPLAYNAME, getDisplayName() ) );
 615  
         }
 616  0
         if ( isCollection() )
 617  
         {
 618  0
             properties.add( new ResourceType( ResourceType.COLLECTION ) );
 619  
             // Windows XP support
 620  0
             properties.add( new DefaultDavProperty( DavPropertyName.ISCOLLECTION, "1" ) );
 621  
         }
 622  
         else
 623  
         {
 624  0
             properties.add( new ResourceType( ResourceType.DEFAULT_RESOURCE ) );
 625  
 
 626  
             // Windows XP support
 627  0
             properties.add( new DefaultDavProperty( DavPropertyName.ISCOLLECTION, "0" ) );
 628  
         }
 629  
 
 630  
         // Need to get the ISO8601 date for properties
 631  0
         DateTime dt = new DateTime( localResource.lastModified() );
 632  0
         DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
 633  0
         String modifiedDate = fmt.print( dt );
 634  
 
 635  0
         properties.add( new DefaultDavProperty( DavPropertyName.GETLASTMODIFIED, modifiedDate ) );
 636  
 
 637  0
         properties.add( new DefaultDavProperty( DavPropertyName.CREATIONDATE, modifiedDate ) );
 638  
 
 639  0
         properties.add( new DefaultDavProperty( DavPropertyName.GETCONTENTLENGTH, localResource.length() ) );
 640  
 
 641  0
         this.properties = properties;
 642  
 
 643  0
         return properties;
 644  
     }
 645  
 
 646  
     private ArchivaDavResource checkDavResourceIsArchivaDavResource( DavResource resource )
 647  
         throws DavException
 648  
     {
 649  0
         if ( !( resource instanceof ArchivaDavResource ) )
 650  
         {
 651  0
             throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
 652  
                                     "DavResource is not instance of ArchivaDavResource" );
 653  
         }
 654  0
         return (ArchivaDavResource) resource;
 655  
     }
 656  
 
 657  
     private void triggerAuditEvent( String remoteIP, String principal, String repositoryId, String resource,
 658  
                                     String action )
 659  
     {
 660  0
         AuditEvent event = new AuditEvent( repositoryId, principal, resource, action );
 661  0
         event.setRemoteIP( remoteIP );
 662  
 
 663  0
         for ( AuditListener listener : auditListeners )
 664  
         {
 665  0
             listener.auditEvent( event );
 666  
         }
 667  
         
 668  
         // identify as artifact deployment/upload
 669  0
         if( action.equals( AuditEvent.CREATE_FILE ) )
 670  
         {
 671  0
             action = AuditEvent.UPLOAD_FILE;
 672  
         }
 673  
         
 674  0
         String user = principal;
 675  0
         if( principal == null )
 676  
         {
 677  0
             user = "guest";
 678  
         }
 679  
         
 680  0
         ArchivaAuditLogs auditLogs = new ArchivaAuditLogs();
 681  0
         auditLogs.setArtifact( resource );
 682  0
         auditLogs.setEvent( action );
 683  0
         auditLogs.setEventDate( Calendar.getInstance().getTime() );
 684  0
         auditLogs.setRepositoryId( repositoryId );
 685  0
         auditLogs.setUsername( user );
 686  
         
 687  0
         auditLogsDao.saveAuditLogs( auditLogs );
 688  0
     }
 689  
 
 690  
     private void queueRepositoryTask( File localFile )
 691  
     {
 692  0
         RepositoryTask task = TaskCreator.createRepositoryTask( repository.getId(), localFile, false, true );
 693  
         
 694  
         try
 695  
         {
 696  0
             scheduler.queueRepositoryTask( task );
 697  
         }
 698  0
         catch ( TaskQueueException e )
 699  
         {
 700  0
             log.error(
 701  
                 "Unable to queue repository task to execute consumers on resource file ['" + localFile.getName() +
 702  
                     "']." );
 703  0
         }
 704  0
     }
 705  
 }