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