1 package org.apache.maven.archiva.webdav;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
36 import javax.servlet.http.HttpServletResponse;
37 import java.util.List;
38 import java.util.ArrayList;
42 * @author <a href="mailto:james@atlassian.com">James William Dumay</a> Portions from the Apache Jackrabbit Project
44 public class ArchivaDavResource
45 implements DavResource
47 public static final String HIDDEN_PATH_PREFIX = ".";
49 private final MimeTypes mimeTypes;
51 private final ArchivaDavResourceLocator locator;
53 private final DavResourceFactory factory;
55 private final File localResource;
57 private final String logicalResource;
59 private DavPropertySet properties;
61 private boolean propsInitialized = false;
63 private LockManager lockManager;
65 private final DavSession session;
67 public ArchivaDavResource( String localResource,
68 String logicalResource,
71 ArchivaDavResourceLocator locator,
72 DavResourceFactory factory )
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();
83 public String getContentType()
85 return mimeTypes.getMimeType( localResource.getName() );
88 public String getComplianceClass()
90 return COMPLIANCE_CLASS;
93 public String getSupportedMethods()
98 public boolean exists()
100 return localResource.exists();
103 public boolean isCollection()
105 return localResource.isDirectory();
108 public String getDisplayName()
110 String resPath = getResourcePath();
111 return ( resPath != null ) ? Text.getName( resPath ) : resPath;
114 public DavResourceLocator getLocator()
119 public File getLocalResource()
121 return localResource;
124 public String getResourcePath()
126 return locator.getResourcePath();
129 public String getHref()
131 return locator.getHref( isCollection() );
134 public long getModificationTime()
137 return localResource.lastModified();
140 public long getContentLength()
143 return localResource.length();
146 public void spool( OutputContext outputContext )
149 if ( !isCollection() )
151 FileInputStream is = null;
154 outputContext.setContentLength( getContentLength() );
155 outputContext.setContentType( getContentType() );
157 // Write content to stream
158 is = new FileInputStream( localResource );
159 IOUtils.copy( is, outputContext.getOutputStream() );
163 IOUtils.closeQuietly( is );
168 IndexWriter writer = new IndexWriter( this, localResource, logicalResource );
169 writer.write( outputContext );
173 public DavPropertyName[] getPropertyNames()
175 return getProperties().getPropertyNames();
178 public DavProperty getProperty( DavPropertyName name )
181 return properties.get( name );
184 public DavPropertySet getProperties()
190 public void setProperty( DavProperty property )
195 public void removeProperty( DavPropertyName propertyName )
200 public MultiStatusResponse alterProperties( DavPropertySet setProperties, DavPropertyNameSet removePropertyNames )
206 public MultiStatusResponse alterProperties( List changeList )
212 public DavResource getCollection()
214 DavResource parent = null;
215 if ( getResourcePath() != null && !getResourcePath().equals( "/" ) )
217 String parentPath = Text.getRelativeParent( getResourcePath(), 1 );
218 if ( parentPath.equals( "" ) )
222 DavResourceLocator parentloc = locator.getFactory().createResourceLocator( locator.getPrefix(), parentPath );
225 parent = factory.createResource( parentloc, session );
227 catch ( DavException e )
235 public void addMember( DavResource resource, InputContext inputContext )
238 File localFile = new File( localResource, resource.getDisplayName() );
239 if ( isCollection() && inputContext.hasStream() ) // New File
241 boolean deleteFile = false;
242 FileOutputStream stream = null;
245 stream = new FileOutputStream( localFile );
246 IOUtils.copy( inputContext.getInputStream(), stream );
247 if ( inputContext.getContentLength() != localFile.length() )
250 throw new DavException( HttpServletResponse.SC_BAD_REQUEST, "Content Header length was " +
251 inputContext.getContentLength() + " but was " + localFile.length() );
254 catch ( IOException e )
256 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
260 IOUtils.closeQuietly( stream );
263 FileUtils.deleteQuietly( localFile );
267 else if ( !inputContext.hasStream() && isCollection() ) // New directory
273 throw new DavException( HttpServletResponse.SC_BAD_REQUEST, "Could not write member " +
274 resource.getResourcePath() + " at " + getResourcePath() );
278 public DavResourceIterator getMembers()
280 ArrayList list = new ArrayList();
281 if ( exists() && isCollection() )
283 for ( String item : localResource.list() )
287 if ( !item.startsWith( HIDDEN_PATH_PREFIX ) )
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 );
297 catch ( DavException e )
303 return new DavResourceIteratorImpl( list );
306 public void removeMember( DavResource member )
309 File resource = checkDavResourceIsArchivaDavResource( member ).getLocalResource();
311 if ( !resource.exists() )
313 throw new DavException( HttpServletResponse.SC_NOT_FOUND, member.getResourcePath() );
316 boolean suceeded = false;
318 if ( resource.isDirectory() )
322 FileUtils.deleteDirectory( resource );
325 catch ( IOException e )
327 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
331 if ( !suceeded && resource.isFile() )
333 suceeded = resource.delete();
338 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not delete resource " +
339 member.getResourcePath() );
343 public void move( DavResource destination )
348 throw new DavException( HttpServletResponse.SC_NOT_FOUND, "Resource to copy does not exist." );
353 ArchivaDavResource resource = checkDavResourceIsArchivaDavResource( destination );
354 if ( isCollection() )
356 FileUtils.moveDirectory( getLocalResource(), resource.getLocalResource() );
360 FileUtils.moveFile( getLocalResource(), resource.getLocalResource() );
363 catch ( IOException e )
365 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
369 public void copy( DavResource destination, boolean shallow )
374 throw new DavException( HttpServletResponse.SC_NOT_FOUND, "Resource to copy does not exist." );
377 if ( shallow && isCollection() )
379 throw new DavException( DavServletResponse.SC_FORBIDDEN, "Unable to perform shallow copy for collection" );
384 ArchivaDavResource resource = checkDavResourceIsArchivaDavResource( destination );
385 if ( isCollection() )
387 FileUtils.copyDirectory( getLocalResource(), resource.getLocalResource() );
391 FileUtils.copyFile( getLocalResource(), resource.getLocalResource() );
394 catch ( IOException e )
396 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
400 public boolean isLockable( Type type, Scope scope )
402 return Type.WRITE.equals(type) && Scope.EXCLUSIVE.equals(scope);
405 public boolean hasLock( Type type, Scope scope )
407 return getLock(type, scope) != null;
410 public ActiveLock getLock( Type type, Scope scope )
412 ActiveLock lock = null;
413 if (exists() && Type.WRITE.equals(type) && Scope.EXCLUSIVE.equals(scope))
415 lock = lockManager.getLock(type, scope, this);
420 public ActiveLock[] getLocks()
422 ActiveLock writeLock = getLock(Type.WRITE, Scope.EXCLUSIVE);
423 return (writeLock != null) ? new ActiveLock[]{writeLock} : new ActiveLock[0];
426 public ActiveLock lock( LockInfo lockInfo )
429 ActiveLock lock = null;
430 if (isLockable(lockInfo.getType(), lockInfo.getScope()))
432 lock = lockManager.createLock(lockInfo, this);
436 throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED, "Unsupported lock type or scope.");
441 public ActiveLock refreshLock( LockInfo lockInfo, String lockToken )
445 throw new DavException(DavServletResponse.SC_NOT_FOUND);
447 ActiveLock lock = getLock(lockInfo.getType(), lockInfo.getScope());
449 throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED, "No lock with the given type/scope present on resource " + getResourcePath());
452 lock = lockManager.refreshLock(lockInfo, lockToken, this);
457 public void unlock( String lockToken )
460 ActiveLock lock = getLock(Type.WRITE, Scope.EXCLUSIVE);
463 throw new DavException(HttpServletResponse.SC_PRECONDITION_FAILED);
465 else if (lock.isLockedByToken(lockToken))
467 lockManager.releaseLock(lockToken, this);
471 throw new DavException(DavServletResponse.SC_LOCKED);
475 public void addLockManager( LockManager lockManager )
477 this.lockManager = lockManager;
480 public DavResourceFactory getFactory()
485 public DavSession getSession()
491 * Fill the set of properties
493 protected void initProperties()
495 if ( !exists() || propsInitialized )
500 // set (or reset) fundamental properties
501 if ( getDisplayName() != null )
503 properties.add( new DefaultDavProperty( DavPropertyName.DISPLAYNAME, getDisplayName() ) );
505 if ( isCollection() )
507 properties.add( new ResourceType( ResourceType.COLLECTION ) );
508 // Windows XP support
509 properties.add( new DefaultDavProperty( DavPropertyName.ISCOLLECTION, "1" ) );
513 properties.add( new ResourceType( ResourceType.DEFAULT_RESOURCE ) );
515 // Windows XP support
516 properties.add( new DefaultDavProperty( DavPropertyName.ISCOLLECTION, "0" ) );
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 );
524 properties.add( new DefaultDavProperty( DavPropertyName.GETLASTMODIFIED, modifiedDate ) );
526 properties.add( new DefaultDavProperty( DavPropertyName.CREATIONDATE, modifiedDate ) );
528 properties.add( new DefaultDavProperty( DavPropertyName.GETCONTENTLENGTH, localResource.length() ) );
530 propsInitialized = true;
533 private ArchivaDavResource checkDavResourceIsArchivaDavResource( DavResource resource )
536 if ( !( resource instanceof ArchivaDavResource ) )
538 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
539 "DavResource is not instance of ArchivaDavResource" );
541 return (ArchivaDavResource) resource;