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