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 junit.framework.TestCase;
23 import org.apache.archiva.admin.model.RepositoryAdminException;
24 import org.apache.archiva.admin.model.beans.ManagedRepository;
25 import org.apache.archiva.admin.model.beans.RepositoryGroup;
26 import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
27 import org.apache.archiva.admin.repository.DefaultRepositoryCommonValidator;
28 import org.apache.archiva.admin.repository.group.DefaultRepositoryGroupAdmin;
29 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
30 import org.apache.archiva.common.filelock.FileLockManager;
31 import org.apache.archiva.configuration.ArchivaConfiguration;
32 import org.apache.archiva.configuration.Configuration;
33 import org.apache.archiva.configuration.FileTypes;
34 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
35 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
36 import org.apache.archiva.proxy.ProxyRegistry;
37 import org.apache.archiva.repository.EditableManagedRepository;
38 import org.apache.archiva.repository.ManagedRepositoryContent;
39 import org.apache.archiva.repository.RemoteRepository;
40 import org.apache.archiva.repository.RemoteRepositoryContent;
41 import org.apache.archiva.repository.Repository;
42 import org.apache.archiva.repository.RepositoryContent;
43 import org.apache.archiva.repository.RepositoryContentFactory;
44 import org.apache.archiva.repository.RepositoryContentProvider;
45 import org.apache.archiva.repository.RepositoryException;
46 import org.apache.archiva.repository.RepositoryRegistry;
47 import org.apache.archiva.repository.RepositoryType;
48 import org.apache.archiva.repository.base.RepositoryHandlerDependencies;
49 import org.apache.archiva.maven.repository.content.ManagedDefaultRepositoryContent;
50 import org.apache.archiva.maven.repository.content.MavenContentHelper;
51 import org.apache.archiva.maven.repository.content.MavenRepositoryRequestInfo;
52 import org.apache.archiva.maven.repository.metadata.storage.ArtifactMappingProvider;
53 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
54 import org.apache.commons.lang3.StringUtils;
55 import org.apache.jackrabbit.webdav.DavException;
56 import org.apache.jackrabbit.webdav.DavResourceLocator;
57 import org.apache.jackrabbit.webdav.DavServletRequest;
58 import org.apache.jackrabbit.webdav.DavServletResponse;
59 import org.junit.After;
60 import org.junit.Before;
61 import org.junit.Test;
62 import org.junit.runner.RunWith;
63 import org.mockito.Mockito;
64 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
65 import org.springframework.context.ApplicationContext;
66 import org.springframework.context.ConfigurableApplicationContext;
67 import org.springframework.test.context.ContextConfiguration;
69 import javax.inject.Inject;
70 import javax.inject.Named;
71 import java.nio.file.Path;
72 import java.nio.file.Paths;
73 import java.util.ArrayList;
74 import java.util.HashSet;
75 import java.util.List;
76 import java.util.Locale;
78 import java.util.concurrent.atomic.AtomicReference;
80 import static org.mockito.Mockito.*;
84 * ArchivaDavResourceFactoryTest
86 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
87 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
88 public class ArchivaDavResourceFactoryTest
91 private AtomicReference<Path> projectBase = new AtomicReference<>();
93 private static final String RELEASES_REPO = "releases";
95 private static final String INTERNAL_REPO = "internal";
97 private static final String LOCAL_MIRROR_REPO = "local-mirror";
99 private static final String LEGACY_REPO = "legacy-repo";
101 private static final String LOCAL_REPO_GROUP = "local";
103 private OverridingArchivaDavResourceFactory resourceFactory;
105 private DavServletRequest request;
107 private MavenRepositoryRequestInfo repoRequest;
109 private DavServletResponse response;
111 private ArchivaConfiguration archivaConfiguration;
113 private Configuration config;
115 private RepositoryContentFactory repoFactory;
118 ApplicationContext applicationContext;
121 DefaultManagedRepositoryAdmin defaultManagedRepositoryAdmin;
124 RepositoryRegistry repositoryRegistry;
127 RemoteRepositoryAdmin remoteRepositoryAdmin;
130 ProxyRegistry proxyRegistry;
133 @Named( "MavenContentHelper" )
134 MavenContentHelper mavenContentHelper;
137 DefaultRepositoryGroupAdmin defaultRepositoryGroupAdmin;
140 List<? extends ArtifactMappingProvider> artifactMappingProviders;
143 @Named( "repositoryPathTranslator#maven2" )
144 RepositoryPathTranslator pathTranslator;
147 FileLockManager fileLockManager;
152 @SuppressWarnings( "unused" )
154 RepositoryHandlerDependencies repositoryHandlerDependencies;
156 public Path getProjectBase() {
157 if (this.projectBase.get()==null) {
158 String pathVal = System.getProperty("mvn.project.base.dir");
160 if (StringUtils.isEmpty(pathVal)) {
161 baseDir= Paths.get("").toAbsolutePath();
163 baseDir = Paths.get(pathVal).toAbsolutePath();
165 this.projectBase.compareAndSet(null, baseDir);
167 return this.projectBase.get();
177 request = mock( DavServletRequest.class );
179 response = mock( DavServletResponse.class );
180 //responseControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
182 archivaConfiguration = mock( ArchivaConfiguration.class );
184 config = new Configuration();
185 when( archivaConfiguration.getConfiguration() ).thenReturn( config );
186 when(archivaConfiguration.getDefaultLocale()).thenReturn( Locale.getDefault() );
187 archivaConfiguration.addListener( any() );
188 archivaConfiguration.save( eq(config));
189 archivaConfiguration.save( eq(config), anyString());
191 defaultManagedRepositoryAdmin.setArchivaConfiguration( archivaConfiguration );
192 repositoryRegistry.setArchivaConfiguration( archivaConfiguration );
193 repositoryRegistry.reload();
194 ( (DefaultRepositoryCommonValidator) defaultManagedRepositoryAdmin.getRepositoryCommonValidator() ).setArchivaConfiguration(
195 archivaConfiguration );
196 if ( defaultManagedRepositoryAdmin.getManagedRepository( RELEASES_REPO ) == null )
198 defaultManagedRepositoryAdmin.addManagedRepository(
199 createManagedRepository( RELEASES_REPO, getProjectBase().resolve( "target/test-classes/" + RELEASES_REPO ).toString(),
200 "default" ), false, null );
202 if ( defaultManagedRepositoryAdmin.getManagedRepository( INTERNAL_REPO ) == null )
204 defaultManagedRepositoryAdmin.addManagedRepository(
205 createManagedRepository( INTERNAL_REPO, getProjectBase().resolve( "target/test-classes/" + INTERNAL_REPO ).toString(),
206 "default" ), false, null );
208 RepositoryGroup repoGroupConfig = new RepositoryGroup();
209 repoGroupConfig.setId( LOCAL_REPO_GROUP );
210 repoGroupConfig.addRepository( RELEASES_REPO );
211 repoGroupConfig.addRepository( INTERNAL_REPO );
213 defaultRepositoryGroupAdmin.setArchivaConfiguration( archivaConfiguration );
214 if ( defaultManagedRepositoryAdmin.getManagedRepository( LOCAL_REPO_GROUP ) == null )
216 defaultRepositoryGroupAdmin.addRepositoryGroup( repoGroupConfig, null );
219 repoFactory = mock( RepositoryContentFactory.class );
221 repoRequest = mock( MavenRepositoryRequestInfo.class );
224 new OverridingArchivaDavResourceFactory( applicationContext, archivaConfiguration );
225 resourceFactory.setArchivaConfiguration( archivaConfiguration );
226 proxyRegistry.getAllHandler().get(RepositoryType.MAVEN).clear();
227 proxyRegistry.getAllHandler().get(RepositoryType.MAVEN).add(new OverridingRepositoryProxyHandler(this));
228 resourceFactory.setProxyRegistry(proxyRegistry);
229 resourceFactory.setRemoteRepositoryAdmin( remoteRepositoryAdmin );
230 resourceFactory.setManagedRepositoryAdmin( defaultManagedRepositoryAdmin );
231 resourceFactory.setRepositoryRegistry( repositoryRegistry );
232 verify( archivaConfiguration, atLeast( 2 )).getConfiguration();
233 verify( archivaConfiguration, atMost( 25 )).getConfiguration();
234 verify( archivaConfiguration, atMost( 4 ) ).addListener( any() );
235 verify( archivaConfiguration, atMost( 5 ) ).save( eq(config) );
236 verify( archivaConfiguration, atMost( 5 ) ).save( eq(config), anyString() );
240 private ManagedRepository createManagedRepository( String id, String location, String layout )
242 ManagedRepository repoConfig = new ManagedRepository( Locale.getDefault());
243 repoConfig.setId( id );
244 repoConfig.setName( id );
245 repoConfig.setLocation( location );
246 repoConfig.setLayout( layout );
251 private ManagedRepositoryContent createManagedRepositoryContent( String repoId )
252 throws RepositoryAdminException
254 org.apache.archiva.repository.ManagedRepository repo = repositoryRegistry.getManagedRepository( repoId );
255 ManagedDefaultRepositoryContent repoContent = new ManagedDefaultRepositoryContent(repo, fileTypes, fileLockManager);
256 if (repo!=null && repo instanceof EditableManagedRepository)
258 ( (EditableManagedRepository) repo ).setContent( repoContent );
260 repoContent.setMavenContentHelper( mavenContentHelper );
261 repoContent.setArtifactMappingProviders( artifactMappingProviders );
262 repoContent.setPathTranslator( pathTranslator );
266 private RepositoryContentProvider createRepositoryContentProvider( ManagedRepositoryContent content) {
267 Set<RepositoryType> TYPES = new HashSet<>( );
268 TYPES.add(RepositoryType.MAVEN);
269 return new RepositoryContentProvider( )
274 public boolean supportsLayout( String layout )
280 public Set<RepositoryType> getSupportedRepositoryTypes( )
286 public boolean supports( RepositoryType type )
292 public RemoteRepositoryContent createRemoteContent( RemoteRepository repository ) throws RepositoryException
298 public ManagedRepositoryContent createManagedContent( org.apache.archiva.repository.ManagedRepository repository ) throws RepositoryException
300 content.setRepository( repository );
305 public <T extends RepositoryContent, V extends Repository> T createContent( Class<T> clazz, V repository ) throws RepositoryException
314 public void tearDown()
318 String appserverBase = System.getProperty( "appserver.base" );
319 if ( StringUtils.isNotEmpty( appserverBase ) )
321 org.apache.archiva.common.utils.FileUtils.deleteDirectory( Paths.get( appserverBase ) );
325 // MRM-1232 - Unable to get artifacts from repositories which requires Repository Manager role using repository group
327 public void testRepositoryGroupFirstRepositoryRequiresAuthentication()
330 DavResourceLocator locator = new ArchivaDavResourceLocator( "", "/repository/" + LOCAL_REPO_GROUP
331 + "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", LOCAL_REPO_GROUP,
332 new ArchivaDavLocatorFactory() );
334 ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
335 ManagedRepositoryContent releasesRepo = createManagedRepositoryContent( RELEASES_REPO );
339 reset( archivaConfiguration );
341 reset( repoFactory );
342 when( archivaConfiguration.getConfiguration( ) ).thenReturn( config );
344 when( request.getMethod() ).thenReturn( "GET" );
346 when( request.getPathInfo() ).thenReturn( "org/apache/archiva" );
348 when( repoFactory.getManagedRepositoryContent( RELEASES_REPO ) ).thenReturn( releasesRepo );
350 when( request.getRemoteAddr( ) ).thenReturn( "http://localhost:8080" );
352 when( request.getDavSession( ) ).thenReturn( new ArchivaDavSession( ) );
354 when( request.getContextPath( ) ).thenReturn( "" );
356 when( repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( true );
359 repoRequest.getLayout( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn(
362 when( repoRequest.toItemSelector(
363 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( null );
365 when( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar"
367 Paths.get( config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
368 "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).toString());
370 when( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).thenReturn( internalRepo );
372 when( repoRequest.isArchetypeCatalog(
373 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( false );
375 resourceFactory.createResource( locator, request, response );
377 verify(archivaConfiguration, times( 3 )).getConfiguration();
378 verify( request, times( 3 ) ).getMethod( );
379 verify( request, atMost( 2 ) ).getPathInfo( );
380 verify(request,times( 2 )).getRemoteAddr();
381 verify( request, times( 2 ) ).getDavSession( );
382 verify( request, times( 2 ) ).getContextPath( );
385 fail( "A DavException with 401 error code should have been thrown." );
387 catch ( DavException e )
389 assertEquals( 401, e.getErrorCode() );
394 public void testRepositoryGroupLastRepositoryRequiresAuthentication()
397 DavResourceLocator locator = new ArchivaDavResourceLocator( "", "/repository/" + LOCAL_REPO_GROUP
398 + "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", LOCAL_REPO_GROUP,
399 new ArchivaDavLocatorFactory() );
401 List<RepositoryGroupConfiguration> repoGroups = new ArrayList<>();
402 RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
403 repoGroup.setId( LOCAL_REPO_GROUP );
404 repoGroup.addRepository( INTERNAL_REPO );
405 repoGroup.addRepository( RELEASES_REPO );
407 repoGroups.add( repoGroup );
409 config.setRepositoryGroups( repoGroups );
411 ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
413 ManagedRepositoryContent releasesRepo = createManagedRepositoryContent( RELEASES_REPO );
417 reset( archivaConfiguration );
419 reset( repoFactory );
421 when( archivaConfiguration.getConfiguration( ) ).thenReturn( config );
423 when( request.getMethod() ).thenReturn( "GET" );
425 when( request.getPathInfo() ).thenReturn( "org/apache/archiva" );
427 when( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).thenReturn( internalRepo );
429 when( repoFactory.getManagedRepositoryContent( RELEASES_REPO ) ).thenReturn( releasesRepo );
431 when( request.getRemoteAddr() ).thenReturn( "http://localhost:8080" );
433 when( request.getDavSession() ).thenReturn( new ArchivaDavSession() );
435 when( request.getContextPath() ).thenReturn( "" );
437 when( repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( false );
440 repoRequest.getLayout( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn(
443 when( repoRequest.toItemSelector(
444 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( null );
446 when( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar"
448 Paths.get( config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
449 "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).toString());
452 when( repoRequest.isArchetypeCatalog(
453 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( false );
455 resourceFactory.createResource( locator, request, response );
456 verify( archivaConfiguration, times( 3 ) ).getConfiguration( );
457 verify( request, times( 3 ) ).getMethod();
458 verify( request, atMost( 2 ) ).getPathInfo( );
459 verify( request, times( 2 ) ).getRemoteAddr( );
460 verify( request, times( 2 ) ).getDavSession( );
461 verify( request, times( 2 ) ).getContextPath( );
463 fail( "A DavException with 401 error code should have been thrown." );
465 catch ( DavException e )
467 assertEquals( 401, e.getErrorCode() );
472 public void testRepositoryGroupArtifactDoesNotExistInAnyOfTheReposAuthenticationDisabled()
475 DavResourceLocator locator = new ArchivaDavResourceLocator( "", "/repository/" + LOCAL_REPO_GROUP
476 + "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", LOCAL_REPO_GROUP,
477 new ArchivaDavLocatorFactory() );
479 defaultManagedRepositoryAdmin.addManagedRepository(
480 createManagedRepository( LOCAL_MIRROR_REPO, Paths.get( "target/test-classes/local-mirror" ).toString(),
481 "default" ), false, null );
483 List<RepositoryGroupConfiguration> repoGroups = new ArrayList<>();
484 RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
485 repoGroup.setId( LOCAL_REPO_GROUP );
486 repoGroup.addRepository( INTERNAL_REPO );
487 repoGroup.addRepository( LOCAL_MIRROR_REPO );
489 repoGroups.add( repoGroup );
491 config.setRepositoryGroups( repoGroups );
493 ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
494 ManagedRepositoryContent localMirrorRepo = createManagedRepositoryContent( LOCAL_MIRROR_REPO );
496 repositoryRegistry.putRepositoryGroup( repoGroup );
500 reset( archivaConfiguration );
502 reset( repoFactory );
504 when( archivaConfiguration.getConfiguration() ).thenReturn( config );
506 when( request.getMethod() ).thenReturn( "GET" );
508 when( request.getPathInfo() ).thenReturn( "org/apache/archiva" );
510 when( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).thenReturn( internalRepo );
512 when( repoFactory.getManagedRepositoryContent( LOCAL_MIRROR_REPO ) ).thenReturn( localMirrorRepo );
514 when( request.getRemoteAddr() ).thenReturn( "http://localhost:8080" );
516 when( request.getDavSession() ).thenReturn( new ArchivaDavSession() );
518 when( request.getContextPath() ).thenReturn( "" );
520 when( repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( false );
523 repoRequest.getLayout( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn(
526 when( repoRequest.toItemSelector(
527 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( null );
529 when( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar"
531 Paths.get( config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
532 "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).toString() );
534 when( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar"
536 .thenReturn( Paths.get( config.findManagedRepositoryById( LOCAL_MIRROR_REPO ).getLocation(),
537 "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).toString());
539 when( repoRequest.isArchetypeCatalog( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( false );
541 resourceFactory.createResource( locator, request, response );
542 verify( archivaConfiguration, times( 3 ) ).getConfiguration( );
543 verify( request, times( 5 ) ).getMethod( );
544 verify( request, atMost( 2 ) ).getPathInfo( );
545 verify( request, times( 4 ) ).getRemoteAddr( );
546 verify( request, times( 4 ) ).getDavSession( );
547 verify( request, times( 2 ) ).getContextPath( );
548 verify( repoRequest, times( 2 ) ).isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" );
549 verify(repoRequest, times( 2 )).getLayout( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" );
550 verify( repoRequest, times( 2 ) ).toItemSelector(
551 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" );
552 verify( repoRequest, times( 2 ) ).isArchetypeCatalog( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" );
554 fail( "A DavException with 404 error code should have been thrown." );
556 catch ( DavException e )
558 assertEquals( 404, e.getErrorCode() );
564 public void testRequestArtifactMetadataThreePartsRepoHasDefaultLayout()
567 // should fetch metadata
568 DavResourceLocator locator =
569 new ArchivaDavResourceLocator( "", "/repository/" + INTERNAL_REPO + "/eclipse/jdtcore/maven-metadata.xml",
570 INTERNAL_REPO, new ArchivaDavLocatorFactory() );
572 ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
574 // use actual object (this performs the isMetadata, isDefault and isSupportFile check!)
575 MavenRepositoryRequestInfo repoRequest = new MavenRepositoryRequestInfo(internalRepo.getRepository() );
581 when( request.getMethod() ).thenReturn( "GET" );
583 when( request.getRemoteAddr() ).thenReturn( "http://localhost:8080" );
585 when( request.getContextPath() ).thenReturn( "" );
587 when( request.getDavSession() ).thenReturn( new ArchivaDavSession() );
589 when( request.getRequestURI() ).thenReturn( "http://localhost:8080/archiva/repository/" + INTERNAL_REPO + "/eclipse/jdtcore/maven-metadata.xml" );
590 response.setHeader( "Pragma", "no-cache" );
591 response.setHeader( "Cache-Control", "no-cache" );
592 response.setDateHeader( eq("Last-Modified"), anyLong() );
594 resourceFactory.createResource( locator, request, response );
595 verify( request, times( 4 ) ).getMethod( );
596 verify( request, times( 3 ) ).getRemoteAddr( );
597 verify( request, times( 1 ) ).getContextPath( );
598 verify( request, times( 2 ) ).getDavSession( );
601 catch ( DavException e )
604 fail( "A DavException should not have been thrown! "+e.getMessage() );
609 public void testRequestArtifactMetadataTwoPartsRepoHasDefaultLayout()
612 // should not fetch metadata
613 DavResourceLocator locator =
614 new ArchivaDavResourceLocator( "", "/repository/" + INTERNAL_REPO + "/eclipse/maven-metadata.xml",
615 INTERNAL_REPO, new ArchivaDavLocatorFactory() );
617 ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
621 reset( archivaConfiguration );
623 reset( repoFactory );
625 when( archivaConfiguration.getConfiguration() ).thenReturn( config );
627 when( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).thenReturn( internalRepo );
629 when( request.getMethod() ).thenReturn( "GET" );
631 when( request.getRemoteAddr() ).thenReturn( "http://localhost:8080" );
633 when( request.getDavSession() ).thenReturn( new ArchivaDavSession() );
635 when( request.getContextPath() ).thenReturn( "" );
637 resourceFactory.createResource( locator, request, response );
638 verify( archivaConfiguration, times( 2 ) ).getConfiguration( );
639 verify( request, times( 3 ) ).getMethod( );
640 verify( request, times( 3 ) ).getRemoteAddr( );
641 verify( request, times( 2 ) ).getDavSession( );
642 verify( request, times( 2 ) ).getContextPath( );
644 fail( "A 404 error should have been thrown!" );
646 catch ( DavException e )
648 assertEquals( 404, e.getErrorCode() );
653 public void testRequestMetadataRepoIsLegacy()
656 ManagedRepositoryContent legacyRepo = createManagedRepositoryContent( LEGACY_REPO );
657 ConfigurableListableBeanFactory beanFactory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory();
658 RepositoryContentProvider provider = createRepositoryContentProvider(legacyRepo );
659 beanFactory.registerSingleton("repositoryContentProvider#legacy", provider);
660 RepositoryContentFactory repoContentFactory = applicationContext.getBean( "repositoryContentFactory#default", RepositoryContentFactory.class );
661 repoContentFactory.getRepositoryContentProviders().add(provider);
662 defaultManagedRepositoryAdmin.addManagedRepository(
663 createManagedRepository( LEGACY_REPO, getProjectBase().resolve( "target/test-classes/" + LEGACY_REPO ).toString(),
664 "legacy" ), false, null );
666 DavResourceLocator locator =
667 new ArchivaDavResourceLocator( "", "/repository/" + LEGACY_REPO + "/eclipse/maven-metadata.xml",
668 LEGACY_REPO, new ArchivaDavLocatorFactory() );
673 reset( archivaConfiguration );
675 reset( repoFactory );
677 when( archivaConfiguration.getConfiguration() ).thenReturn( config );
679 when( repoFactory.getManagedRepositoryContent( LEGACY_REPO ) ).thenReturn( legacyRepo );
681 when( request.getMethod() ).thenReturn( "GET" );
683 when( request.getRemoteAddr() ).thenReturn( "http://localhost:8080" );
685 when( request.getDavSession() ).thenReturn( new ArchivaDavSession() );
687 when( request.getContextPath() ).thenReturn( "" );
689 resourceFactory.createResource( locator, request, response );
691 verify( archivaConfiguration,
692 times( 2 ) ).getConfiguration( );
693 verify( request, times( 3 ) ).getMethod( );
694 verify( request, times( 3 ) ).getRemoteAddr( );
695 verify( request, times( 2 ) ).getDavSession( );
696 verify( request, times( 2 ) ).getContextPath( );
698 fail( "A 404 error should have been thrown!" );
700 catch ( DavException e )
702 assertEquals( 404, e.getErrorCode() );
706 class OverridingArchivaDavResourceFactory
707 extends ArchivaDavResourceFactory
710 OverridingArchivaDavResourceFactory( ApplicationContext applicationContext,
711 ArchivaConfiguration archivaConfiguration )
713 super( applicationContext, archivaConfiguration );
717 protected boolean isAuthorized( DavServletRequest request, String repositoryId )
720 if ( RELEASES_REPO.equals( repositoryId ) )
722 throw new UnauthorizedDavException( repositoryId,
723 "You are not authenticated and authorized to access any repository." );
732 protected String getActivePrincipal( DavServletRequest request )