001package org.eclipse.aether.transfer;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023
024import org.eclipse.aether.RequestTrace;
025
026/**
027 * Describes a resource being uploaded or downloaded by the repository system.
028 */
029public final class TransferResource
030{
031
032    private final String repositoryId;
033
034    private final String repositoryUrl;
035
036    private final String resourceName;
037
038    private final File file;
039
040    private final long startTime;
041
042    private final RequestTrace trace;
043
044    private long contentLength = -1L;
045
046    private long resumeOffset;
047
048    /**
049     * Creates a new transfer resource with the specified properties.
050     *
051     * @param repositoryUrl The base URL of the repository, may be {@code null} or empty if unknown. If not empty, a
052     * trailing slash will automatically be added if missing.
053     * @param resourceName The relative path to the resource within the repository, may be {@code null}. A leading slash
054     * (if any) will be automatically removed.
055     * @param file The source/target file involved in the transfer, may be {@code null}.
056     * @param trace The trace information, may be {@code null}.
057     *
058     * @deprecated As of 1.1.0, replaced by {@link #TransferResource(java.lang.String, java.lang.String,
059     * java.lang.String, java.io.File, org.eclipse.aether.RequestTrace)}
060     */
061    @Deprecated
062    public TransferResource( String repositoryUrl, String resourceName, File file, RequestTrace trace )
063    {
064        this( null, repositoryUrl, resourceName, file, trace );
065    }
066
067    /**
068     * Creates a new transfer resource with the specified properties.
069     *
070     * @param repositoryId The ID of the repository used to transfer the resource, may be {@code null} or
071     *                     empty if unknown.
072     * @param repositoryUrl The base URL of the repository, may be {@code null} or empty if unknown. If not empty, a
073     *            trailing slash will automatically be added if missing.
074     * @param resourceName The relative path to the resource within the repository, may be {@code null}. A leading slash
075     *            (if any) will be automatically removed.
076     * @param file The source/target file involved in the transfer, may be {@code null}.
077     * @param trace The trace information, may be {@code null}.
078     *
079     * @since 1.1.0
080     */
081    public TransferResource( String repositoryId, String repositoryUrl, String resourceName,
082        File file, RequestTrace trace )
083    {
084        if ( repositoryId == null || repositoryId.isEmpty() )
085        {
086            this.repositoryId = "";
087        }
088        else
089        {
090            this.repositoryId = repositoryId;
091        }
092
093        if ( repositoryUrl == null || repositoryUrl.isEmpty() )
094        {
095            this.repositoryUrl = "";
096        }
097        else if ( repositoryUrl.endsWith( "/" ) )
098        {
099            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}