]> source.dussan.org Git - archiva.git/blob
7d0efa9f487542183110d54637a094b2ae0193a8
[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 org.apache.jackrabbit.webdav.*;
23 import org.apache.jackrabbit.webdav.property.*;
24 import org.apache.jackrabbit.webdav.io.InputContext;
25 import org.apache.jackrabbit.webdav.io.OutputContext;
26 import org.apache.jackrabbit.webdav.lock.*;
27 import org.apache.jackrabbit.util.Text;
28 import org.apache.commons.io.IOUtils;
29 import org.apache.commons.io.FileUtils;
30 import org.apache.maven.archiva.webdav.util.MimeTypes;
31 import org.apache.maven.archiva.webdav.util.IndexWriter;
32 import org.joda.time.DateTime;
33 import org.joda.time.format.DateTimeFormatter;
34 import org.joda.time.format.ISODateTimeFormat;
35
36 import javax.servlet.http.HttpServletResponse;
37 import java.util.List;
38 import java.util.ArrayList;
39 import java.io.*;
40
41 /**
42  * @author <a href="mailto:james@atlassian.com">James William Dumay</a> Portions from the Apache Jackrabbit Project
43  */
44 public class ArchivaDavResource
45     implements DavResource
46 {
47     public static final String HIDDEN_PATH_PREFIX = ".";
48
49     private final MimeTypes mimeTypes;
50
51     private final ArchivaDavResourceLocator locator;
52
53     private final DavResourceFactory factory;
54
55     private final File localResource;
56
57     private final String logicalResource;
58
59     private DavPropertySet properties;
60
61     private boolean propsInitialized = false;
62     
63     private LockManager lockManager;
64     
65     private final DavSession session;
66
67     public ArchivaDavResource( String localResource, 
68                                String logicalResource,
69                                MimeTypes mimeTypes,
70                                DavSession session,
71                                ArchivaDavResourceLocator locator, 
72                                DavResourceFactory factory )
73     {
74         this.mimeTypes = mimeTypes;
75         this.localResource = new File( localResource );
76         this.logicalResource = logicalResource;
77         this.locator = locator;
78         this.factory = factory;
79         this.session = session;
80         this.properties = new DavPropertySet();
81     }
82
83     public String getContentType()
84     {
85         return mimeTypes.getMimeType( localResource.getName() );
86     }
87
88     public String getComplianceClass()
89     {
90         return COMPLIANCE_CLASS;
91     }
92
93     public String getSupportedMethods()
94     {
95         return METHODS;
96     }
97
98     public boolean exists()
99     {
100         return localResource.exists();
101     }
102
103     public boolean isCollection()
104     {
105         return localResource.isDirectory();
106     }
107
108     public String getDisplayName()
109     {
110         String resPath = getResourcePath();
111         return ( resPath != null ) ? Text.getName( resPath ) : resPath;
112     }
113
114     public DavResourceLocator getLocator()
115     {
116         return locator;
117     }
118
119     public File getLocalResource()
120     {
121         return localResource;
122     }
123
124     public String getResourcePath()
125     {
126         return locator.getResourcePath();
127     }
128
129     public String getHref()
130     {
131         return locator.getHref( isCollection() );
132     }
133
134     public long getModificationTime()
135     {
136         initProperties();
137         return localResource.lastModified();
138     }
139
140     public long getContentLength()
141     {
142         initProperties();
143         return localResource.length();
144     }
145
146     public void spool( OutputContext outputContext )
147         throws IOException
148     {
149         if ( !isCollection() )
150         {
151             FileInputStream is = null;
152             try
153             {
154                 outputContext.setContentLength( getContentLength() );
155                 outputContext.setContentType( getContentType() );
156
157                 // Write content to stream
158                 is = new FileInputStream( localResource );
159                 IOUtils.copy( is, outputContext.getOutputStream() );
160             }
161             finally
162             {
163                 IOUtils.closeQuietly( is );
164             }
165         }
166         else
167         {
168             IndexWriter writer = new IndexWriter( this, localResource, logicalResource );
169             writer.write( outputContext );
170         }
171     }
172
173     public DavPropertyName[] getPropertyNames()
174     {
175         return getProperties().getPropertyNames();
176     }
177
178     public DavProperty getProperty( DavPropertyName name )
179     {
180         initProperties();
181         return properties.get( name );
182     }
183
184     public DavPropertySet getProperties()
185     {
186         initProperties();
187         return properties;
188     }
189
190     public void setProperty( DavProperty property )
191         throws DavException
192     {
193     }
194
195     public void removeProperty( DavPropertyName propertyName )
196         throws DavException
197     {
198     }
199
200     public MultiStatusResponse alterProperties( DavPropertySet setProperties, DavPropertyNameSet removePropertyNames )
201         throws DavException
202     {
203         return null;
204     }
205
206     public MultiStatusResponse alterProperties( List changeList )
207         throws DavException
208     {
209         return null;
210     }
211
212     public DavResource getCollection()
213     {
214         DavResource parent = null;
215         if ( getResourcePath() != null && !getResourcePath().equals( "/" ) )
216         {
217             String parentPath = Text.getRelativeParent( getResourcePath(), 1 );
218             if ( parentPath.equals( "" ) )
219             {
220                 parentPath = "/";
221             }
222             DavResourceLocator parentloc = locator.getFactory().createResourceLocator( locator.getPrefix(), parentPath );
223             try
224             {
225                 parent = factory.createResource( parentloc, session );
226             }
227             catch ( DavException e )
228             {
229                 // should not occur
230             }
231         }
232         return parent;
233     }
234
235     public void addMember( DavResource resource, InputContext inputContext )
236         throws DavException
237     {
238         File localFile = new File( localResource, resource.getDisplayName() );
239         if ( isCollection() && inputContext.hasStream() ) // New File
240         {
241             boolean deleteFile = false;
242             FileOutputStream stream = null;
243             try
244             {
245                 stream = new FileOutputStream( localFile );
246                 IOUtils.copy( inputContext.getInputStream(), stream );
247                 if ( inputContext.getContentLength() != localFile.length() )
248                 {
249                     deleteFile = true;
250                     throw new DavException( HttpServletResponse.SC_BAD_REQUEST, "Content Header length was " +
251                         inputContext.getContentLength() + " but was " + localFile.length() );
252                 }
253             }
254             catch ( IOException e )
255             {
256                 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
257             }
258             finally
259             {
260                 IOUtils.closeQuietly( stream );
261                 if ( deleteFile )
262                 {
263                     FileUtils.deleteQuietly( localFile );
264                 }
265             }
266         }
267         else if ( !inputContext.hasStream() && isCollection() ) // New directory
268         {
269             localFile.mkdir();
270         }
271         else
272         {
273             throw new DavException( HttpServletResponse.SC_BAD_REQUEST, "Could not write member " +
274                 resource.getResourcePath() + " at " + getResourcePath() );
275         }
276     }
277
278     public DavResourceIterator getMembers()
279     {
280         ArrayList list = new ArrayList();
281         if ( exists() && isCollection() )
282         {
283             for ( String item : localResource.list() )
284             {
285                 try
286                 {
287                     if ( !item.startsWith( HIDDEN_PATH_PREFIX ) )
288                     {
289                         String path = locator.getResourcePath() + '/' + item;
290                         DavResourceLocator resourceLocator =
291                             locator.getFactory().createResourceLocator( locator.getPrefix(), path );
292                         DavResource resource = factory.createResource( resourceLocator, session );
293                         if ( resource != null )
294                             list.add( resource );
295                     }
296                 }
297                 catch ( DavException e )
298                 {
299                     // Should not occur
300                 }
301             }
302         }
303         return new DavResourceIteratorImpl( list );
304     }
305
306     public void removeMember( DavResource member )
307         throws DavException
308     {
309         File resource = checkDavResourceIsArchivaDavResource( member ).getLocalResource();
310
311         if ( !resource.exists() )
312         {
313             throw new DavException( HttpServletResponse.SC_NOT_FOUND, member.getResourcePath() );
314         }
315
316         boolean suceeded = false;
317
318         if ( resource.isDirectory() )
319         {
320             try
321             {
322                 FileUtils.deleteDirectory( resource );
323                 suceeded = true;
324             }
325             catch ( IOException e )
326             {
327                 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
328             }
329         }
330
331         if ( !suceeded && resource.isFile() )
332         {
333             suceeded = resource.delete();
334         }
335
336         if ( !suceeded )
337         {
338             throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not delete resource " +
339                 member.getResourcePath() );
340         }
341     }
342
343     public void move( DavResource destination )
344         throws DavException
345     {
346         if ( !exists() )
347         {
348             throw new DavException( HttpServletResponse.SC_NOT_FOUND, "Resource to copy does not exist." );
349         }
350
351         try
352         {
353             ArchivaDavResource resource = checkDavResourceIsArchivaDavResource( destination );
354             if ( isCollection() )
355             {
356                 FileUtils.moveDirectory( getLocalResource(), resource.getLocalResource() );
357             }
358             else
359             {
360                 FileUtils.moveFile( getLocalResource(), resource.getLocalResource() );
361             }
362         }
363         catch ( IOException e )
364         {
365             throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
366         }
367     }
368
369     public void copy( DavResource destination, boolean shallow )
370         throws DavException
371     {
372         if ( !exists() )
373         {
374             throw new DavException( HttpServletResponse.SC_NOT_FOUND, "Resource to copy does not exist." );
375         }
376
377         if ( shallow && isCollection() )
378         {
379             throw new DavException( DavServletResponse.SC_FORBIDDEN, "Unable to perform shallow copy for collection" );
380         }
381
382         try
383         {
384             ArchivaDavResource resource = checkDavResourceIsArchivaDavResource( destination );
385             if ( isCollection() )
386             {
387                 FileUtils.copyDirectory( getLocalResource(), resource.getLocalResource() );
388             }
389             else
390             {
391                 FileUtils.copyFile( getLocalResource(), resource.getLocalResource() );
392             }
393         }
394         catch ( IOException e )
395         {
396             throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
397         }
398     }
399
400     public boolean isLockable( Type type, Scope scope )
401     {
402         return Type.WRITE.equals(type) && Scope.EXCLUSIVE.equals(scope);
403     }
404
405     public boolean hasLock( Type type, Scope scope )
406     {
407         return getLock(type, scope) != null;
408     }
409
410     public ActiveLock getLock( Type type, Scope scope )
411     {
412         ActiveLock lock = null;
413         if (exists() && Type.WRITE.equals(type) && Scope.EXCLUSIVE.equals(scope)) 
414         {
415             lock = lockManager.getLock(type, scope, this);
416         }
417         return lock;
418     }
419
420     public ActiveLock[] getLocks()
421     {
422         ActiveLock writeLock = getLock(Type.WRITE, Scope.EXCLUSIVE);
423         return (writeLock != null) ? new ActiveLock[]{writeLock} : new ActiveLock[0];
424     }
425
426     public ActiveLock lock( LockInfo lockInfo )
427         throws DavException
428     {
429         ActiveLock lock = null;
430         if (isLockable(lockInfo.getType(), lockInfo.getScope())) 
431         {
432             lock = lockManager.createLock(lockInfo, this);
433         }
434         else 
435         {
436             throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED, "Unsupported lock type or scope.");
437         }
438         return lock;
439     }
440
441     public ActiveLock refreshLock( LockInfo lockInfo, String lockToken )
442         throws DavException
443     {
444         if (!exists()) {
445             throw new DavException(DavServletResponse.SC_NOT_FOUND);
446         }
447         ActiveLock lock = getLock(lockInfo.getType(), lockInfo.getScope());
448         if (lock == null) {
449             throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED, "No lock with the given type/scope present on resource " + getResourcePath());
450         }
451
452         lock = lockManager.refreshLock(lockInfo, lockToken, this);
453
454         return lock;
455     }
456
457     public void unlock( String lockToken )
458         throws DavException
459     {
460         ActiveLock lock = getLock(Type.WRITE, Scope.EXCLUSIVE);
461         if (lock == null)
462         {
463             throw new DavException(HttpServletResponse.SC_PRECONDITION_FAILED);
464         }
465         else if (lock.isLockedByToken(lockToken))
466         {
467             lockManager.releaseLock(lockToken, this);
468         }
469         else
470         {
471             throw new DavException(DavServletResponse.SC_LOCKED);
472         }
473     }
474
475     public void addLockManager( LockManager lockManager )
476     {
477         this.lockManager = lockManager;
478     }
479
480     public DavResourceFactory getFactory()
481     {
482         return factory;
483     }
484
485     public DavSession getSession()
486     {
487         return session;
488     }
489
490     /**
491      * Fill the set of properties
492      */
493     protected void initProperties()
494     {
495         if ( !exists() || propsInitialized )
496         {
497             return;
498         }
499
500         // set (or reset) fundamental properties
501         if ( getDisplayName() != null )
502         {
503             properties.add( new DefaultDavProperty( DavPropertyName.DISPLAYNAME, getDisplayName() ) );
504         }
505         if ( isCollection() )
506         {
507             properties.add( new ResourceType( ResourceType.COLLECTION ) );
508             // Windows XP support
509             properties.add( new DefaultDavProperty( DavPropertyName.ISCOLLECTION, "1" ) );
510         }
511         else
512         {
513             properties.add( new ResourceType( ResourceType.DEFAULT_RESOURCE ) );
514
515             // Windows XP support
516             properties.add( new DefaultDavProperty( DavPropertyName.ISCOLLECTION, "0" ) );
517         }
518
519         // Need to get the ISO8601 date for properties
520         DateTime dt = new DateTime( localResource.lastModified() );
521         DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
522         String modifiedDate = fmt.print( dt );
523
524         properties.add( new DefaultDavProperty( DavPropertyName.GETLASTMODIFIED, modifiedDate ) );
525
526         properties.add( new DefaultDavProperty( DavPropertyName.CREATIONDATE, modifiedDate ) );
527
528         properties.add( new DefaultDavProperty( DavPropertyName.GETCONTENTLENGTH, localResource.length() ) );
529
530         propsInitialized = true;
531     }
532
533     private ArchivaDavResource checkDavResourceIsArchivaDavResource( DavResource resource )
534         throws DavException
535     {
536         if ( !( resource instanceof ArchivaDavResource ) )
537         {
538             throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
539                                     "DavResource is not instance of ArchivaDavResource" );
540         }
541         return (ArchivaDavResource) resource;
542     }
543 }