001package org.eclipse.aether.spi.connector;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.util.Collection;
024import java.util.Collections;
025import java.util.List;
026
027import org.eclipse.aether.RequestTrace;
028import org.eclipse.aether.artifact.Artifact;
029import org.eclipse.aether.repository.RemoteRepository;
030import org.eclipse.aether.transfer.ArtifactTransferException;
031import org.eclipse.aether.transfer.TransferListener;
032
033/**
034 * A download of an artifact from a remote repository. A repository connector processing this download has to use
035 * {@link #setException(ArtifactTransferException)} and {@link #setSupportedContexts(Collection)} (if applicable) to
036 * report the results of the transfer.
037 */
038public final class ArtifactDownload
039    extends ArtifactTransfer
040{
041
042    private boolean existenceCheck;
043
044    private String checksumPolicy = "";
045
046    private String context = "";
047
048    private Collection<String> contexts;
049
050    private List<RemoteRepository> repositories = Collections.emptyList();
051
052    /**
053     * Creates a new uninitialized download.
054     */
055    public ArtifactDownload()
056    {
057        // enables default constructor
058    }
059
060    /**
061     * Creates a new download with the specified properties.
062     * 
063     * @param artifact The artifact to download, may be {@code null}.
064     * @param context The context in which this download is performed, may be {@code null}.
065     * @param file The local file to download the artifact to, may be {@code null}.
066     * @param checksumPolicy The checksum policy, may be {@code null}.
067     */
068    public ArtifactDownload( Artifact artifact, String context, File file, String checksumPolicy )
069    {
070        setArtifact( artifact );
071        setRequestContext( context );
072        setFile( file );
073        setChecksumPolicy( checksumPolicy );
074    }
075
076    @Override
077    public ArtifactDownload setArtifact( Artifact artifact )
078    {
079        super.setArtifact( artifact );
080        return this;
081    }
082
083    @Override
084    public ArtifactDownload setFile( File file )
085    {
086        super.setFile( file );
087        return this;
088    }
089
090    /**
091     * Indicates whether this transfer shall only verify the existence of the artifact in the remote repository rather
092     * than actually downloading the file. Just like with an actual transfer, a connector is expected to signal the
093     * non-existence of the artifact by associating an {@link org.eclipse.aether.transfer.ArtifactNotFoundException
094     * ArtifactNotFoundException} with this download. <em>Note:</em> If an existence check is requested,
095     * {@link #getFile()} may be {@code null}, i.e. the connector must not try to access the local file.
096     * 
097     * @return {@code true} if only the artifact existence shall be verified, {@code false} to actually download the
098     *         artifact.
099     */
100    public boolean isExistenceCheck()
101    {
102        return existenceCheck;
103    }
104
105    /**
106     * Controls whether this transfer shall only verify the existence of the artifact in the remote repository rather
107     * than actually downloading the file.
108     * 
109     * @param existenceCheck {@code true} if only the artifact existence shall be verified, {@code false} to actually
110     *            download the artifact.
111     * @return This transfer for chaining, never {@code null}.
112     */
113    public ArtifactDownload setExistenceCheck( boolean existenceCheck )
114    {
115        this.existenceCheck = existenceCheck;
116        return this;
117    }
118
119    /**
120     * Gets the checksum policy for this transfer.
121     * 
122     * @return The checksum policy, never {@code null}.
123     */
124    public String getChecksumPolicy()
125    {
126        return checksumPolicy;
127    }
128
129    /**
130     * Sets the checksum policy for this transfer.
131     * 
132     * @param checksumPolicy The checksum policy, may be {@code null}.
133     * @return This transfer for chaining, never {@code null}.
134     */
135    public ArtifactDownload setChecksumPolicy( String checksumPolicy )
136    {
137        this.checksumPolicy = ( checksumPolicy != null ) ? checksumPolicy : "";
138        return this;
139    }
140
141    /**
142     * Gets the context of this transfer.
143     * 
144     * @return The context id, never {@code null}.
145     */
146    public String getRequestContext()
147    {
148        return context;
149    }
150
151    /**
152     * Sets the context of this transfer.
153     * 
154     * @param context The context id, may be {@code null}.
155     * @return This transfer for chaining, never {@code null}.
156     */
157    public ArtifactDownload setRequestContext( String context )
158    {
159        this.context = ( context != null ) ? context : "";
160        return this;
161    }
162
163    /**
164     * Gets the set of request contexts in which the artifact is generally available. Repository managers can indicate
165     * that an artifact is available in more than the requested context to avoid future remote trips for the same
166     * artifact in a different context.
167     * 
168     * @return The set of requests context in which the artifact is available, never {@code null}.
169     */
170    public Collection<String> getSupportedContexts()
171    {
172        return ( contexts != null ) ? contexts : Collections.singleton( context );
173    }
174
175    /**
176     * Sets the set of request contexts in which the artifact is generally available. Repository managers can indicate
177     * that an artifact is available in more than the requested context to avoid future remote trips for the same
178     * artifact in a different context. The set of supported contexts defaults to the original request context if not
179     * overridden by the repository connector.
180     * 
181     * @param contexts The set of requests context in which the artifact is available, may be {@code null}.
182     * @return This transfer for chaining, never {@code null}.
183     */
184    public ArtifactDownload setSupportedContexts( Collection<String> contexts )
185    {
186        if ( contexts == null || contexts.isEmpty() )
187        {
188            this.contexts = Collections.singleton( context );
189        }
190        else
191        {
192            this.contexts = contexts;
193        }
194        return this;
195    }
196
197    /**
198     * Gets the remote repositories that are being aggregated by the physically contacted remote repository (i.e. a
199     * repository manager).
200     * 
201     * @return The remote repositories being aggregated, never {@code null}.
202     */
203    public List<RemoteRepository> getRepositories()
204    {
205        return repositories;
206    }
207
208    /**
209     * Sets the remote repositories that are being aggregated by the physically contacted remote repository (i.e. a
210     * repository manager).
211     * 
212     * @param repositories The remote repositories being aggregated, may be {@code null}.
213     * @return This transfer for chaining, never {@code null}.
214     */
215    public ArtifactDownload setRepositories( List<RemoteRepository> repositories )
216    {
217        if ( repositories == null )
218        {
219            this.repositories = Collections.emptyList();
220        }
221        else
222        {
223            this.repositories = repositories;
224        }
225        return this;
226    }
227
228    @Override
229    public ArtifactDownload setException( ArtifactTransferException exception )
230    {
231        super.setException( exception );
232        return this;
233    }
234
235    @Override
236    public ArtifactDownload setListener( TransferListener listener )
237    {
238        super.setListener( listener );
239        return this;
240    }
241
242    @Override
243    public ArtifactDownload setTrace( RequestTrace trace )
244    {
245        super.setTrace( trace );
246        return this;
247    }
248
249    @Override
250    public String toString()
251    {
252        return getArtifact() + " - " + ( isExistenceCheck() ? "?" : "" ) + getFile();
253    }
254
255}