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;
022
023import org.eclipse.aether.RequestTrace;
024import org.eclipse.aether.artifact.Artifact;
025import org.eclipse.aether.transfer.ArtifactTransferException;
026import org.eclipse.aether.transfer.TransferListener;
027import org.eclipse.aether.transform.FileTransformer;
028
029/**
030 * An upload of an artifact to a remote repository. A repository connector processing this upload has to use
031 * {@link #setException(ArtifactTransferException)} to report the results of the transfer.
032 */
033public final class ArtifactUpload extends ArtifactTransfer {
034    private FileTransformer fileTransformer;
035
036    /**
037     * Creates a new uninitialized upload.
038     */
039    public ArtifactUpload() {
040        // enables default constructor
041    }
042
043    /**
044     * Creates a new upload with the specified properties.
045     *
046     * @param artifact The artifact to upload, may be {@code null}.
047     * @param file The local file to upload the artifact from, may be {@code null}.
048     */
049    public ArtifactUpload(Artifact artifact, File file) {
050        setArtifact(artifact);
051        setFile(file);
052    }
053
054    /**
055     * <p>Creates a new upload with the specified properties.</p>
056     *
057     * <p><strong>IMPORTANT</strong> When using a fileTransformer, the
058     * content of the file is stored in memory to ensure that file content and checksums stay in sync!
059     * </p>
060     *
061     * @param artifact The artifact to upload, may be {@code null}.
062     * @param file The local file to upload the artifact from, may be {@code null}.
063     * @param fileTransformer The file transformer, may be {@code null}.
064     */
065    public ArtifactUpload(Artifact artifact, File file, FileTransformer fileTransformer) {
066        setArtifact(artifact);
067        setFile(file);
068        setFileTransformer(fileTransformer);
069    }
070
071    @Override
072    public ArtifactUpload setArtifact(Artifact artifact) {
073        super.setArtifact(artifact);
074        return this;
075    }
076
077    @Override
078    public ArtifactUpload setFile(File file) {
079        super.setFile(file);
080        return this;
081    }
082
083    @Override
084    public ArtifactUpload setException(ArtifactTransferException exception) {
085        super.setException(exception);
086        return this;
087    }
088
089    @Override
090    public ArtifactUpload setListener(TransferListener listener) {
091        super.setListener(listener);
092        return this;
093    }
094
095    @Override
096    public ArtifactUpload setTrace(RequestTrace trace) {
097        super.setTrace(trace);
098        return this;
099    }
100
101    public ArtifactUpload setFileTransformer(FileTransformer fileTransformer) {
102        this.fileTransformer = fileTransformer;
103        return this;
104    }
105
106    public FileTransformer getFileTransformer() {
107        return fileTransformer;
108    }
109
110    @Override
111    public String toString() {
112        if (getFileTransformer() != null) {
113            return getArtifact() + " >>> " + getFileTransformer().transformArtifact(getArtifact()) + " - " + getFile();
114        } else {
115            return getArtifact() + " - " + getFile();
116        }
117    }
118}