1 package org.apache.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.archiva.webdav.util.IndexWriter;
23 import org.apache.archiva.webdav.util.MimeTypes;
24 import org.apache.jackrabbit.util.Text;
25 import org.apache.jackrabbit.webdav.DavException;
26 import org.apache.jackrabbit.webdav.DavResource;
27 import org.apache.jackrabbit.webdav.DavResourceFactory;
28 import org.apache.jackrabbit.webdav.DavResourceIterator;
29 import org.apache.jackrabbit.webdav.DavResourceLocator;
30 import org.apache.jackrabbit.webdav.DavSession;
31 import org.apache.jackrabbit.webdav.MultiStatusResponse;
32 import org.apache.jackrabbit.webdav.io.InputContext;
33 import org.apache.jackrabbit.webdav.io.OutputContext;
34 import org.apache.jackrabbit.webdav.lock.ActiveLock;
35 import org.apache.jackrabbit.webdav.lock.LockInfo;
36 import org.apache.jackrabbit.webdav.lock.LockManager;
37 import org.apache.jackrabbit.webdav.lock.Scope;
38 import org.apache.jackrabbit.webdav.lock.Type;
39 import org.apache.jackrabbit.webdav.property.DavProperty;
40 import org.apache.jackrabbit.webdav.property.DavPropertyName;
41 import org.apache.jackrabbit.webdav.property.DavPropertyNameSet;
42 import org.apache.jackrabbit.webdav.property.DavPropertySet;
43 import org.apache.jackrabbit.webdav.property.DefaultDavProperty;
44 import org.apache.jackrabbit.webdav.property.ResourceType;
45 import org.joda.time.DateTime;
46 import org.joda.time.format.DateTimeFormatter;
47 import org.joda.time.format.ISODateTimeFormat;
49 import java.io.IOException;
50 import java.nio.file.Files;
51 import java.nio.file.Path;
52 import java.util.ArrayList;
53 import java.util.Collections;
54 import java.util.List;
57 * DavResource for virtual repositories
59 public class ArchivaVirtualDavResource
60 implements DavResource
62 private static final String COMPLIANCE_CLASS = "1";
64 private ArchivaDavResourceLocator locator;
66 private DavResourceFactory factory;
68 private String logicalResource;
70 private DavPropertySet properties;
72 private boolean propsInitialized = false;
74 private static final String METHODS = "OPTIONS, GET, HEAD, POST, TRACE, PROPFIND, PROPPATCH, MKCOL";
76 private final List<Path> localResources;
78 public ArchivaVirtualDavResource( List<Path> localResources, String logicalResource, MimeTypes mimeTypes,
79 ArchivaDavResourceLocator locator, DavResourceFactory factory )
81 this.localResources = localResources;
82 this.logicalResource = logicalResource;
83 this.locator = locator;
84 this.factory = factory;
85 this.properties = new DavPropertySet();
89 public void spool( OutputContext outputContext )
92 if ( outputContext.hasStream() )
94 Collections.sort( localResources );
95 List<Path> localResourceFiles = new ArrayList<>();
97 for ( Path resourceFile : localResources )
99 if ( Files.exists(resourceFile) )
101 localResourceFiles.add( resourceFile );
105 IndexWriter writer = new IndexWriter( this, localResourceFiles, logicalResource );
106 writer.write( outputContext );
111 public void addLockManager( LockManager arg0 )
117 public void addMember( DavResource arg0, InputContext arg1 )
123 @SuppressWarnings( "unchecked" )
125 public MultiStatusResponse alterProperties( List arg0 )
131 public MultiStatusResponse alterProperties( DavPropertySet arg0, DavPropertyNameSet arg1 )
138 public void copy( DavResource arg0, boolean arg1 )
145 public boolean exists()
147 // localResources are already filtered (all files in the list are already existing)
152 public ActiveLock getLock( Type arg0, Scope arg1 )
158 public ActiveLock[] getLocks()
164 public DavResourceIterator getMembers()
170 public String getSupportedMethods()
176 public long getModificationTime()
182 public boolean hasLock( Type arg0, Scope arg1 )
188 public boolean isCollection()
194 public boolean isLockable( Type arg0, Scope arg1 )
200 public ActiveLock lock( LockInfo arg0 )
207 public void move( DavResource arg0 )
214 public ActiveLock refreshLock( LockInfo arg0, String arg1 )
221 public void removeMember( DavResource arg0 )
228 public void unlock( String arg0 )
235 public String getComplianceClass()
237 return COMPLIANCE_CLASS;
241 public DavResourceLocator getLocator()
247 public String getResourcePath()
249 return locator.getResourcePath();
253 public String getHref()
255 return locator.getHref( isCollection() );
259 public DavResourceFactory getFactory()
265 public String getDisplayName()
267 String resPath = getResourcePath();
269 return ( resPath != null ) ? Text.getName( resPath ) : resPath;
273 public DavSession getSession()
279 public DavPropertyName[] getPropertyNames()
281 return getProperties().getPropertyNames();
285 public DavProperty getProperty( DavPropertyName name )
288 return properties.get( name );
292 public DavPropertySet getProperties()
299 public void setProperty( DavProperty property )
305 public void removeProperty( DavPropertyName propertyName )
311 public DavResource getCollection()
313 DavResource parent = null;
314 if ( getResourcePath() != null && !getResourcePath().equals( "/" ) )
316 String parentPath = Text.getRelativeParent( getResourcePath(), 1 );
317 if ( parentPath.equals( "" ) )
321 DavResourceLocator parentloc =
322 locator.getFactory().createResourceLocator( locator.getPrefix(), parentPath );
325 // go back to ArchivaDavResourceFactory!
326 parent = factory.createResource( parentloc, null );
328 catch ( DavException e )
337 * Fill the set of properties
339 protected void initProperties()
341 if ( !exists() || propsInitialized )
346 // set (or reset) fundamental properties
347 if ( getDisplayName() != null )
349 properties.add( new DefaultDavProperty( DavPropertyName.DISPLAYNAME, getDisplayName() ) );
351 if ( isCollection() )
353 properties.add( new ResourceType( ResourceType.COLLECTION ) );
354 // Windows XP support
355 properties.add( new DefaultDavProperty( DavPropertyName.ISCOLLECTION, "1" ) );
359 properties.add( new ResourceType( ResourceType.DEFAULT_RESOURCE ) );
361 // Windows XP support
362 properties.add( new DefaultDavProperty( DavPropertyName.ISCOLLECTION, "0" ) );
365 // Need to get the ISO8601 date for properties
366 DateTime dt = new DateTime( 0 );
367 DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
368 String modifiedDate = fmt.print( dt );
370 properties.add( new DefaultDavProperty( DavPropertyName.GETLASTMODIFIED, modifiedDate ) );
372 properties.add( new DefaultDavProperty( DavPropertyName.CREATIONDATE, modifiedDate ) );
374 properties.add( new DefaultDavProperty( DavPropertyName.GETCONTENTLENGTH, 0 ) );
376 propsInitialized = true;
379 public String getLogicalResource()
381 return logicalResource;
384 public void setLogicalResource( String logicalResource )
386 this.logicalResource = logicalResource;