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;
24 import java.util.Enumeration;
26 import javax.servlet.ServletException;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
30 import org.apache.commons.io.FileUtils;
31 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
32 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
33 import org.apache.maven.archiva.configuration.NetworkProxyConfiguration;
34 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
35 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
36 import org.apache.maven.archiva.model.ArtifactReference;
37 import org.apache.maven.archiva.policies.CachedFailuresPolicy;
38 import org.apache.maven.archiva.policies.ChecksumPolicy;
39 import org.apache.maven.archiva.policies.PropagateErrorsDownloadPolicy;
40 import org.apache.maven.archiva.policies.PropagateErrorsOnUpdateDownloadPolicy;
41 import org.apache.maven.archiva.policies.ReleasesPolicy;
42 import org.apache.maven.archiva.policies.SnapshotsPolicy;
43 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
44 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
45 import org.mortbay.jetty.Handler;
46 import org.mortbay.jetty.Request;
47 import org.mortbay.jetty.Server;
48 import org.mortbay.jetty.handler.AbstractHandler;
51 * Integration test for connecting over a HTTP proxy.
53 * @author Brett Porter
54 * @version $Id: ManagedDefaultTransferTest.java 677852 2008-07-18 08:16:24Z brett $
56 public class HttpProxyTransferTest
57 extends PlexusInSpringTestCase
59 private static final String PROXY_ID = "proxy";
61 private static final String MANAGED_ID = "default-managed-repository";
63 private static final String PROXIED_ID = "proxied1";
65 private static final String PROXIED_BASEDIR = "src/test/repositories/proxied1";
67 private RepositoryProxyConnectors proxyHandler;
69 private ArchivaConfiguration config;
71 private ManagedRepositoryContent managedDefaultRepository;
73 private Server server;
76 protected void setUp()
81 // Setup source repository (using default layout)
82 String repoPath = "target/test-repository/managed/" + getName();
84 File destRepoDir = new File( repoPath );
86 // Cleanout destination dirs.
87 if ( destRepoDir.exists() )
89 FileUtils.deleteDirectory( destRepoDir );
92 // Make the destination dir.
95 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
96 repo.setId( MANAGED_ID );
97 repo.setName( "Default Managed Repository" );
98 repo.setLocation( repoPath );
99 repo.setLayout( "default" );
101 ManagedRepositoryContent repoContent =
102 (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
103 repoContent.setRepository( repo );
104 managedDefaultRepository = repoContent;
106 config = (MockConfiguration) lookup( ArchivaConfiguration.class.getName(), "mock" );
107 config.getConfiguration().addManagedRepository( repo );
109 Handler handler = new AbstractHandler()
111 public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch )
112 throws IOException, ServletException
114 response.setContentType( "text/plain" );
115 response.setStatus( HttpServletResponse.SC_OK );
116 response.getWriter().println( "get-default-layout-1.0.jar" );
117 response.getWriter().println();
118 assertNotNull( request.getHeader( "Proxy-Connection" ) );
120 ( (Request) request ).setHandled( true );
124 server = new Server( 0 );
125 server.setHandler( handler );
128 int port = server.getConnectors()[0].getLocalPort();
130 NetworkProxyConfiguration proxyConfig = new NetworkProxyConfiguration();
131 proxyConfig.setHost( "localhost" );
132 proxyConfig.setPort( port );
133 proxyConfig.setProtocol( "http" );
134 proxyConfig.setId( PROXY_ID );
135 config.getConfiguration().addNetworkProxy( proxyConfig );
137 // Setup target (proxied to) repository.
138 RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
140 repoConfig.setId( PROXIED_ID );
141 repoConfig.setName( "Proxied Repository 1" );
142 repoConfig.setLayout( "default" );
143 repoConfig.setUrl( "http://www.example.com/" );
145 config.getConfiguration().addRemoteRepository( repoConfig );
147 // Setup the proxy handler.
148 proxyHandler = (RepositoryProxyConnectors) lookup( RepositoryProxyConnectors.class.getName() );
152 protected void tearDown()
160 public void testGetOverHttpProxy()
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 );
188 private void addConnector()
190 ProxyConnectorConfiguration connectorConfig = new ProxyConnectorConfiguration();
191 connectorConfig.setProxyId( PROXY_ID );
192 connectorConfig.setSourceRepoId( MANAGED_ID );
193 connectorConfig.setTargetRepoId( PROXIED_ID );
194 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, ChecksumPolicy.FIX );
195 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, ReleasesPolicy.ONCE );
196 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, SnapshotsPolicy.ONCE );
197 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, CachedFailuresPolicy.NO );
198 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS,
199 PropagateErrorsDownloadPolicy.QUEUE );
200 connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS_ON_UPDATE,
201 PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT );
203 int count = config.getConfiguration().getProxyConnectors().size();
204 config.getConfiguration().addProxyConnector( connectorConfig );
206 // Proper Triggering ...
207 String prefix = "proxyConnectors.proxyConnector(" + count + ")";
208 ( (MockConfiguration) config ).triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );