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.artifact.Artifact;
022import org.eclipse.aether.artifact.ArtifactProperties;
023import org.eclipse.aether.repository.RemoteRepository;
024
025/**
026 * Thrown when an artifact was not found in a particular repository.
027 */
028public class ArtifactNotFoundException extends ArtifactTransferException {
029
030    /**
031     * Creates a new exception with the specified artifact and repository.
032     *
033     * @param artifact The missing artifact, may be {@code null}.
034     * @param repository The involved remote repository, may be {@code null}.
035     */
036    public ArtifactNotFoundException(Artifact artifact, RemoteRepository repository) {
037        super(artifact, repository, getMessage(artifact, null, repository));
038    }
039
040    /**
041     * Creates a new exception with the specified system artifact and expected local path.
042     *
043     * @param artifact The missing artifact, may be {@code null}.
044     * @param localPath The expected local path of missing artifact, may be {@code null}.
045     *
046     * @since 2.0.0
047     */
048    public ArtifactNotFoundException(Artifact artifact, String localPath) {
049        super(artifact, null, getMessage(artifact, localPath, null));
050    }
051
052    private static String getMessage(Artifact artifact, String localPath, RemoteRepository repository) {
053        StringBuilder buffer = new StringBuilder(256);
054        buffer.append("Could not find artifact ").append(artifact);
055        buffer.append(getString(" in ", repository));
056        if (artifact != null) {
057            if (localPath != null && repository == null) {
058                buffer.append(" at specified path ").append(localPath);
059            }
060            String downloadUrl = artifact.getProperty(ArtifactProperties.DOWNLOAD_URL, null);
061            if (downloadUrl != null) {
062                buffer.append(", try downloading from ").append(downloadUrl);
063            }
064        }
065        return buffer.toString();
066    }
067
068    /**
069     * Creates a new exception with the specified artifact, repository and detail message.
070     *
071     * @param artifact The missing artifact, may be {@code null}.
072     * @param repository The involved remote repository, may be {@code null}.
073     * @param message The detail message, may be {@code null}.
074     */
075    public ArtifactNotFoundException(Artifact artifact, RemoteRepository repository, String message) {
076        super(artifact, repository, message);
077    }
078
079    /**
080     * Creates a new exception with the specified artifact, repository and detail message.
081     *
082     * @param artifact The missing artifact, may be {@code null}.
083     * @param repository The involved remote repository, may be {@code null}.
084     * @param message The detail message, may be {@code null}.
085     * @param fromCache {@code true} if the exception was played back from the error cache, {@code false} if the
086     *            exception actually just occurred.
087     */
088    public ArtifactNotFoundException(
089            Artifact artifact, RemoteRepository repository, String message, boolean fromCache) {
090        super(artifact, repository, message, fromCache);
091    }
092
093    /**
094     * Creates a new exception with the specified artifact, repository, detail message and cause.
095     *
096     * @param artifact The missing artifact, may be {@code null}.
097     * @param repository The involved remote repository, may be {@code null}.
098     * @param message The detail message, may be {@code null}.
099     * @param cause The exception that caused this one, may be {@code null}.
100     */
101    public ArtifactNotFoundException(Artifact artifact, RemoteRepository repository, String message, Throwable cause) {
102        super(artifact, repository, message, cause);
103    }
104}