1 /******************************************************************************* 2 * Copyright (c) 2010, 2013 Sonatype, Inc. 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Eclipse Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/epl-v10.html 7 * 8 * Contributors: 9 * Sonatype, Inc. - initial API and implementation 10 *******************************************************************************/ 11 package org.eclipse.aether.transfer; 12 13 import java.io.File; 14 15 import org.eclipse.aether.RequestTrace; 16 17 /** 18 * Describes a resource being uploaded or downloaded by the repository system. 19 */ 20 public final class TransferResource 21 { 22 23 private final String repositoryUrl; 24 25 private final String resourceName; 26 27 private final File file; 28 29 private final long startTime; 30 31 private final RequestTrace trace; 32 33 private long contentLength = -1; 34 35 private long resumeOffset; 36 37 /** 38 * Creates a new transfer resource with the specified properties. 39 * 40 * @param repositoryUrl The base URL of the repository, may be {@code null} or empty if unknown. If not empty, a 41 * trailing slash will automatically be added if missing. 42 * @param resourceName The relative path to the resource within the repository, may be {@code null}. A leading slash 43 * (if any) will be automatically removed. 44 * @param file The source/target file involved in the transfer, may be {@code null}. 45 * @param trace The trace information, may be {@code null}. 46 */ 47 public TransferResource( String repositoryUrl, String resourceName, File file, RequestTrace trace ) 48 { 49 if ( repositoryUrl == null || repositoryUrl.length() <= 0 ) 50 { 51 this.repositoryUrl = ""; 52 } 53 else if ( repositoryUrl.endsWith( "/" ) ) 54 { 55 this.repositoryUrl = repositoryUrl; 56 } 57 else 58 { 59 this.repositoryUrl = repositoryUrl + '/'; 60 } 61 62 if ( resourceName == null || resourceName.length() <= 0 ) 63 { 64 this.resourceName = ""; 65 } 66 else if ( resourceName.startsWith( "/" ) ) 67 { 68 this.resourceName = resourceName.substring( 1 ); 69 } 70 else 71 { 72 this.resourceName = resourceName; 73 } 74 75 this.file = file; 76 77 this.trace = trace; 78 79 startTime = System.currentTimeMillis(); 80 } 81 82 /** 83 * The base URL of the repository, e.g. "http://repo1.maven.org/maven2/". Unless the URL is unknown, it will be 84 * terminated by a trailing slash. 85 * 86 * @return The base URL of the repository or an empty string if unknown, never {@code null}. 87 */ 88 public String getRepositoryUrl() 89 { 90 return repositoryUrl; 91 } 92 93 /** 94 * The path of the resource relative to the repository's base URL, e.g. "org/apache/maven/maven/3.0/maven-3.0.pom". 95 * 96 * @return The path of the resource, never {@code null}. 97 */ 98 public String getResourceName() 99 { 100 return resourceName; 101 } 102 103 /** 104 * Gets the local file being uploaded or downloaded. When the repository system merely checks for the existence of a 105 * remote resource, no local file will be involved in the transfer. 106 * 107 * @return The source/target file involved in the transfer or {@code null} if none. 108 */ 109 public File getFile() 110 { 111 return file; 112 } 113 114 /** 115 * The size of the resource in bytes. Note that the size of a resource during downloads might be unknown to the 116 * client which is usually the case when transfers employ compression like gzip. In general, the content length is 117 * not known until the transfer has {@link TransferListener#transferStarted(TransferEvent) started}. 118 * 119 * @return The size of the resource in bytes or a negative value if unknown. 120 */ 121 public long getContentLength() 122 { 123 return contentLength; 124 } 125 126 /** 127 * Sets the size of the resource in bytes. 128 * 129 * @param contentLength The size of the resource in bytes or a negative value if unknown. 130 * @return This resource for chaining, never {@code null}. 131 */ 132 public TransferResource setContentLength( long contentLength ) 133 { 134 this.contentLength = contentLength; 135 return this; 136 } 137 138 /** 139 * Gets the byte offset within the resource from which the download starts. A positive offset indicates a previous 140 * download attempt is being resumed, {@code 0} means the transfer starts at the first byte. 141 * 142 * @return The zero-based index of the first byte being transferred, never negative. 143 */ 144 public long getResumeOffset() 145 { 146 return resumeOffset; 147 } 148 149 /** 150 * Sets the byte offset within the resource at which the download starts. 151 * 152 * @param resumeOffset The zero-based index of the first byte being transferred, must not be negative. 153 * @return This resource for chaining, never {@code null}. 154 */ 155 public TransferResource setResumeOffset( long resumeOffset ) 156 { 157 if ( resumeOffset < 0 ) 158 { 159 throw new IllegalArgumentException( "resume offset cannot be negative" ); 160 } 161 this.resumeOffset = resumeOffset; 162 return this; 163 } 164 165 /** 166 * Gets the timestamp when the transfer of this resource was started. 167 * 168 * @return The timestamp when the transfer of this resource was started. 169 */ 170 public long getTransferStartTime() 171 { 172 return startTime; 173 } 174 175 /** 176 * Gets the trace information that describes the higher level request/operation during which this resource is 177 * transferred. 178 * 179 * @return The trace information about the higher level operation or {@code null} if none. 180 */ 181 public RequestTrace getTrace() 182 { 183 return trace; 184 } 185 186 @Override 187 public String toString() 188 { 189 return getRepositoryUrl() + getResourceName() + " <> " + getFile(); 190 } 191 192 }