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.spi.connector;
020
021import java.io.File;
022import java.nio.file.Path;
023
024import org.eclipse.aether.artifact.Artifact;
025import org.eclipse.aether.transfer.ArtifactTransferException;
026
027/**
028 * A download/upload of an artifact.
029 *
030 * @noextend This class is not intended to be extended by clients.
031 */
032public abstract class ArtifactTransfer extends Transfer {
033
034    private Artifact artifact;
035
036    private Path path;
037
038    private ArtifactTransferException exception;
039
040    ArtifactTransfer() {
041        // hide
042    }
043
044    /**
045     * Gets the artifact being transferred.
046     *
047     * @return The artifact being transferred or {@code null} if not set.
048     */
049    public Artifact getArtifact() {
050        return artifact;
051    }
052
053    /**
054     * Sets the artifact to transfer.
055     *
056     * @param artifact The artifact, may be {@code null}.
057     * @return This transfer for chaining, never {@code null}.
058     */
059    public ArtifactTransfer setArtifact(Artifact artifact) {
060        this.artifact = artifact;
061        return this;
062    }
063
064    /**
065     * Gets the local file the artifact is downloaded to or uploaded from. In case of a download, a connector should
066     * first transfer the bytes to a temporary file and only overwrite the target file once the entire download is
067     * completed such that an interrupted/failed download does not corrupt the current file contents.
068     *
069     * @return The local file or {@code null} if not set.
070     * @deprecated Use {@link #getPath()} instead.
071     */
072    @Deprecated
073    public File getFile() {
074        return path != null ? path.toFile() : null;
075    }
076
077    /**
078     * Gets the local file the artifact is downloaded to or uploaded from. In case of a download, a connector should
079     * first transfer the bytes to a temporary file and only overwrite the target file once the entire download is
080     * completed such that an interrupted/failed download does not corrupt the current file contents.
081     *
082     * @return The local file or {@code null} if not set.
083     * @since 2.0.0
084     */
085    public Path getPath() {
086        return path;
087    }
088
089    /**
090     * Sets the local file the artifact is downloaded to or uploaded from.
091     *
092     * @param file The local file, may be {@code null}.
093     * @return This transfer for chaining, never {@code null}.
094     * @deprecated Use {@link #setPath(Path)} instead.
095     */
096    @Deprecated
097    public ArtifactTransfer setFile(File file) {
098        return setPath(file != null ? file.toPath() : null);
099    }
100
101    /**
102     * Sets the local file the artifact is downloaded to or uploaded from.
103     *
104     * @param path The local file, may be {@code null}.
105     * @return This transfer for chaining, never {@code null}.
106     * @since 2.0.0
107     */
108    public ArtifactTransfer setPath(Path path) {
109        this.path = path;
110        return this;
111    }
112
113    /**
114     * Gets the exception that occurred during the transfer (if any).
115     *
116     * @return The exception or {@code null} if the transfer was successful.
117     */
118    public ArtifactTransferException getException() {
119        return exception;
120    }
121
122    /**
123     * Sets the exception that occurred during the transfer.
124     *
125     * @param exception The exception, may be {@code null} to denote a successful transfer.
126     * @return This transfer for chaining, never {@code null}.
127     */
128    public ArtifactTransfer setException(ArtifactTransferException exception) {
129        this.exception = exception;
130        return this;
131    }
132}