1 package org.apache.maven.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
23 import java.io.IOException;
25 import javax.servlet.ServletException;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
29 import org.apache.commons.io.FileUtils;
30 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
31 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
32 import org.apache.maven.archiva.configuration.NetworkProxyConfiguration;
33 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
34 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
35 import org.apache.maven.archiva.model.ArtifactReference;
36 import org.apache.maven.archiva.policies.CachedFailuresPolicy;
37 import org.apache.maven.archiva.policies.ChecksumPolicy;
38 import org.apache.maven.archiva.policies.PropagateErrorsDownloadPolicy;
39 import org.apache.maven.archiva.policies.PropagateErrorsOnUpdateDownloadPolicy;
40 import org.apache.maven.archiva.policies.ReleasesPolicy;
41 import org.apache.maven.archiva.policies.SnapshotsPolicy;
42 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
43 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
44 import org.mortbay.jetty.Handler;
45 import org.mortbay.jetty.Request;
46 import org.mortbay.jetty.Server;
47 import org.mortbay.jetty.handler.AbstractHandler;
50 * Integration test for connecting over a HTTP proxy.
52 * @version $Id: ManagedDefaultTransferTest.java 677852 2008-07-18 08:16:24Z brett $
54 public class HttpProxyTransferTest
55 extends PlexusInSpringTestCase
57 private static final String PROXY_ID = "proxy";
59 private static final String MANAGED_ID = "default-managed-repository";
61 private static final String PROXIED_ID = "proxied1";
63 private static final String PROXIED_BASEDIR = "src/test/repositories/proxied1";
65 private RepositoryProxyConnectors proxyHandler;
67 private ArchivaConfiguration config;
69 private ManagedRepositoryContent managedDefaultRepository;
71 private Server server;
74 protected void setUp()
79 // Setup source repository (using default layout)
80 String repoPath = "target/test-repository/managed/" + getName();
82 File destRepoDir = new File( repoPath );
84 // Cleanout destination dirs.
85 if ( destRepoDir.exists() )
87 FileUtils.deleteDirectory( destRepoDir );
90 // Make the destination dir.
93 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
94 repo.setId( MANAGED_ID );
95 repo.setName( "Default Managed Repository" );
96 repo.setLocation( repoPath );
97 repo.setLayout( "default" );
99 ManagedRepositoryContent repoContent =
100 (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
101 repoContent.setRepository( repo );
102 managedDefaultRepository = repoContent;
104 config = (MockConfiguration) lookup( ArchivaConfiguration.class.getName(), "mock" );
105 config.getConfiguration().addManagedRepository( repo );
107 Handler handler = new AbstractHandler()
109 public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch )
110 throws IOException, ServletException
112 response.setContentType( "text/plain" );
113 response.setStatus( HttpServletResponse.SC_OK );
114 response.getWriter().print( "get-default-layout-1.0.jar\n\n" );
115 assertNotNull( request.getHeader( "Proxy-Connection" ) );
117 ( (Request) request ).setHandled( true );
121 server = new Server( 0 );
122 server.setHandler( handler );
125 int port = server.getConnectors()[0].getLocalPort();
127 NetworkProxyConfiguration proxyConfig = new NetworkProxyConfiguration();
128 proxyConfig.setHost( "localhost" );
129 proxyConfig.setPort( port );
130 proxyConfig.setProtocol( "http" );
131 proxyConfig.setId( PROXY_ID );
132 config.getConfiguration().addNetworkProxy( proxyConfig );
134 // Setup target (proxied to) repository.
135 RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
137 repoConfig.setId( PROXIED_ID );
138 repoConfig.setName( "Proxied Repository 1" );
139 repoConfig.setLayout( "default" );
140 repoConfig.setUrl( "http://www.example.com/" );
142 config.getConfiguration().addRemoteRepository( repoConfig );
144 // Setup the proxy handler.
145 proxyHandler = (RepositoryProxyConnectors) lookup( RepositoryProxyConnectors.class.getName() );
149 protected void tearDown()
157 public void testGetOverHttpProxy()
160 assertNull( System.getProperty( "http.proxyHost" ) );
161 assertNull( System.getProperty( "http.proxyPort" ) );
163 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
165 // Configure Connector (usually done within archiva.xml configuration)
168 File expectedFile = new File( new File( managedDefaultRepository.getRepoRoot() ), path );
169 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
171 // Attempt the proxy fetch.
172 File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
174 File sourceFile = new File( PROXIED_BASEDIR, path );
175 assertNotNull( "Expected File should not be null.", expectedFile );
176 assertNotNull( "Actual File should not be null.", downloadedFile );
178 assertTrue( "Check actual file exists.", downloadedFile.exists() );
179 assertEquals( "Check filename path is appropriate.", expectedFile.getCanonicalPath(),
180 downloadedFile.getCanonicalPath() );
181 assertEquals( "Check file path matches.", expectedFile.getAbsolutePath(), downloadedFile.getAbsolutePath() );
183 String expectedContents = FileUtils.readFileToString( sourceFile, null );
184 String actualContents = FileUtils.readFileToString( downloadedFile, null );
185 assertEquals( "Check file contents.", expectedContents, actualContents );
187 assertNull( System.getProperty( "http.proxyHost" ) );
188 assertNull( System.getProperty( "http.proxyPort" ) );
191 private void addConnector()
193 ProxyConnectorConfiguration connectorConfig = new ProxyConnectorConfiguration();
194 connectorConfig.setProxyId( PROXY_ID );
195 connectorConfig.setSourceRepoId( MANAGED_ID );
196 connectorConfig.setTargetRepoId( PROXIED_ID );
197 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, ChecksumPolicy.FIX );
198 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, ReleasesPolicy.ONCE );
199 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, SnapshotsPolicy.ONCE );
200 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, CachedFailuresPolicy.NO );
201 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS,
202 PropagateErrorsDownloadPolicy.QUEUE );
203 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS_ON_UPDATE,
204 PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT );
206 int count = config.getConfiguration().getProxyConnectors().size();
207 config.getConfiguration().addProxyConnector( connectorConfig );
209 // Proper Triggering ...
210 String prefix = "proxyConnectors.proxyConnector(" + count + ")";
211 ( (MockConfiguration) config ).triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );