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.transfer;
020
021import org.eclipse.aether.RepositoryException;
022import org.eclipse.aether.artifact.Artifact;
023import org.eclipse.aether.repository.RemoteRepository;
024
025/**
026 * Thrown when an artifact could not be uploaded/downloaded to/from a particular remote repository.
027 */
028public class ArtifactTransferException extends RepositoryException {
029
030    private final transient Artifact artifact;
031
032    private final transient RemoteRepository repository;
033
034    private final boolean fromCache;
035
036    static String getString(String prefix, RemoteRepository repository) {
037        if (repository == null) {
038            return "";
039        } else {
040            return prefix + repository.getId() + " (" + repository.getUrl() + ")";
041        }
042    }
043
044    /**
045     * Creates a new exception with the specified artifact, repository and detail message.
046     *
047     * @param artifact The untransferable artifact, may be {@code null}.
048     * @param repository The involved remote repository, may be {@code null}.
049     * @param message The detail message, may be {@code null}.
050     */
051    public ArtifactTransferException(Artifact artifact, RemoteRepository repository, String message) {
052        this(artifact, repository, message, false);
053    }
054
055    /**
056     * Creates a new exception with the specified artifact, repository and detail message.
057     *
058     * @param artifact The untransferable artifact, may be {@code null}.
059     * @param repository The involved remote repository, may be {@code null}.
060     * @param message The detail message, may be {@code null}.
061     * @param fromCache {@code true} if the exception was played back from the error cache, {@code false} if the
062     *            exception actually just occurred.
063     */
064    public ArtifactTransferException(
065            Artifact artifact, RemoteRepository repository, String message, boolean fromCache) {
066        super(message);
067        this.artifact = artifact;
068        this.repository = repository;
069        this.fromCache = fromCache;
070    }
071
072    /**
073     * Creates a new exception with the specified artifact, repository and cause.
074     *
075     * @param artifact The untransferable artifact, may be {@code null}.
076     * @param repository The involved remote repository, may be {@code null}.
077     * @param cause The exception that caused this one, may be {@code null}.
078     */
079    public ArtifactTransferException(Artifact artifact, RemoteRepository repository, Throwable cause) {
080        this(
081                artifact,
082                repository,
083                "Could not transfer artifact " + artifact + getString(" from/to ", repository)
084                        + getMessage(": ", cause),
085                cause);
086    }
087
088    /**
089     * Creates a new exception with the specified artifact, repository, detail message and cause.
090     *
091     * @param artifact The untransferable artifact, may be {@code null}.
092     * @param repository The involved remote repository, may be {@code null}.
093     * @param message The detail message, may be {@code null}.
094     * @param cause The exception that caused this one, may be {@code null}.
095     */
096    public ArtifactTransferException(Artifact artifact, RemoteRepository repository, String message, Throwable cause) {
097        super(message, cause);
098        this.artifact = artifact;
099        this.repository = repository;
100        this.fromCache = false;
101    }
102
103    /**
104     * Gets the artifact that could not be transferred.
105     *
106     * @return The troublesome artifact or {@code null} if unknown.
107     */
108    public Artifact getArtifact() {
109        return artifact;
110    }
111
112    /**
113     * Gets the remote repository involved in the transfer.
114     *
115     * @return The involved remote repository or {@code null} if unknown.
116     */
117    public RemoteRepository getRepository() {
118        return repository;
119    }
120
121    /**
122     * Indicates whether this exception actually just occurred or was played back from the error cache.
123     *
124     * @return {@code true} if the exception was played back from the error cache, {@code false} if the exception
125     *         actually occurred just now.
126     */
127    public boolean isFromCache() {
128        return fromCache;
129    }
130}