]> source.dussan.org Git - archiva.git/blob
f715960e4c1cb5556b2a8906601a3eb4bd7e6ec1
[archiva.git] /
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.List;
28
29 import javax.servlet.http.HttpServletResponse;
30
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;
65
66 /**
67  */
68 public class ArchivaDavResource
69     implements DavResource
70 {
71     public static final String HIDDEN_PATH_PREFIX = ".";
72
73     private final ArchivaDavResourceLocator locator;
74
75     private final DavResourceFactory factory;
76
77     private final File localResource;
78
79     private final String logicalResource;
80
81     private DavPropertySet properties = null;
82
83     private LockManager lockManager;
84     
85     private final DavSession session;
86     
87     private String remoteAddr;
88
89     private final ManagedRepositoryConfiguration repository;
90
91     private final RepositoryContentConsumers consumers;
92
93     private final MimeTypes mimeTypes;
94
95     private List<AuditListener> auditListeners;
96
97     private String principal;
98     
99         public static final String COMPLIANCE_CLASS = "1, 2";
100
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 )
105     {
106         this.localResource = new File( localResource ); 
107         this.logicalResource = logicalResource;
108         this.locator = locator;
109         this.factory = factory;
110         this.session = session;
111         
112         // TODO: push into locator as well as moving any references out of the resource factory
113         this.repository = repository;
114         
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;
119     }
120
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 )
125     {
126         this( localResource, logicalResource, repository, session, locator, factory, mimeTypes, auditListeners,
127               consumers );
128
129         this.remoteAddr = remoteAddr;
130         this.principal = principal;
131     }
132
133     public String getComplianceClass()
134     {
135         return COMPLIANCE_CLASS;
136     }
137
138     public String getSupportedMethods()
139     {
140         return METHODS;
141     }
142
143     public boolean exists()
144     {
145         return localResource.exists();
146     }
147
148     public boolean isCollection()
149     {
150         return localResource.isDirectory();
151     }
152
153     public String getDisplayName()
154     {
155         String resPath = getResourcePath();
156         return ( resPath != null ) ? Text.getName( resPath ) : resPath;
157     }
158
159     public DavResourceLocator getLocator()
160     {
161         return locator;
162     }
163
164     public File getLocalResource()
165     {
166         return localResource;
167     }
168
169     public String getResourcePath()
170     {
171         return locator.getResourcePath();
172     }
173
174     public String getHref()
175     {
176         return locator.getHref( isCollection() );
177     }
178
179     public long getModificationTime()
180     {
181         return localResource.lastModified();
182     }
183
184     public void spool( OutputContext outputContext )
185         throws IOException
186     {
187         if ( !isCollection())
188         {
189             outputContext.setContentLength( localResource.length() );
190             outputContext.setContentType( mimeTypes.getMimeType( localResource.getName() ) );
191         }
192         
193         if ( !isCollection() && outputContext.hasStream() )
194         {
195             FileInputStream is = null;
196             try
197             {
198                 // Write content to stream
199                 is = new FileInputStream( localResource );
200                 IOUtils.copy( is, outputContext.getOutputStream() );
201             }
202             finally
203             {
204                 IOUtils.closeQuietly( is );
205             }
206         }
207         else if (outputContext.hasStream())
208         {
209             IndexWriter writer = new IndexWriter( this, localResource, logicalResource );
210             writer.write( outputContext );
211         }
212     }
213
214     public DavPropertyName[] getPropertyNames()
215     {
216         return getProperties().getPropertyNames();
217     }
218
219     public DavProperty getProperty( DavPropertyName name )
220     {
221         return getProperties().get( name );
222     }
223
224     public DavPropertySet getProperties()
225     {
226         return initProperties();
227     }
228
229     public void setProperty( DavProperty property )
230         throws DavException
231     {
232     }
233
234     public void removeProperty( DavPropertyName propertyName )
235         throws DavException
236     {
237     }
238
239     public MultiStatusResponse alterProperties( DavPropertySet setProperties, DavPropertyNameSet removePropertyNames )
240         throws DavException
241     {
242         return null;
243     }
244
245     @SuppressWarnings("unchecked")
246     public MultiStatusResponse alterProperties( List changeList )
247         throws DavException
248     {
249         return null;
250     }
251
252     public DavResource getCollection()
253     {
254         DavResource parent = null;
255         if ( getResourcePath() != null && !getResourcePath().equals( "/" ) )
256         {
257             String parentPath = Text.getRelativeParent( getResourcePath(), 1 );
258             if ( parentPath.equals( "" ) )
259             {
260                 parentPath = "/";
261             }
262             DavResourceLocator parentloc = locator.getFactory().createResourceLocator( locator.getPrefix(), parentPath );
263             try
264             {
265                 parent = factory.createResource( parentloc, session );
266             }
267             catch ( DavException e )
268             {
269                 // should not occur
270             }
271         }
272         return parent;
273     }
274
275     public void addMember( DavResource resource, InputContext inputContext )
276         throws DavException
277     {
278         File localFile = new File( localResource, resource.getDisplayName() );
279         boolean exists = localFile.exists();
280
281         if ( isCollection() && inputContext.hasStream() ) // New File
282         {
283             FileOutputStream stream = null;
284             try
285             {
286                 stream = new FileOutputStream( localFile );
287                 IOUtils.copy( inputContext.getInputStream(), stream );
288             }
289             catch ( IOException e )
290             {
291                 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
292             }
293             finally
294             {
295                 IOUtils.closeQuietly( stream );
296             }
297             
298             if ( inputContext.getContentLength() != localFile.length() )
299             {
300                 FileUtils.deleteQuietly( localFile );
301                 
302                 throw new DavException( HttpServletResponse.SC_BAD_REQUEST, "Content Header length was " +
303                     inputContext.getContentLength() + " but was " + localFile.length() );
304             }
305             
306             // Just-in-time update of the index and database by executing the consumers for this artifact
307             consumers.executeConsumers( repository, localFile );
308             
309             triggerAuditEvent( resource, exists ? AuditEvent.MODIFY_FILE : AuditEvent.CREATE_FILE );
310         }
311         else if ( !inputContext.hasStream() && isCollection() ) // New directory
312         {
313             localFile.mkdir();
314             
315             triggerAuditEvent( resource, AuditEvent.CREATE_DIR );
316         }
317         else
318         {
319             throw new DavException( HttpServletResponse.SC_BAD_REQUEST, "Could not write member " +
320                 resource.getResourcePath() + " at " + getResourcePath() );
321         }
322     }
323
324     public DavResourceIterator getMembers()
325     {
326         List<DavResource> list = new ArrayList<DavResource>();
327         if ( exists() && isCollection() )
328         {
329             for ( String item : localResource.list() )
330             {
331                 try
332                 {
333                     if ( !item.startsWith( HIDDEN_PATH_PREFIX ) )
334                     {
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 )
340                         {
341                             list.add( resource );
342                         }
343                     }
344                 }
345                 catch ( DavException e )
346                 {
347                     // Should not occur
348                 }
349             }
350         }
351         return new DavResourceIteratorImpl( list );
352     }
353
354     public void removeMember( DavResource member )
355         throws DavException
356     {
357         File resource = checkDavResourceIsArchivaDavResource( member ).getLocalResource();
358         
359         if ( resource.exists() )
360         {
361             try
362             {
363                 if ( resource.isDirectory() )
364                 {
365                     FileUtils.deleteDirectory( resource );
366
367                     triggerAuditEvent( member, AuditEvent.REMOVE_DIR );
368                 }
369                 else
370                 {
371                     if ( !resource.delete() )
372                     {
373                         throw new IOException( "Could not remove file" );
374                     }
375
376                     triggerAuditEvent( member, AuditEvent.REMOVE_FILE );
377                 }
378             }
379             catch ( IOException e )
380             {
381                 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
382             }
383         }
384         else
385         {
386             throw new DavException( HttpServletResponse.SC_NOT_FOUND );
387         }
388     }
389
390     private void triggerAuditEvent( DavResource member, String event ) throws DavException
391     {
392         String path = logicalResource + "/" + member.getDisplayName();
393         
394         triggerAuditEvent( checkDavResourceIsArchivaDavResource( member ).remoteAddr, locator.getRepositoryId(), path,
395                            event );
396     }
397
398     public void move( DavResource destination )
399         throws DavException
400     {
401         if ( !exists() )
402         {
403             throw new DavException( HttpServletResponse.SC_NOT_FOUND, "Resource to copy does not exist." );
404         }
405
406         try
407         {
408             ArchivaDavResource resource = checkDavResourceIsArchivaDavResource( destination );
409             if ( isCollection() )
410             {
411                 FileUtils.moveDirectory( getLocalResource(), resource.getLocalResource() );
412
413                 triggerAuditEvent( remoteAddr, locator.getRepositoryId(), logicalResource, AuditEvent.MOVE_DIRECTORY );
414             }
415             else
416             {
417                 FileUtils.moveFile( getLocalResource(), resource.getLocalResource() );
418
419                 triggerAuditEvent( remoteAddr, locator.getRepositoryId(), logicalResource, AuditEvent.MOVE_FILE );
420             }
421         }
422         catch ( IOException e )
423         {
424             throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
425         }
426     }
427
428     public void copy( DavResource destination, boolean shallow )
429         throws DavException
430     {
431         if ( !exists() )
432         {
433             throw new DavException( HttpServletResponse.SC_NOT_FOUND, "Resource to copy does not exist." );
434         }
435
436         if ( shallow && isCollection() )
437         {
438             throw new DavException( DavServletResponse.SC_FORBIDDEN, "Unable to perform shallow copy for collection" );
439         }
440
441         try
442         {
443             ArchivaDavResource resource = checkDavResourceIsArchivaDavResource( destination );
444             if ( isCollection() )
445             {
446                 FileUtils.copyDirectory( getLocalResource(), resource.getLocalResource() );
447
448                 triggerAuditEvent( remoteAddr, locator.getRepositoryId(), logicalResource, AuditEvent.COPY_DIRECTORY );
449             }
450             else
451             {
452                 FileUtils.copyFile( getLocalResource(), resource.getLocalResource() );
453
454                 triggerAuditEvent( remoteAddr, locator.getRepositoryId(), logicalResource, AuditEvent.COPY_FILE );
455             }
456         }
457         catch ( IOException e )
458         {
459             throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
460         }
461     }
462
463     public boolean isLockable( Type type, Scope scope )
464     {
465         return Type.WRITE.equals(type) && Scope.EXCLUSIVE.equals(scope);
466     }
467
468     public boolean hasLock( Type type, Scope scope )
469     {
470         return getLock(type, scope) != null;
471     }
472
473     public ActiveLock getLock( Type type, Scope scope )
474     {
475         ActiveLock lock = null;
476         if (exists() && Type.WRITE.equals(type) && Scope.EXCLUSIVE.equals(scope)) 
477         {
478             lock = lockManager.getLock(type, scope, this);
479         }
480         return lock;
481     }
482
483     public ActiveLock[] getLocks()
484     {
485         ActiveLock writeLock = getLock(Type.WRITE, Scope.EXCLUSIVE);
486         return (writeLock != null) ? new ActiveLock[]{writeLock} : new ActiveLock[0];
487     }
488
489     public ActiveLock lock( LockInfo lockInfo )
490         throws DavException
491     {
492         ActiveLock lock = null;
493         if (isLockable(lockInfo.getType(), lockInfo.getScope())) 
494         {
495             lock = lockManager.createLock(lockInfo, this);
496         }
497         else 
498         {
499             throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED, "Unsupported lock type or scope.");
500         }
501         return lock;
502     }
503
504     public ActiveLock refreshLock( LockInfo lockInfo, String lockToken )
505         throws DavException
506     {
507         if (!exists()) {
508             throw new DavException(DavServletResponse.SC_NOT_FOUND);
509         }
510         ActiveLock lock = getLock(lockInfo.getType(), lockInfo.getScope());
511         if (lock == null) {
512             throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED, "No lock with the given type/scope present on resource " + getResourcePath());
513         }
514
515         lock = lockManager.refreshLock(lockInfo, lockToken, this);
516
517         return lock;
518     }
519
520     public void unlock( String lockToken )
521         throws DavException
522     {
523         ActiveLock lock = getLock(Type.WRITE, Scope.EXCLUSIVE);
524         if (lock == null)
525         {
526             throw new DavException(HttpServletResponse.SC_PRECONDITION_FAILED);
527         }
528         else if (lock.isLockedByToken(lockToken))
529         {
530             lockManager.releaseLock(lockToken, this);
531         }
532         else
533         {
534             throw new DavException(DavServletResponse.SC_LOCKED);
535         }
536     }
537
538     public void addLockManager( LockManager lockManager )
539     {
540         this.lockManager = lockManager;
541     }
542
543     public DavResourceFactory getFactory()
544     {
545         return factory;
546     }
547
548     public DavSession getSession()
549     {
550         return session;
551     }
552
553     /**
554      * Fill the set of properties
555      */
556     protected DavPropertySet initProperties()
557     {
558         if ( !exists() )
559         {
560             properties = new DavPropertySet();
561         }
562         
563         if ( properties != null )
564         {
565             return properties;
566         }
567
568         DavPropertySet properties = new DavPropertySet();
569         
570         // set (or reset) fundamental properties
571         if ( getDisplayName() != null )
572         {
573             properties.add( new DefaultDavProperty( DavPropertyName.DISPLAYNAME, getDisplayName() ) );
574         }
575         if ( isCollection() )
576         {
577             properties.add( new ResourceType( ResourceType.COLLECTION ) );
578             // Windows XP support
579             properties.add( new DefaultDavProperty( DavPropertyName.ISCOLLECTION, "1" ) );
580         }
581         else
582         {
583             properties.add( new ResourceType( ResourceType.DEFAULT_RESOURCE ) );
584
585             // Windows XP support
586             properties.add( new DefaultDavProperty( DavPropertyName.ISCOLLECTION, "0" ) );
587         }
588
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 );
593
594         properties.add( new DefaultDavProperty( DavPropertyName.GETLASTMODIFIED, modifiedDate ) );
595
596         properties.add( new DefaultDavProperty( DavPropertyName.CREATIONDATE, modifiedDate ) );
597
598         properties.add( new DefaultDavProperty( DavPropertyName.GETCONTENTLENGTH, localResource.length() ) );
599         
600         this.properties = properties;
601         
602         return properties;
603     }
604
605     private ArchivaDavResource checkDavResourceIsArchivaDavResource( DavResource resource )
606         throws DavException
607     {
608         if ( !( resource instanceof ArchivaDavResource ) )
609         {
610             throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
611                                     "DavResource is not instance of ArchivaDavResource" );
612         }
613         return (ArchivaDavResource) resource;
614     }
615
616     private void triggerAuditEvent( String remoteIP, String repositoryId, String resource, String action )
617     {
618         AuditEvent event = new AuditEvent( repositoryId, principal, resource, action );
619         event.setRemoteIP( remoteIP );
620
621         for ( AuditListener listener : auditListeners )
622         {
623             listener.auditEvent( event );
624         }
625     }
626 }