1 package org.eclipse.aether.transfer; 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 24 import org.eclipse.aether.RequestTrace; 25 26 /** 27 * Describes a resource being uploaded or downloaded by the repository system. 28 */ 29 public final class TransferResource 30 { 31 32 private final String repositoryId; 33 34 private final String repositoryUrl; 35 36 private final String resourceName; 37 38 private final File file; 39 40 private final long startTime; 41 42 private final RequestTrace trace; 43 44 private long contentLength = -1L; 45 46 private long resumeOffset; 47 48 /** 49 * Creates a new transfer resource with the specified properties. 50 * 51 * @param repositoryUrl The base URL of the repository, may be {@code null} or empty if unknown. If not empty, a 52 * trailing slash will automatically be added if missing. 53 * @param resourceName The relative path to the resource within the repository, may be {@code null}. A leading slash 54 * (if any) will be automatically removed. 55 * @param file The source/target file involved in the transfer, may be {@code null}. 56 * @param trace The trace information, may be {@code null}. 57 * 58 * @deprecated As of 1.1.0, replaced by {@link #TransferResource(java.lang.String, java.lang.String, 59 * java.lang.String, java.io.File, org.eclipse.aether.RequestTrace)} 60 */ 61 @Deprecated 62 public TransferResource( String repositoryUrl, String resourceName, File file, RequestTrace trace ) 63 { 64 this( null, repositoryUrl, resourceName, file, trace ); 65 } 66 67 /** 68 * Creates a new transfer resource with the specified properties. 69 * 70 * @param repositoryId The ID of the repository used to transfer the resource, may be {@code null} or empty if unknown. 71 * @param repositoryUrl The base URL of the repository, may be {@code null} or empty if unknown. If not empty, a 72 * trailing slash will automatically be added if missing. 73 * @param resourceName The relative path to the resource within the repository, may be {@code null}. A leading slash 74 * (if any) will be automatically removed. 75 * @param file The source/target file involved in the transfer, may be {@code null}. 76 * @param trace The trace information, may be {@code null}. 77 * 78 * @since 1.1.0 79 */ 80 public TransferResource( String repositoryId, String repositoryUrl, String resourceName, 81 File file, RequestTrace trace ) 82 { 83 if ( repositoryId == null || repositoryId.length() <= 0 ) 84 { 85 this.repositoryId = ""; 86 } 87 else 88 { 89 this.repositoryId = repositoryId; 90 } 91 92 if ( repositoryUrl == null || repositoryUrl.length() <= 0 ) 93 { 94 this.repositoryUrl = ""; 95 } 96 else if ( repositoryUrl.endsWith( "/" ) ) 97 { 98 this.repositoryUrl = repositoryUrl; 99 } 100 else 101 { 102 this.repositoryUrl = repositoryUrl + '/'; 103 } 104 105 if ( resourceName == null || resourceName.length() <= 0 ) 106 { 107 this.resourceName = ""; 108 } 109 else if ( resourceName.startsWith( "/" ) ) 110 { 111 this.resourceName = resourceName.substring( 1 ); 112 } 113 else 114 { 115 this.resourceName = resourceName; 116 } 117 118 this.file = file; 119 120 this.trace = trace; 121 122 startTime = System.currentTimeMillis(); 123 } 124 125 /** 126 * The ID of the repository, e.g., "central". 127 * 128 * @return The ID of the repository or an empty string if unknown, never {@code null}. 129 * 130 * @since 1.1.0 131 */ 132 public String getRepositoryId() 133 { 134 return repositoryId; 135 } 136 137 /** 138 * The base URL of the repository, e.g. "http://repo1.maven.org/maven2/". Unless the URL is unknown, it will be 139 * terminated by a trailing slash. 140 * 141 * @return The base URL of the repository or an empty string if unknown, never {@code null}. 142 */ 143 public String getRepositoryUrl() 144 { 145 return repositoryUrl; 146 } 147 148 /** 149 * The path of the resource relative to the repository's base URL, e.g. "org/apache/maven/maven/3.0/maven-3.0.pom". 150 * 151 * @return The path of the resource, never {@code null}. 152 */ 153 public String getResourceName() 154 { 155 return resourceName; 156 } 157 158 /** 159 * Gets the local file being uploaded or downloaded. When the repository system merely checks for the existence of a 160 * remote resource, no local file will be involved in the transfer. 161 * 162 * @return The source/target file involved in the transfer or {@code null} if none. 163 */ 164 public File getFile() 165 { 166 return file; 167 } 168 169 /** 170 * The size of the resource in bytes. Note that the size of a resource during downloads might be unknown to the 171 * client which is usually the case when transfers employ compression like gzip. In general, the content length is 172 * not known until the transfer has {@link TransferListener#transferStarted(TransferEvent) started}. 173 * 174 * @return The size of the resource in bytes or a negative value if unknown. 175 */ 176 public long getContentLength() 177 { 178 return contentLength; 179 } 180 181 /** 182 * Sets the size of the resource in bytes. 183 * 184 * @param contentLength The size of the resource in bytes or a negative value if unknown. 185 * @return This resource for chaining, never {@code null}. 186 */ 187 public TransferResource setContentLength( long contentLength ) 188 { 189 this.contentLength = contentLength; 190 return this; 191 } 192 193 /** 194 * Gets the byte offset within the resource from which the download starts. A positive offset indicates a previous 195 * download attempt is being resumed, {@code 0} means the transfer starts at the first byte. 196 * 197 * @return The zero-based index of the first byte being transferred, never negative. 198 */ 199 public long getResumeOffset() 200 { 201 return resumeOffset; 202 } 203 204 /** 205 * Sets the byte offset within the resource at which the download starts. 206 * 207 * @param resumeOffset The zero-based index of the first byte being transferred, must not be negative. 208 * @return This resource for chaining, never {@code null}. 209 */ 210 public TransferResource setResumeOffset( long resumeOffset ) 211 { 212 if ( resumeOffset < 0L ) 213 { 214 throw new IllegalArgumentException( "resume offset cannot be negative" ); 215 } 216 this.resumeOffset = resumeOffset; 217 return this; 218 } 219 220 /** 221 * Gets the timestamp when the transfer of this resource was started. 222 * 223 * @return The timestamp when the transfer of this resource was started. 224 */ 225 public long getTransferStartTime() 226 { 227 return startTime; 228 } 229 230 /** 231 * Gets the trace information that describes the higher level request/operation during which this resource is 232 * transferred. 233 * 234 * @return The trace information about the higher level operation or {@code null} if none. 235 */ 236 public RequestTrace getTrace() 237 { 238 return trace; 239 } 240 241 @Override 242 public String toString() 243 { 244 return getRepositoryUrl() + getResourceName() + " <> " + getFile(); 245 } 246 247 }