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 org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
57 import static org.junit.Assert.*;
60 * Integration test for connecting over a HTTP proxy.
62 * @version $Id: ManagedDefaultTransferTest.java 677852 2008-07-18 08:16:24Z brett $
64 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
65 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
66 public class HttpProxyTransferTest
68 private static final String PROXY_ID = "proxy";
70 private static final String MANAGED_ID = "default-managed-repository";
72 private static final String PROXIED_ID = "proxied1";
74 private static final String PROXIED_BASEDIR = "src/test/repositories/proxied1";
76 private RepositoryProxyConnectors proxyHandler;
78 private ArchivaConfiguration config;
80 private ManagedRepositoryContent managedDefaultRepository;
83 private ApplicationContext applicationContext;
85 private Server server;
91 proxyHandler = applicationContext.getBean( "repositoryProxyConnectors#test", RepositoryProxyConnectors.class );
93 config = applicationContext.getBean( "archivaConfiguration#mock", ArchivaConfiguration.class );
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();
99 // Setup source repository (using default layout)
100 String repoPath = "target/test-repository/managed/" + getClass().getSimpleName();
102 File destRepoDir = new File( repoPath );
104 // Cleanout destination dirs.
105 if ( destRepoDir.exists() )
107 FileUtils.deleteDirectory( destRepoDir );
110 // Make the destination dir.
111 destRepoDir.mkdirs();
113 ManagedRepository repo = new ManagedRepository();
114 repo.setId( MANAGED_ID );
115 repo.setName( "Default Managed Repository" );
116 repo.setLocation( repoPath );
117 repo.setLayout( "default" );
119 ManagedRepositoryContent repoContent =
120 applicationContext.getBean( "managedRepositoryContent#default", ManagedRepositoryContent.class );
122 repoContent.setRepository( repo );
123 managedDefaultRepository = repoContent;
125 ( (DefaultManagedRepositoryAdmin) applicationContext.getBean(
126 ManagedRepositoryAdmin.class ) ).setArchivaConfiguration( config );
128 ManagedRepositoryAdmin managedRepositoryAdmin = applicationContext.getBean( ManagedRepositoryAdmin.class );
129 if ( managedRepositoryAdmin.getManagedRepository( repo.getId() ) == null )
131 managedRepositoryAdmin.addManagedRepository( repo, false, null );
134 //config.getConfiguration().addManagedRepository( repo );
136 Handler handler = new AbstractHandler()
138 public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch )
139 throws IOException, ServletException
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" ) );
146 ( (Request) request ).setHandled( true );
150 server = new Server( 0 );
151 server.setHandler( handler );
154 int port = server.getConnectors()[0].getLocalPort();
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 );
163 // Setup target (proxied to) repository.
164 RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
166 repoConfig.setId( PROXIED_ID );
167 repoConfig.setName( "Proxied Repository 1" );
168 repoConfig.setLayout( "default" );
169 repoConfig.setUrl( "http://www.example.com/" );
171 config.getConfiguration().addRemoteRepository( repoConfig );
176 public void tearDown()
183 public void testGetOverHttpProxy()
186 assertNull( System.getProperty( "http.proxyHost" ) );
187 assertNull( System.getProperty( "http.proxyPort" ) );
189 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
191 // Configure Connector (usually done within archiva.xml configuration)
194 File expectedFile = new File( new File( managedDefaultRepository.getRepoRoot() ), path );
195 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
197 // Attempt the proxy fetch.
198 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
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 );
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() );
209 String expectedContents = FileUtils.readFileToString( sourceFile, null );
210 String actualContents = FileUtils.readFileToString( downloadedFile, null );
211 assertEquals( "Check file contents.", expectedContents, actualContents );
213 assertNull( System.getProperty( "http.proxyHost" ) );
214 assertNull( System.getProperty( "http.proxyPort" ) );
217 private void addConnector()
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 );
232 int count = config.getConfiguration().getProxyConnectors().size();
233 config.getConfiguration().addProxyConnector( connectorConfig );
235 // Proper Triggering ...
236 String prefix = "proxyConnectors.proxyConnector(" + count + ")";
237 ( (MockConfiguration) config ).triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );