]> source.dussan.org Git - archiva.git/blob
2140e8450dac4a875d6df00de61604ccb7540354
[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.repository.maven.content.ManagedDefaultRepositoryContent;
50 import org.apache.archiva.repository.maven.content.MavenContentHelper;
51 import org.apache.archiva.repository.maven.content.MavenRepositoryRequestInfo;
52 import org.apache.archiva.repository.maven.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.easymock.EasyMock;
60 import org.easymock.IMocksControl;
61 import org.junit.After;
62 import org.junit.Before;
63 import org.junit.Test;
64 import org.junit.runner.RunWith;
65 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
66 import org.springframework.context.ApplicationContext;
67 import org.springframework.context.ConfigurableApplicationContext;
68 import org.springframework.test.context.ContextConfiguration;
69
70 import javax.inject.Inject;
71 import javax.inject.Named;
72 import java.nio.file.Path;
73 import java.nio.file.Paths;
74 import java.util.ArrayList;
75 import java.util.HashSet;
76 import java.util.List;
77 import java.util.Locale;
78 import java.util.Set;
79 import java.util.concurrent.atomic.AtomicReference;
80
81 import static org.easymock.EasyMock.*;
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 IMocksControl requestControl;
106
107     private DavServletRequest request;
108
109     private IMocksControl repoRequestControl;
110
111     private MavenRepositoryRequestInfo repoRequest;
112
113     private IMocksControl responseControl;
114
115     private DavServletResponse response;
116
117     private IMocksControl archivaConfigurationControl;
118
119     private ArchivaConfiguration archivaConfiguration;
120
121     private Configuration config;
122
123     private IMocksControl repoContentFactoryControl;
124
125     private RepositoryContentFactory repoFactory;
126
127     @Inject
128     ApplicationContext applicationContext;
129
130     @Inject
131     DefaultManagedRepositoryAdmin defaultManagedRepositoryAdmin;
132
133     @Inject
134     RepositoryRegistry repositoryRegistry;
135
136     @Inject
137     RemoteRepositoryAdmin remoteRepositoryAdmin;
138
139     @Inject
140     ProxyRegistry proxyRegistry;
141
142     @Inject
143     @Named( "MavenContentHelper" )
144     MavenContentHelper mavenContentHelper;
145
146     @Inject
147     DefaultRepositoryGroupAdmin defaultRepositoryGroupAdmin;
148
149     @Inject
150     List<? extends ArtifactMappingProvider> artifactMappingProviders;
151
152     @Inject
153     @Named( "repositoryPathTranslator#maven2" )
154     RepositoryPathTranslator pathTranslator;
155
156     @Inject
157     FileLockManager fileLockManager;
158
159     @Inject
160     FileTypes fileTypes;
161
162     @SuppressWarnings( "unused" )
163     @Inject
164     RepositoryHandlerDependencies repositoryHandlerDependencies;
165
166     public Path getProjectBase() {
167         if (this.projectBase.get()==null) {
168             String pathVal = System.getProperty("mvn.project.base.dir");
169             Path baseDir;
170             if (StringUtils.isEmpty(pathVal)) {
171                 baseDir= Paths.get("").toAbsolutePath();
172             } else {
173                 baseDir = Paths.get(pathVal).toAbsolutePath();
174             }
175             this.projectBase.compareAndSet(null, baseDir);
176         }
177         return this.projectBase.get();
178     }
179
180     @Before
181     @Override
182     public void setUp()
183         throws Exception
184     {
185         super.setUp();
186
187         requestControl = createControl();
188         request = requestControl.createMock( DavServletRequest.class );
189
190         responseControl = createControl();
191         response = responseControl.createMock( DavServletResponse.class );
192         //responseControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
193
194         archivaConfigurationControl = createControl();
195         archivaConfiguration = archivaConfigurationControl.createMock( ArchivaConfiguration.class );
196
197         config = new Configuration();
198         expect( archivaConfiguration.getConfiguration() ).andReturn( config ).times( 2, 25 );
199         expect (archivaConfiguration.getDefaultLocale()).andReturn( Locale.getDefault() ).anyTimes();
200         archivaConfiguration.addListener( EasyMock.anyObject(  ) );
201         expectLastCall().times(0, 4);
202         archivaConfiguration.save( eq(config));
203         expectLastCall().times( 0, 5 );
204         archivaConfiguration.save( eq(config), EasyMock.anyString());
205         expectLastCall().times( 0, 5 );
206         archivaConfigurationControl.replay();
207
208         defaultManagedRepositoryAdmin.setArchivaConfiguration( archivaConfiguration );
209         repositoryRegistry.setArchivaConfiguration( archivaConfiguration );
210         repositoryRegistry.reload();
211         ( (DefaultRepositoryCommonValidator) defaultManagedRepositoryAdmin.getRepositoryCommonValidator() ).setArchivaConfiguration(
212             archivaConfiguration );
213         if ( defaultManagedRepositoryAdmin.getManagedRepository( RELEASES_REPO ) == null )
214         {
215             defaultManagedRepositoryAdmin.addManagedRepository(
216                 createManagedRepository( RELEASES_REPO, getProjectBase().resolve( "target/test-classes/" + RELEASES_REPO ).toString(),
217                                          "default" ), false, null );
218         }
219         if ( defaultManagedRepositoryAdmin.getManagedRepository( INTERNAL_REPO ) == null )
220         {
221             defaultManagedRepositoryAdmin.addManagedRepository(
222                 createManagedRepository( INTERNAL_REPO, getProjectBase().resolve( "target/test-classes/" + INTERNAL_REPO ).toString(),
223                                          "default" ), false, null );
224         }
225         RepositoryGroup repoGroupConfig = new RepositoryGroup();
226         repoGroupConfig.setId( LOCAL_REPO_GROUP );
227         repoGroupConfig.addRepository( RELEASES_REPO );
228         repoGroupConfig.addRepository( INTERNAL_REPO );
229
230         defaultRepositoryGroupAdmin.setArchivaConfiguration( archivaConfiguration );
231         if ( defaultManagedRepositoryAdmin.getManagedRepository( LOCAL_REPO_GROUP ) == null )
232         {
233             defaultRepositoryGroupAdmin.addRepositoryGroup( repoGroupConfig, null );
234         }
235
236         repoContentFactoryControl = createControl();
237         repoFactory = repoContentFactoryControl.createMock( RepositoryContentFactory.class );
238
239         repoRequestControl = createControl();
240         repoRequest = repoRequestControl.createMock( MavenRepositoryRequestInfo.class );
241
242         resourceFactory =
243             new OverridingArchivaDavResourceFactory( applicationContext, archivaConfiguration );
244         resourceFactory.setArchivaConfiguration( archivaConfiguration );
245         proxyRegistry.getAllHandler().get(RepositoryType.MAVEN).clear();
246         proxyRegistry.getAllHandler().get(RepositoryType.MAVEN).add(new OverridingRepositoryProxyHandler(this));
247         resourceFactory.setProxyRegistry(proxyRegistry);
248         resourceFactory.setRemoteRepositoryAdmin( remoteRepositoryAdmin );
249         resourceFactory.setManagedRepositoryAdmin( defaultManagedRepositoryAdmin );
250         resourceFactory.setRepositoryRegistry( repositoryRegistry );
251     }
252
253     private ManagedRepository createManagedRepository( String id, String location, String layout )
254     {
255         ManagedRepository repoConfig = new ManagedRepository( Locale.getDefault());
256         repoConfig.setId( id );
257         repoConfig.setName( id );
258         repoConfig.setLocation( location );
259         repoConfig.setLayout( layout );
260
261         return repoConfig;
262     }
263
264     private ManagedRepositoryContent createManagedRepositoryContent( String repoId )
265         throws RepositoryAdminException
266     {
267         org.apache.archiva.repository.ManagedRepository repo = repositoryRegistry.getManagedRepository( repoId );
268         ManagedDefaultRepositoryContent repoContent = new ManagedDefaultRepositoryContent(repo, fileTypes, fileLockManager);
269         if (repo!=null && repo instanceof EditableManagedRepository)
270         {
271             ( (EditableManagedRepository) repo ).setContent( repoContent );
272         }
273         repoContent.setMavenContentHelper( mavenContentHelper );
274         repoContent.setArtifactMappingProviders( artifactMappingProviders );
275         repoContent.setPathTranslator( pathTranslator );
276         return repoContent;
277     }
278
279     private RepositoryContentProvider createRepositoryContentProvider( ManagedRepositoryContent content) {
280         Set<RepositoryType> TYPES = new HashSet<>(  );
281         TYPES.add(RepositoryType.MAVEN);
282         return new RepositoryContentProvider( )
283         {
284
285
286             @Override
287             public boolean supportsLayout( String layout )
288             {
289                 return true;
290             }
291
292             @Override
293             public Set<RepositoryType> getSupportedRepositoryTypes( )
294             {
295                 return TYPES;
296             }
297
298             @Override
299             public boolean supports( RepositoryType type )
300             {
301                 return true;
302             }
303
304             @Override
305             public RemoteRepositoryContent createRemoteContent( RemoteRepository repository ) throws RepositoryException
306             {
307                 return null;
308             }
309
310             @Override
311             public ManagedRepositoryContent createManagedContent( org.apache.archiva.repository.ManagedRepository repository ) throws RepositoryException
312             {
313                 content.setRepository( repository );
314                 return content;
315             }
316
317             @Override
318             public <T extends RepositoryContent, V extends Repository> T createContent( Class<T> clazz, V repository ) throws RepositoryException
319             {
320                 return null;
321             }
322         };
323     }
324
325     @After
326     @Override
327     public void tearDown()
328         throws Exception
329     {
330         super.tearDown();
331         String appserverBase = System.getProperty( "appserver.base" );
332         if ( StringUtils.isNotEmpty( appserverBase ) )
333         {
334             org.apache.archiva.common.utils.FileUtils.deleteDirectory( Paths.get( appserverBase ) );
335         }
336     }
337
338     // MRM-1232 - Unable to get artifacts from repositories which requires Repository Manager role using repository group
339     @Test
340     public void testRepositoryGroupFirstRepositoryRequiresAuthentication()
341         throws Exception
342     {
343         DavResourceLocator locator = new ArchivaDavResourceLocator( "", "/repository/" + LOCAL_REPO_GROUP
344             + "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", LOCAL_REPO_GROUP,
345                                                                     new ArchivaDavLocatorFactory() );
346
347         ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
348         ManagedRepositoryContent releasesRepo = createManagedRepositoryContent( RELEASES_REPO );
349
350         try
351         {
352             archivaConfigurationControl.reset();
353
354             expect( archivaConfiguration.getConfiguration() ).andReturn( config ).times( 3 );
355
356             expect( request.getMethod() ).andReturn( "GET" ).times( 3 );
357
358             expect( request.getPathInfo() ).andReturn( "org/apache/archiva" ).times( 0, 2 );
359
360             expect( repoFactory.getManagedRepositoryContent( RELEASES_REPO ) ).andReturn( releasesRepo );
361
362             expect( request.getRemoteAddr() ).andReturn( "http://localhost:8080" ).times( 2 );
363
364             expect( request.getDavSession() ).andReturn( new ArchivaDavSession() ).times( 2 );
365
366             expect( request.getContextPath() ).andReturn( "" ).times( 2 );
367
368             expect( repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( true );
369
370             expect(
371                 repoRequest.getLayout( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn(
372                 "legacy" );
373
374             expect( repoRequest.toItemSelector(
375                 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( null );
376
377             expect( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar"
378             ) ).andReturn(
379                 Paths.get( config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
380                           "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).toString());
381
382             expect( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).andReturn( internalRepo );
383
384             expect( repoRequest.isArchetypeCatalog(
385                 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( false );
386             archivaConfigurationControl.replay();
387             requestControl.replay();
388             repoContentFactoryControl.replay();
389             repoRequestControl.replay();
390
391             resourceFactory.createResource( locator, request, response );
392
393             archivaConfigurationControl.verify();
394             requestControl.verify();
395             repoContentFactoryControl.verify();
396             repoRequestControl.verify();
397
398             fail( "A DavException with 401 error code should have been thrown." );
399         }
400         catch ( DavException e )
401         {
402             assertEquals( 401, e.getErrorCode() );
403         }
404     }
405
406     @Test
407     public void testRepositoryGroupLastRepositoryRequiresAuthentication()
408         throws Exception
409     {
410         DavResourceLocator locator = new ArchivaDavResourceLocator( "", "/repository/" + LOCAL_REPO_GROUP
411             + "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", LOCAL_REPO_GROUP,
412                                                                     new ArchivaDavLocatorFactory() );
413
414         List<RepositoryGroupConfiguration> repoGroups = new ArrayList<>();
415         RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
416         repoGroup.setId( LOCAL_REPO_GROUP );
417         repoGroup.addRepository( INTERNAL_REPO );
418         repoGroup.addRepository( RELEASES_REPO );
419
420         repoGroups.add( repoGroup );
421
422         config.setRepositoryGroups( repoGroups );
423
424         ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
425
426         ManagedRepositoryContent releasesRepo = createManagedRepositoryContent( RELEASES_REPO );
427
428         try
429         {
430             archivaConfigurationControl.reset();
431
432             expect( archivaConfiguration.getConfiguration() ).andReturn( config ).times( 3 );
433
434             expect( request.getMethod() ).andReturn( "GET" ).times( 3 );
435
436             expect( request.getPathInfo() ).andReturn( "org/apache/archiva" ).times( 0, 2 );
437
438             expect( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).andReturn( internalRepo );
439
440             expect( repoFactory.getManagedRepositoryContent( RELEASES_REPO ) ).andReturn( releasesRepo );
441
442             expect( request.getRemoteAddr() ).andReturn( "http://localhost:8080" ).times( 2 );
443
444             expect( request.getDavSession() ).andReturn( new ArchivaDavSession() ).times( 2 );
445
446             expect( request.getContextPath() ).andReturn( "" ).times( 2 );
447
448             expect( repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( false );
449
450             expect(
451                 repoRequest.getLayout( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn(
452                 "legacy" );
453
454             expect( repoRequest.toItemSelector(
455                 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( null );
456
457             expect( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar"
458             ) ).andReturn(
459                 Paths.get( config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
460                           "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).toString());
461
462
463             expect( repoRequest.isArchetypeCatalog(
464                 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( false );
465             archivaConfigurationControl.replay();
466             requestControl.replay();
467             repoContentFactoryControl.replay();
468             repoRequestControl.replay();
469
470             resourceFactory.createResource( locator, request, response );
471
472             archivaConfigurationControl.verify();
473             requestControl.verify();
474             repoContentFactoryControl.verify();
475             repoRequestControl.verify();
476
477             fail( "A DavException with 401 error code should have been thrown." );
478         }
479         catch ( DavException e )
480         {
481             assertEquals( 401, e.getErrorCode() );
482         }
483     }
484
485     @Test
486     public void testRepositoryGroupArtifactDoesNotExistInAnyOfTheReposAuthenticationDisabled()
487         throws Exception
488     {
489         DavResourceLocator locator = new ArchivaDavResourceLocator( "", "/repository/" + LOCAL_REPO_GROUP
490             + "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", LOCAL_REPO_GROUP,
491                                                                     new ArchivaDavLocatorFactory() );
492
493         defaultManagedRepositoryAdmin.addManagedRepository(
494             createManagedRepository( LOCAL_MIRROR_REPO, Paths.get( "target/test-classes/local-mirror" ).toString(),
495                                      "default" ), false, null );
496
497         List<RepositoryGroupConfiguration> repoGroups = new ArrayList<>();
498         RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
499         repoGroup.setId( LOCAL_REPO_GROUP );
500         repoGroup.addRepository( INTERNAL_REPO );
501         repoGroup.addRepository( LOCAL_MIRROR_REPO );
502
503         repoGroups.add( repoGroup );
504
505         config.setRepositoryGroups( repoGroups );
506
507         ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
508         ManagedRepositoryContent localMirrorRepo = createManagedRepositoryContent( LOCAL_MIRROR_REPO );
509
510         repositoryRegistry.putRepositoryGroup( repoGroup );
511
512         try
513         {
514             archivaConfigurationControl.reset();
515
516             expect( archivaConfiguration.getConfiguration() ).andReturn( config ).times( 3 );
517
518             expect( request.getMethod() ).andReturn( "GET" ).times( 5 );
519
520             expect( request.getPathInfo() ).andReturn( "org/apache/archiva" ).times( 0, 2 );
521
522             expect( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).andReturn( internalRepo );
523
524             expect( repoFactory.getManagedRepositoryContent( LOCAL_MIRROR_REPO ) ).andReturn( localMirrorRepo );
525
526             expect( request.getRemoteAddr() ).andReturn( "http://localhost:8080" ).times( 4 );
527
528             expect( request.getDavSession() ).andReturn( new ArchivaDavSession() ).times( 4 );
529
530             expect( request.getContextPath() ).andReturn( "" ).times( 2 );
531
532             expect( repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( false ).times( 2 );
533
534             expect(
535                 repoRequest.getLayout( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn(
536                 "legacy" ).times( 2 );
537
538             expect( repoRequest.toItemSelector(
539                 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( null ).times( 2 );
540
541             expect( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar"
542             ) ).andReturn(
543                 Paths.get( config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
544                           "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).toString() );
545
546             expect( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar"
547             ) )
548                 .andReturn( Paths.get( config.findManagedRepositoryById( LOCAL_MIRROR_REPO ).getLocation(),
549                                       "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).toString());
550
551             expect( repoRequest.isArchetypeCatalog( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( false ).times( 2 );
552             archivaConfigurationControl.replay();
553             requestControl.replay();
554             repoContentFactoryControl.replay();
555             repoRequestControl.replay();
556
557             resourceFactory.createResource( locator, request, response );
558
559             archivaConfigurationControl.verify();
560             requestControl.verify();
561             repoContentFactoryControl.verify();
562             repoRequestControl.verify();
563
564             fail( "A DavException with 404 error code should have been thrown." );
565         }
566         catch ( DavException e )
567         {
568             assertEquals( 404, e.getErrorCode() );
569         }
570     }
571
572     // MRM-1239
573     @Test
574     public void testRequestArtifactMetadataThreePartsRepoHasDefaultLayout()
575         throws Exception
576     {
577         // should fetch metadata 
578         DavResourceLocator locator =
579             new ArchivaDavResourceLocator( "", "/repository/" + INTERNAL_REPO + "/eclipse/jdtcore/maven-metadata.xml",
580                                            INTERNAL_REPO, new ArchivaDavLocatorFactory() );
581
582         ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
583
584         // use actual object (this performs the isMetadata, isDefault and isSupportFile check!)
585         MavenRepositoryRequestInfo repoRequest = new MavenRepositoryRequestInfo(internalRepo.getRepository() );
586
587         try
588         {
589             archivaConfigurationControl.reset();
590
591             expect( request.getMethod() ).andReturn( "GET" ).times( 4 );
592
593             expect( request.getRemoteAddr() ).andReturn( "http://localhost:8080" ).times( 3 );
594
595             expect( request.getContextPath() ).andReturn( "" ).times( 1 );
596
597             expect( request.getDavSession() ).andReturn( new ArchivaDavSession() ).times( 2 );
598
599             expect( request.getRequestURI() ).andReturn( "http://localhost:8080/archiva/repository/" + INTERNAL_REPO + "/eclipse/jdtcore/maven-metadata.xml" );
600             response.setHeader( "Pragma", "no-cache" );
601
602             expectLastCall();
603
604             response.setHeader( "Cache-Control", "no-cache" );
605
606             expectLastCall();
607
608             response.setDateHeader( eq("Last-Modified"), anyLong() );
609             expectLastCall();
610
611             archivaConfigurationControl.replay();
612             repoContentFactoryControl.replay();
613             requestControl.replay();
614             responseControl.replay();
615
616             resourceFactory.createResource( locator, request, response );
617
618             archivaConfigurationControl.verify();
619             repoContentFactoryControl.verify();
620             requestControl.verify();
621             responseControl.verify();
622         }
623         catch ( DavException e )
624         {
625             e.printStackTrace();
626             fail( "A DavException should not have been thrown! "+e.getMessage() );
627         }
628     }
629
630     @Test
631     public void testRequestArtifactMetadataTwoPartsRepoHasDefaultLayout()
632         throws Exception
633     {
634         // should not fetch metadata
635         DavResourceLocator locator =
636             new ArchivaDavResourceLocator( "", "/repository/" + INTERNAL_REPO + "/eclipse/maven-metadata.xml",
637                                            INTERNAL_REPO, new ArchivaDavLocatorFactory() );
638
639         ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
640
641         try
642         {
643             archivaConfigurationControl.reset();
644
645             expect( archivaConfiguration.getConfiguration() ).andReturn( config ).times( 2 );
646
647             expect( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).andReturn( internalRepo );
648
649             expect( request.getMethod() ).andReturn( "GET" ).times( 3 );
650
651             expect( request.getRemoteAddr() ).andReturn( "http://localhost:8080" ).times( 3 );
652
653             expect( request.getDavSession() ).andReturn( new ArchivaDavSession() ).times( 2 );
654
655             expect( request.getContextPath() ).andReturn( "" ).times( 2 );
656
657             archivaConfigurationControl.replay();
658             repoContentFactoryControl.replay();
659             requestControl.replay();
660
661             resourceFactory.createResource( locator, request, response );
662
663             archivaConfigurationControl.verify();
664             repoContentFactoryControl.verify();
665             requestControl.verify();
666
667             fail( "A 404 error should have been thrown!" );
668         }
669         catch ( DavException e )
670         {
671             assertEquals( 404, e.getErrorCode() );
672         }
673     }
674
675     @Test
676     public void testRequestMetadataRepoIsLegacy()
677         throws Exception
678     {
679         ManagedRepositoryContent legacyRepo = createManagedRepositoryContent( LEGACY_REPO );
680         ConfigurableListableBeanFactory beanFactory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory();
681         RepositoryContentProvider provider = createRepositoryContentProvider(legacyRepo );
682         beanFactory.registerSingleton("repositoryContentProvider#legacy", provider);
683         RepositoryContentFactory repoContentFactory = applicationContext.getBean( "repositoryContentFactory#default", RepositoryContentFactory.class );
684         repoContentFactory.getRepositoryContentProviders().add(provider);
685         defaultManagedRepositoryAdmin.addManagedRepository(
686             createManagedRepository( LEGACY_REPO, getProjectBase().resolve( "target/test-classes/" + LEGACY_REPO ).toString(),
687                 "legacy" ), false, null );
688
689         DavResourceLocator locator =
690             new ArchivaDavResourceLocator( "", "/repository/" + LEGACY_REPO + "/eclipse/maven-metadata.xml",
691                                            LEGACY_REPO, new ArchivaDavLocatorFactory() );
692
693
694         try
695         {
696             archivaConfigurationControl.reset();
697
698             expect( archivaConfiguration.getConfiguration() ).andReturn( config ).times( 2 );
699
700             expect( repoFactory.getManagedRepositoryContent( LEGACY_REPO ) ).andReturn( legacyRepo );
701
702             expect( request.getMethod() ).andReturn( "GET" ).times( 3 );
703
704             expect( request.getRemoteAddr() ).andReturn( "http://localhost:8080" ).times( 3 );
705
706             expect( request.getDavSession() ).andReturn( new ArchivaDavSession() ).times( 2 );
707
708             expect( request.getContextPath() ).andReturn( "" ).times( 2 );
709
710             archivaConfigurationControl.replay();
711             repoContentFactoryControl.replay();
712             requestControl.replay();
713
714             resourceFactory.createResource( locator, request, response );
715
716             archivaConfigurationControl.verify();
717             repoContentFactoryControl.verify();
718             requestControl.verify();
719
720             fail( "A 404 error should have been thrown!" );
721         }
722         catch ( DavException e )
723         {
724             assertEquals( 404, e.getErrorCode() );
725         }
726     }
727
728     class OverridingArchivaDavResourceFactory
729         extends ArchivaDavResourceFactory
730     {
731
732         OverridingArchivaDavResourceFactory( ApplicationContext applicationContext,
733                                              ArchivaConfiguration archivaConfiguration )
734         {
735             super( applicationContext, archivaConfiguration );
736         }
737
738         @Override
739         protected boolean isAuthorized( DavServletRequest request, String repositoryId )
740             throws DavException
741         {
742             if ( RELEASES_REPO.equals( repositoryId ) )
743             {
744                 throw new UnauthorizedDavException( repositoryId,
745                                                     "You are not authenticated and authorized to access any repository." );
746             }
747             else
748             {
749                 return true;
750             }
751         }
752
753         @Override
754         protected String getActivePrincipal( DavServletRequest request )
755         {
756             return "guest";
757         }
758     }
759
760 }