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