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