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 static final String METHODS =
60 "OPTIONS, GET, HEAD, POST, TRACE, PROPFIND, PROPPATCH, MKCOL, COPY, PUT, DELETE, MOVE";
62 private static final String COMPLIANCE_CLASS = "1";
64 private DavPropertySet properties;
66 private boolean propsInitialized = false;
68 public ArchivaDavResource( String localResource, String logicalResource, MimeTypes mimeTypes,
69 ArchivaDavResourceLocator locator, DavResourceFactory factory )
71 this.mimeTypes = mimeTypes;
72 this.localResource = new File( localResource );
73 this.logicalResource = logicalResource;
74 this.locator = locator;
75 this.factory = factory;
76 this.properties = new DavPropertySet();
79 public String getContentType()
81 return mimeTypes.getMimeType( localResource.getName() );
84 public String getComplianceClass()
86 return COMPLIANCE_CLASS;
89 public String getSupportedMethods()
94 public boolean exists()
96 return localResource.exists();
99 public boolean isCollection()
101 return localResource.isDirectory();
104 public String getDisplayName()
106 String resPath = getResourcePath();
107 return ( resPath != null ) ? Text.getName( resPath ) : resPath;
110 public DavResourceLocator getLocator()
115 public File getLocalResource()
117 return localResource;
120 public String getResourcePath()
122 return locator.getResourcePath();
125 public String getHref()
127 return locator.getHref( isCollection() );
130 public long getModificationTime()
133 return localResource.lastModified();
136 public long getContentLength()
139 return localResource.length();
142 public void spool( OutputContext outputContext )
145 if ( !isCollection() )
147 FileInputStream is = null;
150 outputContext.setContentLength( getContentLength() );
151 outputContext.setContentType( getContentType() );
153 // Write content to stream
154 is = new FileInputStream( localResource );
155 IOUtils.copy( is, outputContext.getOutputStream() );
159 IOUtils.closeQuietly( is );
164 IndexWriter writer = new IndexWriter( this, localResource, logicalResource );
165 writer.write( outputContext );
169 public DavPropertyName[] getPropertyNames()
171 return getProperties().getPropertyNames();
174 public DavProperty getProperty( DavPropertyName name )
177 return properties.get( name );
180 public DavPropertySet getProperties()
186 public void setProperty( DavProperty property )
191 public void removeProperty( DavPropertyName propertyName )
196 public MultiStatusResponse alterProperties( DavPropertySet setProperties, DavPropertyNameSet removePropertyNames )
202 public MultiStatusResponse alterProperties( List changeList )
208 public DavResource getCollection()
210 DavResource parent = null;
211 if ( getResourcePath() != null && !getResourcePath().equals( "/" ) )
213 String parentPath = Text.getRelativeParent( getResourcePath(), 1 );
214 if ( parentPath.equals( "" ) )
218 DavResourceLocator parentloc = locator.getFactory().createResourceLocator( locator.getPrefix(), parentPath );
221 parent = factory.createResource( parentloc, null );
223 catch ( DavException e )
231 public void addMember( DavResource resource, InputContext inputContext )
234 File localFile = new File( localResource, resource.getDisplayName() );
235 if ( isCollection() && inputContext.hasStream() ) // New File
237 boolean deleteFile = false;
238 FileOutputStream stream = null;
241 stream = new FileOutputStream( localFile );
242 IOUtils.copy( inputContext.getInputStream(), stream );
243 if ( inputContext.getContentLength() != localFile.length() )
246 throw new DavException( HttpServletResponse.SC_BAD_REQUEST, "Content Header length was " +
247 inputContext.getContentLength() + " but was " + localFile.length() );
250 catch ( IOException e )
252 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
256 IOUtils.closeQuietly( stream );
259 FileUtils.deleteQuietly( localFile );
263 else if ( !inputContext.hasStream() && isCollection() ) // New directory
269 throw new DavException( HttpServletResponse.SC_BAD_REQUEST, "Could not write member " +
270 resource.getResourcePath() + " at " + getResourcePath() );
274 public DavResourceIterator getMembers()
276 ArrayList list = new ArrayList();
277 if ( exists() && isCollection() )
279 for ( String item : localResource.list() )
283 if ( !item.startsWith( HIDDEN_PATH_PREFIX ) )
285 String path = locator.getResourcePath() + '/' + item;
286 DavResourceLocator resourceLocator =
287 locator.getFactory().createResourceLocator( locator.getPrefix(), path );
288 DavResource resource = factory.createResource( resourceLocator, null );
289 if ( resource != null )
290 list.add( resource );
293 catch ( DavException e )
299 return new DavResourceIteratorImpl( list );
302 public void removeMember( DavResource member )
305 File localResource = checkDavResourceIsArchivaDavResource( member ).getLocalResource();
307 if ( !localResource.exists() )
309 throw new DavException( HttpServletResponse.SC_NOT_FOUND, member.getResourcePath() );
312 boolean suceeded = false;
314 if ( localResource.isDirectory() )
318 FileUtils.deleteDirectory( localResource );
321 catch ( IOException e )
323 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
327 if ( !suceeded && localResource.isFile() )
329 suceeded = localResource.delete();
334 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not delete resource " +
335 member.getResourcePath() );
339 public void move( DavResource destination )
344 throw new DavException( HttpServletResponse.SC_NOT_FOUND, "Resource to copy does not exist." );
349 ArchivaDavResource localResource = checkDavResourceIsArchivaDavResource( destination );
350 if ( isCollection() )
352 FileUtils.moveDirectory( getLocalResource(), localResource.getLocalResource() );
356 FileUtils.moveFile( getLocalResource(), localResource.getLocalResource() );
359 catch ( IOException e )
361 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
365 public void copy( DavResource destination, boolean shallow )
370 throw new DavException( HttpServletResponse.SC_NOT_FOUND, "Resource to copy does not exist." );
373 if ( shallow && isCollection() )
375 throw new DavException( DavServletResponse.SC_FORBIDDEN, "Unable to perform shallow copy for collection" );
380 ArchivaDavResource localResource = checkDavResourceIsArchivaDavResource( destination );
381 if ( isCollection() )
383 FileUtils.copyDirectory( getLocalResource(), localResource.getLocalResource() );
387 FileUtils.copyFile( getLocalResource(), localResource.getLocalResource() );
390 catch ( IOException e )
392 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
396 public boolean isLockable( Type type, Scope scope )
401 public boolean hasLock( Type type, Scope scope )
406 public ActiveLock getLock( Type type, Scope scope )
411 public ActiveLock[] getLocks()
413 return new ActiveLock[0];
416 public ActiveLock lock( LockInfo reqLockInfo )
422 public ActiveLock refreshLock( LockInfo reqLockInfo, String lockToken )
428 public void unlock( String lockToken )
433 public void addLockManager( LockManager lockmgr )
437 public DavResourceFactory getFactory()
442 public DavSession getSession()
448 * Fill the set of properties
450 protected void initProperties()
452 if ( !exists() || propsInitialized )
457 // set (or reset) fundamental properties
458 if ( getDisplayName() != null )
460 properties.add( new DefaultDavProperty( DavPropertyName.DISPLAYNAME, getDisplayName() ) );
462 if ( isCollection() )
464 properties.add( new ResourceType( ResourceType.COLLECTION ) );
465 // Windows XP support
466 properties.add( new DefaultDavProperty( DavPropertyName.ISCOLLECTION, "1" ) );
470 properties.add( new ResourceType( ResourceType.DEFAULT_RESOURCE ) );
472 // Windows XP support
473 properties.add( new DefaultDavProperty( DavPropertyName.ISCOLLECTION, "0" ) );
476 // Need to get the ISO8601 date for properties
477 DateTime dt = new DateTime( localResource.lastModified() );
478 DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
479 String modifiedDate = fmt.print( dt );
481 properties.add( new DefaultDavProperty( DavPropertyName.GETLASTMODIFIED, modifiedDate ) );
483 properties.add( new DefaultDavProperty( DavPropertyName.CREATIONDATE, modifiedDate ) );
485 properties.add( new DefaultDavProperty( DavPropertyName.GETCONTENTLENGTH, localResource.length() ) );
487 propsInitialized = true;
490 private ArchivaDavResource checkDavResourceIsArchivaDavResource( DavResource resource )
493 if ( !( resource instanceof ArchivaDavResource ) )
495 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
496 "DavResource is not instance of ArchivaDavResource" );
498 return (ArchivaDavResource) resource;