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