123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- package org.apache.archiva.webdav;
-
- /*
- * 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.gargoylesoftware.htmlunit.WebClient;
- import org.apache.archiva.common.utils.FileUtils;
- import org.apache.archiva.configuration.ProxyConnectorConfiguration;
- import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
- import org.apache.archiva.policies.*;
- import org.apache.commons.lang3.StringUtils;
- import org.eclipse.jetty.server.HttpConnectionFactory;
- import org.eclipse.jetty.server.Server;
- import org.eclipse.jetty.server.ServerConnector;
- import org.eclipse.jetty.server.handler.ContextHandlerCollection;
- import org.eclipse.jetty.servlet.DefaultServlet;
- import org.eclipse.jetty.servlet.ServletContextHandler;
- import org.eclipse.jetty.servlet.ServletHolder;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Rule;
-
- import javax.servlet.http.HttpServletResponse;
- import java.nio.charset.Charset;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
-
- import static org.assertj.core.api.Assertions.assertThat;
-
- /**
- * AbstractRepositoryServletProxiedTestCase
- */
- public abstract class AbstractRepositoryServletProxiedTestCase
- extends AbstractRepositoryServletTestCase
- {
- class RemoteRepoInfo
- {
- public String id;
-
- public String url;
-
- public String context;
-
- public Server server;
-
- public Path root;
-
- public RemoteRepositoryConfiguration config;
- }
-
- protected static final long ONE_SECOND = ( 1000 /* milliseconds */ );
-
- protected static final long ONE_MINUTE = ( ONE_SECOND * 60 );
-
- protected static final long ONE_HOUR = ( ONE_MINUTE * 60 );
-
- protected static final long ONE_DAY = ( ONE_HOUR * 24 );
-
- protected static final long OVER_ONE_HOUR = ( ONE_HOUR + ONE_MINUTE );
-
- protected static final long OVER_ONE_DAY = ( ONE_DAY + ONE_HOUR );
-
- protected static final long OLDER = ( -1 );
-
- protected static final long NEWER = 0;
-
- protected static final int EXPECT_MANAGED_CONTENTS = 1;
-
- protected static final int EXPECT_REMOTE_CONTENTS = 2;
-
- protected static final int EXPECT_NOT_FOUND = 3;
-
- protected static final boolean HAS_MANAGED_COPY = true;
-
- protected static final boolean NO_MANAGED_COPY = false;
-
- protected RemoteRepoInfo remoteCentral;
-
- protected RemoteRepoInfo remoteSnapshots;
-
- @Rule
- public ArchivaTemporaryFolderRule repoRootInternali = new ArchivaTemporaryFolderRule();
-
- @Before
- @Override
- public void setUp()
- throws Exception
- {
- super.setUp();
- startRepository();
- }
-
- @Override
- @After
- public void tearDown()
- throws Exception
- {
- shutdownServer( remoteCentral );
- shutdownServer( remoteSnapshots );
- super.tearDown();
- String baseDirProp = System.getProperty( "appserver.base" );
- if ( StringUtils.isNotEmpty( baseDirProp )) {
- Path baseDir = Paths.get(baseDirProp);
- log.info("Deleting appserver base {}", baseDir);
- FileUtils.deleteDirectory( baseDir );
- log.info("exist {}", Files.exists(baseDir));
- }
- }
-
- protected RemoteRepoInfo createServer( String id )
- throws Exception
- {
- RemoteRepoInfo repo = new RemoteRepoInfo();
- repo.id = id;
- repo.context = "/" + id;
- repo.root = repoRootInternali.getRoot();/*Files.createTempDirectory(
- "temp" ).toFile();*/// new File( System.getProperty( "basedir" ) + "target/remote-repos/" + id + "/" );
-
- // Remove exising root contents.
- if ( Files.exists(repo.root) )
- {
- FileUtils.deleteDirectory( repo.root );
- }
-
- // Establish root directory.
- if ( !Files.exists(repo.root) )
- {
- Files.createDirectories( repo.root );
- }
-
- repo.server = new Server( );
- ServerConnector serverConnector = new ServerConnector( repo.server, new HttpConnectionFactory());
- repo.server.addConnector( serverConnector );
- ContextHandlerCollection contexts = new ContextHandlerCollection();
- repo.server.setHandler( contexts );
-
- ServletContextHandler context = new ServletContextHandler();
- context.setContextPath( repo.context );
- context.setResourceBase( repo.root.toAbsolutePath().toString() );
- context.setAttribute( "dirAllowed", true );
- context.setAttribute( "maxCacheSize", 0 );
-
- ServletHolder sh = new ServletHolder( DefaultServlet.class );
- context.addServlet( sh, "/" );
-
- contexts.addHandler( context );
-
- repo.server.start();
-
- int port = serverConnector.getLocalPort();
- repo.url = "http://localhost:" + port + repo.context;
- log.info( "Remote HTTP Server started on {}", repo.url );
-
- repo.config = createRemoteRepository( repo.id, "Testable [" + repo.id + "] Remote Repo", repo.url );
-
- return repo;
- }
-
- protected void assertServerSetupCorrectly( RemoteRepoInfo remoteRepo )
- throws Exception
- {
-
- WebClient client = newClient();
- int status = client.getPage( remoteRepo.url ).getWebResponse().getStatusCode();
- assertThat( status ).isEqualTo( HttpServletResponse.SC_OK );
-
- }
-
- private void setupConnector( String repoId, RemoteRepoInfo remoteRepo, PolicyOption releasesPolicy,
- PolicyOption snapshotsPolicy )
- {
- ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
- connector.setSourceRepoId( repoId );
- connector.setTargetRepoId( remoteRepo.id );
- connector.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, releasesPolicy.getId() );
- connector.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, snapshotsPolicy.getId() );
- connector.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, ChecksumPolicy.IGNORE.getId() );
- connector.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, CachedFailuresPolicy.NO.getId() );
-
- archivaConfiguration.getConfiguration().addProxyConnector( connector );
- }
-
- protected void shutdownServer( RemoteRepoInfo remoteRepo )
- {
- if ( remoteRepo != null )
- {
- if ( remoteRepo.server != null )
- {
- if ( remoteRepo.server.isRunning() )
- {
- try
- {
- remoteRepo.server.stop();
- // int graceful = remoteRepo.server.getGracefulShutdown();
- // System.out.println( "server set to graceful shutdown: " + graceful );
- // remoteRepo = null;
- }
- catch ( Exception e )
- {
- e.printStackTrace( System.err );
- }
- }
- }
- }
- }
-
- protected Path populateRepo( RemoteRepoInfo remoteRepo, String path, String contents )
- throws Exception
- {
- Path destFile = remoteRepo.root.resolve( path );
- if ( Files.exists(destFile) )
- {
- Files.delete(destFile);
- }
- Files.createDirectories( destFile.getParent());
- org.apache.archiva.common.utils.FileUtils.writeStringToFile( destFile, Charset.defaultCharset(), contents);
- return destFile;
- }
-
- protected void setupCentralRemoteRepo()
- throws Exception
- {
- remoteCentral = createServer( "central" );
-
- assertServerSetupCorrectly( remoteCentral );
-
- RemoteRepositoryConfiguration remoteRepositoryConfiguration =
- archivaConfiguration.getConfiguration().getRemoteRepositoriesAsMap().get( remoteCentral.id );
- if ( remoteRepositoryConfiguration != null )
- {
- archivaConfiguration.getConfiguration().removeRemoteRepository( remoteRepositoryConfiguration );
- }
-
- archivaConfiguration.getConfiguration().addRemoteRepository( remoteCentral.config );
- setupCleanRepo( remoteCentral.root );
- }
-
- protected void setupConnector( String repoId, RemoteRepoInfo remoteRepo )
- {
- setupConnector( repoId, remoteRepo, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS );
- }
-
- protected void setupReleaseConnector( String managedRepoId, RemoteRepoInfo remoteRepo, PolicyOption releasePolicy )
- {
- setupConnector( managedRepoId, remoteRepo, releasePolicy, SnapshotsPolicy.ALWAYS );
- }
-
- protected void setupSnapshotConnector( String managedRepoId, RemoteRepoInfo remoteRepo, PolicyOption snapshotsPolicy )
- {
- setupConnector( managedRepoId, remoteRepo, ReleasesPolicy.ALWAYS, snapshotsPolicy );
- }
-
- protected void setupSnapshotsRemoteRepo()
- throws Exception
- {
- remoteSnapshots = createServer( "snapshots" );
-
- assertServerSetupCorrectly( remoteSnapshots );
- RemoteRepositoryConfiguration remoteRepositoryConfiguration =
- archivaConfiguration.getConfiguration().getRemoteRepositoriesAsMap().get( remoteSnapshots.id );
- if ( remoteRepositoryConfiguration != null )
- {
- archivaConfiguration.getConfiguration().removeRemoteRepository( remoteRepositoryConfiguration );
- }
- archivaConfiguration.getConfiguration().addRemoteRepository( remoteSnapshots.config );
- setupCleanRepo( remoteSnapshots.root );
- }
-
-
- }
|