Search in sources :

Example 1 with RepositoryProxyConnectors

use of org.apache.archiva.proxy.model.RepositoryProxyConnectors in project archiva by apache.

the class HttpProxyTransferTest method setUp.

@Before
public void setUp() throws Exception {
    proxyHandler = applicationContext.getBean("repositoryProxyConnectors#test", RepositoryProxyConnectors.class);
    config = applicationContext.getBean("archivaConfiguration#mock", ArchivaConfiguration.class);
    // clear from previous tests - TODO the spring context should be initialised per test instead, or the config
    // made a complete mock
    config.getConfiguration().getProxyConnectors().clear();
    // Setup source repository (using default layout)
    String repoPath = "target/test-repository/managed/" + getClass().getSimpleName();
    Path destRepoDir = Paths.get(repoPath);
    // Cleanout destination dirs.
    if (Files.exists(destRepoDir)) {
        FileUtils.deleteDirectory(destRepoDir.toFile());
    }
    // Make the destination dir.
    Files.createDirectories(destRepoDir);
    MavenManagedRepository repo = new MavenManagedRepository(MANAGED_ID, "Default Managed Repository", Paths.get(repoPath).getParent());
    repo.setLocation(new URI(repoPath));
    repo.setLayout("default");
    RepositoryContentProvider provider = applicationContext.getBean("repositoryContentProvider#maven", RepositoryContentProvider.class);
    ManagedRepositoryContent repoContent = provider.createManagedContent(repo);
    managedDefaultRepository = repoContent;
    ((DefaultManagedRepositoryAdmin) applicationContext.getBean(ManagedRepositoryAdmin.class)).setArchivaConfiguration(config);
    RepositoryRegistry managedRepositoryAdmin = applicationContext.getBean(RepositoryRegistry.class);
    if (managedRepositoryAdmin.getManagedRepository(repo.getId()) == null) {
        managedRepositoryAdmin.putRepository(repo);
    }
    // config.getConfiguration().addManagedRepository( repo );
    Handler handler = new AbstractHandler() {

        @Override
        public void handle(String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse response) throws IOException, ServletException {
            response.setContentType("text/plain");
            response.setStatus(HttpServletResponse.SC_OK);
            response.getWriter().print("get-default-layout-1.0.jar\n\n");
            assertNotNull(request.getHeader("Proxy-Connection"));
            ((Request) request).setHandled(true);
        }

        public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException {
            response.setContentType("text/plain");
            response.setStatus(HttpServletResponse.SC_OK);
            response.getWriter().print("get-default-layout-1.0.jar\n\n");
            assertNotNull(request.getHeader("Proxy-Connection"));
            ((Request) request).setHandled(true);
        }
    };
    server = new Server();
    ServerConnector serverConnector = new ServerConnector(server, new HttpConnectionFactory());
    server.addConnector(serverConnector);
    server.setHandler(handler);
    server.start();
    int port = serverConnector.getLocalPort();
    NetworkProxyConfiguration proxyConfig = new NetworkProxyConfiguration();
    proxyConfig.setHost("localhost");
    proxyConfig.setPort(port);
    proxyConfig.setProtocol("http");
    proxyConfig.setId(PROXY_ID);
    config.getConfiguration().addNetworkProxy(proxyConfig);
    // Setup target (proxied to) repository.
    RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
    repoConfig.setId(PROXIED_ID);
    repoConfig.setName("Proxied Repository 1");
    repoConfig.setLayout("default");
    repoConfig.setUrl("http://www.example.com/");
    config.getConfiguration().addRemoteRepository(repoConfig);
}
Also used : Path(java.nio.file.Path) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) MavenManagedRepository(org.apache.archiva.repository.maven2.MavenManagedRepository) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) Handler(org.eclipse.jetty.server.Handler) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) RepositoryProxyConnectors(org.apache.archiva.proxy.model.RepositoryProxyConnectors) URI(java.net.URI) ArchivaConfiguration(org.apache.archiva.configuration.ArchivaConfiguration) DefaultManagedRepositoryAdmin(org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServerConnector(org.eclipse.jetty.server.ServerConnector) RepositoryContentProvider(org.apache.archiva.repository.RepositoryContentProvider) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ManagedRepositoryAdmin(org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin) DefaultManagedRepositoryAdmin(org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin) NetworkProxyConfiguration(org.apache.archiva.configuration.NetworkProxyConfiguration) RepositoryRegistry(org.apache.archiva.repository.RepositoryRegistry) RemoteRepositoryConfiguration(org.apache.archiva.configuration.RemoteRepositoryConfiguration) Before(org.junit.Before)

Example 2 with RepositoryProxyConnectors

use of org.apache.archiva.proxy.model.RepositoryProxyConnectors in project archiva by apache.

the class Maven2RepositoryStorage method applyServerSideRelocation.

@Override
public void applyServerSideRelocation(ManagedRepositoryContent managedRepository, ArtifactReference artifact) throws ProxyDownloadException {
    if ("pom".equals(artifact.getType())) {
        return;
    }
    // Build the artifact POM reference
    ArtifactReference pomReference = new ArtifactReference();
    pomReference.setGroupId(artifact.getGroupId());
    pomReference.setArtifactId(artifact.getArtifactId());
    pomReference.setVersion(artifact.getVersion());
    pomReference.setType("pom");
    RepositoryProxyConnectors connectors = applicationContext.getBean("repositoryProxyConnectors#default", RepositoryProxyConnectors.class);
    // Get the artifact POM from proxied repositories if needed
    connectors.fetchFromProxies(managedRepository, pomReference);
    // Open and read the POM from the managed repo
    Path pom = managedRepository.toFile(pomReference);
    if (!Files.exists(pom)) {
        return;
    }
    try {
        // MavenXpp3Reader leaves the file open, so we need to close it ourselves.
        Model model = null;
        try (Reader reader = Files.newBufferedReader(pom, Charset.defaultCharset())) {
            model = MAVEN_XPP_3_READER.read(reader);
        }
        DistributionManagement dist = model.getDistributionManagement();
        if (dist != null) {
            Relocation relocation = dist.getRelocation();
            if (relocation != null) {
                // artifact is relocated : update the repositoryPath
                if (relocation.getGroupId() != null) {
                    artifact.setGroupId(relocation.getGroupId());
                }
                if (relocation.getArtifactId() != null) {
                    artifact.setArtifactId(relocation.getArtifactId());
                }
                if (relocation.getVersion() != null) {
                    artifact.setVersion(relocation.getVersion());
                }
            }
        }
    } catch (IOException e) {
    // Unable to read POM : ignore.
    } catch (XmlPullParserException e) {
    // Invalid POM : ignore
    }
}
Also used : Path(java.nio.file.Path) Model(org.apache.maven.model.Model) Reader(java.io.Reader) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) MavenMetadataReader(org.apache.archiva.maven2.metadata.MavenMetadataReader) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) RepositoryProxyConnectors(org.apache.archiva.proxy.model.RepositoryProxyConnectors) IOException(java.io.IOException) DistributionManagement(org.apache.maven.model.DistributionManagement) ArtifactReference(org.apache.archiva.model.ArtifactReference) Relocation(org.apache.maven.model.Relocation)

Aggregations

Path (java.nio.file.Path)2 RepositoryProxyConnectors (org.apache.archiva.proxy.model.RepositoryProxyConnectors)2 IOException (java.io.IOException)1 Reader (java.io.Reader)1 URI (java.net.URI)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 ManagedRepositoryAdmin (org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin)1 DefaultManagedRepositoryAdmin (org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin)1 ArchivaConfiguration (org.apache.archiva.configuration.ArchivaConfiguration)1 NetworkProxyConfiguration (org.apache.archiva.configuration.NetworkProxyConfiguration)1 RemoteRepositoryConfiguration (org.apache.archiva.configuration.RemoteRepositoryConfiguration)1 MavenMetadataReader (org.apache.archiva.maven2.metadata.MavenMetadataReader)1 ArtifactReference (org.apache.archiva.model.ArtifactReference)1 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)1 RepositoryContentProvider (org.apache.archiva.repository.RepositoryContentProvider)1 RepositoryRegistry (org.apache.archiva.repository.RepositoryRegistry)1 MavenManagedRepository (org.apache.archiva.repository.maven2.MavenManagedRepository)1 DistributionManagement (org.apache.maven.model.DistributionManagement)1 Model (org.apache.maven.model.Model)1