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.repository.storage.StorageAsset;
23 import org.apache.archiva.webdav.util.IndexWriter;
24 import org.apache.archiva.webdav.util.MimeTypes;
25 import org.apache.jackrabbit.util.Text;
26 import org.apache.jackrabbit.webdav.DavException;
27 import org.apache.jackrabbit.webdav.DavResource;
28 import org.apache.jackrabbit.webdav.DavResourceFactory;
29 import org.apache.jackrabbit.webdav.DavResourceIterator;
30 import org.apache.jackrabbit.webdav.DavResourceLocator;
31 import org.apache.jackrabbit.webdav.DavSession;
32 import org.apache.jackrabbit.webdav.MultiStatusResponse;
33 import org.apache.jackrabbit.webdav.io.InputContext;
34 import org.apache.jackrabbit.webdav.io.OutputContext;
35 import org.apache.jackrabbit.webdav.lock.ActiveLock;
36 import org.apache.jackrabbit.webdav.lock.LockInfo;
37 import org.apache.jackrabbit.webdav.lock.LockManager;
38 import org.apache.jackrabbit.webdav.lock.Scope;
39 import org.apache.jackrabbit.webdav.lock.Type;
40 import org.apache.jackrabbit.webdav.property.DavProperty;
41 import org.apache.jackrabbit.webdav.property.DavPropertyName;
42 import org.apache.jackrabbit.webdav.property.DavPropertyNameSet;
43 import org.apache.jackrabbit.webdav.property.DavPropertySet;
44 import org.apache.jackrabbit.webdav.property.DefaultDavProperty;
45 import org.apache.jackrabbit.webdav.property.ResourceType;
46 import org.joda.time.DateTime;
47 import org.joda.time.format.DateTimeFormatter;
48 import org.joda.time.format.ISODateTimeFormat;
51 import java.util.stream.Collectors;
54 * DavResource for virtual repositories
56 public class ArchivaVirtualDavResource
57 implements DavResource
59 private static final String COMPLIANCE_CLASS = "1";
61 private ArchivaDavResourceLocator locator;
63 private DavResourceFactory factory;
65 private String logicalResource;
67 private DavPropertySet properties;
69 private boolean propsInitialized = false;
71 private static final String METHODS = "OPTIONS, GET, HEAD, POST, TRACE, PROPFIND, PROPPATCH, MKCOL";
73 private final List<StorageAsset> localResources;
75 public ArchivaVirtualDavResource(List<StorageAsset> localResources, String logicalResource, MimeTypes mimeTypes,
76 ArchivaDavResourceLocator locator, DavResourceFactory factory )
78 this.localResources = localResources;
79 this.logicalResource = logicalResource;
80 this.locator = locator;
81 this.factory = factory;
82 this.properties = new DavPropertySet();
86 public void spool( OutputContext outputContext ) {
87 if ( outputContext.hasStream() )
89 List<StorageAsset> localResourceFiles = localResources.stream().filter(Objects::nonNull)
90 .filter(repoAsset -> repoAsset.exists())
91 .sorted(Comparator.comparing(o -> o.getName())).collect(Collectors.toList());
93 IndexWriter writer = new IndexWriter(localResourceFiles, logicalResource );
94 writer.write( outputContext );
99 public void addLockManager( LockManager arg0 )
105 public void addMember( DavResource arg0, InputContext arg1 )
111 @SuppressWarnings( "unchecked" )
113 public MultiStatusResponse alterProperties( List arg0 )
119 public MultiStatusResponse alterProperties( DavPropertySet arg0, DavPropertyNameSet arg1 )
126 public void copy( DavResource arg0, boolean arg1 )
133 public boolean exists()
135 // localResources are already filtered (all files in the list are already existing)
140 public ActiveLock getLock( Type arg0, Scope arg1 )
146 public ActiveLock[] getLocks()
152 public DavResourceIterator getMembers()
158 public String getSupportedMethods()
164 public long getModificationTime()
170 public boolean hasLock( Type arg0, Scope arg1 )
176 public boolean isCollection()
182 public boolean isLockable( Type arg0, Scope arg1 )
188 public ActiveLock lock( LockInfo arg0 )
195 public void move( DavResource arg0 )
202 public ActiveLock refreshLock( LockInfo arg0, String arg1 )
209 public void removeMember( DavResource arg0 )
216 public void unlock( String arg0 )
223 public String getComplianceClass()
225 return COMPLIANCE_CLASS;
229 public DavResourceLocator getLocator()
235 public String getResourcePath()
237 return locator.getResourcePath();
241 public String getHref()
243 return locator.getHref( isCollection() );
247 public DavResourceFactory getFactory()
253 public String getDisplayName()
255 String resPath = getResourcePath();
257 return ( resPath != null ) ? Text.getName( resPath ) : resPath;
261 public DavSession getSession()
267 public DavPropertyName[] getPropertyNames()
269 return getProperties().getPropertyNames();
273 public DavProperty getProperty( DavPropertyName name )
276 return properties.get( name );
280 public DavPropertySet getProperties()
287 public void setProperty( DavProperty property )
293 public void removeProperty( DavPropertyName propertyName )
299 public DavResource getCollection()
301 DavResource parent = null;
302 if ( getResourcePath() != null && !getResourcePath().equals( "/" ) )
304 String parentPath = Text.getRelativeParent( getResourcePath(), 1 );
305 if ( parentPath.equals( "" ) )
309 DavResourceLocator parentloc =
310 locator.getFactory().createResourceLocator( locator.getPrefix(), parentPath );
313 // go back to ArchivaDavResourceFactory!
314 parent = factory.createResource( parentloc, null );
316 catch ( DavException e )
325 * Fill the set of properties
327 protected void initProperties()
329 if ( !exists() || propsInitialized )
334 // set (or reset) fundamental properties
335 if ( getDisplayName() != null )
337 properties.add( new DefaultDavProperty<>( DavPropertyName.DISPLAYNAME, getDisplayName() ) );
339 if ( isCollection() )
341 properties.add( new ResourceType( ResourceType.COLLECTION ) );
342 // Windows XP support
343 properties.add( new DefaultDavProperty<>( DavPropertyName.ISCOLLECTION, "1" ) );
347 properties.add( new ResourceType( ResourceType.DEFAULT_RESOURCE ) );
349 // Windows XP support
350 properties.add( new DefaultDavProperty<>( DavPropertyName.ISCOLLECTION, "0" ) );
353 // Need to get the ISO8601 date for properties
354 DateTime dt = new DateTime( 0 );
355 DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
356 String modifiedDate = fmt.print( dt );
358 properties.add( new DefaultDavProperty<>( DavPropertyName.GETLASTMODIFIED, modifiedDate ) );
360 properties.add( new DefaultDavProperty<>( DavPropertyName.CREATIONDATE, modifiedDate ) );
362 properties.add( new DefaultDavProperty<>( DavPropertyName.GETCONTENTLENGTH, 0 ) );
364 propsInitialized = true;
367 public String getLogicalResource()
369 return logicalResource;
372 public void setLogicalResource( String logicalResource )
374 this.logicalResource = logicalResource;