123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- package org.apache.archiva.webdav;
-
- /*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
- import org.apache.archiva.webdav.util.IndexWriter;
- import org.apache.archiva.webdav.util.MimeTypes;
- import org.apache.jackrabbit.util.Text;
- import org.apache.jackrabbit.webdav.DavException;
- import org.apache.jackrabbit.webdav.DavResource;
- import org.apache.jackrabbit.webdav.DavResourceFactory;
- import org.apache.jackrabbit.webdav.DavResourceIterator;
- import org.apache.jackrabbit.webdav.DavResourceLocator;
- import org.apache.jackrabbit.webdav.DavSession;
- import org.apache.jackrabbit.webdav.MultiStatusResponse;
- import org.apache.jackrabbit.webdav.io.InputContext;
- import org.apache.jackrabbit.webdav.io.OutputContext;
- import org.apache.jackrabbit.webdav.lock.ActiveLock;
- import org.apache.jackrabbit.webdav.lock.LockInfo;
- import org.apache.jackrabbit.webdav.lock.LockManager;
- import org.apache.jackrabbit.webdav.lock.Scope;
- import org.apache.jackrabbit.webdav.lock.Type;
- import org.apache.jackrabbit.webdav.property.DavProperty;
- import org.apache.jackrabbit.webdav.property.DavPropertyName;
- import org.apache.jackrabbit.webdav.property.DavPropertyNameSet;
- import org.apache.jackrabbit.webdav.property.DavPropertySet;
- import org.apache.jackrabbit.webdav.property.DefaultDavProperty;
- import org.apache.jackrabbit.webdav.property.ResourceType;
- import org.joda.time.DateTime;
- import org.joda.time.format.DateTimeFormatter;
- import org.joda.time.format.ISODateTimeFormat;
-
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
-
- /**
- * DavResource for virtual repositories
- */
- public class ArchivaVirtualDavResource
- implements DavResource
- {
- private static final String COMPLIANCE_CLASS = "1";
-
- private ArchivaDavResourceLocator locator;
-
- private DavResourceFactory factory;
-
- private String logicalResource;
-
- private DavPropertySet properties;
-
- private boolean propsInitialized = false;
-
- private static final String METHODS = "OPTIONS, GET, HEAD, POST, TRACE, PROPFIND, PROPPATCH, MKCOL";
-
- private final List<Path> localResources;
-
- public ArchivaVirtualDavResource( List<Path> localResources, String logicalResource, MimeTypes mimeTypes,
- ArchivaDavResourceLocator locator, DavResourceFactory factory )
- {
- this.localResources = localResources;
- this.logicalResource = logicalResource;
- this.locator = locator;
- this.factory = factory;
- this.properties = new DavPropertySet();
- }
-
- @Override
- public void spool( OutputContext outputContext )
- throws IOException
- {
- if ( outputContext.hasStream() )
- {
- Collections.sort( localResources );
- List<Path> localResourceFiles = new ArrayList<>();
-
- for ( Path resourceFile : localResources )
- {
- if ( Files.exists(resourceFile) )
- {
- localResourceFiles.add( resourceFile );
- }
- }
-
- IndexWriter writer = new IndexWriter( this, localResourceFiles, logicalResource );
- writer.write( outputContext );
- }
- }
-
- @Override
- public void addLockManager( LockManager arg0 )
- {
-
- }
-
- @Override
- public void addMember( DavResource arg0, InputContext arg1 )
- throws DavException
- {
-
- }
-
- @SuppressWarnings( "unchecked" )
- @Override
- public MultiStatusResponse alterProperties( List arg0 )
- throws DavException
- {
- return null;
- }
-
- public MultiStatusResponse alterProperties( DavPropertySet arg0, DavPropertyNameSet arg1 )
- throws DavException
- {
- return null;
- }
-
- @Override
- public void copy( DavResource arg0, boolean arg1 )
- throws DavException
- {
-
- }
-
- @Override
- public boolean exists()
- {
- // localResources are already filtered (all files in the list are already existing)
- return true;
- }
-
- @Override
- public ActiveLock getLock( Type arg0, Scope arg1 )
- {
- return null;
- }
-
- @Override
- public ActiveLock[] getLocks()
- {
- return null;
- }
-
- @Override
- public DavResourceIterator getMembers()
- {
- return null;
- }
-
- @Override
- public String getSupportedMethods()
- {
- return METHODS;
- }
-
- @Override
- public long getModificationTime()
- {
- return 0;
- }
-
- @Override
- public boolean hasLock( Type arg0, Scope arg1 )
- {
- return false;
- }
-
- @Override
- public boolean isCollection()
- {
- return true;
- }
-
- @Override
- public boolean isLockable( Type arg0, Scope arg1 )
- {
- return false;
- }
-
- @Override
- public ActiveLock lock( LockInfo arg0 )
- throws DavException
- {
- return null;
- }
-
- @Override
- public void move( DavResource arg0 )
- throws DavException
- {
-
- }
-
- @Override
- public ActiveLock refreshLock( LockInfo arg0, String arg1 )
- throws DavException
- {
- return null;
- }
-
- @Override
- public void removeMember( DavResource arg0 )
- throws DavException
- {
-
- }
-
- @Override
- public void unlock( String arg0 )
- throws DavException
- {
-
- }
-
- @Override
- public String getComplianceClass()
- {
- return COMPLIANCE_CLASS;
- }
-
- @Override
- public DavResourceLocator getLocator()
- {
- return locator;
- }
-
- @Override
- public String getResourcePath()
- {
- return locator.getResourcePath();
- }
-
- @Override
- public String getHref()
- {
- return locator.getHref( isCollection() );
- }
-
- @Override
- public DavResourceFactory getFactory()
- {
- return factory;
- }
-
- @Override
- public String getDisplayName()
- {
- String resPath = getResourcePath();
-
- return ( resPath != null ) ? Text.getName( resPath ) : resPath;
- }
-
- @Override
- public DavSession getSession()
- {
- return null;
- }
-
- @Override
- public DavPropertyName[] getPropertyNames()
- {
- return getProperties().getPropertyNames();
- }
-
- @Override
- public DavProperty getProperty( DavPropertyName name )
- {
- initProperties();
- return properties.get( name );
- }
-
- @Override
- public DavPropertySet getProperties()
- {
- initProperties();
- return properties;
- }
-
- @Override
- public void setProperty( DavProperty property )
- throws DavException
- {
- }
-
- @Override
- public void removeProperty( DavPropertyName propertyName )
- throws DavException
- {
- }
-
- @Override
- public DavResource getCollection()
- {
- DavResource parent = null;
- if ( getResourcePath() != null && !getResourcePath().equals( "/" ) )
- {
- String parentPath = Text.getRelativeParent( getResourcePath(), 1 );
- if ( parentPath.equals( "" ) )
- {
- parentPath = "/";
- }
- DavResourceLocator parentloc =
- locator.getFactory().createResourceLocator( locator.getPrefix(), parentPath );
- try
- {
- // go back to ArchivaDavResourceFactory!
- parent = factory.createResource( parentloc, null );
- }
- catch ( DavException e )
- {
- // should not occur
- }
- }
- return parent;
- }
-
- /**
- * Fill the set of properties
- */
- protected void initProperties()
- {
- if ( !exists() || propsInitialized )
- {
- return;
- }
-
- // set (or reset) fundamental properties
- if ( getDisplayName() != null )
- {
- properties.add( new DefaultDavProperty<>( DavPropertyName.DISPLAYNAME, getDisplayName() ) );
- }
- if ( isCollection() )
- {
- properties.add( new ResourceType( ResourceType.COLLECTION ) );
- // Windows XP support
- properties.add( new DefaultDavProperty<>( DavPropertyName.ISCOLLECTION, "1" ) );
- }
- else
- {
- properties.add( new ResourceType( ResourceType.DEFAULT_RESOURCE ) );
-
- // Windows XP support
- properties.add( new DefaultDavProperty<>( DavPropertyName.ISCOLLECTION, "0" ) );
- }
-
- // Need to get the ISO8601 date for properties
- DateTime dt = new DateTime( 0 );
- DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
- String modifiedDate = fmt.print( dt );
-
- properties.add( new DefaultDavProperty<>( DavPropertyName.GETLASTMODIFIED, modifiedDate ) );
-
- properties.add( new DefaultDavProperty<>( DavPropertyName.CREATIONDATE, modifiedDate ) );
-
- properties.add( new DefaultDavProperty<>( DavPropertyName.GETCONTENTLENGTH, 0 ) );
-
- propsInitialized = true;
- }
-
- public String getLogicalResource()
- {
- return logicalResource;
- }
-
- public void setLogicalResource( String logicalResource )
- {
- this.logicalResource = logicalResource;
- }
- }
|