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.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;
50 import javax.inject.Inject;
51 import javax.servlet.ServletException;
52 import javax.servlet.http.HttpServletRequest;
53 import javax.servlet.http.HttpServletResponse;
55 import java.io.IOException;
56 import java.nio.charset.Charset;
58 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
60 import static org.junit.Assert.*;
63 * Integration test for connecting over a HTTP proxy.
67 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
68 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
69 public class HttpProxyTransferTest
71 private static final String PROXY_ID = "proxy";
73 private static final String MANAGED_ID = "default-managed-repository";
75 private static final String PROXIED_ID = "proxied1";
77 private static final String PROXIED_BASEDIR = "src/test/repositories/proxied1";
79 private RepositoryProxyConnectors proxyHandler;
81 private ArchivaConfiguration config;
83 private ManagedRepositoryContent managedDefaultRepository;
86 private ApplicationContext applicationContext;
88 private Server server;
94 proxyHandler = applicationContext.getBean( "repositoryProxyConnectors#test", RepositoryProxyConnectors.class );
96 config = applicationContext.getBean( "archivaConfiguration#mock", ArchivaConfiguration.class );
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();
102 // Setup source repository (using default layout)
103 String repoPath = "target/test-repository/managed/" + getClass().getSimpleName();
105 File destRepoDir = new File( repoPath );
107 // Cleanout destination dirs.
108 if ( destRepoDir.exists() )
110 FileUtils.deleteDirectory( destRepoDir );
113 // Make the destination dir.
114 destRepoDir.mkdirs();
116 ManagedRepository repo = new ManagedRepository();
117 repo.setId( MANAGED_ID );
118 repo.setName( "Default Managed Repository" );
119 repo.setLocation( repoPath );
120 repo.setLayout( "default" );
122 ManagedRepositoryContent repoContent =
123 applicationContext.getBean( "managedRepositoryContent#default", ManagedRepositoryContent.class );
125 repoContent.setRepository( repo );
126 managedDefaultRepository = repoContent;
128 ( (DefaultManagedRepositoryAdmin) applicationContext.getBean(
129 ManagedRepositoryAdmin.class ) ).setArchivaConfiguration( config );
131 ManagedRepositoryAdmin managedRepositoryAdmin = applicationContext.getBean( ManagedRepositoryAdmin.class );
132 if ( managedRepositoryAdmin.getManagedRepository( repo.getId() ) == null )
134 managedRepositoryAdmin.addManagedRepository( repo, false, null );
137 //config.getConfiguration().addManagedRepository( repo );
139 Handler handler = new AbstractHandler()
141 public void handle( String s, Request request, HttpServletRequest httpServletRequest,
142 HttpServletResponse response )
143 throws IOException, ServletException
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" ) );
150 ( (Request) request ).setHandled( true );
153 public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch )
154 throws IOException, ServletException
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" ) );
161 ( (Request) request ).setHandled( true );
165 server = new Server( 0 );
166 server.setHandler( handler );
169 int port = server.getConnectors()[0].getLocalPort();
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 );
178 // Setup target (proxied to) repository.
179 RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
181 repoConfig.setId( PROXIED_ID );
182 repoConfig.setName( "Proxied Repository 1" );
183 repoConfig.setLayout( "default" );
184 repoConfig.setUrl( "http://www.example.com/" );
186 config.getConfiguration().addRemoteRepository( repoConfig );
191 public void tearDown()
198 public void testGetOverHttpProxy()
201 assertNull( System.getProperty( "http.proxyHost" ) );
202 assertNull( System.getProperty( "http.proxyPort" ) );
204 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
206 // Configure Connector (usually done within archiva.xml configuration)
209 File expectedFile = new File( new File( managedDefaultRepository.getRepoRoot() ), path );
210 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
212 // Attempt the proxy fetch.
213 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
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 );
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() );
224 String expectedContents = FileUtils.readFileToString( sourceFile, Charset.defaultCharset() );
225 String actualContents = FileUtils.readFileToString( downloadedFile, Charset.defaultCharset() );
226 assertEquals( "Check file contents.", expectedContents, actualContents );
228 assertNull( System.getProperty( "http.proxyHost" ) );
229 assertNull( System.getProperty( "http.proxyPort" ) );
232 private void addConnector()
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 );
247 int count = config.getConfiguration().getProxyConnectors().size();
248 config.getConfiguration().addProxyConnector( connectorConfig );
250 // Proper Triggering ...
251 String prefix = "proxyConnectors.proxyConnector(" + count + ")";
252 ( (MockConfiguration) config ).triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );