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