View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.spi.connector;
20  
21  import java.io.File;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.eclipse.aether.RequestTrace;
27  import org.eclipse.aether.artifact.Artifact;
28  import org.eclipse.aether.repository.RemoteRepository;
29  import org.eclipse.aether.transfer.ArtifactTransferException;
30  import org.eclipse.aether.transfer.TransferListener;
31  
32  /**
33   * A download of an artifact from a remote repository. A repository connector processing this download has to use
34   * {@link #setException(ArtifactTransferException)} and {@link #setSupportedContexts(Collection)} (if applicable) to
35   * report the results of the transfer.
36   */
37  public final class ArtifactDownload extends ArtifactTransfer {
38  
39      private boolean existenceCheck;
40  
41      private String checksumPolicy = "";
42  
43      private String context = "";
44  
45      private Collection<String> contexts;
46  
47      private List<RemoteRepository> repositories = Collections.emptyList();
48  
49      /**
50       * Creates a new uninitialized download.
51       */
52      public ArtifactDownload() {
53          // enables default constructor
54      }
55  
56      /**
57       * Creates a new download with the specified properties.
58       *
59       * @param artifact The artifact to download, may be {@code null}.
60       * @param context The context in which this download is performed, may be {@code null}.
61       * @param file The local file to download the artifact to, may be {@code null}.
62       * @param checksumPolicy The checksum policy, may be {@code null}.
63       */
64      public ArtifactDownload(Artifact artifact, String context, File file, String checksumPolicy) {
65          setArtifact(artifact);
66          setRequestContext(context);
67          setFile(file);
68          setChecksumPolicy(checksumPolicy);
69      }
70  
71      @Override
72      public ArtifactDownload setArtifact(Artifact artifact) {
73          super.setArtifact(artifact);
74          return this;
75      }
76  
77      @Override
78      public ArtifactDownload setFile(File file) {
79          super.setFile(file);
80          return this;
81      }
82  
83      /**
84       * Indicates whether this transfer shall only verify the existence of the artifact in the remote repository rather
85       * than actually downloading the file. Just like with an actual transfer, a connector is expected to signal the
86       * non-existence of the artifact by associating an {@link org.eclipse.aether.transfer.ArtifactNotFoundException
87       * ArtifactNotFoundException} with this download. <em>Note:</em> If an existence check is requested,
88       * {@link #getFile()} may be {@code null}, i.e. the connector must not try to access the local file.
89       *
90       * @return {@code true} if only the artifact existence shall be verified, {@code false} to actually download the
91       *         artifact.
92       */
93      public boolean isExistenceCheck() {
94          return existenceCheck;
95      }
96  
97      /**
98       * Controls whether this transfer shall only verify the existence of the artifact in the remote repository rather
99       * than actually downloading the file.
100      *
101      * @param existenceCheck {@code true} if only the artifact existence shall be verified, {@code false} to actually
102      *            download the artifact.
103      * @return This transfer for chaining, never {@code null}.
104      */
105     public ArtifactDownload setExistenceCheck(boolean existenceCheck) {
106         this.existenceCheck = existenceCheck;
107         return this;
108     }
109 
110     /**
111      * Gets the checksum policy for this transfer.
112      *
113      * @return The checksum policy, never {@code null}.
114      */
115     public String getChecksumPolicy() {
116         return checksumPolicy;
117     }
118 
119     /**
120      * Sets the checksum policy for this transfer.
121      *
122      * @param checksumPolicy The checksum policy, may be {@code null}.
123      * @return This transfer for chaining, never {@code null}.
124      */
125     public ArtifactDownload setChecksumPolicy(String checksumPolicy) {
126         this.checksumPolicy = (checksumPolicy != null) ? checksumPolicy : "";
127         return this;
128     }
129 
130     /**
131      * Gets the context of this transfer.
132      *
133      * @return The context id, never {@code null}.
134      */
135     public String getRequestContext() {
136         return context;
137     }
138 
139     /**
140      * Sets the context of this transfer.
141      *
142      * @param context The context id, may be {@code null}.
143      * @return This transfer for chaining, never {@code null}.
144      */
145     public ArtifactDownload setRequestContext(String context) {
146         this.context = (context != null) ? context : "";
147         return this;
148     }
149 
150     /**
151      * Gets the set of request contexts in which the artifact is generally available. Repository managers can indicate
152      * that an artifact is available in more than the requested context to avoid future remote trips for the same
153      * artifact in a different context.
154      *
155      * @return The set of requests context in which the artifact is available, never {@code null}.
156      */
157     public Collection<String> getSupportedContexts() {
158         return (contexts != null) ? contexts : Collections.singleton(context);
159     }
160 
161     /**
162      * Sets the set of request contexts in which the artifact is generally available. Repository managers can indicate
163      * that an artifact is available in more than the requested context to avoid future remote trips for the same
164      * artifact in a different context. The set of supported contexts defaults to the original request context if not
165      * overridden by the repository connector.
166      *
167      * @param contexts The set of requests context in which the artifact is available, may be {@code null}.
168      * @return This transfer for chaining, never {@code null}.
169      */
170     public ArtifactDownload setSupportedContexts(Collection<String> contexts) {
171         if (contexts == null || contexts.isEmpty()) {
172             this.contexts = Collections.singleton(context);
173         } else {
174             this.contexts = contexts;
175         }
176         return this;
177     }
178 
179     /**
180      * Gets the remote repositories that are being aggregated by the physically contacted remote repository (i.e. a
181      * repository manager).
182      *
183      * @return The remote repositories being aggregated, never {@code null}.
184      */
185     public List<RemoteRepository> getRepositories() {
186         return repositories;
187     }
188 
189     /**
190      * Sets the remote repositories that are being aggregated by the physically contacted remote repository (i.e. a
191      * repository manager).
192      *
193      * @param repositories The remote repositories being aggregated, may be {@code null}.
194      * @return This transfer for chaining, never {@code null}.
195      */
196     public ArtifactDownload setRepositories(List<RemoteRepository> repositories) {
197         if (repositories == null) {
198             this.repositories = Collections.emptyList();
199         } else {
200             this.repositories = repositories;
201         }
202         return this;
203     }
204 
205     @Override
206     public ArtifactDownload setException(ArtifactTransferException exception) {
207         super.setException(exception);
208         return this;
209     }
210 
211     @Override
212     public ArtifactDownload setListener(TransferListener listener) {
213         super.setListener(listener);
214         return this;
215     }
216 
217     @Override
218     public ArtifactDownload setTrace(RequestTrace trace) {
219         super.setTrace(trace);
220         return this;
221     }
222 
223     @Override
224     public String toString() {
225         return getArtifact() + " - " + (isExistenceCheck() ? "?" : "") + getFile();
226     }
227 }