1 package org.eclipse.aether.spi.connector; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import java.io.File; 23 import java.util.Collection; 24 import java.util.Collections; 25 import java.util.List; 26 27 import org.eclipse.aether.RequestTrace; 28 import org.eclipse.aether.artifact.Artifact; 29 import org.eclipse.aether.repository.RemoteRepository; 30 import org.eclipse.aether.transfer.ArtifactTransferException; 31 import org.eclipse.aether.transfer.TransferListener; 32 33 /** 34 * A download of an artifact from a remote repository. A repository connector processing this download has to use 35 * {@link #setException(ArtifactTransferException)} and {@link #setSupportedContexts(Collection)} (if applicable) to 36 * report the results of the transfer. 37 */ 38 public final class ArtifactDownload 39 extends ArtifactTransfer 40 { 41 42 private boolean existenceCheck; 43 44 private String checksumPolicy = ""; 45 46 private String context = ""; 47 48 private Collection<String> contexts; 49 50 private List<RemoteRepository> repositories = Collections.emptyList(); 51 52 /** 53 * Creates a new uninitialized download. 54 */ 55 public ArtifactDownload() 56 { 57 // enables default constructor 58 } 59 60 /** 61 * Creates a new download with the specified properties. 62 * 63 * @param artifact The artifact to download, may be {@code null}. 64 * @param context The context in which this download is performed, may be {@code null}. 65 * @param file The local file to download the artifact to, may be {@code null}. 66 * @param checksumPolicy The checksum policy, may be {@code null}. 67 */ 68 public ArtifactDownload( Artifact artifact, String context, File file, String checksumPolicy ) 69 { 70 setArtifact( artifact ); 71 setRequestContext( context ); 72 setFile( file ); 73 setChecksumPolicy( checksumPolicy ); 74 } 75 76 @Override 77 public ArtifactDownload setArtifact( Artifact artifact ) 78 { 79 super.setArtifact( artifact ); 80 return this; 81 } 82 83 @Override 84 public ArtifactDownload setFile( File file ) 85 { 86 super.setFile( file ); 87 return this; 88 } 89 90 /** 91 * Indicates whether this transfer shall only verify the existence of the artifact in the remote repository rather 92 * than actually downloading the file. Just like with an actual transfer, a connector is expected to signal the 93 * non-existence of the artifact by associating an {@link org.eclipse.aether.transfer.ArtifactNotFoundException 94 * ArtifactNotFoundException} with this download. <em>Note:</em> If an existence check is requested, 95 * {@link #getFile()} may be {@code null}, i.e. the connector must not try to access the local file. 96 * 97 * @return {@code true} if only the artifact existence shall be verified, {@code false} to actually download the 98 * artifact. 99 */ 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 }