Search in sources :

Example 1 with RepositoryProxyHandler

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

the class ArchivaProxyRegistry method updateNetworkProxies.

private void updateNetworkProxies() {
    this.networkProxyMap.clear();
    List<NetworkProxyConfiguration> networkProxies = getArchivaConfiguration().getConfiguration().getNetworkProxies();
    for (NetworkProxyConfiguration networkProxyConfig : networkProxies) {
        String key = networkProxyConfig.getId();
        NetworkProxy proxy = new NetworkProxy();
        proxy.setProtocol(networkProxyConfig.getProtocol());
        proxy.setHost(networkProxyConfig.getHost());
        proxy.setPort(networkProxyConfig.getPort());
        proxy.setUsername(networkProxyConfig.getUsername());
        proxy.setPassword(networkProxyConfig.getPassword() == null ? new char[0] : networkProxyConfig.getPassword().toCharArray());
        proxy.setUseNtlm(networkProxyConfig.isUseNtlm());
        this.networkProxyMap.put(key, proxy);
    }
    for (RepositoryProxyHandler proxyHandler : repositoryProxyHandlers) {
        proxyHandler.setNetworkProxies(this.networkProxyMap);
    }
}
Also used : NetworkProxyConfiguration(org.apache.archiva.configuration.model.NetworkProxyConfiguration) NetworkProxy(org.apache.archiva.proxy.model.NetworkProxy) RepositoryProxyHandler(org.apache.archiva.proxy.model.RepositoryProxyHandler)

Example 2 with RepositoryProxyHandler

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

the class ArchivaProxyRegistry method updateHandler.

private void updateHandler() {
    for (RepositoryProxyHandler handler : repositoryProxyHandlers) {
        List<RepositoryType> types = handler.supports();
        for (RepositoryType type : types) {
            if (!handlerMap.containsKey(type)) {
                handlerMap.put(type, new ArrayList<>());
            }
            handlerMap.get(type).add(handler);
        }
        handler.setPolicies(policies);
    }
}
Also used : RepositoryType(org.apache.archiva.repository.RepositoryType) RepositoryProxyHandler(org.apache.archiva.proxy.model.RepositoryProxyHandler)

Example 3 with RepositoryProxyHandler

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

the class HttpProxyTransferTest method setUp.

@Before
public void setUp() throws Exception {
    proxyHandler = applicationContext.getBean("repositoryProxyHandler#test", RepositoryProxyHandler.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);
    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);
    ((MockConfiguration) config).triggerChange("networkProxies.networkProxy(0).host", "localhost");
    // 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);
    Wagon wagon = new HttpWagon();
    WagonDelegate delegate = (WagonDelegate) applicationContext.getBean("wagon#http", Wagon.class);
    delegate.setDelegate(wagon);
    proxyRegistry.reload();
    repositoryRegistry.reload();
    managedDefaultRepository = createRepository(MANAGED_ID, "Default Managed Repository", repoPath, "default");
}
Also used : Path(java.nio.file.Path) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) HttpWagon(org.apache.maven.wagon.providers.http.HttpWagon) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) Handler(org.eclipse.jetty.server.Handler) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) RepositoryProxyHandler(org.apache.archiva.proxy.model.RepositoryProxyHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpWagon(org.apache.maven.wagon.providers.http.HttpWagon) Wagon(org.apache.maven.wagon.Wagon) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServerConnector(org.eclipse.jetty.server.ServerConnector) NetworkProxyConfiguration(org.apache.archiva.configuration.model.NetworkProxyConfiguration) RemoteRepositoryConfiguration(org.apache.archiva.configuration.model.RemoteRepositoryConfiguration) RepositoryProxyHandler(org.apache.archiva.proxy.model.RepositoryProxyHandler) Before(org.junit.Before)

Example 4 with RepositoryProxyHandler

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

the class DefaultBrowseService method artifactAvailable.

@Override
public AvailabilityStatus artifactAvailable(String groupId, String artifactId, String version, String classifier, String repositoryId) throws ArchivaRestServiceException {
    List<String> selectedRepos = getSelectedRepos(repositoryId);
    boolean snapshot = VersionUtil.isSnapshot(version);
    try {
        for (String repoId : selectedRepos) {
            org.apache.archiva.repository.ManagedRepository managedRepo = repositoryRegistry.getManagedRepository(repoId);
            if (!proxyRegistry.hasHandler(managedRepo.getType())) {
                throw new RepositoryException("No proxy handler found for repository type " + managedRepo.getType());
            }
            RepositoryProxyHandler proxyHandler = proxyRegistry.getHandler(managedRepo.getType()).get(0);
            if ((snapshot && !managedRepo.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT)) || (!snapshot && managedRepo.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT))) {
                continue;
            }
            ManagedRepositoryContent managedRepositoryContent = getManagedRepositoryContent(repoId);
            // FIXME default to jar which can be wrong for war zip etc....
            ArchivaItemSelector itemSelector = ArchivaItemSelector.builder().withNamespace(groupId).withProjectId(artifactId).withVersion(version).withClassifier(StringUtils.isEmpty(classifier) ? "" : classifier).withType("jar").withArtifactId(artifactId).build();
            org.apache.archiva.repository.content.Artifact archivaArtifact = managedRepositoryContent.getItem(itemSelector).adapt(org.apache.archiva.repository.content.Artifact.class);
            StorageAsset file = archivaArtifact.getAsset();
            if (file != null && file.exists()) {
                return new AvailabilityStatus(true);
            }
            // in case of SNAPSHOT we can have timestamped version locally !
            if (StringUtils.endsWith(version, VersionUtil.SNAPSHOT)) {
                StorageAsset metadataFile = file.getStorage().getAsset(file.getParent().getPath() + "/" + MetadataTools.MAVEN_METADATA);
                if (metadataFile.exists()) {
                    MetadataReader metadataReader = repositoryRegistry.getMetadataReader(managedRepositoryContent.getRepository().getType());
                    ArchivaRepositoryMetadata archivaRepositoryMetadata = metadataReader.read(metadataFile);
                    int buildNumber = archivaRepositoryMetadata.getSnapshotVersion().getBuildNumber();
                    String timeStamp = archivaRepositoryMetadata.getSnapshotVersion().getTimestamp();
                    // rebuild file name with timestamped version and build number
                    String timeStampFileName = // 
                    new StringBuilder(artifactId).append('-').append(// 
                    StringUtils.remove(version, "-" + VersionUtil.SNAPSHOT)).append('-').append(// 
                    timeStamp).append('-').append(// 
                    Integer.toString(buildNumber)).append(// 
                    (StringUtils.isEmpty(classifier) ? "" : "-" + classifier)).append(".jar").toString();
                    StorageAsset timeStampFile = file.getStorage().getAsset(file.getParent().getPath() + "/" + timeStampFileName);
                    log.debug("try to find timestamped snapshot version file: {}", timeStampFile.getPath());
                    if (timeStampFile.exists()) {
                        return new AvailabilityStatus(true);
                    }
                }
            }
            String path = managedRepositoryContent.toPath(archivaArtifact);
            file = proxyHandler.fetchFromProxies(managedRepositoryContent.getRepository(), path);
            if (file != null && file.exists()) {
                // download pom now
                String pomPath = StringUtils.substringBeforeLast(path, ".jar") + ".pom";
                proxyHandler.fetchFromProxies(managedRepositoryContent.getRepository(), pomPath);
                return new AvailabilityStatus(true);
            }
        }
    } catch (RepositoryException e) {
        log.error(e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
    }
    return new AvailabilityStatus(false);
}
Also used : ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) MetadataReader(org.apache.archiva.repository.metadata.MetadataReader) RepositoryException(org.apache.archiva.repository.RepositoryException) org.apache.archiva.metadata.repository(org.apache.archiva.metadata.repository) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata) RepositoryProxyHandler(org.apache.archiva.proxy.model.RepositoryProxyHandler)

Example 5 with RepositoryProxyHandler

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

the class ArchivaDavResourceFactory method fetchContentFromProxies.

private boolean fetchContentFromProxies(ManagedRepository managedRepository, DavServletRequest request, LogicalResource resource) throws DavException {
    String path = resource.getPath();
    if (!proxyRegistry.hasHandler(managedRepository.getType())) {
        throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "No proxy handler found for repository type " + managedRepository.getType());
    }
    RepositoryRequestInfo repositoryRequestInfo = managedRepository.getRequestInfo();
    RepositoryProxyHandler proxyHandler = proxyRegistry.getHandler(managedRepository.getType()).get(0);
    if (repositoryRequestInfo.isSupportFile(path)) {
        StorageAsset proxiedFile = proxyHandler.fetchFromProxies(managedRepository, path);
        return (proxiedFile != null);
    }
    // Is it a Metadata resource?
    if ("default".equals(repositoryRequestInfo.getLayout(path)) && repositoryRequestInfo.isMetadata(path)) {
        return proxyHandler.fetchMetadataFromProxies(managedRepository, path).isModified();
    }
    // Is it an Archetype Catalog?
    if (repositoryRequestInfo.isArchetypeCatalog(path)) {
        // FIXME we must implement a merge of remote archetype catalog from remote servers.
        StorageAsset proxiedFile = proxyHandler.fetchFromProxies(managedRepository, path);
        return (proxiedFile != null);
    }
    // Not any of the above? Then it's gotta be an artifact reference.
    try {
        // Get the artifact reference in a layout neutral way.
        // ArtifactReference artifact = repositoryRequestInfo.toArtifactReference( path );
        ItemSelector selector = repositoryRequestInfo.toItemSelector(path);
        if (selector != null) {
            String repositoryLayout = managedRepository.getLayout();
            RepositoryStorage repositoryStorage = this.applicationContext.getBean("repositoryStorage#" + repositoryLayout, RepositoryStorage.class);
            selector = repositoryStorage.applyServerSideRelocation(managedRepository, selector);
            StorageAsset proxiedFile = proxyHandler.fetchFromProxies(managedRepository, selector);
            resource.setPath(managedRepository.getContent().toPath(selector));
            log.debug("Proxied artifact '{}:{}:{}:{}'", selector.getNamespace(), selector.getArtifactId(), selector.getVersion(), selector.getArtifactVersion());
            return (proxiedFile != null);
        }
    } catch (LayoutException e) {
    /* eat it */
    } catch (ProxyDownloadException e) {
        log.error(e.getMessage(), e);
        throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Unable to fetch artifact resource.");
    }
    return false;
}
Also used : RepositoryStorage(org.apache.archiva.metadata.repository.storage.RepositoryStorage) ItemSelector(org.apache.archiva.repository.content.ItemSelector) DavException(org.apache.jackrabbit.webdav.DavException) LayoutException(org.apache.archiva.repository.content.LayoutException) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ProxyDownloadException(org.apache.archiva.policies.ProxyDownloadException) RepositoryRequestInfo(org.apache.archiva.repository.RepositoryRequestInfo) RepositoryProxyHandler(org.apache.archiva.proxy.model.RepositoryProxyHandler)

Aggregations

RepositoryProxyHandler (org.apache.archiva.proxy.model.RepositoryProxyHandler)8 Path (java.nio.file.Path)2 NetworkProxyConfiguration (org.apache.archiva.configuration.model.NetworkProxyConfiguration)2 ProxyConnectorConfiguration (org.apache.archiva.configuration.model.ProxyConnectorConfiguration)2 RemoteRepositoryConfiguration (org.apache.archiva.configuration.model.RemoteRepositoryConfiguration)2 ProxyDownloadException (org.apache.archiva.policies.ProxyDownloadException)2 RepositoryType (org.apache.archiva.repository.RepositoryType)2 ItemSelector (org.apache.archiva.repository.content.ItemSelector)2 LayoutException (org.apache.archiva.repository.content.LayoutException)2 ArchivaItemSelector (org.apache.archiva.repository.content.base.ArchivaItemSelector)2 StorageAsset (org.apache.archiva.repository.storage.StorageAsset)2 Wagon (org.apache.maven.wagon.Wagon)2 Before (org.junit.Before)2 IOException (java.io.IOException)1 Reader (java.io.Reader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 ArchivaRuntimeConfiguration (org.apache.archiva.configuration.model.ArchivaRuntimeConfiguration)1