You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ArchivaDavResource.java 25KB

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