]> source.dussan.org Git - archiva.git/blob
3650a9704f5789a6e4047a29441e86121207072f
[archiva.git] /
1 package org.apache.archiva.webdav;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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;
68
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;
77 import java.util.Set;
78 import java.util.concurrent.atomic.AtomicReference;
79
80 import static org.mockito.Mockito.*;
81
82
83 /**
84  * ArchivaDavResourceFactoryTest
85  */
86 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
87 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
88 public class ArchivaDavResourceFactoryTest
89     extends TestCase
90 {
91     private AtomicReference<Path> projectBase = new AtomicReference<>();
92
93     private static final String RELEASES_REPO = "releases";
94
95     private static final String INTERNAL_REPO = "internal";
96
97     private static final String LOCAL_MIRROR_REPO = "local-mirror";
98
99     private static final String LEGACY_REPO = "legacy-repo";
100
101     private static final String LOCAL_REPO_GROUP = "local";
102
103     private OverridingArchivaDavResourceFactory resourceFactory;
104
105     private DavServletRequest request;
106
107     private MavenRepositoryRequestInfo repoRequest;
108
109     private DavServletResponse response;
110
111     private ArchivaConfiguration archivaConfiguration;
112
113     private Configuration config;
114
115     private RepositoryContentFactory repoFactory;
116
117     @Inject
118     ApplicationContext applicationContext;
119
120     @Inject
121     DefaultManagedRepositoryAdmin defaultManagedRepositoryAdmin;
122
123     @Inject
124     RepositoryRegistry repositoryRegistry;
125
126     @Inject
127     RemoteRepositoryAdmin remoteRepositoryAdmin;
128
129     @Inject
130     ProxyRegistry proxyRegistry;
131
132     @Inject
133     @Named( "MavenContentHelper" )
134     MavenContentHelper mavenContentHelper;
135
136     @Inject
137     DefaultRepositoryGroupAdmin defaultRepositoryGroupAdmin;
138
139     @Inject
140     List<? extends ArtifactMappingProvider> artifactMappingProviders;
141
142     @Inject
143     @Named( "repositoryPathTranslator#maven2" )
144     RepositoryPathTranslator pathTranslator;
145
146     @Inject
147     FileLockManager fileLockManager;
148
149     @Inject
150     FileTypes fileTypes;
151
152     @SuppressWarnings( "unused" )
153     @Inject
154     RepositoryHandlerDependencies repositoryHandlerDependencies;
155
156     public Path getProjectBase() {
157         if (this.projectBase.get()==null) {
158             String pathVal = System.getProperty("mvn.project.base.dir");
159             Path baseDir;
160             if (StringUtils.isEmpty(pathVal)) {
161                 baseDir= Paths.get("").toAbsolutePath();
162             } else {
163                 baseDir = Paths.get(pathVal).toAbsolutePath();
164             }
165             this.projectBase.compareAndSet(null, baseDir);
166         }
167         return this.projectBase.get();
168     }
169
170     @Before
171     @Override
172     public void setUp()
173         throws Exception
174     {
175         super.setUp();
176
177         request = mock( DavServletRequest.class );
178
179         response = mock( DavServletResponse.class );
180         //responseControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
181
182         archivaConfiguration = mock( ArchivaConfiguration.class );
183
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());
190
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 )
197         {
198             defaultManagedRepositoryAdmin.addManagedRepository(
199                 createManagedRepository( RELEASES_REPO, getProjectBase().resolve( "target/test-classes/" + RELEASES_REPO ).toString(),
200                                          "default" ), false, null );
201         }
202         if ( defaultManagedRepositoryAdmin.getManagedRepository( INTERNAL_REPO ) == null )
203         {
204             defaultManagedRepositoryAdmin.addManagedRepository(
205                 createManagedRepository( INTERNAL_REPO, getProjectBase().resolve( "target/test-classes/" + INTERNAL_REPO ).toString(),
206                                          "default" ), false, null );
207         }
208         RepositoryGroup repoGroupConfig = new RepositoryGroup();
209         repoGroupConfig.setId( LOCAL_REPO_GROUP );
210         repoGroupConfig.addRepository( RELEASES_REPO );
211         repoGroupConfig.addRepository( INTERNAL_REPO );
212
213         defaultRepositoryGroupAdmin.setArchivaConfiguration( archivaConfiguration );
214         if ( defaultManagedRepositoryAdmin.getManagedRepository( LOCAL_REPO_GROUP ) == null )
215         {
216             defaultRepositoryGroupAdmin.addRepositoryGroup( repoGroupConfig, null );
217         }
218
219         repoFactory = mock( RepositoryContentFactory.class );
220
221         repoRequest = mock( MavenRepositoryRequestInfo.class );
222
223         resourceFactory =
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() );
237
238     }
239
240     private ManagedRepository createManagedRepository( String id, String location, String layout )
241     {
242         ManagedRepository repoConfig = new ManagedRepository( Locale.getDefault());
243         repoConfig.setId( id );
244         repoConfig.setName( id );
245         repoConfig.setLocation( location );
246         repoConfig.setLayout( layout );
247
248         return repoConfig;
249     }
250
251     private ManagedRepositoryContent createManagedRepositoryContent( String repoId )
252         throws RepositoryAdminException
253     {
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)
257         {
258             ( (EditableManagedRepository) repo ).setContent( repoContent );
259         }
260         repoContent.setMavenContentHelper( mavenContentHelper );
261         repoContent.setArtifactMappingProviders( artifactMappingProviders );
262         repoContent.setPathTranslator( pathTranslator );
263         return repoContent;
264     }
265
266     private RepositoryContentProvider createRepositoryContentProvider( ManagedRepositoryContent content) {
267         Set<RepositoryType> TYPES = new HashSet<>(  );
268         TYPES.add(RepositoryType.MAVEN);
269         return new RepositoryContentProvider( )
270         {
271
272
273             @Override
274             public boolean supportsLayout( String layout )
275             {
276                 return true;
277             }
278
279             @Override
280             public Set<RepositoryType> getSupportedRepositoryTypes( )
281             {
282                 return TYPES;
283             }
284
285             @Override
286             public boolean supports( RepositoryType type )
287             {
288                 return true;
289             }
290
291             @Override
292             public RemoteRepositoryContent createRemoteContent( RemoteRepository repository ) throws RepositoryException
293             {
294                 return null;
295             }
296
297             @Override
298             public ManagedRepositoryContent createManagedContent( org.apache.archiva.repository.ManagedRepository repository ) throws RepositoryException
299             {
300                 content.setRepository( repository );
301                 return content;
302             }
303
304             @Override
305             public <T extends RepositoryContent, V extends Repository> T createContent( Class<T> clazz, V repository ) throws RepositoryException
306             {
307                 return null;
308             }
309         };
310     }
311
312     @After
313     @Override
314     public void tearDown()
315         throws Exception
316     {
317         super.tearDown();
318         String appserverBase = System.getProperty( "appserver.base" );
319         if ( StringUtils.isNotEmpty( appserverBase ) )
320         {
321             org.apache.archiva.common.utils.FileUtils.deleteDirectory( Paths.get( appserverBase ) );
322         }
323     }
324
325     // MRM-1232 - Unable to get artifacts from repositories which requires Repository Manager role using repository group
326     @Test
327     public void testRepositoryGroupFirstRepositoryRequiresAuthentication()
328         throws Exception
329     {
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() );
333
334         ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
335         ManagedRepositoryContent releasesRepo = createManagedRepositoryContent( RELEASES_REPO );
336
337         try
338         {
339             reset( archivaConfiguration );
340             reset( request );
341             reset( repoFactory );
342             when( archivaConfiguration.getConfiguration( ) ).thenReturn( config );
343
344             when( request.getMethod() ).thenReturn( "GET" );
345
346             when( request.getPathInfo() ).thenReturn( "org/apache/archiva" );
347
348             when( repoFactory.getManagedRepositoryContent( RELEASES_REPO ) ).thenReturn( releasesRepo );
349
350             when( request.getRemoteAddr( ) ).thenReturn( "http://localhost:8080" );
351
352             when( request.getDavSession( ) ).thenReturn( new ArchivaDavSession( ) );
353
354             when( request.getContextPath( ) ).thenReturn( "" );
355
356             when( repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( true );
357
358             when(
359                 repoRequest.getLayout( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn(
360                 "legacy" );
361
362             when( repoRequest.toItemSelector(
363                 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( null );
364
365             when( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar"
366             ) ).thenReturn(
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());
369
370             when( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).thenReturn( internalRepo );
371
372             when( repoRequest.isArchetypeCatalog(
373                 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( false );
374
375             resourceFactory.createResource( locator, request, response );
376
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( );
383
384
385             fail( "A DavException with 401 error code should have been thrown." );
386         }
387         catch ( DavException e )
388         {
389             assertEquals( 401, e.getErrorCode() );
390         }
391     }
392
393     @Test
394     public void testRepositoryGroupLastRepositoryRequiresAuthentication()
395         throws Exception
396     {
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() );
400
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 );
406
407         repoGroups.add( repoGroup );
408
409         config.setRepositoryGroups( repoGroups );
410
411         ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
412
413         ManagedRepositoryContent releasesRepo = createManagedRepositoryContent( RELEASES_REPO );
414
415         try
416         {
417             reset( archivaConfiguration );
418             reset( request );
419             reset( repoFactory );
420
421             when( archivaConfiguration.getConfiguration( ) ).thenReturn( config );
422
423             when( request.getMethod() ).thenReturn( "GET" );
424
425             when( request.getPathInfo() ).thenReturn( "org/apache/archiva" );
426
427             when( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).thenReturn( internalRepo );
428
429             when( repoFactory.getManagedRepositoryContent( RELEASES_REPO ) ).thenReturn( releasesRepo );
430
431             when( request.getRemoteAddr() ).thenReturn( "http://localhost:8080" );
432
433             when( request.getDavSession() ).thenReturn( new ArchivaDavSession() );
434
435             when( request.getContextPath() ).thenReturn( "" );
436
437             when( repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( false );
438
439             when(
440                 repoRequest.getLayout( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn(
441                 "legacy" );
442
443             when( repoRequest.toItemSelector(
444                 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( null );
445
446             when( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar"
447             ) ).thenReturn(
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());
450
451
452             when( repoRequest.isArchetypeCatalog(
453                 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( false );
454
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( );
462
463             fail( "A DavException with 401 error code should have been thrown." );
464         }
465         catch ( DavException e )
466         {
467             assertEquals( 401, e.getErrorCode() );
468         }
469     }
470
471     @Test
472     public void testRepositoryGroupArtifactDoesNotExistInAnyOfTheReposAuthenticationDisabled()
473         throws Exception
474     {
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() );
478
479         defaultManagedRepositoryAdmin.addManagedRepository(
480             createManagedRepository( LOCAL_MIRROR_REPO, Paths.get( "target/test-classes/local-mirror" ).toString(),
481                                      "default" ), false, null );
482
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 );
488
489         repoGroups.add( repoGroup );
490
491         config.setRepositoryGroups( repoGroups );
492
493         ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
494         ManagedRepositoryContent localMirrorRepo = createManagedRepositoryContent( LOCAL_MIRROR_REPO );
495
496         repositoryRegistry.putRepositoryGroup( repoGroup );
497
498         try
499         {
500             reset( archivaConfiguration );
501             reset( request );
502             reset( repoFactory );
503
504             when( archivaConfiguration.getConfiguration() ).thenReturn( config );
505
506             when( request.getMethod() ).thenReturn( "GET" );
507
508             when( request.getPathInfo() ).thenReturn( "org/apache/archiva" );
509
510             when( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).thenReturn( internalRepo );
511
512             when( repoFactory.getManagedRepositoryContent( LOCAL_MIRROR_REPO ) ).thenReturn( localMirrorRepo );
513
514             when( request.getRemoteAddr() ).thenReturn( "http://localhost:8080" );
515
516             when( request.getDavSession() ).thenReturn( new ArchivaDavSession() );
517
518             when( request.getContextPath() ).thenReturn( "" );
519
520             when( repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( false );
521
522             when(
523                 repoRequest.getLayout( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn(
524                 "legacy" );
525
526             when( repoRequest.toItemSelector(
527                 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( null );
528
529             when( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar"
530             ) ).thenReturn(
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() );
533
534             when( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar"
535             ) )
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());
538
539             when( repoRequest.isArchetypeCatalog( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).thenReturn( false );
540
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" );
553
554             fail( "A DavException with 404 error code should have been thrown." );
555         }
556         catch ( DavException e )
557         {
558             assertEquals( 404, e.getErrorCode() );
559         }
560     }
561
562     // MRM-1239
563     @Test
564     public void testRequestArtifactMetadataThreePartsRepoHasDefaultLayout()
565         throws Exception
566     {
567         // should fetch metadata 
568         DavResourceLocator locator =
569             new ArchivaDavResourceLocator( "", "/repository/" + INTERNAL_REPO + "/eclipse/jdtcore/maven-metadata.xml",
570                                            INTERNAL_REPO, new ArchivaDavLocatorFactory() );
571
572         ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
573
574         // use actual object (this performs the isMetadata, isDefault and isSupportFile check!)
575         MavenRepositoryRequestInfo repoRequest = new MavenRepositoryRequestInfo(internalRepo.getRepository() );
576
577         try
578         {
579             reset( request );
580
581             when( request.getMethod() ).thenReturn( "GET" );
582
583             when( request.getRemoteAddr() ).thenReturn( "http://localhost:8080" );
584
585             when( request.getContextPath() ).thenReturn( "" );
586
587             when( request.getDavSession() ).thenReturn( new ArchivaDavSession() );
588
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() );
593
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( );
599
600         }
601         catch ( DavException e )
602         {
603             e.printStackTrace();
604             fail( "A DavException should not have been thrown! "+e.getMessage() );
605         }
606     }
607
608     @Test
609     public void testRequestArtifactMetadataTwoPartsRepoHasDefaultLayout()
610         throws Exception
611     {
612         // should not fetch metadata
613         DavResourceLocator locator =
614             new ArchivaDavResourceLocator( "", "/repository/" + INTERNAL_REPO + "/eclipse/maven-metadata.xml",
615                                            INTERNAL_REPO, new ArchivaDavLocatorFactory() );
616
617         ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
618
619         try
620         {
621             reset( archivaConfiguration );
622             reset( request );
623             reset( repoFactory );
624
625             when( archivaConfiguration.getConfiguration() ).thenReturn( config );
626
627             when( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).thenReturn( internalRepo );
628
629             when( request.getMethod() ).thenReturn( "GET" );
630
631             when( request.getRemoteAddr() ).thenReturn( "http://localhost:8080" );
632
633             when( request.getDavSession() ).thenReturn( new ArchivaDavSession() );
634
635             when( request.getContextPath() ).thenReturn( "" );
636
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( );
643
644             fail( "A 404 error should have been thrown!" );
645         }
646         catch ( DavException e )
647         {
648             assertEquals( 404, e.getErrorCode() );
649         }
650     }
651
652     @Test
653     public void testRequestMetadataRepoIsLegacy()
654         throws Exception
655     {
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 );
665
666         DavResourceLocator locator =
667             new ArchivaDavResourceLocator( "", "/repository/" + LEGACY_REPO + "/eclipse/maven-metadata.xml",
668                                            LEGACY_REPO, new ArchivaDavLocatorFactory() );
669
670
671         try
672         {
673             reset( archivaConfiguration );
674             reset( request );
675             reset( repoFactory );
676
677             when( archivaConfiguration.getConfiguration() ).thenReturn( config );
678
679             when( repoFactory.getManagedRepositoryContent( LEGACY_REPO ) ).thenReturn( legacyRepo );
680
681             when( request.getMethod() ).thenReturn( "GET" );
682
683             when( request.getRemoteAddr() ).thenReturn( "http://localhost:8080" );
684
685             when( request.getDavSession() ).thenReturn( new ArchivaDavSession() );
686
687             when( request.getContextPath() ).thenReturn( "" );
688
689             resourceFactory.createResource( locator, request, response );
690
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( );
697
698             fail( "A 404 error should have been thrown!" );
699         }
700         catch ( DavException e )
701         {
702             assertEquals( 404, e.getErrorCode() );
703         }
704     }
705
706     class OverridingArchivaDavResourceFactory
707         extends ArchivaDavResourceFactory
708     {
709
710         OverridingArchivaDavResourceFactory( ApplicationContext applicationContext,
711                                              ArchivaConfiguration archivaConfiguration )
712         {
713             super( applicationContext, archivaConfiguration );
714         }
715
716         @Override
717         protected boolean isAuthorized( DavServletRequest request, String repositoryId )
718             throws DavException
719         {
720             if ( RELEASES_REPO.equals( repositoryId ) )
721             {
722                 throw new UnauthorizedDavException( repositoryId,
723                                                     "You are not authenticated and authorized to access any repository." );
724             }
725             else
726             {
727                 return true;
728             }
729         }
730
731         @Override
732         protected String getActivePrincipal( DavServletRequest request )
733         {
734             return "guest";
735         }
736     }
737
738 }