org.springframework*;version="[3,4)",
org.apache.archiva.redback.components.taskqueue*,
org.apache.maven.index*,
- org.apache.http,
- org.apache.http.auth,
- org.apache.http.client,
- org.apache.http.client.methods,
- org.apache.http.concurrent,
- org.apache.http.entity,
- org.apache.http.impl.client,
- org.apache.http.impl.nio.client,
- org.apache.http.nio,
- org.apache.http.nio.client.methods,
- org.apache.http.nio.protocol,
- org.apache.http.protocol,
- org.apache.http.impl.nio.codecs,
+ org.apache.maven.wagon,
+ org.apache.maven.wagon.authentication,
+ org.apache.maven.wagon.authorization,
+ org.apache.maven.wagon.events,
+ org.apache.maven.wagon.providers.http,
+ org.apache.maven.wagon.proxy,
+ org.apache.maven.wagon.repository,
+ org.apache.maven.wagon.resource,
org.slf4j;resolution:=optional
</Import-Package>
</instructions>
import org.apache.archiva.admin.model.beans.RemoteRepository;
import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
import org.apache.archiva.proxy.common.WagonFactory;
+import org.apache.archiva.proxy.common.WagonFactoryException;
+import org.apache.archiva.proxy.common.WagonFactoryRequest;
import org.apache.commons.io.FileUtils;
-import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.StopWatch;
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpException;
-import org.apache.http.HttpHost;
-import org.apache.http.HttpRequest;
-import org.apache.http.HttpResponse;
-import org.apache.http.HttpStatus;
-import org.apache.http.auth.AuthScope;
-import org.apache.http.auth.UsernamePasswordCredentials;
-import org.apache.http.client.ClientProtocolException;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.entity.ContentType;
-import org.apache.http.impl.client.BasicCredentialsProvider;
-import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
-import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
-import org.apache.http.impl.nio.codecs.LengthDelimitedDecoder;
-import org.apache.http.nio.ContentDecoder;
-import org.apache.http.nio.ContentEncoder;
-import org.apache.http.nio.IOControl;
-import org.apache.http.nio.client.methods.ZeroCopyConsumer;
-import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
-import org.apache.http.protocol.HttpContext;
import org.apache.maven.index.context.IndexingContext;
import org.apache.maven.index.updater.IndexUpdateRequest;
import org.apache.maven.index.updater.IndexUpdater;
import org.apache.maven.index.updater.ResourceFetcher;
+import org.apache.maven.wagon.ConnectionException;
+import org.apache.maven.wagon.ResourceDoesNotExistException;
+import org.apache.maven.wagon.StreamWagon;
+import org.apache.maven.wagon.TransferFailedException;
+import org.apache.maven.wagon.Wagon;
+import org.apache.maven.wagon.authentication.AuthenticationException;
+import org.apache.maven.wagon.authentication.AuthenticationInfo;
+import org.apache.maven.wagon.authorization.AuthorizationException;
+import org.apache.maven.wagon.events.TransferEvent;
+import org.apache.maven.wagon.events.TransferListener;
+import org.apache.maven.wagon.providers.http.AbstractHttpClientWagon;
+import org.apache.maven.wagon.providers.http.HttpConfiguration;
+import org.apache.maven.wagon.providers.http.HttpMethodConfiguration;
+import org.apache.maven.wagon.proxy.ProxyInfo;
+import org.apache.maven.wagon.repository.Repository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
-import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.Future;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
/**
* @author Olivier Lamy
tempIndexDirectory.deleteOnExit();
String baseIndexUrl = indexingContext.getIndexUpdateUrl();
- URL indexUrl = new URL( baseIndexUrl );
-
- /*
String wagonProtocol = new URL( this.remoteRepository.getUrl() ).getProtocol();
final StreamWagon wagon = (StreamWagon) wagonFactory.getWagon(
}
wagon.connect( new Repository( this.remoteRepository.getId(), baseIndexUrl ), authenticationInfo,
proxyInfo );
- */
- //---------------------------------------------
-
- HttpAsyncClientBuilder builder = HttpAsyncClientBuilder.create();
-
- BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();
-
- if ( this.networkProxy != null )
- {
- HttpHost httpHost = new HttpHost( this.networkProxy.getHost(), this.networkProxy.getPort() );
- builder = builder.setProxy( httpHost );
-
- if ( this.networkProxy.getUsername() != null )
- {
- basicCredentialsProvider.setCredentials(
- new AuthScope( this.networkProxy.getHost(), this.networkProxy.getPort(), null, null ),
- new UsernamePasswordCredentials( this.networkProxy.getUsername(),
- this.networkProxy.getPassword() ) );
- }
-
- }
-
- if ( this.remoteRepository.getUserName() != null )
- {
- basicCredentialsProvider.setCredentials(
- new AuthScope( indexUrl.getHost(), indexUrl.getPort(), null, null ),
- new UsernamePasswordCredentials( this.remoteRepository.getUserName(),
- this.remoteRepository.getPassword() ) );
-
- }
-
- builder = builder.setDefaultCredentialsProvider( basicCredentialsProvider );
File indexDirectory = indexingContext.getIndexDirectoryFile();
if ( !indexDirectory.exists() )
indexDirectory.mkdirs();
}
- CloseableHttpAsyncClient closeableHttpAsyncClient = builder.build();
- closeableHttpAsyncClient.start();
ResourceFetcher resourceFetcher =
- new ZeroCopyResourceFetcher( log, tempIndexDirectory, remoteRepository, closeableHttpAsyncClient,
- baseIndexUrl );
-
+ new WagonResourceFetcher( log, tempIndexDirectory, wagon, remoteRepository );
IndexUpdateRequest request = new IndexUpdateRequest( indexingContext, resourceFetcher );
request.setForceFullUpdate( this.fullDownload );
request.setLocalIndexCacheDir( indexCacheDirectory );
this.indexUpdater.fetchAndUpdateIndex( request );
+ stopWatch.stop();
+ log.info( "time update index from remote for repository {}: {} s", this.remoteRepository.getId(),
+ ( stopWatch.getTime() / 1000 ) );
// index packing optionnal ??
//IndexPackingRequest indexPackingRequest =
// new IndexPackingRequest( indexingContext, indexingContext.getIndexDirectoryFile() );
//indexPacker.packIndex( indexPackingRequest );
-
indexingContext.updateTimestamp( true );
- stopWatch.stop();
- log.info( "time update index from remote for repository {}: {} s", this.remoteRepository.getId(),
- ( stopWatch.getTime() / 1000 ) );
-
-
}
catch ( MalformedURLException e )
{
log.error( e.getMessage(), e );
throw new RuntimeException( e.getMessage(), e );
}
+ catch ( WagonFactoryException e )
+ {
+ log.error( e.getMessage(), e );
+ throw new RuntimeException( e.getMessage(), e );
+ }
+ catch ( ConnectionException e )
+ {
+ log.error( e.getMessage(), e );
+ throw new RuntimeException( e.getMessage(), e );
+ }
+ catch ( AuthenticationException e )
+ {
+ log.error( e.getMessage(), e );
+ throw new RuntimeException( e.getMessage(), e );
+ }
catch ( IOException e )
{
log.error( e.getMessage(), e );
deleteDirectoryQuiet( tempIndexDirectory );
this.runningRemoteDownloadIds.remove( this.remoteRepository.getId() );
}
- log.info( "end download remote index for remote repository {}", this.remoteRepository.getId() );
+ log.info( "end download remote index for remote repository " + this.remoteRepository.getId() );
}
private void deleteDirectoryQuiet( File f )
}
}
- private static class ZeroCopyConsumerListener
- extends ZeroCopyConsumer
+
+ private static final class DownloadListener
+ implements TransferListener
{
private Logger log = LoggerFactory.getLogger( getClass() );
private long startTime;
- private long totalLength = 0;
-
- //private long currentLength = 0;
+ private int totalLength = 0;
- private ZeroCopyConsumerListener( File file, String resourceName )
- throws FileNotFoundException
+ public void transferInitiated( TransferEvent transferEvent )
{
- super( file );
- this.resourceName = resourceName;
+ startTime = System.currentTimeMillis();
+ resourceName = transferEvent.getResource().getName();
+ log.debug( "initiate transfer of {}", resourceName );
}
- @Override
- protected File process( final HttpResponse response, final File file, final ContentType contentType )
- throws Exception
+ public void transferStarted( TransferEvent transferEvent )
{
- if ( response.getStatusLine().getStatusCode() != HttpStatus.SC_OK )
- {
- throw new ClientProtocolException( "Download failed: " + response.getStatusLine() );
- }
- long endTime = System.currentTimeMillis();
- log.info( "end of transfer file {} {} kb: {}s", resourceName, this.totalLength / 1024,
- ( endTime - startTime ) / 1000 );
- return file;
+ this.totalLength = 0;
+ resourceName = transferEvent.getResource().getName();
+ log.info( "start transfer of {}", transferEvent.getResource().getName() );
}
- @Override
- protected void onContentReceived( ContentDecoder decoder, IOControl ioControl )
- throws IOException
+ public void transferProgress( TransferEvent transferEvent, byte[] buffer, int length )
{
- if ( decoder instanceof LengthDelimitedDecoder )
- {
- LengthDelimitedDecoder ldl = LengthDelimitedDecoder.class.cast( decoder );
- long len = getLen( ldl );
- if ( len > -1 )
- {
- log.debug( "transfer of {} : {}/{}", resourceName, len / 1024, this.totalLength / 1024 );
- }
- }
-
- super.onContentReceived( decoder, ioControl );
+ log.debug( "transfer of {} : {}/{}", transferEvent.getResource().getName(), buffer.length, length );
+ this.totalLength += length;
}
- @Override
- protected void onResponseReceived( HttpResponse response )
+ public void transferCompleted( TransferEvent transferEvent )
{
- this.startTime = System.currentTimeMillis();
- super.onResponseReceived( response );
- this.totalLength = response.getEntity().getContentLength();
- log.info( "start transfer of {}, contentLength: {}", resourceName, this.totalLength / 1024 );
+ resourceName = transferEvent.getResource().getName();
+ long endTime = System.currentTimeMillis();
+ log.info( "end of transfer file {} {} kb: {}s", transferEvent.getResource().getName(),
+ this.totalLength / 1024, ( endTime - startTime ) / 1000 );
}
- @Override
- protected void onEntityEnclosed( HttpEntity entity, ContentType contentType )
- throws IOException
+ public void transferError( TransferEvent transferEvent )
{
- super.onEntityEnclosed( entity, contentType );
+ log.info( "error of transfer file {}: {}", transferEvent.getResource().getName(),
+ transferEvent.getException().getMessage(), transferEvent.getException() );
}
- private long getLen( LengthDelimitedDecoder ldl )
+ public void debug( String message )
{
- try
- {
- Field lenField = LengthDelimitedDecoder.class.getDeclaredField( "len" );
- lenField.setAccessible( true );
- long len = (Long) lenField.get( ldl );
- return len;
- }
- catch ( NoSuchFieldException e )
- {
- log.debug( e.getMessage(), e );
- return -1;
- }
- catch ( IllegalAccessException e )
- {
- log.debug( e.getMessage(), e );
- return -1;
- }
+ log.debug( "transfer debug {}", message );
}
}
- private static class ZeroCopyResourceFetcher
+ private static class WagonResourceFetcher
implements ResourceFetcher
{
File tempIndexDirectory;
- final RemoteRepository remoteRepository;
+ Wagon wagon;
- CloseableHttpAsyncClient httpclient;
+ RemoteRepository remoteRepository;
- String baseIndexUrl;
-
- private ZeroCopyResourceFetcher( Logger log, File tempIndexDirectory, RemoteRepository remoteRepository,
- CloseableHttpAsyncClient httpclient, String baseIndexUrl )
+ private WagonResourceFetcher( Logger log, File tempIndexDirectory, Wagon wagon,
+ RemoteRepository remoteRepository )
{
this.log = log;
this.tempIndexDirectory = tempIndexDirectory;
+ this.wagon = wagon;
this.remoteRepository = remoteRepository;
- this.httpclient = httpclient;
- this.baseIndexUrl = baseIndexUrl;
}
public void connect( String id, String url )
throws IOException
{
- //no op
+ //no op
}
public void disconnect()
// no op
}
- public InputStream retrieve( final String name )
- throws IOException
+ public InputStream retrieve( String name )
+ throws IOException, FileNotFoundException
{
-
- log.info( "index update retrieve file, name:{}", name );
- File file = new File( tempIndexDirectory, name );
- if ( file.exists() )
- {
- file.delete();
- }
- file.deleteOnExit();
-
- ZeroCopyConsumer<File> consumer = new ZeroCopyConsumerListener( file, name );
-
- URL targetUrl = new URL( this.baseIndexUrl );
- final HttpHost targetHost = new HttpHost( targetUrl.getHost(), targetUrl.getPort() );
-
- Future<File> httpResponseFuture = httpclient.execute( new HttpAsyncRequestProducer()
+ try
{
- @Override
- public HttpHost getTarget()
- {
- return targetHost;
- }
-
- @Override
- public HttpRequest generateRequest()
- throws IOException, HttpException
- {
- StringBuilder url = new StringBuilder( baseIndexUrl );
- if ( !StringUtils.endsWith( baseIndexUrl, "/" ) )
- {
- url.append( '/' );
- }
- HttpGet httpGet = new HttpGet( url.append( addParameters( name, remoteRepository ) ).toString() );
- return httpGet;
- }
-
- @Override
- public void produceContent( ContentEncoder encoder, IOControl ioctrl )
- throws IOException
- {
- // no op
- }
-
- @Override
- public void requestCompleted( HttpContext context )
- {
- log.debug( "requestCompleted" );
- }
-
- @Override
- public void failed( Exception ex )
+ log.info( "index update retrieve file, name:{}", name );
+ File file = new File( tempIndexDirectory, name );
+ if ( file.exists() )
{
- log.error( "http request failed", ex );
+ file.delete();
}
-
- @Override
- public boolean isRepeatable()
- {
- log.debug( "isRepeatable" );
- return true;
- }
-
- @Override
- public void resetRequest()
- throws IOException
- {
- log.debug( "resetRequest" );
- }
-
- @Override
- public void close()
- throws IOException
- {
- log.debug( "close" );
- }
-
- }, consumer, null );
- try
- {
- int timeOut = this.remoteRepository.getRemoteDownloadTimeout();
- file = timeOut > 0 ? httpResponseFuture.get( timeOut, TimeUnit.SECONDS ) : httpResponseFuture.get();
+ file.deleteOnExit();
+ wagon.get( addParameters( name, this.remoteRepository ), file );
+ return new FileInputStream( file );
}
- catch ( InterruptedException e )
+ catch ( AuthorizationException e )
{
throw new IOException( e.getMessage(), e );
}
- catch ( ExecutionException e )
+ catch ( TransferFailedException e )
{
throw new IOException( e.getMessage(), e );
}
- catch ( TimeoutException e )
+ catch ( ResourceDoesNotExistException e )
{
- throw new IOException( e.getMessage(), e );
+ FileNotFoundException fnfe = new FileNotFoundException( e.getMessage() );
+ fnfe.initCause( e );
+ throw fnfe;
}
- return new FileInputStream( file );
}
- }
-
- // FIXME remove crappy copy/paste
- protected static String addParameters( String path, RemoteRepository remoteRepository )
- {
- if ( remoteRepository.getExtraParameters().isEmpty() )
+ // FIXME remove crappy copy/paste
+ protected String addParameters( String path, RemoteRepository remoteRepository )
{
- return path;
- }
+ if ( remoteRepository.getExtraParameters().isEmpty() )
+ {
+ return path;
+ }
- boolean question = false;
+ boolean question = false;
- StringBuilder res = new StringBuilder( path == null ? "" : path );
+ StringBuilder res = new StringBuilder( path == null ? "" : path );
- for ( Map.Entry<String, String> entry : remoteRepository.getExtraParameters().entrySet() )
- {
- if ( !question )
+ for ( Map.Entry<String, String> entry : remoteRepository.getExtraParameters().entrySet() )
{
- res.append( '?' ).append( entry.getKey() ).append( '=' ).append( entry.getValue() );
+ if ( !question )
+ {
+ res.append( '?' ).append( entry.getKey() ).append( '=' ).append( entry.getValue() );
+ }
}
+
+ return res.toString();
}
- return res.toString();
}
}
-
+++ /dev/null
-package org.apache.archiva;
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
-import junit.framework.TestCase;
-import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
-import org.apache.archiva.rest.api.services.ProxyConnectorService;
-import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
-import org.apache.archiva.rest.api.services.RepositoriesService;
-import org.apache.archiva.rest.api.services.RepositoryGroupService;
-import org.apache.archiva.rest.api.services.SearchService;
-import org.apache.archiva.webdav.RepositoryServlet;
-import org.apache.commons.lang.StringUtils;
-import org.apache.cxf.common.util.Base64Utility;
-import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
-import org.apache.cxf.jaxrs.client.WebClient;
-import org.apache.cxf.transport.servlet.CXFServlet;
-import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants;
-import org.apache.archiva.redback.rest.api.model.User;
-import org.apache.archiva.redback.rest.api.services.RoleManagementService;
-import org.apache.archiva.redback.rest.api.services.UserService;
-import org.apache.archiva.redback.rest.services.FakeCreateAdminService;
-import org.eclipse.jetty.server.Connector;
-import org.eclipse.jetty.server.Server;
-import org.eclipse.jetty.server.session.SessionHandler;
-import org.eclipse.jetty.servlet.ServletContextHandler;
-import org.eclipse.jetty.servlet.ServletHolder;
-import org.junit.After;
-import org.junit.Before;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.web.context.ContextLoaderListener;
-
-import java.util.Collections;
-import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
-import org.junit.runner.RunWith;
-
-/**
- * @author Olivier Lamy
- */
-@RunWith( ArchivaBlockJUnit4ClassRunner.class )
-public abstract class AbstractDownloadTest
- extends TestCase
-{
-
- protected Logger log = LoggerFactory.getLogger( getClass() );
-
- static String previousAppServerBase;
-
- public String authorizationHeader = getAdminAuthzHeader();
-
- public Server server = null;
-
- public int port;
-
- public static String encode( String uid, String password )
- {
- return "Basic " + Base64Utility.encode( ( uid + ":" + password ).getBytes() );
- }
-
- public static String getAdminAuthzHeader()
- {
- String adminPwdSysProps = System.getProperty( "rest.admin.pwd" );
- if ( StringUtils.isBlank( adminPwdSysProps ) )
- {
- return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, FakeCreateAdminService.ADMIN_TEST_PWD );
- }
- return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, adminPwdSysProps );
- }
-
-
- protected abstract String getSpringConfigLocation();
-
-
- protected String getRestServicesPath()
- {
- return "restServices";
- }
-
-
- @Before
- public void startServer()
- throws Exception
- {
-
- System.setProperty( "redback.admin.creation.file", "target/auto-admin-creation.properties" );
- this.server = new Server( 0 );
-
- ServletContextHandler context = new ServletContextHandler();
-
- context.setContextPath( "/" );
-
- context.setInitParameter( "contextConfigLocation", getSpringConfigLocation() );
-
- ContextLoaderListener contextLoaderListener = new ContextLoaderListener();
-
- context.addEventListener( contextLoaderListener );
-
- ServletHolder sh = new ServletHolder( CXFServlet.class );
-
- SessionHandler sessionHandler = new SessionHandler();
-
- context.setSessionHandler( sessionHandler );
-
- context.addServlet( sh, "/" + getRestServicesPath() + "/*" );
-
- ServletHolder repoSh = new ServletHolder( RepositoryServlet.class );
- context.addServlet( repoSh, "/repository/*" );
-
- server.setHandler( context );
- this.server.start();
- Connector connector = this.server.getConnectors()[0];
- this.port = connector.getLocalPort();
- log.info( "start server on port " + this.port );
-
- User user = new User();
- user.setEmail( "toto@toto.fr" );
- user.setFullName( "the root user" );
- user.setUsername( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
- user.setPassword( FakeCreateAdminService.ADMIN_TEST_PWD );
-
- getUserService( null ).createAdminUser( user );
-
-
- }
-
-
- @After
- public void tearDown()
- throws Exception
- {
- System.clearProperty( "redback.admin.creation.file" );
- super.tearDown();
- if ( this.server != null )
- {
- this.server.stop();
- }
- }
-
-
- protected ProxyConnectorService getProxyConnectorService()
- {
- ProxyConnectorService service =
- JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
- ProxyConnectorService.class,
- Collections.singletonList( new JacksonJaxbJsonProvider() ) );
-
- WebClient.client( service ).header( "Authorization", authorizationHeader );
- WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
- return service;
- }
-
- protected RemoteRepositoriesService getRemoteRepositoriesService()
- {
- RemoteRepositoriesService service =
- JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
- RemoteRepositoriesService.class,
- Collections.singletonList( new JacksonJaxbJsonProvider() ) );
-
- WebClient.client( service ).header( "Authorization", authorizationHeader );
- WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
- return service;
- }
-
- protected ManagedRepositoriesService getManagedRepositoriesService()
- {
- ManagedRepositoriesService service =
- JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
- ManagedRepositoriesService.class,
- Collections.singletonList( new JacksonJaxbJsonProvider() ) );
-
- WebClient.client( service ).header( "Authorization", authorizationHeader );
- WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
- return service;
- }
-
-
- protected RepositoryGroupService getRepositoryGroupService()
- {
- RepositoryGroupService service =
- JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
- RepositoryGroupService.class,
- Collections.singletonList( new JacksonJaxbJsonProvider() ) );
-
- WebClient.client( service ).header( "Authorization", authorizationHeader );
- WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
- return service;
- }
-
- protected RepositoriesService getRepositoriesService()
- {
- RepositoriesService service =
- JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
- RepositoriesService.class,
- Collections.singletonList( new JacksonJaxbJsonProvider() ) );
-
- WebClient.client( service ).header( "Authorization", authorizationHeader );
- WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
- return service;
- }
-
- protected SearchService getSearchService()
- {
- SearchService service =
- JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
- SearchService.class,
- Collections.singletonList( new JacksonJaxbJsonProvider() ) );
-
- WebClient.client( service ).header( "Authorization", authorizationHeader );
- WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
- return service;
- }
-
- protected String getBaseUrl()
- {
- String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
- return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
- }
-
-
- protected RoleManagementService getRoleManagementService( String authzHeader )
- {
- RoleManagementService service =
- JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
- RoleManagementService.class,
- Collections.singletonList( new JacksonJaxbJsonProvider() ) );
-
- // for debuging purpose
- WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
-
- if ( authzHeader != null )
- {
- WebClient.client( service ).header( "Authorization", authzHeader );
- }
- return service;
- }
-
- protected UserService getUserService( String authzHeader )
- {
- UserService service =
- JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
- UserService.class, Collections.singletonList( new JacksonJaxbJsonProvider() ) );
-
- // for debuging purpose
- WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
-
- if ( authzHeader != null )
- {
- WebClient.client( service ).header( "Authorization", authzHeader );
- }
- return service;
- }
-
- protected FakeCreateAdminService getFakeCreateAdminService()
- {
- return JAXRSClientFactory.create(
- "http://localhost:" + port + "/" + getRestServicesPath() + "/fakeCreateAdminService/",
- FakeCreateAdminService.class );
- }
-
-}
+++ /dev/null
-package org.apache.archiva;
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import org.apache.archiva.admin.model.beans.RemoteRepository;
-import org.apache.archiva.redback.rest.api.services.RoleManagementService;
-import org.apache.archiva.security.common.ArchivaRoleConstants;
-import org.apache.commons.compress.utils.IOUtils;
-import org.apache.commons.io.FileUtils;
-import org.apache.maven.wagon.providers.http.HttpWagon;
-import org.apache.maven.wagon.repository.Repository;
-import org.eclipse.jetty.server.Server;
-import org.eclipse.jetty.servlet.ServletContextHandler;
-import org.eclipse.jetty.servlet.ServletHolder;
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServlet;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Enumeration;
-import java.util.List;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipFile;
-import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
-
-/**
- * @author Olivier Lamy
- */
-@RunWith( ArchivaBlockJUnit4ClassRunner.class )
-public class DownloadArtifactsTest
- extends AbstractDownloadTest
-{
-
- protected Logger log = LoggerFactory.getLogger( DownloadArtifactsTest.class );
-
- public Server redirectServer = null;
-
- public int redirectPort;
-
- public Server repoServer = null;
-
- public int repoServerPort;
-
- @BeforeClass
- public static void setAppServerBase()
- {
- previousAppServerBase = System.getProperty( "appserver.base" );
- System.setProperty( "appserver.base", "target/" + DownloadArtifactsTest.class.getName() );
- }
-
-
- @AfterClass
- public static void resetAppServerBase()
- {
- System.setProperty( "appserver.base", previousAppServerBase );
- }
-
- protected String getSpringConfigLocation()
- {
- return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-artifacts-download.xml";
- }
-
- @Before
- public void startServer()
- throws Exception
- {
- super.startServer();
-
- // repo handler
-
- this.repoServer = new Server( 0 );
-
- ServletHolder shRepo = new ServletHolder( RepoServlet.class );
- ServletContextHandler contextRepo = new ServletContextHandler();
-
- contextRepo.setContextPath( "/" );
- contextRepo.addServlet( shRepo, "/*" );
-
- repoServer.setHandler( contextRepo );
- repoServer.start();
- this.repoServerPort = repoServer.getConnectors()[0].getLocalPort();
-
- //redirect handler
-
- this.redirectServer = new Server( 0 );
- ServletHolder shRedirect = new ServletHolder( RedirectServlet.class );
- ServletContextHandler contextRedirect = new ServletContextHandler();
- contextRedirect.setAttribute( "redirectToPort", Integer.toString( this.repoServerPort ) );
-
- contextRedirect.setContextPath( "/" );
- contextRedirect.addServlet( shRedirect, "/*" );
-
- redirectServer.setHandler( contextRedirect );
- redirectServer.start();
- this.redirectPort = redirectServer.getConnectors()[0].getLocalPort();
- log.info( "redirect server port {}", redirectPort );
-
-
-
- }
-
- @After
- public void tearDown()
- throws Exception
- {
- super.tearDown();
- if ( this.redirectServer != null )
- {
- this.redirectServer.stop();
- }
- }
-
- @Test
- public void downloadWithRemoteRedirect()
- throws Exception
- {
- RemoteRepository remoteRepository = getRemoteRepositoriesService().getRemoteRepository( "central" );
- remoteRepository.setUrl( "http://localhost:" + redirectPort );
- getRemoteRepositoriesService().updateRemoteRepository( remoteRepository );
-
- RoleManagementService roleManagementService = getRoleManagementService( authorizationHeader );
-
- if ( !roleManagementService.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER,
- "internal" ) )
- {
- roleManagementService.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, "internal" );
- }
-
- getUserService( authorizationHeader ).createGuestUser();
- roleManagementService.assignRole( ArchivaRoleConstants.TEMPLATE_GUEST, "guest" );
-
- roleManagementService.assignTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, "internal",
- "guest" );
-
- getUserService( authorizationHeader ).removeFromCache( "guest" );
-
- File file = new File( "target/junit-4.9.jar" );
- if ( file.exists() )
- {
- file.delete();
- }
-
- HttpWagon httpWagon = new HttpWagon();
- httpWagon.connect( new Repository( "foo", "http://localhost:" + port ) );
-
- httpWagon.get( "/repository/internal/junit/junit/4.9/junit-4.9.jar", file );
-
- ZipFile zipFile = new ZipFile( file );
- List<String> entries = getZipEntriesNames( zipFile );
- ZipEntry zipEntry = zipFile.getEntry( "org/junit/runners/JUnit4.class" );
- assertNotNull( "cannot find zipEntry org/junit/runners/JUnit4.class, entries: " + entries + ", content is: "
- + FileUtils.readFileToString( file ), zipEntry );
- zipFile.close();
- file.deleteOnExit();
- }
-
- private List<String> getZipEntriesNames( ZipFile zipFile )
- {
- try
- {
- List<String> entriesNames = new ArrayList<String>();
- Enumeration<? extends ZipEntry> entries = zipFile.entries();
- while ( entries.hasMoreElements() )
- {
- entriesNames.add( entries.nextElement().getName() );
- }
- return entriesNames;
- }
- catch ( Throwable e )
- {
- log.info( "fail to get zipEntries " + e.getMessage(), e );
- }
- return Collections.emptyList();
- }
-
-
- public static class RedirectServlet
- extends HttpServlet
- {
- @Override
- protected void doGet( HttpServletRequest req, HttpServletResponse resp )
- throws ServletException, IOException
- {
-
- LoggerFactory.getLogger( getClass() ).info( "redirect servlet receive: {}", req.getRequestURI() );
- resp.setStatus( 302 );
- resp.getWriter().write( "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" + "<html><head>\n"
- + "<title>302 Found</title>\n" + "</head><body>\n" + "<h1>Found</h1>\n"
- + "<p>The document has moved <a href=\"http://repo.maven.apache.org/maven2/junit/junit/4.9/junit-4.9.jar\">here</a>.</p>\n"
- + "</body></html>\n" + "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
- + "<html><head>\n" );
- resp.sendRedirect( "http://localhost:" + getServletContext().getAttribute( "redirectToPort" ) + "/maven2/"
- + req.getRequestURI() );
- }
- }
-
- public static class RepoServlet
- extends HttpServlet
- {
- @Override
- protected void doGet( HttpServletRequest req, HttpServletResponse resp )
- throws ServletException, IOException
- {
- File jar = new File( System.getProperty( "basedir" ), "src/test/junit-4.9.jar" );
- IOUtils.copy( new FileInputStream( jar ), resp.getOutputStream() );
-
- }
- }
-
-
-}
+++ /dev/null
-package org.apache.archiva;
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import org.apache.archiva.admin.model.beans.ManagedRepository;
-import org.apache.archiva.admin.model.beans.ProxyConnector;
-import org.apache.archiva.admin.model.beans.RemoteRepository;
-import org.apache.archiva.admin.model.beans.RepositoryGroup;
-import org.apache.archiva.maven2.model.Artifact;
-import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants;
-import org.apache.archiva.redback.rest.services.FakeCreateAdminService;
-import org.apache.archiva.rest.api.model.SearchRequest;
-import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
-import org.apache.archiva.rest.api.services.ProxyConnectorService;
-import org.apache.archiva.rest.api.services.RepositoriesService;
-import org.apache.archiva.rest.api.services.RepositoryGroupService;
-import org.apache.archiva.rest.api.services.SearchService;
-import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
-import org.apache.commons.io.FileUtils;
-import org.fest.assertions.api.Assertions;
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-import java.io.File;
-import java.util.Arrays;
-import java.util.List;
-
-/**
- * @author Olivier Lamy
- */
-@RunWith( ArchivaBlockJUnit4ClassRunner.class )
-public class DownloadMergedIndexNonDefaultPathTest
- extends AbstractDownloadTest
-{
-
- @BeforeClass
- public static void setAppServerBase()
- {
- previousAppServerBase = System.getProperty( "appserver.base" );
- System.setProperty( "appserver.base", System.getProperty( "basedir" ) + "/target/" + DownloadMergedIndexNonDefaultPathTest.class.getName() );
- }
-
- @AfterClass
- public static void resetAppServerBase()
- {
- System.setProperty( "appserver.base", previousAppServerBase );
- }
-
- protected String getSpringConfigLocation()
- {
- return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-merge-index-download.xml";
- }
-
- @After
- public void cleanup()
- throws Exception
- {
- super.tearDown();
- File tmpIndexDir = new File( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex" );
- if ( tmpIndexDir.exists() )
- {
- FileUtils.deleteDirectory( tmpIndexDir );
- }
- }
-
-
- @Test
- public void downloadMergedIndexWithNonDefaultPath()
- throws Exception
- {
- File tmpIndexDir = new File( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex" );
- if ( tmpIndexDir.exists() )
- {
- FileUtils.deleteDirectory( tmpIndexDir );
- }
- String id = Long.toString( System.currentTimeMillis() );
- ManagedRepository managedRepository = new ManagedRepository();
- managedRepository.setId( id );
- managedRepository.setName( "name of " + id );
- managedRepository.setLocation( System.getProperty( "basedir" ) + "/src/test/repositories/test-repo" );
- managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex/" + id );
-
- ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
-
- if ( managedRepositoriesService.getManagedRepository( id ) != null )
- {
- managedRepositoriesService.deleteManagedRepository( id, false );
- }
-
- getManagedRepositoriesService().addManagedRepository( managedRepository );
-
- RepositoriesService repositoriesService = getRepositoriesService();
-
- repositoriesService.scanRepositoryNow( id, true );
-
- // wait a bit to ensure index is finished
- int timeout = 20000;
- while ( timeout > 0 && repositoriesService.alreadyScanning( id ) )
- {
- Thread.sleep( 500 );
- timeout -= 500;
- }
-
- RepositoryGroupService repositoryGroupService = getRepositoryGroupService();
-
- String repoGroupId = "test-group";
-
- if ( repositoryGroupService.getRepositoryGroup( repoGroupId ) != null )
- {
- repositoryGroupService.deleteRepositoryGroup( repoGroupId );
- }
-
- RepositoryGroup repositoryGroup = new RepositoryGroup();
- repositoryGroup.setId( repoGroupId );
- String path = ".fooooo";
- repositoryGroup.setRepositories( Arrays.asList( id ) );
- repositoryGroup.setMergedIndexPath( path );
-
- repositoryGroupService.addRepositoryGroup( repositoryGroup );
-
- // create a repo with a remote on the one with index
- id = Long.toString( System.currentTimeMillis() );
- managedRepository = new ManagedRepository();
- managedRepository.setId( id );
- managedRepository.setName( "name of " + id );
- managedRepository.setLocation( System.getProperty( "basedir" ) + "/src/test/repositories/test-repo" );
- managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex/" + id );
-
- if ( managedRepositoriesService.getManagedRepository( id ) != null )
- {
- managedRepositoriesService.deleteManagedRepository( id, false );
- }
-
- getManagedRepositoriesService().addManagedRepository( managedRepository );
-
- String remoteId = Long.toString( System.currentTimeMillis() );
-
- RemoteRepository remoteRepository = new RemoteRepository();
- remoteRepository.setId( remoteId );
- remoteRepository.setName( remoteId );
- remoteRepository.setDownloadRemoteIndex( true );
- remoteRepository.setUrl( "http://localhost:" + port + "/repository/test-group" );
- remoteRepository.setRemoteIndexUrl( "http://localhost:" + port + "/repository/test-group/" + path );
- remoteRepository.setUserName( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
- remoteRepository.setPassword( FakeCreateAdminService.ADMIN_TEST_PWD );
-
- getRemoteRepositoriesService().addRemoteRepository( remoteRepository );
-
- ProxyConnectorService proxyConnectorService = getProxyConnectorService();
- ProxyConnector proxyConnector = new ProxyConnector();
- proxyConnector.setProxyId( "foo-bar2" );
- proxyConnector.setSourceRepoId( id );
- proxyConnector.setTargetRepoId( remoteId );
- proxyConnectorService.addProxyConnector( proxyConnector );
-
- repositoriesService.scheduleDownloadRemoteIndex( remoteId, true, true );
-
- // wait a bit
- /*
- timeout = 20000;
- while ( timeout > 0 )
- {
- Thread.sleep( 500 );
- timeout -= 500;
- }*/
- // wait the end
- while ( !repositoriesService.getRunningRemoteDownloadIds().getStrings().isEmpty() )
- {
- Thread.sleep( 500 );
- }
-
- SearchService searchService = getSearchService();
-
- SearchRequest request = new SearchRequest();
- request.setRepositories( Arrays.asList( id ) );
- request.setGroupId( "org.apache.felix" );
-
- List<Artifact> artifacts = searchService.searchArtifacts( request );
- Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 );
-
- }
-}
+++ /dev/null
-package org.apache.archiva;
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import org.apache.archiva.admin.model.beans.ManagedRepository;
-import org.apache.archiva.admin.model.beans.ProxyConnector;
-import org.apache.archiva.admin.model.beans.RemoteRepository;
-import org.apache.archiva.admin.model.beans.RepositoryGroup;
-import org.apache.archiva.maven2.model.Artifact;
-import org.apache.archiva.rest.api.model.SearchRequest;
-import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
-import org.apache.archiva.rest.api.services.ProxyConnectorService;
-import org.apache.archiva.rest.api.services.RepositoriesService;
-import org.apache.archiva.rest.api.services.RepositoryGroupService;
-import org.apache.archiva.rest.api.services.SearchService;
-import org.apache.commons.io.FileUtils;
-import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants;
-import org.apache.archiva.redback.rest.services.FakeCreateAdminService;
-import org.fest.assertions.api.Assertions;
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.Arrays;
-import java.util.List;
-
-import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
-
-/**
- * @author Olivier Lamy
- */
-@RunWith(ArchivaBlockJUnit4ClassRunner.class)
-public class DownloadMergedIndexTest
- extends AbstractDownloadTest
-{
-
- @BeforeClass
- public static void setAppServerBase()
- throws IOException
- {
- previousAppServerBase = System.getProperty( "appserver.base" );
- System.setProperty( "appserver.base",
- new File( System.getProperty( "java.io.tmpdir" ) ).getCanonicalPath() + "/target/"
- + DownloadMergedIndexTest.class.getName() );
- }
-
- @AfterClass
- public static void resetAppServerBase()
- {
- System.setProperty( "appserver.base", previousAppServerBase );
- }
-
- protected String getSpringConfigLocation()
- {
- return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-merge-index-download.xml";
- }
-
- @After
- public void cleanup()
- throws Exception
- {
- super.tearDown();
- File tmpIndexDir = new File( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex" );
- if ( tmpIndexDir.exists() )
- {
- FileUtils.deleteDirectory( tmpIndexDir );
- }
- }
-
-
- @Test
- public void downloadMergedIndex()
- throws Exception
- {
- File tmpIndexDir = new File( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex" );
- if ( tmpIndexDir.exists() )
- {
- FileUtils.deleteDirectory( tmpIndexDir );
- }
- String id = Long.toString( System.currentTimeMillis() );
- ManagedRepository managedRepository = new ManagedRepository();
- managedRepository.setId( id );
- managedRepository.setName( "name of " + id );
- managedRepository.setLocation( System.getProperty( "basedir" ) + "/src/test/repositories/test-repo" );
- managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex/" + id );
-
- ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
-
- if ( managedRepositoriesService.getManagedRepository( id ) != null )
- {
- managedRepositoriesService.deleteManagedRepository( id, false );
- }
-
- getManagedRepositoriesService().addManagedRepository( managedRepository );
-
- RepositoriesService repositoriesService = getRepositoriesService();
-
- repositoriesService.scanRepositoryNow( id, true );
-
- // wait a bit to ensure index is finished
- int timeout = 20000;
- while ( timeout > 0 && repositoriesService.alreadyScanning( id ) )
- {
- Thread.sleep( 500 );
- timeout -= 500;
- }
-
- RepositoryGroupService repositoryGroupService = getRepositoryGroupService();
-
- String repoGroupId = "test-group";
-
- if ( repositoryGroupService.getRepositoryGroup( repoGroupId ) != null )
- {
- repositoryGroupService.deleteRepositoryGroup( repoGroupId );
- }
-
- RepositoryGroup repositoryGroup = new RepositoryGroup();
- repositoryGroup.setId( repoGroupId );
- repositoryGroup.setRepositories( Arrays.asList( id ) );
-
- repositoryGroupService.addRepositoryGroup( repositoryGroup );
-
- // create a repo with a remote on the one with index
- id = Long.toString( System.currentTimeMillis() );
- managedRepository = new ManagedRepository();
- managedRepository.setId( id );
- managedRepository.setName( "name of " + id );
- managedRepository.setLocation( System.getProperty( "basedir" ) + "/src/test/repositories/test-repo" );
- managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex/" + id );
-
- if ( managedRepositoriesService.getManagedRepository( id ) != null )
- {
- managedRepositoriesService.deleteManagedRepository( id, false );
- }
-
- getManagedRepositoriesService().addManagedRepository( managedRepository );
-
- RemoteRepository remoteRepository = new RemoteRepository();
- remoteRepository.setId( "all-merged" );
- remoteRepository.setName( "all-merged" );
- remoteRepository.setDownloadRemoteIndex( true );
- remoteRepository.setUrl( "http://localhost:" + port + "/repository/test-group" );
- remoteRepository.setRemoteIndexUrl( "http://localhost:" + port + "/repository/test-group/.indexer" );
- remoteRepository.setUserName( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
- remoteRepository.setPassword( FakeCreateAdminService.ADMIN_TEST_PWD );
-
- getRemoteRepositoriesService().addRemoteRepository( remoteRepository );
-
- ProxyConnectorService proxyConnectorService = getProxyConnectorService();
- ProxyConnector proxyConnector = new ProxyConnector();
- proxyConnector.setProxyId( "foo-bar1" );
- proxyConnector.setSourceRepoId( id );
- proxyConnector.setTargetRepoId( "all-merged" );
- proxyConnectorService.addProxyConnector( proxyConnector );
-
- repositoriesService.scheduleDownloadRemoteIndex( "all-merged", true, true );
-
- // wait a bit
- timeout = 20000;
- while ( timeout > 0 )
- {
- Thread.sleep( 500 );
- timeout -= 500;
- }
-
- SearchService searchService = getSearchService();
-
- SearchRequest request = new SearchRequest();
- request.setRepositories( Arrays.asList( id ) );
- request.setGroupId( "org.apache.felix" );
-
- List<Artifact> artifacts = searchService.searchArtifacts( request );
- Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 );
-
- }
-}
--- /dev/null
+package org.apache.archiva.remotedownload;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
+import junit.framework.TestCase;
+import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
+import org.apache.archiva.rest.api.services.ProxyConnectorService;
+import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
+import org.apache.archiva.rest.api.services.RepositoriesService;
+import org.apache.archiva.rest.api.services.RepositoryGroupService;
+import org.apache.archiva.rest.api.services.SearchService;
+import org.apache.archiva.webdav.RepositoryServlet;
+import org.apache.commons.lang.StringUtils;
+import org.apache.cxf.common.util.Base64Utility;
+import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
+import org.apache.cxf.jaxrs.client.WebClient;
+import org.apache.cxf.transport.servlet.CXFServlet;
+import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants;
+import org.apache.archiva.redback.rest.api.model.User;
+import org.apache.archiva.redback.rest.api.services.RoleManagementService;
+import org.apache.archiva.redback.rest.api.services.UserService;
+import org.apache.archiva.redback.rest.services.FakeCreateAdminService;
+import org.eclipse.jetty.server.Connector;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.session.SessionHandler;
+import org.eclipse.jetty.servlet.ServletContextHandler;
+import org.eclipse.jetty.servlet.ServletHolder;
+import org.junit.After;
+import org.junit.Before;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.web.context.ContextLoaderListener;
+
+import java.util.Collections;
+import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
+import org.junit.runner.RunWith;
+
+/**
+ * @author Olivier Lamy
+ */
+@RunWith( ArchivaBlockJUnit4ClassRunner.class )
+public abstract class AbstractDownloadTest
+ extends TestCase
+{
+
+ protected Logger log = LoggerFactory.getLogger( getClass() );
+
+ static String previousAppServerBase;
+
+ public String authorizationHeader = getAdminAuthzHeader();
+
+ public Server server = null;
+
+ public int port;
+
+ public static String encode( String uid, String password )
+ {
+ return "Basic " + Base64Utility.encode( ( uid + ":" + password ).getBytes() );
+ }
+
+ public static String getAdminAuthzHeader()
+ {
+ String adminPwdSysProps = System.getProperty( "rest.admin.pwd" );
+ if ( StringUtils.isBlank( adminPwdSysProps ) )
+ {
+ return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, FakeCreateAdminService.ADMIN_TEST_PWD );
+ }
+ return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, adminPwdSysProps );
+ }
+
+
+ protected abstract String getSpringConfigLocation();
+
+
+ protected String getRestServicesPath()
+ {
+ return "restServices";
+ }
+
+
+ @Before
+ public void startServer()
+ throws Exception
+ {
+
+ System.setProperty( "redback.admin.creation.file", "target/auto-admin-creation.properties" );
+ this.server = new Server( 0 );
+
+ ServletContextHandler context = new ServletContextHandler();
+
+ context.setContextPath( "/" );
+
+ context.setInitParameter( "contextConfigLocation", getSpringConfigLocation() );
+
+ ContextLoaderListener contextLoaderListener = new ContextLoaderListener();
+
+ context.addEventListener( contextLoaderListener );
+
+ ServletHolder sh = new ServletHolder( CXFServlet.class );
+
+ SessionHandler sessionHandler = new SessionHandler();
+
+ context.setSessionHandler( sessionHandler );
+
+ context.addServlet( sh, "/" + getRestServicesPath() + "/*" );
+
+ ServletHolder repoSh = new ServletHolder( RepositoryServlet.class );
+ context.addServlet( repoSh, "/repository/*" );
+
+ server.setHandler( context );
+ this.server.start();
+ Connector connector = this.server.getConnectors()[0];
+ this.port = connector.getLocalPort();
+ log.info( "start server on port " + this.port );
+
+ User user = new User();
+ user.setEmail( "toto@toto.fr" );
+ user.setFullName( "the root user" );
+ user.setUsername( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
+ user.setPassword( FakeCreateAdminService.ADMIN_TEST_PWD );
+
+ getUserService( null ).createAdminUser( user );
+
+
+ }
+
+
+ @After
+ public void tearDown()
+ throws Exception
+ {
+ System.clearProperty( "redback.admin.creation.file" );
+ super.tearDown();
+ if ( this.server != null )
+ {
+ this.server.stop();
+ }
+ }
+
+
+ protected ProxyConnectorService getProxyConnectorService()
+ {
+ ProxyConnectorService service =
+ JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
+ ProxyConnectorService.class,
+ Collections.singletonList( new JacksonJaxbJsonProvider() ) );
+
+ WebClient.client( service ).header( "Authorization", authorizationHeader );
+ WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
+ return service;
+ }
+
+ protected RemoteRepositoriesService getRemoteRepositoriesService()
+ {
+ RemoteRepositoriesService service =
+ JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
+ RemoteRepositoriesService.class,
+ Collections.singletonList( new JacksonJaxbJsonProvider() ) );
+
+ WebClient.client( service ).header( "Authorization", authorizationHeader );
+ WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
+ return service;
+ }
+
+ protected ManagedRepositoriesService getManagedRepositoriesService()
+ {
+ ManagedRepositoriesService service =
+ JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
+ ManagedRepositoriesService.class,
+ Collections.singletonList( new JacksonJaxbJsonProvider() ) );
+
+ WebClient.client( service ).header( "Authorization", authorizationHeader );
+ WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
+ return service;
+ }
+
+
+ protected RepositoryGroupService getRepositoryGroupService()
+ {
+ RepositoryGroupService service =
+ JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
+ RepositoryGroupService.class,
+ Collections.singletonList( new JacksonJaxbJsonProvider() ) );
+
+ WebClient.client( service ).header( "Authorization", authorizationHeader );
+ WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
+ return service;
+ }
+
+ protected RepositoriesService getRepositoriesService()
+ {
+ RepositoriesService service =
+ JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
+ RepositoriesService.class,
+ Collections.singletonList( new JacksonJaxbJsonProvider() ) );
+
+ WebClient.client( service ).header( "Authorization", authorizationHeader );
+ WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
+ return service;
+ }
+
+ protected SearchService getSearchService()
+ {
+ SearchService service =
+ JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
+ SearchService.class,
+ Collections.singletonList( new JacksonJaxbJsonProvider() ) );
+
+ WebClient.client( service ).header( "Authorization", authorizationHeader );
+ WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
+ return service;
+ }
+
+ protected String getBaseUrl()
+ {
+ String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
+ return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
+ }
+
+
+ protected RoleManagementService getRoleManagementService( String authzHeader )
+ {
+ RoleManagementService service =
+ JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
+ RoleManagementService.class,
+ Collections.singletonList( new JacksonJaxbJsonProvider() ) );
+
+ // for debuging purpose
+ WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
+
+ if ( authzHeader != null )
+ {
+ WebClient.client( service ).header( "Authorization", authzHeader );
+ }
+ return service;
+ }
+
+ protected UserService getUserService( String authzHeader )
+ {
+ UserService service =
+ JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
+ UserService.class, Collections.singletonList( new JacksonJaxbJsonProvider() ) );
+
+ // for debuging purpose
+ WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
+
+ if ( authzHeader != null )
+ {
+ WebClient.client( service ).header( "Authorization", authzHeader );
+ }
+ return service;
+ }
+
+ protected FakeCreateAdminService getFakeCreateAdminService()
+ {
+ return JAXRSClientFactory.create(
+ "http://localhost:" + port + "/" + getRestServicesPath() + "/fakeCreateAdminService/",
+ FakeCreateAdminService.class );
+ }
+
+}
--- /dev/null
+package org.apache.archiva.remotedownload;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.archiva.admin.model.beans.RemoteRepository;
+import org.apache.archiva.redback.rest.api.services.RoleManagementService;
+import org.apache.archiva.security.common.ArchivaRoleConstants;
+import org.apache.commons.compress.utils.IOUtils;
+import org.apache.commons.io.FileUtils;
+import org.apache.maven.wagon.providers.http.HttpWagon;
+import org.apache.maven.wagon.repository.Repository;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.servlet.ServletContextHandler;
+import org.eclipse.jetty.servlet.ServletHolder;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
+
+/**
+ * @author Olivier Lamy
+ */
+@RunWith( ArchivaBlockJUnit4ClassRunner.class )
+public class DownloadArtifactsTest
+ extends AbstractDownloadTest
+{
+
+ protected Logger log = LoggerFactory.getLogger( DownloadArtifactsTest.class );
+
+ public Server redirectServer = null;
+
+ public int redirectPort;
+
+ public Server repoServer = null;
+
+ public int repoServerPort;
+
+ @BeforeClass
+ public static void setAppServerBase()
+ {
+ previousAppServerBase = System.getProperty( "appserver.base" );
+ System.setProperty( "appserver.base", "target/" + DownloadArtifactsTest.class.getName() );
+ }
+
+
+ @AfterClass
+ public static void resetAppServerBase()
+ {
+ System.setProperty( "appserver.base", previousAppServerBase );
+ }
+
+ protected String getSpringConfigLocation()
+ {
+ return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-artifacts-download.xml";
+ }
+
+ @Before
+ public void startServer()
+ throws Exception
+ {
+ super.startServer();
+
+ // repo handler
+
+ this.repoServer = new Server( 0 );
+
+ ServletHolder shRepo = new ServletHolder( RepoServlet.class );
+ ServletContextHandler contextRepo = new ServletContextHandler();
+
+ contextRepo.setContextPath( "/" );
+ contextRepo.addServlet( shRepo, "/*" );
+
+ repoServer.setHandler( contextRepo );
+ repoServer.start();
+ this.repoServerPort = repoServer.getConnectors()[0].getLocalPort();
+
+ //redirect handler
+
+ this.redirectServer = new Server( 0 );
+ ServletHolder shRedirect = new ServletHolder( RedirectServlet.class );
+ ServletContextHandler contextRedirect = new ServletContextHandler();
+ contextRedirect.setAttribute( "redirectToPort", Integer.toString( this.repoServerPort ) );
+
+ contextRedirect.setContextPath( "/" );
+ contextRedirect.addServlet( shRedirect, "/*" );
+
+ redirectServer.setHandler( contextRedirect );
+ redirectServer.start();
+ this.redirectPort = redirectServer.getConnectors()[0].getLocalPort();
+ log.info( "redirect server port {}", redirectPort );
+
+
+
+ }
+
+ @After
+ public void tearDown()
+ throws Exception
+ {
+ super.tearDown();
+ if ( this.redirectServer != null )
+ {
+ this.redirectServer.stop();
+ }
+ }
+
+ @Test
+ public void downloadWithRemoteRedirect()
+ throws Exception
+ {
+ RemoteRepository remoteRepository = getRemoteRepositoriesService().getRemoteRepository( "central" );
+ remoteRepository.setUrl( "http://localhost:" + redirectPort );
+ getRemoteRepositoriesService().updateRemoteRepository( remoteRepository );
+
+ RoleManagementService roleManagementService = getRoleManagementService( authorizationHeader );
+
+ if ( !roleManagementService.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER,
+ "internal" ) )
+ {
+ roleManagementService.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, "internal" );
+ }
+
+ getUserService( authorizationHeader ).createGuestUser();
+ roleManagementService.assignRole( ArchivaRoleConstants.TEMPLATE_GUEST, "guest" );
+
+ roleManagementService.assignTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, "internal",
+ "guest" );
+
+ getUserService( authorizationHeader ).removeFromCache( "guest" );
+
+ File file = new File( "target/junit-4.9.jar" );
+ if ( file.exists() )
+ {
+ file.delete();
+ }
+
+ HttpWagon httpWagon = new HttpWagon();
+ httpWagon.connect( new Repository( "foo", "http://localhost:" + port ) );
+
+ httpWagon.get( "/repository/internal/junit/junit/4.9/junit-4.9.jar", file );
+
+ ZipFile zipFile = new ZipFile( file );
+ List<String> entries = getZipEntriesNames( zipFile );
+ ZipEntry zipEntry = zipFile.getEntry( "org/junit/runners/JUnit4.class" );
+ assertNotNull( "cannot find zipEntry org/junit/runners/JUnit4.class, entries: " + entries + ", content is: "
+ + FileUtils.readFileToString( file ), zipEntry );
+ zipFile.close();
+ file.deleteOnExit();
+ }
+
+ private List<String> getZipEntriesNames( ZipFile zipFile )
+ {
+ try
+ {
+ List<String> entriesNames = new ArrayList<String>();
+ Enumeration<? extends ZipEntry> entries = zipFile.entries();
+ while ( entries.hasMoreElements() )
+ {
+ entriesNames.add( entries.nextElement().getName() );
+ }
+ return entriesNames;
+ }
+ catch ( Throwable e )
+ {
+ log.info( "fail to get zipEntries " + e.getMessage(), e );
+ }
+ return Collections.emptyList();
+ }
+
+
+ public static class RedirectServlet
+ extends HttpServlet
+ {
+ @Override
+ protected void doGet( HttpServletRequest req, HttpServletResponse resp )
+ throws ServletException, IOException
+ {
+
+ LoggerFactory.getLogger( getClass() ).info( "redirect servlet receive: {}", req.getRequestURI() );
+ resp.setStatus( 302 );
+ resp.getWriter().write( "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" + "<html><head>\n"
+ + "<title>302 Found</title>\n" + "</head><body>\n" + "<h1>Found</h1>\n"
+ + "<p>The document has moved <a href=\"http://repo.maven.apache.org/maven2/junit/junit/4.9/junit-4.9.jar\">here</a>.</p>\n"
+ + "</body></html>\n" + "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
+ + "<html><head>\n" );
+ resp.sendRedirect( "http://localhost:" + getServletContext().getAttribute( "redirectToPort" ) + "/maven2/"
+ + req.getRequestURI() );
+ }
+ }
+
+ public static class RepoServlet
+ extends HttpServlet
+ {
+ @Override
+ protected void doGet( HttpServletRequest req, HttpServletResponse resp )
+ throws ServletException, IOException
+ {
+ File jar = new File( System.getProperty( "basedir" ), "src/test/junit-4.9.jar" );
+ IOUtils.copy( new FileInputStream( jar ), resp.getOutputStream() );
+
+ }
+ }
+
+
+}
--- /dev/null
+package org.apache.archiva.remotedownload;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.archiva.admin.model.beans.ManagedRepository;
+import org.apache.archiva.admin.model.beans.ProxyConnector;
+import org.apache.archiva.admin.model.beans.RemoteRepository;
+import org.apache.archiva.admin.model.beans.RepositoryGroup;
+import org.apache.archiva.maven2.model.Artifact;
+import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants;
+import org.apache.archiva.redback.rest.services.FakeCreateAdminService;
+import org.apache.archiva.rest.api.model.SearchRequest;
+import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
+import org.apache.archiva.rest.api.services.ProxyConnectorService;
+import org.apache.archiva.rest.api.services.RepositoriesService;
+import org.apache.archiva.rest.api.services.RepositoryGroupService;
+import org.apache.archiva.rest.api.services.SearchService;
+import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
+import org.apache.commons.io.FileUtils;
+import org.fest.assertions.api.Assertions;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.io.File;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * @author Olivier Lamy
+ */
+@RunWith( ArchivaBlockJUnit4ClassRunner.class )
+public class DownloadMergedIndexNonDefaultPathTest
+ extends AbstractDownloadTest
+{
+
+ @BeforeClass
+ public static void setAppServerBase()
+ {
+ previousAppServerBase = System.getProperty( "appserver.base" );
+ System.setProperty( "appserver.base", System.getProperty( "basedir" ) + "/target/" + DownloadMergedIndexNonDefaultPathTest.class.getName() );
+ }
+
+ @AfterClass
+ public static void resetAppServerBase()
+ {
+ System.setProperty( "appserver.base", previousAppServerBase );
+ }
+
+ protected String getSpringConfigLocation()
+ {
+ return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-merge-index-download.xml";
+ }
+
+ @After
+ public void cleanup()
+ throws Exception
+ {
+ super.tearDown();
+ File tmpIndexDir = new File( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex" );
+ if ( tmpIndexDir.exists() )
+ {
+ FileUtils.deleteDirectory( tmpIndexDir );
+ }
+ }
+
+
+ @Test
+ public void downloadMergedIndexWithNonDefaultPath()
+ throws Exception
+ {
+ File tmpIndexDir = new File( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex" );
+ if ( tmpIndexDir.exists() )
+ {
+ FileUtils.deleteDirectory( tmpIndexDir );
+ }
+ String id = Long.toString( System.currentTimeMillis() );
+ ManagedRepository managedRepository = new ManagedRepository();
+ managedRepository.setId( id );
+ managedRepository.setName( "name of " + id );
+ managedRepository.setLocation( System.getProperty( "basedir" ) + "/src/test/repositories/test-repo" );
+ managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex/" + id );
+
+ ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
+
+ if ( managedRepositoriesService.getManagedRepository( id ) != null )
+ {
+ managedRepositoriesService.deleteManagedRepository( id, false );
+ }
+
+ getManagedRepositoriesService().addManagedRepository( managedRepository );
+
+ RepositoriesService repositoriesService = getRepositoriesService();
+
+ repositoriesService.scanRepositoryNow( id, true );
+
+ // wait a bit to ensure index is finished
+ int timeout = 20000;
+ while ( timeout > 0 && repositoriesService.alreadyScanning( id ) )
+ {
+ Thread.sleep( 500 );
+ timeout -= 500;
+ }
+
+ RepositoryGroupService repositoryGroupService = getRepositoryGroupService();
+
+ String repoGroupId = "test-group";
+
+ if ( repositoryGroupService.getRepositoryGroup( repoGroupId ) != null )
+ {
+ repositoryGroupService.deleteRepositoryGroup( repoGroupId );
+ }
+
+ RepositoryGroup repositoryGroup = new RepositoryGroup();
+ repositoryGroup.setId( repoGroupId );
+ String path = ".fooooo";
+ repositoryGroup.setRepositories( Arrays.asList( id ) );
+ repositoryGroup.setMergedIndexPath( path );
+
+ repositoryGroupService.addRepositoryGroup( repositoryGroup );
+
+ // create a repo with a remote on the one with index
+ id = Long.toString( System.currentTimeMillis() );
+ managedRepository = new ManagedRepository();
+ managedRepository.setId( id );
+ managedRepository.setName( "name of " + id );
+ managedRepository.setLocation( System.getProperty( "basedir" ) + "/src/test/repositories/test-repo" );
+ managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex/" + id );
+
+ if ( managedRepositoriesService.getManagedRepository( id ) != null )
+ {
+ managedRepositoriesService.deleteManagedRepository( id, false );
+ }
+
+ getManagedRepositoriesService().addManagedRepository( managedRepository );
+
+ String remoteId = Long.toString( System.currentTimeMillis() );
+
+ RemoteRepository remoteRepository = new RemoteRepository();
+ remoteRepository.setId( remoteId );
+ remoteRepository.setName( remoteId );
+ remoteRepository.setDownloadRemoteIndex( true );
+ remoteRepository.setUrl( "http://localhost:" + port + "/repository/test-group" );
+ remoteRepository.setRemoteIndexUrl( "http://localhost:" + port + "/repository/test-group/" + path );
+ remoteRepository.setUserName( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
+ remoteRepository.setPassword( FakeCreateAdminService.ADMIN_TEST_PWD );
+
+ getRemoteRepositoriesService().addRemoteRepository( remoteRepository );
+
+ ProxyConnectorService proxyConnectorService = getProxyConnectorService();
+ ProxyConnector proxyConnector = new ProxyConnector();
+ proxyConnector.setProxyId( "foo-bar2" );
+ proxyConnector.setSourceRepoId( id );
+ proxyConnector.setTargetRepoId( remoteId );
+ proxyConnectorService.addProxyConnector( proxyConnector );
+
+ repositoriesService.scheduleDownloadRemoteIndex( remoteId, true, true );
+
+ // wait a bit
+ /*
+ timeout = 20000;
+ while ( timeout > 0 )
+ {
+ Thread.sleep( 500 );
+ timeout -= 500;
+ }*/
+ // wait the end
+ while ( !repositoriesService.getRunningRemoteDownloadIds().getStrings().isEmpty() )
+ {
+ Thread.sleep( 500 );
+ log.debug( "still running remote download" );
+ }
+
+ SearchService searchService = getSearchService();
+
+ SearchRequest request = new SearchRequest();
+ request.setRepositories( Arrays.asList( id ) );
+ request.setGroupId( "org.apache.felix" );
+
+ List<Artifact> artifacts = searchService.searchArtifacts( request );
+ Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 );
+
+ }
+}
--- /dev/null
+package org.apache.archiva.remotedownload;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.archiva.admin.model.beans.ManagedRepository;
+import org.apache.archiva.admin.model.beans.ProxyConnector;
+import org.apache.archiva.admin.model.beans.RemoteRepository;
+import org.apache.archiva.admin.model.beans.RepositoryGroup;
+import org.apache.archiva.maven2.model.Artifact;
+import org.apache.archiva.rest.api.model.SearchRequest;
+import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
+import org.apache.archiva.rest.api.services.ProxyConnectorService;
+import org.apache.archiva.rest.api.services.RepositoriesService;
+import org.apache.archiva.rest.api.services.RepositoryGroupService;
+import org.apache.archiva.rest.api.services.SearchService;
+import org.apache.commons.io.FileUtils;
+import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants;
+import org.apache.archiva.redback.rest.services.FakeCreateAdminService;
+import org.fest.assertions.api.Assertions;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
+
+/**
+ * @author Olivier Lamy
+ */
+@RunWith(ArchivaBlockJUnit4ClassRunner.class)
+public class DownloadMergedIndexTest
+ extends AbstractDownloadTest
+{
+
+ @BeforeClass
+ public static void setAppServerBase()
+ throws IOException
+ {
+ previousAppServerBase = System.getProperty( "appserver.base" );
+ System.setProperty( "appserver.base",
+ new File( System.getProperty( "java.io.tmpdir" ) ).getCanonicalPath() + "/target/"
+ + DownloadMergedIndexTest.class.getName() );
+ }
+
+ @AfterClass
+ public static void resetAppServerBase()
+ {
+ System.setProperty( "appserver.base", previousAppServerBase );
+ }
+
+ protected String getSpringConfigLocation()
+ {
+ return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-merge-index-download.xml";
+ }
+
+ @After
+ public void cleanup()
+ throws Exception
+ {
+ super.tearDown();
+ File tmpIndexDir = new File( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex" );
+ if ( tmpIndexDir.exists() )
+ {
+ FileUtils.deleteDirectory( tmpIndexDir );
+ }
+ }
+
+
+ @Test
+ public void downloadMergedIndex()
+ throws Exception
+ {
+ File tmpIndexDir = new File( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex" );
+ if ( tmpIndexDir.exists() )
+ {
+ FileUtils.deleteDirectory( tmpIndexDir );
+ }
+ String id = Long.toString( System.currentTimeMillis() );
+ ManagedRepository managedRepository = new ManagedRepository();
+ managedRepository.setId( id );
+ managedRepository.setName( "name of " + id );
+ managedRepository.setLocation( System.getProperty( "basedir" ) + "/src/test/repositories/test-repo" );
+ managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex/" + id );
+
+ ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
+
+ if ( managedRepositoriesService.getManagedRepository( id ) != null )
+ {
+ managedRepositoriesService.deleteManagedRepository( id, false );
+ }
+
+ getManagedRepositoriesService().addManagedRepository( managedRepository );
+
+ RepositoriesService repositoriesService = getRepositoriesService();
+
+ repositoriesService.scanRepositoryNow( id, true );
+
+ // wait a bit to ensure index is finished
+ int timeout = 20000;
+ while ( timeout > 0 && repositoriesService.alreadyScanning( id ) )
+ {
+ Thread.sleep( 500 );
+ timeout -= 500;
+ }
+
+ RepositoryGroupService repositoryGroupService = getRepositoryGroupService();
+
+ String repoGroupId = "test-group";
+
+ if ( repositoryGroupService.getRepositoryGroup( repoGroupId ) != null )
+ {
+ repositoryGroupService.deleteRepositoryGroup( repoGroupId );
+ }
+
+ RepositoryGroup repositoryGroup = new RepositoryGroup();
+ repositoryGroup.setId( repoGroupId );
+ repositoryGroup.setRepositories( Arrays.asList( id ) );
+
+ repositoryGroupService.addRepositoryGroup( repositoryGroup );
+
+ // create a repo with a remote on the one with index
+ id = Long.toString( System.currentTimeMillis() );
+ managedRepository = new ManagedRepository();
+ managedRepository.setId( id );
+ managedRepository.setName( "name of " + id );
+ managedRepository.setLocation( System.getProperty( "basedir" ) + "/src/test/repositories/test-repo" );
+ managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex/" + id );
+
+ if ( managedRepositoriesService.getManagedRepository( id ) != null )
+ {
+ managedRepositoriesService.deleteManagedRepository( id, false );
+ }
+
+ getManagedRepositoriesService().addManagedRepository( managedRepository );
+
+ RemoteRepository remoteRepository = new RemoteRepository();
+ remoteRepository.setId( "all-merged" );
+ remoteRepository.setName( "all-merged" );
+ remoteRepository.setDownloadRemoteIndex( true );
+ remoteRepository.setUrl( "http://localhost:" + port + "/repository/test-group" );
+ remoteRepository.setRemoteIndexUrl( "http://localhost:" + port + "/repository/test-group/.indexer" );
+ remoteRepository.setUserName( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
+ remoteRepository.setPassword( FakeCreateAdminService.ADMIN_TEST_PWD );
+
+ getRemoteRepositoriesService().addRemoteRepository( remoteRepository );
+
+ ProxyConnectorService proxyConnectorService = getProxyConnectorService();
+ ProxyConnector proxyConnector = new ProxyConnector();
+ proxyConnector.setProxyId( "foo-bar1" );
+ proxyConnector.setSourceRepoId( id );
+ proxyConnector.setTargetRepoId( "all-merged" );
+ proxyConnectorService.addProxyConnector( proxyConnector );
+
+ repositoriesService.scheduleDownloadRemoteIndex( "all-merged", true, true );
+
+ // wait a bit
+ timeout = 20000;
+ while ( timeout > 0 )
+ {
+ Thread.sleep( 500 );
+ timeout -= 500;
+ }
+
+ SearchService searchService = getSearchService();
+
+ SearchRequest request = new SearchRequest();
+ request.setRepositories( Arrays.asList( id ) );
+ request.setGroupId( "org.apache.felix" );
+
+ List<Artifact> artifacts = searchService.searchArtifacts( request );
+ Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 );
+
+ }
+}
<logger name="org.apache.archiva.scheduler.indexing" level="debug"/>
+ <logger name="org.apache.archiva.remotedownload" level="debug"/>
+
<root level="info">
<appender-ref ref="console"/>
</root>