001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.internal.impl;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023import javax.inject.Singleton;
024
025import java.util.ArrayList;
026import java.util.Collections;
027import java.util.List;
028import java.util.Map;
029
030import org.eclipse.aether.RepositorySystemSession;
031import org.eclipse.aether.impl.RemoteRepositoryFilterManager;
032import org.eclipse.aether.impl.RepositoryConnectorProvider;
033import org.eclipse.aether.internal.impl.filter.FilteringRepositoryConnector;
034import org.eclipse.aether.repository.Authentication;
035import org.eclipse.aether.repository.Proxy;
036import org.eclipse.aether.repository.RemoteRepository;
037import org.eclipse.aether.spi.connector.RepositoryConnector;
038import org.eclipse.aether.spi.connector.RepositoryConnectorFactory;
039import org.eclipse.aether.spi.connector.filter.RemoteRepositoryFilter;
040import org.eclipse.aether.transfer.NoRepositoryConnectorException;
041import org.slf4j.Logger;
042import org.slf4j.LoggerFactory;
043
044import static java.util.Objects.requireNonNull;
045
046/**
047 */
048@Singleton
049@Named
050public class DefaultRepositoryConnectorProvider implements RepositoryConnectorProvider {
051
052    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultRepositoryConnectorProvider.class);
053
054    private final Map<String, RepositoryConnectorFactory> connectorFactories;
055
056    private final RemoteRepositoryFilterManager remoteRepositoryFilterManager;
057
058    @Inject
059    public DefaultRepositoryConnectorProvider(
060            Map<String, RepositoryConnectorFactory> connectorFactories,
061            RemoteRepositoryFilterManager remoteRepositoryFilterManager) {
062        this.connectorFactories = Collections.unmodifiableMap(connectorFactories);
063        this.remoteRepositoryFilterManager = requireNonNull(remoteRepositoryFilterManager);
064    }
065
066    @Override
067    public RepositoryConnector newRepositoryConnector(RepositorySystemSession session, RemoteRepository repository)
068            throws NoRepositoryConnectorException {
069        requireNonNull(repository, "remote repository cannot be null");
070
071        if (repository.isBlocked()) {
072            if (repository.getMirroredRepositories().isEmpty()) {
073                throw new NoRepositoryConnectorException(repository, "Blocked repository: " + repository);
074            } else {
075                throw new NoRepositoryConnectorException(
076                        repository, "Blocked mirror for repositories: " + repository.getMirroredRepositories());
077            }
078        }
079
080        PrioritizedComponents<RepositoryConnectorFactory> factories = PrioritizedComponents.reuseOrCreate(
081                session, connectorFactories, RepositoryConnectorFactory::getPriority);
082
083        RemoteRepositoryFilter filter = remoteRepositoryFilterManager.getRemoteRepositoryFilter(session);
084        List<NoRepositoryConnectorException> errors = new ArrayList<>();
085        for (PrioritizedComponent<RepositoryConnectorFactory> factory : factories.getEnabled()) {
086            try {
087                RepositoryConnector connector = factory.getComponent().newInstance(session, repository);
088
089                if (LOGGER.isDebugEnabled()) {
090                    StringBuilder buffer = new StringBuilder(256);
091                    buffer.append("Using connector ")
092                            .append(connector.getClass().getSimpleName());
093                    Utils.appendClassLoader(buffer, connector);
094                    buffer.append(" with priority ").append(factory.getPriority());
095                    buffer.append(" for ").append(repository.getUrl());
096
097                    Authentication auth = repository.getAuthentication();
098                    if (auth != null) {
099                        buffer.append(" with ").append(auth);
100                    }
101
102                    Proxy proxy = repository.getProxy();
103                    if (proxy != null) {
104                        buffer.append(" via ")
105                                .append(proxy.getHost())
106                                .append(':')
107                                .append(proxy.getPort());
108
109                        auth = proxy.getAuthentication();
110                        if (auth != null) {
111                            buffer.append(" with ").append(auth);
112                        }
113                    }
114
115                    LOGGER.debug(buffer.toString());
116                }
117
118                if (filter != null) {
119                    return new FilteringRepositoryConnector(repository, connector, filter);
120                } else {
121                    return connector;
122                }
123            } catch (NoRepositoryConnectorException e) {
124                // continue and try next factory
125                errors.add(e);
126            }
127        }
128        if (LOGGER.isDebugEnabled() && errors.size() > 1) {
129            for (Exception e : errors) {
130                LOGGER.debug("Could not obtain connector factory for {}", repository, e);
131            }
132        }
133
134        StringBuilder buffer = new StringBuilder(256);
135        if (factories.isEmpty()) {
136            buffer.append("No connector factories available");
137        } else {
138            buffer.append("Cannot access ").append(repository.getUrl());
139            buffer.append(" with type ").append(repository.getContentType());
140            buffer.append(" using the available connector factories: ");
141            factories.list(buffer);
142        }
143
144        throw new NoRepositoryConnectorException(
145                repository, buffer.toString(), errors.size() == 1 ? errors.get(0) : null);
146    }
147}