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