1 package org.apache.archiva.proxy;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
49 import javax.inject.Inject;
50 import javax.servlet.ServletException;
51 import javax.servlet.http.HttpServletRequest;
52 import javax.servlet.http.HttpServletResponse;
54 import java.io.IOException;
55 import java.nio.charset.Charset;
57 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
59 import static org.junit.Assert.*;
62 * Integration test for connecting over a HTTP proxy.
66 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
67 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
68 public class HttpProxyTransferTest
70 private static final String PROXY_ID = "proxy";
72 private static final String MANAGED_ID = "default-managed-repository";
74 private static final String PROXIED_ID = "proxied1";
76 private static final String PROXIED_BASEDIR = "src/test/repositories/proxied1";
78 private RepositoryProxyConnectors proxyHandler;
80 private ArchivaConfiguration config;
82 private ManagedRepositoryContent managedDefaultRepository;
85 private ApplicationContext applicationContext;
87 private Server server;
93 proxyHandler = applicationContext.getBean( "repositoryProxyConnectors#test", RepositoryProxyConnectors.class );
95 config = applicationContext.getBean( "archivaConfiguration#mock", ArchivaConfiguration.class );
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();
101 // Setup source repository (using default layout)
102 String repoPath = "target/test-repository/managed/" + getClass().getSimpleName();
104 File destRepoDir = new File( repoPath );
106 // Cleanout destination dirs.
107 if ( destRepoDir.exists() )
109 FileUtils.deleteDirectory( destRepoDir );
112 // Make the destination dir.
113 destRepoDir.mkdirs();
115 ManagedRepository repo = new ManagedRepository();
116 repo.setId( MANAGED_ID );
117 repo.setName( "Default Managed Repository" );
118 repo.setLocation( repoPath );
119 repo.setLayout( "default" );
121 ManagedRepositoryContent repoContent =
122 applicationContext.getBean( "managedRepositoryContent#default", ManagedRepositoryContent.class );
124 repoContent.setRepository( repo );
125 managedDefaultRepository = repoContent;
127 ( (DefaultManagedRepositoryAdmin) applicationContext.getBean(
128 ManagedRepositoryAdmin.class ) ).setArchivaConfiguration( config );
130 ManagedRepositoryAdmin managedRepositoryAdmin = applicationContext.getBean( ManagedRepositoryAdmin.class );
131 if ( managedRepositoryAdmin.getManagedRepository( repo.getId() ) == null )
133 managedRepositoryAdmin.addManagedRepository( repo, false, null );
136 //config.getConfiguration().addManagedRepository( repo );
138 Handler handler = new AbstractHandler()
140 public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch )
141 throws IOException, ServletException
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" ) );
148 ( (Request) request ).setHandled( true );
152 server = new Server( 0 );
153 server.setHandler( handler );
156 int port = server.getConnectors()[0].getLocalPort();
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 );
165 // Setup target (proxied to) repository.
166 RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
168 repoConfig.setId( PROXIED_ID );
169 repoConfig.setName( "Proxied Repository 1" );
170 repoConfig.setLayout( "default" );
171 repoConfig.setUrl( "http://www.example.com/" );
173 config.getConfiguration().addRemoteRepository( repoConfig );
178 public void tearDown()
185 public void testGetOverHttpProxy()
188 assertNull( System.getProperty( "http.proxyHost" ) );
189 assertNull( System.getProperty( "http.proxyPort" ) );
191 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
193 // Configure Connector (usually done within archiva.xml configuration)
196 File expectedFile = new File( new File( managedDefaultRepository.getRepoRoot() ), path );
197 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
199 // Attempt the proxy fetch.
200 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
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 );
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() );
211 String expectedContents = FileUtils.readFileToString( sourceFile, Charset.defaultCharset() );
212 String actualContents = FileUtils.readFileToString( downloadedFile, Charset.defaultCharset() );
213 assertEquals( "Check file contents.", expectedContents, actualContents );
215 assertNull( System.getProperty( "http.proxyHost" ) );
216 assertNull( System.getProperty( "http.proxyPort" ) );
219 private void addConnector()
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 );
234 int count = config.getConfiguration().getProxyConnectors().size();
235 config.getConfiguration().addProxyConnector( connectorConfig );
237 // Proper Triggering ...
238 String prefix = "proxyConnectors.proxyConnector(" + count + ")";
239 ( (MockConfiguration) config ).triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );