]> source.dussan.org Git - archiva.git/blob
8af5ea5360385469c71f22516be0b35930006977
[archiva.git] /
1 package org.apache.archiva.proxy;
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 org.apache.archiva.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
24 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
25 import org.apache.commons.io.FileUtils;
26 import org.apache.archiva.configuration.ArchivaConfiguration;
27 import org.apache.archiva.configuration.NetworkProxyConfiguration;
28 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
29 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
30 import org.apache.archiva.model.ArtifactReference;
31 import org.apache.archiva.policies.CachedFailuresPolicy;
32 import org.apache.archiva.policies.ChecksumPolicy;
33 import org.apache.archiva.policies.PropagateErrorsDownloadPolicy;
34 import org.apache.archiva.policies.PropagateErrorsOnUpdateDownloadPolicy;
35 import org.apache.archiva.policies.ReleasesPolicy;
36 import org.apache.archiva.policies.SnapshotsPolicy;
37 import org.apache.archiva.repository.ManagedRepositoryContent;
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mortbay.jetty.Handler;
43 import org.mortbay.jetty.Request;
44 import org.mortbay.jetty.Server;
45 import org.mortbay.jetty.handler.AbstractHandler;
46 import org.springframework.context.ApplicationContext;
47 import org.springframework.test.context.ContextConfiguration;
48 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
49
50 import javax.inject.Inject;
51 import javax.servlet.ServletException;
52 import javax.servlet.http.HttpServletRequest;
53 import javax.servlet.http.HttpServletResponse;
54 import java.io.File;
55 import java.io.IOException;
56
57 import static org.junit.Assert.*;
58
59 /**
60  * Integration test for connecting over a HTTP proxy.
61  *
62  * @version $Id: ManagedDefaultTransferTest.java 677852 2008-07-18 08:16:24Z brett $
63  */
64 @RunWith( SpringJUnit4ClassRunner.class )
65 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
66 public class HttpProxyTransferTest
67 {
68     private static final String PROXY_ID = "proxy";
69
70     private static final String MANAGED_ID = "default-managed-repository";
71
72     private static final String PROXIED_ID = "proxied1";
73
74     private static final String PROXIED_BASEDIR = "src/test/repositories/proxied1";
75
76     private RepositoryProxyConnectors proxyHandler;
77
78     private ArchivaConfiguration config;
79
80     private ManagedRepositoryContent managedDefaultRepository;
81
82     @Inject
83     private ApplicationContext applicationContext;
84
85     private Server server;
86
87     @Before
88     public void setUp()
89         throws Exception
90     {
91         proxyHandler = applicationContext.getBean( "repositoryProxyConnectors#test", RepositoryProxyConnectors.class );
92
93         config = applicationContext.getBean( "archivaConfiguration#mock", ArchivaConfiguration.class );
94
95         // clear from previous tests - TODO the spring context should be initialised per test instead, or the config
96         // made a complete mock
97         config.getConfiguration().getProxyConnectors().clear();
98
99         // Setup source repository (using default layout)
100         String repoPath = "target/test-repository/managed/" + getClass().getSimpleName();
101
102         File destRepoDir = new File( repoPath );
103
104         // Cleanout destination dirs.
105         if ( destRepoDir.exists() )
106         {
107             FileUtils.deleteDirectory( destRepoDir );
108         }
109
110         // Make the destination dir.
111         destRepoDir.mkdirs();
112
113         ManagedRepository repo = new ManagedRepository();
114         repo.setId( MANAGED_ID );
115         repo.setName( "Default Managed Repository" );
116         repo.setLocation( repoPath );
117         repo.setLayout( "default" );
118
119         ManagedRepositoryContent repoContent =
120             applicationContext.getBean( "managedRepositoryContent#default", ManagedRepositoryContent.class );
121
122         repoContent.setRepository( repo );
123         managedDefaultRepository = repoContent;
124
125         ( (DefaultManagedRepositoryAdmin) applicationContext.getBean(
126             ManagedRepositoryAdmin.class ) ).setArchivaConfiguration( config );
127
128         ManagedRepositoryAdmin managedRepositoryAdmin = applicationContext.getBean( ManagedRepositoryAdmin.class );
129         if ( managedRepositoryAdmin.getManagedRepository( repo.getId() ) == null )
130         {
131             managedRepositoryAdmin.addManagedRepository( repo, false, null );
132         }
133
134         //config.getConfiguration().addManagedRepository( repo );
135
136         Handler handler = new AbstractHandler()
137         {
138             public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch )
139                 throws IOException, ServletException
140             {
141                 response.setContentType( "text/plain" );
142                 response.setStatus( HttpServletResponse.SC_OK );
143                 response.getWriter().print( "get-default-layout-1.0.jar\n\n" );
144                 assertNotNull( request.getHeader( "Proxy-Connection" ) );
145
146                 ( (Request) request ).setHandled( true );
147             }
148         };
149
150         server = new Server( 0 );
151         server.setHandler( handler );
152         server.start();
153
154         int port = server.getConnectors()[0].getLocalPort();
155
156         NetworkProxyConfiguration proxyConfig = new NetworkProxyConfiguration();
157         proxyConfig.setHost( "localhost" );
158         proxyConfig.setPort( port );
159         proxyConfig.setProtocol( "http" );
160         proxyConfig.setId( PROXY_ID );
161         config.getConfiguration().addNetworkProxy( proxyConfig );
162
163         // Setup target (proxied to) repository.
164         RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
165
166         repoConfig.setId( PROXIED_ID );
167         repoConfig.setName( "Proxied Repository 1" );
168         repoConfig.setLayout( "default" );
169         repoConfig.setUrl( "http://www.example.com/" );
170
171         config.getConfiguration().addRemoteRepository( repoConfig );
172
173     }
174
175     @After
176     public void tearDown()
177         throws Exception
178     {
179         server.stop();
180     }
181
182     @Test
183     public void testGetOverHttpProxy()
184         throws Exception
185     {
186         assertNull( System.getProperty( "http.proxyHost" ) );
187         assertNull( System.getProperty( "http.proxyPort" ) );
188
189         String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
190
191         // Configure Connector (usually done within archiva.xml configuration)
192         addConnector();
193
194         File expectedFile = new File( new File( managedDefaultRepository.getRepoRoot() ), path );
195         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
196
197         // Attempt the proxy fetch.
198         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
199
200         File sourceFile = new File( PROXIED_BASEDIR, path );
201         assertNotNull( "Expected File should not be null.", expectedFile );
202         assertNotNull( "Actual File should not be null.", downloadedFile );
203
204         assertTrue( "Check actual file exists.", downloadedFile.exists() );
205         assertEquals( "Check filename path is appropriate.", expectedFile.getCanonicalPath(),
206                       downloadedFile.getCanonicalPath() );
207         assertEquals( "Check file path matches.", expectedFile.getAbsolutePath(), downloadedFile.getAbsolutePath() );
208
209         String expectedContents = FileUtils.readFileToString( sourceFile, null );
210         String actualContents = FileUtils.readFileToString( downloadedFile, null );
211         assertEquals( "Check file contents.", expectedContents, actualContents );
212
213         assertNull( System.getProperty( "http.proxyHost" ) );
214         assertNull( System.getProperty( "http.proxyPort" ) );
215     }
216
217     private void addConnector()
218     {
219         ProxyConnectorConfiguration connectorConfig = new ProxyConnectorConfiguration();
220         connectorConfig.setProxyId( PROXY_ID );
221         connectorConfig.setSourceRepoId( MANAGED_ID );
222         connectorConfig.setTargetRepoId( PROXIED_ID );
223         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, ChecksumPolicy.FIX );
224         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, ReleasesPolicy.ONCE );
225         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, SnapshotsPolicy.ONCE );
226         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, CachedFailuresPolicy.NO );
227         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS,
228                                    PropagateErrorsDownloadPolicy.QUEUE );
229         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS_ON_UPDATE,
230                                    PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT );
231
232         int count = config.getConfiguration().getProxyConnectors().size();
233         config.getConfiguration().addProxyConnector( connectorConfig );
234
235         // Proper Triggering ...
236         String prefix = "proxyConnectors.proxyConnector(" + count + ")";
237         ( (MockConfiguration) config ).triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );
238     }
239 }